blob: 5cda7cf09505d4c945f7dc152409f189af15e784 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_SCHED_H
2#define _LINUX_SCHED_H
3
David Howells607ca462012-10-13 10:46:48 +01004#include <uapi/linux/sched.h>
David Woodhouseb7b3c762006-04-27 00:12:56 +01005
Dongsheng Yang5c228072014-01-27 17:15:37 -05006#include <linux/sched/prio.h>
7
David Woodhouseb7b3c762006-04-27 00:12:56 +01008
9struct sched_param {
10 int sched_priority;
11};
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/param.h> /* for HZ */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/capability.h>
16#include <linux/threads.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/timex.h>
20#include <linux/jiffies.h>
Ingo Molnarb69339ba2017-02-05 16:15:03 +010021#include <linux/mutex.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010022#include <linux/plist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/rbtree.h>
24#include <linux/thread_info.h>
25#include <linux/cpumask.h>
26#include <linux/errno.h>
27#include <linux/nodemask.h>
Martin Schwidefskyc92ff1b2007-10-16 01:24:43 -070028#include <linux/mm_types.h>
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020029#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/page.h>
32#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/smp.h>
35#include <linux/sem.h>
Jack Millerab602f72014-08-08 14:23:19 -070036#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/compiler.h>
39#include <linux/completion.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010040#include <linux/signal_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/pid.h>
42#include <linux/percpu.h>
43#include <linux/topology.h>
44#include <linux/seccomp.h>
Ingo Molnare56d0902006-01-08 01:01:37 -080045#include <linux/rcupdate.h>
Jiri Pirko05725f72009-04-14 20:17:16 +020046#include <linux/rculist.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070047#include <linux/rtmutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David Woodhousea3b67142006-04-25 14:54:40 +010049#include <linux/time.h>
50#include <linux/param.h>
51#include <linux/resource.h>
52#include <linux/timer.h>
53#include <linux/hrtimer.h>
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070054#include <linux/kcov.h>
Andrew Morton7c3ab7382006-12-10 02:19:19 -080055#include <linux/task_io_accounting.h>
Arjan van de Ven97455122008-01-25 21:08:34 +010056#include <linux/latencytop.h>
David Howells9e2b2dc2008-08-13 16:20:04 +010057#include <linux/cred.h>
Peter Zijlstrafa14ff42011-09-12 13:06:17 +020058#include <linux/llist.h>
Eric W. Biederman7b44ab92011-11-16 23:20:58 -080059#include <linux/uidgid.h>
Ming Lei21caf2f2013-02-22 16:34:08 -080060#include <linux/gfp.h>
Ingo Molnarfd771232017-02-02 20:56:33 +010061#include <linux/topology.h>
Aaron Tomlind4311ff2014-09-12 14:16:17 +010062#include <linux/magic.h>
Tejun Heo7d7efec2015-05-13 16:35:16 -040063#include <linux/cgroup-defs.h>
David Woodhousea3b67142006-04-25 14:54:40 +010064
65#include <asm/processor.h>
H. J. Lu36d57ac2005-09-06 15:16:49 -070066
Dario Faggiolid50dde52013-11-07 14:43:36 +010067#define SCHED_ATTR_SIZE_VER0 48 /* sizeof first published struct */
68
69/*
70 * Extended scheduling parameters data structure.
71 *
72 * This is needed because the original struct sched_param can not be
73 * altered without introducing ABI issues with legacy applications
74 * (e.g., in sched_getparam()).
75 *
76 * However, the possibility of specifying more than just a priority for
77 * the tasks may be useful for a wide variety of application fields, e.g.,
78 * multimedia, streaming, automation and control, and many others.
79 *
80 * This variant (sched_attr) is meant at describing a so-called
81 * sporadic time-constrained task. In such model a task is specified by:
82 * - the activation period or minimum instance inter-arrival time;
83 * - the maximum (or average, depending on the actual scheduling
84 * discipline) computation time of all instances, a.k.a. runtime;
85 * - the deadline (relative to the actual activation time) of each
86 * instance.
87 * Very briefly, a periodic (sporadic) task asks for the execution of
88 * some specific computation --which is typically called an instance--
89 * (at most) every period. Moreover, each instance typically lasts no more
90 * than the runtime and must be completed by time instant t equal to
91 * the instance activation time + the deadline.
92 *
93 * This is reflected by the actual fields of the sched_attr structure:
94 *
95 * @size size of the structure, for fwd/bwd compat.
96 *
97 * @sched_policy task's scheduling policy
98 * @sched_flags for customizing the scheduler behaviour
99 * @sched_nice task's nice value (SCHED_NORMAL/BATCH)
100 * @sched_priority task's static priority (SCHED_FIFO/RR)
101 * @sched_deadline representative of the task's deadline
102 * @sched_runtime representative of the task's runtime
103 * @sched_period representative of the task's period
104 *
105 * Given this task model, there are a multiplicity of scheduling algorithms
106 * and policies, that can be used to ensure all the tasks will make their
107 * timing constraints.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100108 *
109 * As of now, the SCHED_DEADLINE policy (sched_dl scheduling class) is the
110 * only user of this new interface. More information about the algorithm
111 * available in the scheduling class file or in Documentation/.
Dario Faggiolid50dde52013-11-07 14:43:36 +0100112 */
113struct sched_attr {
114 u32 size;
115
116 u32 sched_policy;
117 u64 sched_flags;
118
119 /* SCHED_NORMAL, SCHED_BATCH */
120 s32 sched_nice;
121
122 /* SCHED_FIFO, SCHED_RR */
123 u32 sched_priority;
124
125 /* SCHED_DEADLINE */
126 u64 sched_runtime;
127 u64 sched_deadline;
128 u64 sched_period;
129};
130
Ingo Molnarc87e2832006-06-27 02:54:58 -0700131struct futex_pi_state;
Alexey Dobriyan286100a2008-01-25 21:08:34 +0100132struct robust_list_head;
Akinobu Mitabddd87c2010-02-23 08:55:42 +0100133struct bio_list;
Al Viro5ad4e532009-03-29 19:50:06 -0400134struct fs_struct;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200135struct perf_event_context;
Jens Axboe73c10102011-03-08 13:19:51 +0100136struct blk_plug;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800137struct filename;
Al Viro89076bc2015-05-12 08:29:38 -0400138struct nameidata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * These are the constant used to fake the fixed-point load-average
142 * counting. Some notes:
143 * - 11 bit fractions expand to 22 bits by the multiplies: this gives
144 * a load-average precision of 10 bits integer + 11 bits fractional
145 * - if you want to count load-averages more often, you need more
146 * precision, or rounding will get you. With 2-second counting freq,
147 * the EXP_n values would be 1981, 2034 and 2043 if still using only
148 * 11 bit fractions.
149 */
150extern unsigned long avenrun[]; /* Load averages */
Thomas Gleixner2d024942009-05-02 20:08:52 +0200151extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#define FSHIFT 11 /* nr of bits of precision */
154#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
Linus Torvalds0c2043a2007-10-07 16:17:38 -0700155#define LOAD_FREQ (5*HZ+1) /* 5 sec intervals */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
157#define EXP_5 2014 /* 1/exp(5sec/5min) */
158#define EXP_15 2037 /* 1/exp(5sec/15min) */
159
160#define CALC_LOAD(load,exp,n) \
161 load *= exp; \
162 load += n*(FIXED_1-exp); \
163 load >>= FSHIFT;
164
165extern unsigned long total_forks;
166extern int nr_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167DECLARE_PER_CPU(unsigned long, process_counts);
168extern int nr_processes(void);
169extern unsigned long nr_running(void);
Tim Chen2ee507c2014-07-31 10:29:48 -0700170extern bool single_task_running(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171extern unsigned long nr_iowait(void);
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200172extern unsigned long nr_iowait_cpu(int cpu);
Mel Gorman372ba8c2014-08-06 14:19:21 +0100173extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
Arjan van de Ven69d25872009-09-21 17:04:08 -0700174
Peter Zijlstra0f004f52010-11-30 19:48:45 +0100175extern void calc_global_load(unsigned long ticks);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200176
177#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200178extern void cpu_load_update_nohz_start(void);
179extern void cpu_load_update_nohz_stop(void);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200180#else
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200181static inline void cpu_load_update_nohz_start(void) { }
182static inline void cpu_load_update_nohz_stop(void) { }
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Paul E. McKenneyb637a322012-09-19 16:58:38 -0700185extern void dump_cpu_task(int cpu);
186
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200187struct seq_file;
188struct cfs_rq;
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200189struct task_group;
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200190#ifdef CONFIG_SCHED_DEBUG
191extern void proc_sched_show_task(struct task_struct *p, struct seq_file *m);
192extern void proc_sched_set_task(struct task_struct *p);
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds4a8342d2005-09-29 15:18:21 -0700195/*
196 * Task state bitmask. NOTE! These bits are also
197 * encoded in fs/proc/array.c: get_task_state().
198 *
199 * We have two separate sets of flags: task->state
200 * is about runnability, while task->exit_state are
201 * about the task exiting. Confusing, but this way
202 * modifying one set can't modify the other one by
203 * mistake.
204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#define TASK_RUNNING 0
206#define TASK_INTERRUPTIBLE 1
207#define TASK_UNINTERRUPTIBLE 2
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500208#define __TASK_STOPPED 4
209#define __TASK_TRACED 8
Linus Torvalds4a8342d2005-09-29 15:18:21 -0700210/* in tsk->exit_state */
Oleg Nesterovad866222014-04-07 15:38:46 -0700211#define EXIT_DEAD 16
212#define EXIT_ZOMBIE 32
Oleg Nesterovabd50b32014-04-07 15:38:42 -0700213#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
Linus Torvalds4a8342d2005-09-29 15:18:21 -0700214/* in tsk->state again */
Mike Galbraithaf927232007-10-15 17:00:13 +0200215#define TASK_DEAD 64
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500216#define TASK_WAKEKILL 128
Peter Zijlstrae9c84312009-09-15 14:43:03 +0200217#define TASK_WAKING 256
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200218#define TASK_PARKED 512
Peter Zijlstra80ed87c2015-05-08 14:23:45 +0200219#define TASK_NOLOAD 1024
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200220#define TASK_NEW 2048
221#define TASK_STATE_MAX 4096
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500222
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200223#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
Peter Zijlstra73342152009-12-17 13:16:27 +0100224
Davidlohr Bueso642fa442017-01-03 13:43:14 -0800225/* Convenience macros for the sake of set_current_state */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500226#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
227#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
228#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Peter Zijlstra80ed87c2015-05-08 14:23:45 +0200230#define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
231
Matthew Wilcox92a1f4b2007-12-06 10:55:25 -0500232/* Convenience macros for the sake of wake_up */
233#define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500234#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
Matthew Wilcox92a1f4b2007-12-06 10:55:25 -0500235
236/* get_task_state() */
237#define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500238 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
Oleg Nesterov74e37202014-01-23 15:55:35 -0800239 __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
Matthew Wilcox92a1f4b2007-12-06 10:55:25 -0500240
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500241#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
242#define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
Matthew Wilcox92a1f4b2007-12-06 10:55:25 -0500243#define task_is_stopped_or_traced(task) \
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500244 ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
Matthew Wilcox92a1f4b2007-12-06 10:55:25 -0500245#define task_contributes_to_load(task) \
Nathan Lynche3c8ca82009-04-08 19:45:12 -0500246 ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
Peter Zijlstra80ed87c2015-05-08 14:23:45 +0200247 (task->flags & PF_FROZEN) == 0 && \
248 (task->state & TASK_NOLOAD) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Peter Zijlstra8eb23b92014-09-24 10:18:55 +0200250#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
251
Peter Zijlstra8eb23b92014-09-24 10:18:55 +0200252#define __set_current_state(state_value) \
253 do { \
254 current->task_state_change = _THIS_IP_; \
255 current->state = (state_value); \
256 } while (0)
257#define set_current_state(state_value) \
258 do { \
259 current->task_state_change = _THIS_IP_; \
Peter Zijlstraa2250232016-10-19 15:45:27 +0200260 smp_store_mb(current->state, (state_value)); \
Peter Zijlstra8eb23b92014-09-24 10:18:55 +0200261 } while (0)
262
263#else
Andrew Morton498d0c52005-09-13 01:25:14 -0700264/*
265 * set_current_state() includes a barrier so that the write of current->state
266 * is correctly serialised wrt the caller's subsequent test of whether to
267 * actually sleep:
268 *
Peter Zijlstraa2250232016-10-19 15:45:27 +0200269 * for (;;) {
Andrew Morton498d0c52005-09-13 01:25:14 -0700270 * set_current_state(TASK_UNINTERRUPTIBLE);
Peter Zijlstraa2250232016-10-19 15:45:27 +0200271 * if (!need_sleep)
272 * break;
Andrew Morton498d0c52005-09-13 01:25:14 -0700273 *
Peter Zijlstraa2250232016-10-19 15:45:27 +0200274 * schedule();
275 * }
276 * __set_current_state(TASK_RUNNING);
277 *
278 * If the caller does not need such serialisation (because, for instance, the
279 * condition test and condition change and wakeup are under the same lock) then
280 * use __set_current_state().
281 *
282 * The above is typically ordered against the wakeup, which does:
283 *
284 * need_sleep = false;
285 * wake_up_state(p, TASK_UNINTERRUPTIBLE);
286 *
287 * Where wake_up_state() (and all other wakeup primitives) imply enough
288 * barriers to order the store of the variable against wakeup.
289 *
290 * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
291 * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
292 * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
293 *
294 * This is obviously fine, since they both store the exact same value.
295 *
296 * Also see the comments of try_to_wake_up().
Andrew Morton498d0c52005-09-13 01:25:14 -0700297 */
Peter Zijlstra8eb23b92014-09-24 10:18:55 +0200298#define __set_current_state(state_value) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 do { current->state = (state_value); } while (0)
Peter Zijlstra8eb23b92014-09-24 10:18:55 +0200300#define set_current_state(state_value) \
Peter Zijlstrab92b8b32015-05-12 10:51:55 +0200301 smp_store_mb(current->state, (state_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Peter Zijlstra8eb23b92014-09-24 10:18:55 +0200303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/* Task command name length */
306#define TASK_COMM_LEN 16
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#include <linux/spinlock.h>
309
310/*
311 * This serializes "schedule()" and also protects
312 * the run-queue from deletions/modifications (but
313 * _adding_ to the beginning of the run-queue has
314 * a separate lock).
315 */
316extern rwlock_t tasklist_lock;
317extern spinlock_t mmlist_lock;
318
Ingo Molnar36c8b582006-07-03 00:25:41 -0700319struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Paul E. McKenneydb1466b2010-03-03 07:46:56 -0800321#ifdef CONFIG_PROVE_RCU
322extern int lockdep_tasklist_lock_is_held(void);
323#endif /* #ifdef CONFIG_PROVE_RCU */
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325extern void sched_init(void);
326extern void sched_init_smp(void);
Harvey Harrison2d07b252008-02-15 09:56:34 -0800327extern asmlinkage void schedule_tail(struct task_struct *prev);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700328extern void init_idle(struct task_struct *idle, int cpu);
Ingo Molnar1df21052007-07-09 18:51:58 +0200329extern void init_idle_bootup_task(struct task_struct *idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Rik van Riel3fa08182015-03-09 12:12:07 -0400331extern cpumask_var_t cpu_isolated_map;
332
Andrew Morton89f19f02009-09-19 11:55:44 -0700333extern int runqueue_is_locked(int cpu);
Ingo Molnar017730c2008-05-12 21:20:52 +0200334
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200335#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
Alex Shic1cc0172012-09-10 15:10:58 +0800336extern void nohz_balance_enter_idle(int cpu);
Suresh Siddha69e1e812011-12-01 17:07:33 -0800337extern void set_cpu_sd_state_idle(void);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000338extern int get_nohz_timer_target(void);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700339#else
Alex Shic1cc0172012-09-10 15:10:58 +0800340static inline void nohz_balance_enter_idle(int cpu) { }
Peter Zijlstrafdaabd82011-12-06 12:47:55 +0100341static inline void set_cpu_sd_state_idle(void) { }
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800344/*
Ingo Molnar39bc89f2007-04-25 20:50:03 -0700345 * Only dump TASK_* tasks. (0 for all tasks)
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800346 */
347extern void show_state_filter(unsigned long state_filter);
348
349static inline void show_state(void)
350{
Ingo Molnar39bc89f2007-04-25 20:50:03 -0700351 show_state_filter(0);
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354extern void show_regs(struct pt_regs *);
355
356/*
357 * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
358 * task), SP is the stack pointer of the first frame that should be shown in the back
359 * trace (or NULL if the entire call-chain of the task should be shown).
360 */
361extern void show_stack(struct task_struct *task, unsigned long *sp);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363extern void cpu_init (void);
364extern void trap_init(void);
365extern void update_process_times(int user);
366extern void scheduler_tick(void);
Thomas Gleixner9cf72432016-03-10 12:54:09 +0100367extern int sched_cpu_starting(unsigned int cpu);
Thomas Gleixner40190a72016-03-10 12:54:13 +0100368extern int sched_cpu_activate(unsigned int cpu);
369extern int sched_cpu_deactivate(unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +0100371#ifdef CONFIG_HOTPLUG_CPU
372extern int sched_cpu_dying(unsigned int cpu);
373#else
374# define sched_cpu_dying NULL
375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ingo Molnar82a1fcb2008-01-25 21:08:02 +0100377extern void sched_show_task(struct task_struct *p);
378
Frederic Weisbecker19cc36c2010-05-13 02:30:49 +0200379#ifdef CONFIG_LOCKUP_DETECTOR
Tejun Heo03e0d462015-12-08 11:28:04 -0500380extern void touch_softlockup_watchdog_sched(void);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700381extern void touch_softlockup_watchdog(void);
Jason Wesseld6ad3e22010-01-27 16:25:22 -0600382extern void touch_softlockup_watchdog_sync(void);
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -0700383extern void touch_all_softlockup_watchdogs(void);
Don Zickus332fbdb2010-05-07 17:11:45 -0400384extern int proc_dowatchdog_thresh(struct ctl_table *table, int write,
385 void __user *buffer,
386 size_t *lenp, loff_t *ppos);
Ingo Molnar9c44bc02008-05-12 21:21:04 +0200387extern unsigned int softlockup_panic;
Don Zickusac1f5912015-11-05 18:44:44 -0800388extern unsigned int hardlockup_panic;
Peter Zijlstra004417a2010-11-25 18:38:29 +0100389void lockup_detector_init(void);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700390#else
Tejun Heo03e0d462015-12-08 11:28:04 -0500391static inline void touch_softlockup_watchdog_sched(void)
392{
393}
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700394static inline void touch_softlockup_watchdog(void)
395{
396}
Jason Wesseld6ad3e22010-01-27 16:25:22 -0600397static inline void touch_softlockup_watchdog_sync(void)
398{
399}
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -0700400static inline void touch_all_softlockup_watchdogs(void)
401{
402}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100403static inline void lockup_detector_init(void)
404{
405}
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700406#endif
407
Marcelo Tosatti8b414522013-10-11 21:39:26 -0300408#ifdef CONFIG_DETECT_HUNG_TASK
409void reset_hung_task_detector(void);
410#else
411static inline void reset_hung_task_detector(void)
412{
413}
414#endif
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/* Attach to any functions which should be ignored in wchan output. */
417#define __sched __attribute__((__section__(".sched.text")))
Ingo Molnardeaf2222007-11-28 15:52:56 +0100418
419/* Linker adds these: start and end of __sched functions */
420extern char __sched_text_start[], __sched_text_end[];
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* Is this address in the __sched functions? */
423extern int in_sched_functions(unsigned long addr);
424
425#define MAX_SCHEDULE_TIMEOUT LONG_MAX
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800426extern signed long schedule_timeout(signed long timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -0700427extern signed long schedule_timeout_interruptible(signed long timeout);
Matthew Wilcox294d5cc2007-12-06 11:59:46 -0500428extern signed long schedule_timeout_killable(signed long timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -0700429extern signed long schedule_timeout_uninterruptible(signed long timeout);
Andrew Morton69b27ba2016-03-25 14:20:21 -0700430extern signed long schedule_timeout_idle(signed long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431asmlinkage void schedule(void);
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +0100432extern void schedule_preempt_disabled(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Tejun Heo10ab5642016-10-28 12:58:10 -0400434extern int __must_check io_schedule_prepare(void);
435extern void io_schedule_finish(int token);
NeilBrown9cff8ad2015-02-13 15:49:17 +1100436extern long io_schedule_timeout(long timeout);
Tejun Heo10ab5642016-10-28 12:58:10 -0400437extern void io_schedule(void);
NeilBrown9cff8ad2015-02-13 15:49:17 +1100438
Peter Zijlstra9af65282016-09-13 18:37:29 +0200439void __noreturn do_task_dead(void);
440
Serge E. Hallynab516012006-10-02 02:18:06 -0700441struct nsproxy;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700442struct user_namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Howellsefc1a3b2010-01-15 17:01:35 -0800444#ifdef CONFIG_MMU
445extern void arch_pick_mmap_layout(struct mm_struct *mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446extern unsigned long
447arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
448 unsigned long, unsigned long);
449extern unsigned long
450arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
451 unsigned long len, unsigned long pgoff,
452 unsigned long flags);
David Howellsefc1a3b2010-01-15 17:01:35 -0800453#else
454static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Kees Cookd049f742013-11-12 15:11:17 -0800457#define SUID_DUMP_DISABLE 0 /* No setuid dumping */
458#define SUID_DUMP_USER 1 /* Dump as user of process */
459#define SUID_DUMP_ROOT 2 /* Dump as root */
460
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700461/* mm flags */
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700462
Oleg Nesterov7288e112014-01-23 15:55:32 -0800463/* for SUID_DUMP_* above */
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -0700464#define MMF_DUMPABLE_BITS 2
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700465#define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -0700466
Oleg Nesterov942be382014-01-23 15:55:34 -0800467extern void set_dumpable(struct mm_struct *mm, int value);
468/*
469 * This returns the actual value of the suid_dumpable flag. For things
470 * that are using this for checking for privilege transitions, it must
471 * test against SUID_DUMP_USER rather than treating it as a boolean
472 * value.
473 */
474static inline int __get_dumpable(unsigned long mm_flags)
475{
476 return mm_flags & MMF_DUMPABLE_MASK;
477}
478
479static inline int get_dumpable(struct mm_struct *mm)
480{
481 return __get_dumpable(mm->flags);
482}
483
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -0700484/* coredump filter bits */
485#define MMF_DUMP_ANON_PRIVATE 2
486#define MMF_DUMP_ANON_SHARED 3
487#define MMF_DUMP_MAPPED_PRIVATE 4
488#define MMF_DUMP_MAPPED_SHARED 5
Roland McGrath82df3972007-10-16 23:27:02 -0700489#define MMF_DUMP_ELF_HEADERS 6
KOSAKI Motohiroe575f112008-10-18 20:27:08 -0700490#define MMF_DUMP_HUGETLB_PRIVATE 7
491#define MMF_DUMP_HUGETLB_SHARED 8
Ross Zwisler50378352015-10-05 16:33:36 -0600492#define MMF_DUMP_DAX_PRIVATE 9
493#define MMF_DUMP_DAX_SHARED 10
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700494
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -0700495#define MMF_DUMP_FILTER_SHIFT MMF_DUMPABLE_BITS
Ross Zwisler50378352015-10-05 16:33:36 -0600496#define MMF_DUMP_FILTER_BITS 9
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -0700497#define MMF_DUMP_FILTER_MASK \
498 (((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
499#define MMF_DUMP_FILTER_DEFAULT \
KOSAKI Motohiroe575f112008-10-18 20:27:08 -0700500 ((1 << MMF_DUMP_ANON_PRIVATE) | (1 << MMF_DUMP_ANON_SHARED) |\
Roland McGrath656eb2c2008-10-18 20:28:23 -0700501 (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
502
503#ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
504# define MMF_DUMP_MASK_DEFAULT_ELF (1 << MMF_DUMP_ELF_HEADERS)
505#else
506# define MMF_DUMP_MASK_DEFAULT_ELF 0
507#endif
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700508 /* leave room for more dump flags */
509#define MMF_VM_MERGEABLE 16 /* KSM may merge identical pages */
Andrea Arcangeliba761492011-01-13 15:46:58 -0800510#define MMF_VM_HUGEPAGE 17 /* set when VM_HUGEPAGE is set on vma */
Stanislav Kinsburskiy3fb4afd2016-12-12 16:40:42 -0800511/*
512 * This one-shot flag is dropped due to necessity of changing exe once again
513 * on NFS restore
514 */
515//#define MMF_EXE_FILE_CHANGED 18 /* see prctl_set_mm_exe_file() */
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700516
Oleg Nesterov9f68f6722012-08-19 16:15:09 +0200517#define MMF_HAS_UPROBES 19 /* has uprobes */
518#define MMF_RECALC_UPROBES 20 /* MMF_HAS_UPROBES can be wrong */
Michal Hocko862e3072016-10-07 16:58:57 -0700519#define MMF_OOM_SKIP 21 /* mm is of no interest for the OOM killer */
Michal Hocko3f70dc32016-10-07 16:59:06 -0700520#define MMF_UNSTABLE 22 /* mm is unstable for copy_from_user */
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700521#define MMF_HUGE_ZERO_PAGE 23 /* mm has ever used the global huge zero page */
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200522
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700523#define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525struct sighand_struct {
526 atomic_t count;
527 struct k_sigaction action[_NSIG];
528 spinlock_t siglock;
Davide Libenzib8fceee2007-09-20 12:40:16 -0700529 wait_queue_head_t signalfd_wqh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530};
531
KaiGai Kohei0e464812006-06-25 05:49:24 -0700532struct pacct_struct {
KaiGai Koheif6ec29a2006-06-25 05:49:25 -0700533 int ac_flag;
534 long ac_exitcode;
KaiGai Kohei0e464812006-06-25 05:49:24 -0700535 unsigned long ac_mem;
Frederic Weisbeckerd4bc42a2017-01-31 04:09:28 +0100536 u64 ac_utime, ac_stime;
KaiGai Kohei77787bf2006-06-25 05:49:26 -0700537 unsigned long ac_minflt, ac_majflt;
KaiGai Kohei0e464812006-06-25 05:49:24 -0700538};
539
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200540struct cpu_itimer {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100541 u64 expires;
542 u64 incr;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200543};
544
Frank Mayharf06febc2008-09-12 09:54:39 -0700545/**
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200546 * struct prev_cputime - snaphsot of system and user cputime
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100547 * @utime: time spent in user mode
548 * @stime: time spent in system mode
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200549 * @lock: protects the above two fields
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100550 *
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200551 * Stores previous user/system time values such that we can guarantee
552 * monotonicity.
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100553 */
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200554struct prev_cputime {
555#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100556 u64 utime;
557 u64 stime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200558 raw_spinlock_t lock;
559#endif
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100560};
561
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200562static inline void prev_cputime_init(struct prev_cputime *prev)
563{
564#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
565 prev->utime = prev->stime = 0;
566 raw_spin_lock_init(&prev->lock);
567#endif
568}
569
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100570/**
Frank Mayharf06febc2008-09-12 09:54:39 -0700571 * struct task_cputime - collected CPU time counts
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100572 * @utime: time spent in user mode, in nanoseconds
573 * @stime: time spent in kernel mode, in nanoseconds
Frank Mayharf06febc2008-09-12 09:54:39 -0700574 * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
Ingo Molnar5ce73a42008-09-14 17:11:46 +0200575 *
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200576 * This structure groups together three kinds of CPU time that are tracked for
577 * threads and thread groups. Most things considering CPU time want to group
578 * these counts together and treat all three of them in parallel.
Frank Mayharf06febc2008-09-12 09:54:39 -0700579 */
580struct task_cputime {
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100581 u64 utime;
582 u64 stime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700583 unsigned long long sum_exec_runtime;
584};
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200585
Frank Mayharf06febc2008-09-12 09:54:39 -0700586/* Alternate field names when used to cache expirations. */
Frank Mayharf06febc2008-09-12 09:54:39 -0700587#define virt_exp utime
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200588#define prof_exp stime
Frank Mayharf06febc2008-09-12 09:54:39 -0700589#define sched_exp sum_exec_runtime
590
Jason Low971e8a982015-04-28 13:00:23 -0700591/*
592 * This is the atomic variant of task_cputime, which can be used for
593 * storing and updating task_cputime statistics without locking.
594 */
595struct task_cputime_atomic {
596 atomic64_t utime;
597 atomic64_t stime;
598 atomic64_t sum_exec_runtime;
599};
600
601#define INIT_CPUTIME_ATOMIC \
602 (struct task_cputime_atomic) { \
603 .utime = ATOMIC64_INIT(0), \
604 .stime = ATOMIC64_INIT(0), \
605 .sum_exec_runtime = ATOMIC64_INIT(0), \
606 }
607
Peter Zijlstra609ca062015-09-28 17:52:18 +0200608#define PREEMPT_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
Peter Zijlstraa233f112013-09-23 19:04:26 +0200609
Peter Zijlstrac99e6ef2009-07-10 14:57:56 +0200610/*
Peter Zijlstra87dcbc02015-09-28 17:45:40 +0200611 * Disable preemption until the scheduler is running -- use an unconditional
612 * value so that it also works on !PREEMPT_COUNT kernels.
Peter Zijlstrad86ee482009-07-10 14:57:57 +0200613 *
Peter Zijlstra87dcbc02015-09-28 17:45:40 +0200614 * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
Peter Zijlstrac99e6ef2009-07-10 14:57:56 +0200615 */
Peter Zijlstra87dcbc02015-09-28 17:45:40 +0200616#define INIT_PREEMPT_COUNT PREEMPT_OFFSET
Peter Zijlstrac99e6ef2009-07-10 14:57:56 +0200617
Peter Zijlstra609ca062015-09-28 17:52:18 +0200618/*
619 * Initial preempt_count value; reflects the preempt_count schedule invariant
620 * which states that during context switches:
621 *
622 * preempt_count() == 2*PREEMPT_DISABLE_OFFSET
623 *
624 * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
625 * Note: See finish_task_switch().
626 */
627#define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100628
Frank Mayharf06febc2008-09-12 09:54:39 -0700629/**
630 * struct thread_group_cputimer - thread group interval timer counts
Jason Low920ce392015-05-08 14:31:50 -0700631 * @cputime_atomic: atomic thread group interval timers.
Jason Lowd5c373e2015-10-14 12:07:55 -0700632 * @running: true when there are timers running and
633 * @cputime_atomic receives updates.
Jason Lowc8d75aa2015-10-14 12:07:56 -0700634 * @checking_timer: true when a thread in the group is in the
635 * process of checking for thread group timers.
Frank Mayharf06febc2008-09-12 09:54:39 -0700636 *
637 * This structure contains the version of task_cputime, above, that is
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100638 * used for thread group CPU timer calculations.
Frank Mayharf06febc2008-09-12 09:54:39 -0700639 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100640struct thread_group_cputimer {
Jason Low71107442015-04-28 13:00:24 -0700641 struct task_cputime_atomic cputime_atomic;
Jason Lowd5c373e2015-10-14 12:07:55 -0700642 bool running;
Jason Lowc8d75aa2015-10-14 12:07:56 -0700643 bool checking_timer;
Frank Mayharf06febc2008-09-12 09:54:39 -0700644};
Frank Mayharf06febc2008-09-12 09:54:39 -0700645
Ben Blum4714d1d2011-05-26 16:25:18 -0700646#include <linux/rwsem.h>
Mike Galbraith5091faa2010-11-30 14:18:03 +0100647struct autogroup;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
Jonathan Neuschäfere815f0a2011-03-21 20:24:47 +0100650 * NOTE! "signal_struct" does not have its own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * locking, because a shared signal_struct always
652 * implies a shared sighand_struct, so locking
653 * sighand_struct is always a proper superset of
654 * the locking of signal_struct.
655 */
656struct signal_struct {
Oleg Nesterovea6d2902010-05-26 14:43:16 -0700657 atomic_t sigcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 atomic_t live;
Oleg Nesterovb3ac0222010-05-26 14:43:24 -0700659 int nr_threads;
Oleg Nesterov0c740d02014-01-21 15:49:56 -0800660 struct list_head thread_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 wait_queue_head_t wait_chldexit; /* for wait4() */
663
664 /* current thread group signal load-balancing target: */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700665 struct task_struct *curr_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* shared signal handling: */
668 struct sigpending shared_pending;
669
670 /* thread group exit support */
671 int group_exit_code;
672 /* overloaded:
673 * - notify group_exit_task when ->count is equal to notify_count
674 * - everyone except group_exit_task is stopped during signal delivery
675 * of fatal signals, group_exit_task processes the signal.
676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int notify_count;
Richard Kennedy07dd20e2008-08-01 13:18:04 +0100678 struct task_struct *group_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /* thread group stop support, overloads group_exit_code too */
681 int group_stop_count;
682 unsigned int flags; /* see SIGNAL_* flags below */
683
Lennart Poetteringebec18a2012-03-23 15:01:54 -0700684 /*
685 * PR_SET_CHILD_SUBREAPER marks a process, like a service
686 * manager, to re-parent orphan (double-forking) child processes
687 * to this process instead of 'init'. The service manager is
688 * able to receive SIGCHLD signals and is able to investigate
689 * the process until it calls wait(). All children of this
690 * process will inherit a flag if they should look for a
691 * child_subreaper process at exit.
692 */
693 unsigned int is_child_subreaper:1;
694 unsigned int has_child_subreaper:1;
695
Nicolas Pitreb18b6a92017-01-21 00:09:08 -0500696#ifdef CONFIG_POSIX_TIMERS
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* POSIX.1b Interval Timers */
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400699 int posix_timer_id;
700 struct list_head posix_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* ITIMER_REAL timer for the process */
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800703 struct hrtimer real_timer;
704 ktime_t it_real_incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200706 /*
707 * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
708 * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
709 * values are defined to 0 and 1 respectively
710 */
711 struct cpu_itimer it[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Frank Mayharf06febc2008-09-12 09:54:39 -0700713 /*
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100714 * Thread group totals for process CPU timers.
715 * See thread_group_cputimer(), et al, for details.
Frank Mayharf06febc2008-09-12 09:54:39 -0700716 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100717 struct thread_group_cputimer cputimer;
Frank Mayharf06febc2008-09-12 09:54:39 -0700718
719 /* Earliest-expiration cache. */
720 struct task_cputime cputime_expires;
721
Nicolas Pitreb18b6a92017-01-21 00:09:08 -0500722 struct list_head cpu_timers[3];
723
724#endif
725
726 struct pid *leader_pid;
727
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200728#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100729 atomic_t tick_dep_mask;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200730#endif
731
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800732 struct pid *tty_old_pgrp;
Cedric Le Goater1ec320a2006-12-08 02:37:55 -0800733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* boolean value for session group leader */
735 int leader;
736
737 struct tty_struct *tty; /* NULL if no tty */
738
Mike Galbraith5091faa2010-11-30 14:18:03 +0100739#ifdef CONFIG_SCHED_AUTOGROUP
740 struct autogroup *autogroup;
741#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
743 * Cumulative resource counters for dead threads in the group,
744 * and for reaped dead child processes forked by this group.
745 * Live threads maintain their own counters and add to these
746 * in __exit_signal, except for the group leader.
747 */
Rik van Riele78c3492014-08-16 13:40:10 -0400748 seqlock_t stats_lock;
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100749 u64 utime, stime, cutime, cstime;
Frederic Weisbecker16a6d9b2017-01-31 04:09:21 +0100750 u64 gtime;
751 u64 cgtime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200752 struct prev_cputime prev_cputime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
754 unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -0700755 unsigned long inblock, oublock, cinblock, coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -0700756 unsigned long maxrss, cmaxrss;
Andrea Righi940389b2008-07-28 00:48:12 +0200757 struct task_io_accounting ioac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
Peter Zijlstra32bd6712009-02-05 12:24:15 +0100760 * Cumulative ns of schedule CPU time fo dead threads in the
761 * group, not including a zombie group leader, (This only differs
762 * from jiffies_to_ns(utime + stime) if sched_clock uses something
763 * other than jiffies.)
764 */
765 unsigned long long sum_sched_runtime;
766
767 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * We don't bother to synchronize most readers of this at all,
769 * because there is no reader checking a limit that actually needs
770 * to get both rlim_cur and rlim_max atomically, and either one
771 * alone is a single word that can safely be read normally.
772 * getrlimit/setrlimit use task_lock(current->group_leader) to
773 * protect this instead of the siglock, because they really
774 * have no need to disable irqs.
775 */
776 struct rlimit rlim[RLIM_NLIMITS];
777
KaiGai Kohei0e464812006-06-25 05:49:24 -0700778#ifdef CONFIG_BSD_PROCESS_ACCT
779 struct pacct_struct pacct; /* per-process accounting information */
780#endif
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700781#ifdef CONFIG_TASKSTATS
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700782 struct taskstats *stats;
783#endif
Miloslav Trmac522ed772007-07-15 23:40:56 -0700784#ifdef CONFIG_AUDIT
785 unsigned audit_tty;
786 struct tty_audit_buf *tty_audit_buf;
787#endif
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700788
Tetsuo Handac96fc2d2016-05-23 16:23:57 -0700789 /*
790 * Thread is the potential origin of an oom condition; kill first on
791 * oom
792 */
793 bool oom_flag_origin;
David Rientjesa9c58b902012-12-11 16:02:54 -0800794 short oom_score_adj; /* OOM kill score adjustment */
795 short oom_score_adj_min; /* OOM kill score adjustment min value.
796 * Only settable by CAP_SYS_RESOURCE. */
Michal Hocko26db62f2016-10-07 16:58:51 -0700797 struct mm_struct *oom_mm; /* recorded mm when the thread group got
798 * killed by the oom killer */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700799
800 struct mutex cred_guard_mutex; /* guard against foreign influences on
801 * credential calculations
802 * (notably. ptrace) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
805/*
806 * Bits in flags field of signal_struct.
807 */
808#define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */
Oleg Nesterovee77f072011-04-01 20:12:38 +0200809#define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */
810#define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */
Oleg Nesterov403bad72013-04-30 15:28:10 -0700811#define SIGNAL_GROUP_COREDUMP 0x00000008 /* coredump in progress */
Oleg Nesterove4420552008-04-30 00:52:44 -0700812/*
813 * Pending notifications to parent.
814 */
815#define SIGNAL_CLD_STOPPED 0x00000010
816#define SIGNAL_CLD_CONTINUED 0x00000020
817#define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700819#define SIGNAL_UNKILLABLE 0x00000040 /* for init: ignore fatal signals */
820
Jamie Iles2d39b3c2017-01-10 16:57:54 -0800821#define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
822 SIGNAL_STOP_CONTINUED)
823
824static inline void signal_set_stop_flags(struct signal_struct *sig,
825 unsigned int flags)
826{
827 WARN_ON(sig->flags & (SIGNAL_GROUP_EXIT|SIGNAL_GROUP_COREDUMP));
828 sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
829}
830
Oleg Nesteroved5d2ca2008-02-04 22:27:24 -0800831/* If true, all threads except ->group_exit_task have pending SIGKILL */
832static inline int signal_group_exit(const struct signal_struct *sig)
833{
834 return (sig->flags & SIGNAL_GROUP_EXIT) ||
835 (sig->group_exit_task != NULL);
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
839 * Some day this will be a full-fledged user tracking system..
840 */
841struct user_struct {
842 atomic_t __count; /* reference count */
843 atomic_t processes; /* How many processes does this user have? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 atomic_t sigpending; /* How many pending signals does this user have? */
Eric Paris4afeff82010-10-28 17:21:58 -0400845#ifdef CONFIG_FANOTIFY
846 atomic_t fanotify_listeners;
847#endif
Davide Libenzi7ef99642008-12-01 13:13:55 -0800848#ifdef CONFIG_EPOLL
Robin Holt52bd19f72011-01-12 17:00:01 -0800849 atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
Davide Libenzi7ef99642008-12-01 13:13:55 -0800850#endif
Alexey Dobriyan970a8642007-10-16 23:30:09 -0700851#ifdef CONFIG_POSIX_MQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* protected by mq_lock */
853 unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
Alexey Dobriyan970a8642007-10-16 23:30:09 -0700854#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned long locked_shm; /* How many pages of mlocked shm ? */
willy tarreau712f4aa2016-01-10 07:54:56 +0100856 unsigned long unix_inflight; /* How many files in flight in unix sockets */
Willy Tarreau759c0112016-01-18 16:36:09 +0100857 atomic_long_t pipe_bufs; /* how many pages are allocated in pipe buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859#ifdef CONFIG_KEYS
860 struct key *uid_keyring; /* UID specific keyring */
861 struct key *session_keyring; /* UID's default session keyring */
862#endif
863
864 /* Hash table maintenance information */
Pavel Emelyanov735de222007-09-18 22:46:44 -0700865 struct hlist_node uidhash_node;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800866 kuid_t uid;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200867
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700868#if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL)
Peter Zijlstra789f90f2009-05-15 15:19:27 +0200869 atomic_long_t locked_vm;
870#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871};
872
Kay Sieverseb41d942007-11-02 13:47:53 +0100873extern int uids_sysfs_init(void);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200874
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800875extern struct user_struct *find_user(kuid_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877extern struct user_struct root_user;
878#define INIT_USER (&root_user)
879
David Howellsb6dff3e2008-11-14 10:39:16 +1100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881struct backing_dev_info;
882struct reclaim_state;
883
Naveen N. Raof6db83472015-06-25 23:53:37 +0530884#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885struct sched_info {
886 /* cumulative counters */
Ingo Molnar2d723762007-10-15 17:00:12 +0200887 unsigned long pcount; /* # of times run on this cpu */
Ken Chen9c2c4802008-12-16 23:41:22 -0800888 unsigned long long run_delay; /* time spent waiting on a runqueue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* timestamps */
Balbir Singh172ba842007-07-09 18:52:00 +0200891 unsigned long long last_arrival,/* when we last ran on a cpu */
892 last_queued; /* when we were last queued to run */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893};
Naveen N. Raof6db83472015-06-25 23:53:37 +0530894#endif /* CONFIG_SCHED_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Shailabh Nagarca74e922006-07-14 00:24:36 -0700896#ifdef CONFIG_TASK_DELAY_ACCT
897struct task_delay_info {
898 spinlock_t lock;
899 unsigned int flags; /* Private per-task flags */
900
901 /* For each stat XXX, add following, aligned appropriately
902 *
903 * struct timespec XXX_start, XXX_end;
904 * u64 XXX_delay;
905 * u32 XXX_count;
906 *
907 * Atomicity of updates to XXX_delay, XXX_count protected by
908 * single lock above (split into XXX_lock if contention is an issue).
909 */
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700910
911 /*
912 * XXX_count is incremented on every XXX operation, the delay
913 * associated with the operation is added to XXX_delay.
914 * XXX_delay contains the accumulated delay time in nanoseconds.
915 */
Thomas Gleixner9667a232014-07-16 21:04:35 +0000916 u64 blkio_start; /* Shared by blkio, swapin */
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700917 u64 blkio_delay; /* wait for sync block io completion */
918 u64 swapin_delay; /* wait for swapin block io completion */
919 u32 blkio_count; /* total count of the number of sync block */
920 /* io operations performed */
921 u32 swapin_count; /* total count of the number of swapin block */
922 /* io operations performed */
Keika Kobayashi873b4772008-07-25 01:48:52 -0700923
Thomas Gleixner9667a232014-07-16 21:04:35 +0000924 u64 freepages_start;
Keika Kobayashi873b4772008-07-25 01:48:52 -0700925 u64 freepages_delay; /* wait for memory reclaim */
926 u32 freepages_count; /* total count of memory reclaim */
Shailabh Nagarca74e922006-07-14 00:24:36 -0700927};
Chandra Seetharaman52f17b62006-07-14 00:24:38 -0700928#endif /* CONFIG_TASK_DELAY_ACCT */
929
930static inline int sched_info_on(void)
931{
932#ifdef CONFIG_SCHEDSTATS
933 return 1;
934#elif defined(CONFIG_TASK_DELAY_ACCT)
935 extern int delayacct_on;
936 return delayacct_on;
937#else
938 return 0;
Shailabh Nagarca74e922006-07-14 00:24:36 -0700939#endif
Chandra Seetharaman52f17b62006-07-14 00:24:38 -0700940}
Shailabh Nagarca74e922006-07-14 00:24:36 -0700941
Mel Gormancb251762016-02-05 09:08:36 +0000942#ifdef CONFIG_SCHEDSTATS
943void force_schedstat_enabled(void);
944#endif
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
Yuyang Du6ecdd742016-04-05 12:12:26 +0800947 * Integer metrics need fixed point arithmetic, e.g., sched/fair
948 * has a few: load, load_avg, util_avg, freq, and capacity.
949 *
950 * We define a basic fixed point arithmetic range, and then formalize
951 * all these metrics based on that basic range.
952 */
953# define SCHED_FIXEDPOINT_SHIFT 10
954# define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956struct io_context; /* See blkdev.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Chen, Kenneth W383f2832005-09-09 13:02:02 -0700959#ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
Ingo Molnar36c8b582006-07-03 00:25:41 -0700960extern void prefetch_stack(struct task_struct *t);
Chen, Kenneth W383f2832005-09-09 13:02:02 -0700961#else
962static inline void prefetch_stack(struct task_struct *t) { }
963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965struct audit_context; /* See audit.c */
966struct mempolicy;
Jens Axboeb92ce552006-04-11 13:52:07 +0200967struct pipe_inode_info;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700968struct uts_namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Ingo Molnar20b8a592007-07-09 18:51:58 +0200970struct load_weight {
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100971 unsigned long weight;
972 u32 inv_weight;
Ingo Molnar20b8a592007-07-09 18:51:58 +0200973};
974
Yuyang Du9d89c252015-07-15 08:04:37 +0800975/*
Yuyang Du7b595332016-04-05 12:12:28 +0800976 * The load_avg/util_avg accumulates an infinite geometric series
977 * (see __update_load_avg() in kernel/sched/fair.c).
978 *
979 * [load_avg definition]
980 *
981 * load_avg = runnable% * scale_load_down(load)
982 *
983 * where runnable% is the time ratio that a sched_entity is runnable.
984 * For cfs_rq, it is the aggregated load_avg of all runnable and
Yuyang Du9d89c252015-07-15 08:04:37 +0800985 * blocked sched_entities.
Yuyang Du7b595332016-04-05 12:12:28 +0800986 *
987 * load_avg may also take frequency scaling into account:
988 *
989 * load_avg = runnable% * scale_load_down(load) * freq%
990 *
991 * where freq% is the CPU frequency normalized to the highest frequency.
992 *
993 * [util_avg definition]
994 *
995 * util_avg = running% * SCHED_CAPACITY_SCALE
996 *
997 * where running% is the time ratio that a sched_entity is running on
998 * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
999 * and blocked sched_entities.
1000 *
1001 * util_avg may also factor frequency scaling and CPU capacity scaling:
1002 *
1003 * util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
1004 *
1005 * where freq% is the same as above, and capacity% is the CPU capacity
1006 * normalized to the greatest capacity (due to uarch differences, etc).
1007 *
1008 * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
1009 * themselves are in the range of [0, 1]. To do fixed point arithmetics,
1010 * we therefore scale them to as large a range as necessary. This is for
1011 * example reflected by util_avg's SCHED_CAPACITY_SCALE.
1012 *
1013 * [Overflow issue]
1014 *
1015 * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
1016 * with the highest load (=88761), always runnable on a single cfs_rq,
1017 * and should not overflow as the number already hits PID_MAX_LIMIT.
1018 *
1019 * For all other cases (including 32-bit kernels), struct load_weight's
1020 * weight will overflow first before we do, because:
1021 *
1022 * Max(load_avg) <= Max(load.weight)
1023 *
1024 * Then it is the load_weight's responsibility to consider overflow
1025 * issues.
Yuyang Du9d89c252015-07-15 08:04:37 +08001026 */
Paul Turner9d85f212012-10-04 13:18:29 +02001027struct sched_avg {
Yuyang Du9d89c252015-07-15 08:04:37 +08001028 u64 last_update_time, load_sum;
1029 u32 util_sum, period_contrib;
1030 unsigned long load_avg, util_avg;
Paul Turner9d85f212012-10-04 13:18:29 +02001031};
1032
Ingo Molnar94c18222007-08-02 17:41:40 +02001033#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03001034struct sched_statistics {
Ingo Molnar94c18222007-08-02 17:41:40 +02001035 u64 wait_start;
1036 u64 wait_max;
Arjan van de Ven6d082592008-01-25 21:08:35 +01001037 u64 wait_count;
1038 u64 wait_sum;
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07001039 u64 iowait_count;
1040 u64 iowait_sum;
Ingo Molnar94c18222007-08-02 17:41:40 +02001041
1042 u64 sleep_start;
Ingo Molnar20b8a592007-07-09 18:51:58 +02001043 u64 sleep_max;
Ingo Molnar94c18222007-08-02 17:41:40 +02001044 s64 sum_sleep_runtime;
1045
1046 u64 block_start;
Ingo Molnar20b8a592007-07-09 18:51:58 +02001047 u64 block_max;
1048 u64 exec_max;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001049 u64 slice_max;
Ingo Molnarcc367732007-10-15 17:00:18 +02001050
Ingo Molnarcc367732007-10-15 17:00:18 +02001051 u64 nr_migrations_cold;
1052 u64 nr_failed_migrations_affine;
1053 u64 nr_failed_migrations_running;
1054 u64 nr_failed_migrations_hot;
1055 u64 nr_forced_migrations;
Ingo Molnarcc367732007-10-15 17:00:18 +02001056
1057 u64 nr_wakeups;
1058 u64 nr_wakeups_sync;
1059 u64 nr_wakeups_migrate;
1060 u64 nr_wakeups_local;
1061 u64 nr_wakeups_remote;
1062 u64 nr_wakeups_affine;
1063 u64 nr_wakeups_affine_attempts;
1064 u64 nr_wakeups_passive;
1065 u64 nr_wakeups_idle;
Lucas De Marchi41acab82010-03-10 23:37:45 -03001066};
1067#endif
1068
1069struct sched_entity {
1070 struct load_weight load; /* for load-balancing */
1071 struct rb_node run_node;
1072 struct list_head group_node;
1073 unsigned int on_rq;
1074
1075 u64 exec_start;
1076 u64 sum_exec_runtime;
1077 u64 vruntime;
1078 u64 prev_sum_exec_runtime;
1079
Lucas De Marchi41acab82010-03-10 23:37:45 -03001080 u64 nr_migrations;
1081
Lucas De Marchi41acab82010-03-10 23:37:45 -03001082#ifdef CONFIG_SCHEDSTATS
1083 struct sched_statistics statistics;
Ingo Molnar94c18222007-08-02 17:41:40 +02001084#endif
1085
Ingo Molnar20b8a592007-07-09 18:51:58 +02001086#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrafed14d42012-02-11 06:05:00 +01001087 int depth;
Ingo Molnar20b8a592007-07-09 18:51:58 +02001088 struct sched_entity *parent;
1089 /* rq on which this entity is (to be) queued: */
1090 struct cfs_rq *cfs_rq;
1091 /* rq "owned" by this entity/group: */
1092 struct cfs_rq *my_q;
1093#endif
Clark Williams8bd75c72013-02-07 09:47:07 -06001094
Alex Shi141965c2013-06-26 13:05:39 +08001095#ifdef CONFIG_SMP
Jiri Olsa5a107802015-12-08 21:23:59 +01001096 /*
1097 * Per entity load average tracking.
1098 *
1099 * Put into separate cache line so it does not
1100 * collide with read-mostly values above.
1101 */
1102 struct sched_avg avg ____cacheline_aligned_in_smp;
Paul Turner9d85f212012-10-04 13:18:29 +02001103#endif
Ingo Molnar20b8a592007-07-09 18:51:58 +02001104};
Ingo Molnar70b97a72006-07-03 00:25:42 -07001105
Peter Zijlstrafa717062008-01-25 21:08:27 +01001106struct sched_rt_entity {
1107 struct list_head run_list;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001108 unsigned long timeout;
Ying Xue57d2aa02012-07-17 15:03:43 +08001109 unsigned long watchdog_stamp;
Richard Kennedybee367e2008-08-01 13:24:08 +01001110 unsigned int time_slice;
Peter Zijlstraff77e462016-01-18 15:27:07 +01001111 unsigned short on_rq;
1112 unsigned short on_list;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001113
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001114 struct sched_rt_entity *back;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001115#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001116 struct sched_rt_entity *parent;
1117 /* rq on which this entity is (to be) queued: */
1118 struct rt_rq *rt_rq;
1119 /* rq "owned" by this entity/group: */
1120 struct rt_rq *my_q;
1121#endif
Peter Zijlstrafa717062008-01-25 21:08:27 +01001122};
1123
Dario Faggioliaab03e02013-11-28 11:14:43 +01001124struct sched_dl_entity {
1125 struct rb_node rb_node;
1126
1127 /*
1128 * Original scheduling parameters. Copied here from sched_attr
xiaofeng.yan4027d082014-05-09 03:21:27 +00001129 * during sched_setattr(), they will remain the same until
1130 * the next sched_setattr().
Dario Faggioliaab03e02013-11-28 11:14:43 +01001131 */
1132 u64 dl_runtime; /* maximum runtime for each instance */
1133 u64 dl_deadline; /* relative deadline of each instance */
Harald Gustafsson755378a2013-11-07 14:43:40 +01001134 u64 dl_period; /* separation of two instances (period) */
Dario Faggioli332ac172013-11-07 14:43:45 +01001135 u64 dl_bw; /* dl_runtime / dl_deadline */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001136
1137 /*
1138 * Actual scheduling parameters. Initialized with the values above,
1139 * they are continously updated during task execution. Note that
1140 * the remaining runtime could be < 0 in case we are in overrun.
1141 */
1142 s64 runtime; /* remaining runtime for this instance */
1143 u64 deadline; /* absolute deadline for this instance */
1144 unsigned int flags; /* specifying the scheduler behaviour */
1145
1146 /*
1147 * Some bool flags:
1148 *
1149 * @dl_throttled tells if we exhausted the runtime. If so, the
1150 * task has to wait for a replenishment to be performed at the
1151 * next firing of dl_timer.
1152 *
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001153 * @dl_boosted tells if we are boosted due to DI. If so we are
1154 * outside bandwidth enforcement mechanism (but only until we
Juri Lelli5bfd1262014-04-15 13:49:04 +02001155 * exit the critical section);
1156 *
1157 * @dl_yielded tells if task gave up the cpu before consuming
1158 * all its available runtime during the last job.
Dario Faggioliaab03e02013-11-28 11:14:43 +01001159 */
Luca Abeni72f9f3f2016-03-07 12:27:04 +01001160 int dl_throttled, dl_boosted, dl_yielded;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001161
1162 /*
1163 * Bandwidth enforcement timer. Each -deadline task has its
1164 * own bandwidth to be enforced, thus we need one timer per task.
1165 */
1166 struct hrtimer dl_timer;
1167};
Clark Williams8bd75c72013-02-07 09:47:07 -06001168
Paul E. McKenney1d082fd2014-08-14 16:01:53 -07001169union rcu_special {
1170 struct {
Paul E. McKenney8203d6d2015-08-02 13:53:17 -07001171 u8 blocked;
1172 u8 need_qs;
1173 u8 exp_need_qs;
1174 u8 pad; /* Otherwise the compiler can store garbage here. */
1175 } b; /* Bits. */
1176 u32 s; /* Set of bits. */
Paul E. McKenney1d082fd2014-08-14 16:01:53 -07001177};
Paul E. McKenney86848962009-08-27 15:00:12 -07001178struct rcu_node;
1179
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001180enum perf_event_task_context {
1181 perf_invalid_context = -1,
1182 perf_hw_context = 0,
Peter Zijlstra89a1e182010-09-07 17:34:50 +02001183 perf_sw_context,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001184 perf_nr_task_contexts,
1185};
1186
Ingo Molnareb61baf2017-02-01 17:09:06 +01001187struct wake_q_node {
1188 struct wake_q_node *next;
1189};
1190
Mel Gorman72b252a2015-09-04 15:47:32 -07001191/* Track pages that require TLB flushes */
1192struct tlbflush_unmap_batch {
1193 /*
1194 * Each bit set is a CPU that potentially has a TLB entry for one of
1195 * the PFNs being flushed. See set_tlb_ubc_flush_pending().
1196 */
1197 struct cpumask cpumask;
1198
1199 /* True if any bit in cpumask is set */
1200 bool flush_required;
Mel Gormand950c942015-09-04 15:47:35 -07001201
1202 /*
1203 * If true then the PTE was dirty when unmapped. The entry must be
1204 * flushed before IO is initiated or a stale TLB entry potentially
1205 * allows an update without redirtying the page.
1206 */
1207 bool writable;
Mel Gorman72b252a2015-09-04 15:47:32 -07001208};
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210struct task_struct {
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001211#ifdef CONFIG_THREAD_INFO_IN_TASK
1212 /*
1213 * For reasons of header soup (see current_thread_info()), this
1214 * must be the first element of task_struct.
1215 */
1216 struct thread_info thread_info;
1217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
Roman Zippelf7e42172007-05-09 02:35:17 -07001219 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 atomic_t usage;
William Cohen97dc32c2007-05-08 00:23:41 -07001221 unsigned int flags; /* per process flags, defined below */
1222 unsigned int ptrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Peter Williams2dd73a42006-06-27 02:54:34 -07001224#ifdef CONFIG_SMP
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001225 struct llist_node wake_entry;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02001226 int on_cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001227#ifdef CONFIG_THREAD_INFO_IN_TASK
1228 unsigned int cpu; /* current CPU */
1229#endif
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02001230 unsigned int wakee_flips;
Michael Wang62470412013-07-04 12:55:51 +08001231 unsigned long wakee_flip_decay_ts;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02001232 struct task_struct *last_wakee;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001233
1234 int wake_cpu;
Nick Piggin4866cde2005-06-25 14:57:23 -07001235#endif
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001236 int on_rq;
Ingo Molnar50e645a2007-07-09 18:52:00 +02001237
Ingo Molnarb29739f2006-06-27 02:54:51 -07001238 int prio, static_prio, normal_prio;
Richard Kennedyc7aceab2008-05-15 12:09:15 +01001239 unsigned int rt_priority;
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001240 const struct sched_class *sched_class;
Ingo Molnar20b8a592007-07-09 18:51:58 +02001241 struct sched_entity se;
Peter Zijlstrafa717062008-01-25 21:08:27 +01001242 struct sched_rt_entity rt;
Peter Zijlstra8323f262012-06-22 13:36:05 +02001243#ifdef CONFIG_CGROUP_SCHED
1244 struct task_group *sched_task_group;
1245#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +01001246 struct sched_dl_entity dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Avi Kivitye107be32007-07-26 13:40:43 +02001248#ifdef CONFIG_PREEMPT_NOTIFIERS
1249 /* list of struct preempt_notifier: */
1250 struct hlist_head preempt_notifiers;
1251#endif
1252
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -07001253#ifdef CONFIG_BLK_DEV_IO_TRACE
Jens Axboe2056a782006-03-23 20:00:26 +01001254 unsigned int btrace_seq;
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -07001255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
William Cohen97dc32c2007-05-08 00:23:41 -07001257 unsigned int policy;
Peter Zijlstra29baa742012-04-23 12:11:21 +02001258 int nr_cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 cpumask_t cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Paul E. McKenneya57eb942010-06-29 16:49:16 -07001261#ifdef CONFIG_PREEMPT_RCU
Paul E. McKenneye260be62008-01-25 21:08:24 +01001262 int rcu_read_lock_nesting;
Paul E. McKenney1d082fd2014-08-14 16:01:53 -07001263 union rcu_special rcu_read_unlock_special;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001264 struct list_head rcu_node_entry;
Paul E. McKenneya57eb942010-06-29 16:49:16 -07001265 struct rcu_node *rcu_blocked_node;
Pranith Kumar28f65692014-09-22 14:00:48 -04001266#endif /* #ifdef CONFIG_PREEMPT_RCU */
Paul E. McKenney8315f422014-06-27 13:42:20 -07001267#ifdef CONFIG_TASKS_RCU
1268 unsigned long rcu_tasks_nvcsw;
1269 bool rcu_tasks_holdout;
1270 struct list_head rcu_tasks_holdout_list;
Paul E. McKenney176f8f72014-08-04 17:43:50 -07001271 int rcu_tasks_idle_cpu;
Paul E. McKenney8315f422014-06-27 13:42:20 -07001272#endif /* #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneye260be62008-01-25 21:08:24 +01001273
Naveen N. Raof6db83472015-06-25 23:53:37 +05301274#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct sched_info sched_info;
1276#endif
1277
1278 struct list_head tasks;
Dario Faggioli806c09a2010-11-30 19:51:33 +01001279#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05001280 struct plist_node pushable_tasks;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001281 struct rb_node pushable_dl_tasks;
Dario Faggioli806c09a2010-11-30 19:51:33 +01001282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 struct mm_struct *mm, *active_mm;
Ingo Molnar314ff782017-02-03 11:03:31 +01001285
1286 /* Per-thread vma caching: */
1287 struct vmacache vmacache;
1288
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -08001289#if defined(SPLIT_RSS_COUNTING)
1290 struct task_rss_stat rss_stat;
1291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/* task state */
William Cohen97dc32c2007-05-08 00:23:41 -07001293 int exit_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 int exit_code, exit_signal;
1295 int pdeath_signal; /* The signal sent when the parent dies */
Palmer Dabbelte7cc4172015-04-30 21:19:55 -07001296 unsigned long jobctl; /* JOBCTL_*, siglock protected */
Andrei Epure9b89f6b2013-04-11 20:30:29 +03001297
1298 /* Used for emulating ABI behavior of previous Linux versions */
William Cohen97dc32c2007-05-08 00:23:41 -07001299 unsigned int personality;
Andrei Epure9b89f6b2013-04-11 20:30:29 +03001300
Peter Zijlstrabe958bd2015-11-25 16:02:07 +01001301 /* scheduler bits, serialized by scheduler locks */
Lennart Poetteringca94c442009-06-15 17:17:47 +02001302 unsigned sched_reset_on_fork:1;
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02001303 unsigned sched_contributes_to_load:1;
Peter Zijlstraff303e62015-04-17 20:05:30 +02001304 unsigned sched_migrated:1;
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02001305 unsigned sched_remote_wakeup:1;
Peter Zijlstrabe958bd2015-11-25 16:02:07 +01001306 unsigned :0; /* force alignment to the next boundary */
1307
1308 /* unserialized, strictly 'current' */
1309 unsigned in_execve:1; /* bit to tell LSMs we're in execve */
1310 unsigned in_iowait:1;
Andy Lutomirski7e781412016-08-02 14:05:36 -07001311#if !defined(TIF_RESTORE_SIGMASK)
1312 unsigned restore_sigmask:1;
1313#endif
Tejun Heo626ebc42015-11-05 18:46:09 -08001314#ifdef CONFIG_MEMCG
1315 unsigned memcg_may_oom:1;
Johannes Weiner127424c2016-01-20 15:02:32 -08001316#ifndef CONFIG_SLOB
Vladimir Davydov6f185c22014-12-12 16:55:15 -08001317 unsigned memcg_kmem_skip_account:1;
1318#endif
Johannes Weiner127424c2016-01-20 15:02:32 -08001319#endif
Peter Zijlstraff303e62015-04-17 20:05:30 +02001320#ifdef CONFIG_COMPAT_BRK
1321 unsigned brk_randomized:1;
1322#endif
Vladimir Davydov6f185c22014-12-12 16:55:15 -08001323
Kees Cook1d4457f2014-05-21 15:23:46 -07001324 unsigned long atomic_flags; /* Flags needing atomic access. */
1325
Andy Lutomirskif56141e2015-02-12 15:01:14 -08001326 struct restart_block restart_block;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 pid_t pid;
1329 pid_t tgid;
Arjan van de Ven0a4254052006-09-26 10:52:38 +02001330
Hiroshi Shimamoto13145622009-08-18 15:06:02 +09001331#ifdef CONFIG_CC_STACKPROTECTOR
Arjan van de Ven0a4254052006-09-26 10:52:38 +02001332 /* Canary value for the -fstack-protector gcc feature */
1333 unsigned long stack_canary;
Hiroshi Shimamoto13145622009-08-18 15:06:02 +09001334#endif
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001335 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * pointers to (original) parent process, youngest child, younger sibling,
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001337 * older sibling, respectively. (p->father can be replaced with
Roland McGrathf4700212008-03-24 18:36:23 -07001338 * p->real_parent->pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Kees Cookabd63bc2011-12-14 14:39:26 -08001340 struct task_struct __rcu *real_parent; /* real parent process */
1341 struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /*
Roland McGrathf4700212008-03-24 18:36:23 -07001343 * children/sibling forms the list of my natural children
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
1345 struct list_head children; /* list of my children */
1346 struct list_head sibling; /* linkage in my parent's children list */
1347 struct task_struct *group_leader; /* threadgroup leader */
1348
Roland McGrathf4700212008-03-24 18:36:23 -07001349 /*
1350 * ptraced is the list of tasks this task is using ptrace on.
1351 * This includes both natural children and PTRACE_ATTACH targets.
1352 * p->ptrace_entry is p's link on the p->parent->ptraced list.
1353 */
1354 struct list_head ptraced;
1355 struct list_head ptrace_entry;
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* PID/PID hash table linkage. */
Eric W. Biederman92476d72006-03-31 02:31:42 -08001358 struct pid_link pids[PIDTYPE_MAX];
Oleg Nesterov47e65322006-03-28 16:11:25 -08001359 struct list_head thread_group;
Oleg Nesterov0c740d02014-01-21 15:49:56 -08001360 struct list_head thread_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 struct completion *vfork_done; /* for vfork() */
1363 int __user *set_child_tid; /* CLONE_CHILD_SETTID */
1364 int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
1365
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001366 u64 utime, stime;
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01001367#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001368 u64 utimescaled, stimescaled;
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01001369#endif
Frederic Weisbecker16a6d9b2017-01-31 04:09:21 +01001370 u64 gtime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +02001371 struct prev_cputime prev_cputime;
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001372#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +01001373 seqcount_t vtime_seqcount;
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001374 unsigned long long vtime_snap;
1375 enum {
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +01001376 /* Task is sleeping or running in a CPU with VTIME inactive */
1377 VTIME_INACTIVE = 0,
1378 /* Task runs in userspace in a CPU with VTIME active */
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001379 VTIME_USER,
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +01001380 /* Task runs in kernelspace in a CPU with VTIME active */
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001381 VTIME_SYS,
1382 } vtime_snap_whence;
1383#endif
Frederic Weisbeckerd027d452015-06-07 15:54:30 +02001384
1385#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +01001386 atomic_t tick_dep_mask;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +02001387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unsigned long nvcsw, nivcsw; /* context switch counts */
Thomas Gleixnerccbf62d2014-07-16 21:04:34 +00001389 u64 start_time; /* monotonic time in nsec */
Thomas Gleixner57e0be02014-07-16 21:04:32 +00001390 u64 real_start_time; /* boot based time in nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
1392 unsigned long min_flt, maj_flt;
1393
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05001394#ifdef CONFIG_POSIX_TIMERS
Frank Mayharf06febc2008-09-12 09:54:39 -07001395 struct task_cputime cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct list_head cpu_timers[3];
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05001397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399/* process credentials */
Eric W. Biederman64b875f2016-11-14 18:48:07 -06001400 const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
Arnd Bergmann1b0ba1c2010-02-24 19:45:09 +01001401 const struct cred __rcu *real_cred; /* objective and real subjective task
David Howells3b11a1d2008-11-14 10:39:26 +11001402 * credentials (COW) */
Arnd Bergmann1b0ba1c2010-02-24 19:45:09 +01001403 const struct cred __rcu *cred; /* effective (overridable) subjective task
David Howells3b11a1d2008-11-14 10:39:26 +11001404 * credentials (COW) */
Paolo 'Blaisorblade' Giarrusso36772092005-05-05 16:16:12 -07001405 char comm[TASK_COMM_LEN]; /* executable name excluding path
1406 - access with [gs]et_task_comm (which lock
1407 it with task_lock())
Linus Torvalds221af7f2010-01-28 22:14:42 -08001408 - initialized normally by setup_new_exec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/* file system info */
NeilBrown756daf22015-03-23 13:37:38 +11001410 struct nameidata *nameidata;
Alexey Dobriyan3d5b6fc2006-09-29 01:59:40 -07001411#ifdef CONFIG_SYSVIPC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/* ipc stuff */
1413 struct sysv_sem sysvsem;
Jack Millerab602f72014-08-08 14:23:19 -07001414 struct sysv_shm sysvshm;
Alexey Dobriyan3d5b6fc2006-09-29 01:59:40 -07001415#endif
Mandeep Singh Bainese162b392009-01-15 11:08:40 -08001416#ifdef CONFIG_DETECT_HUNG_TASK
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01001417/* hung task detection */
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01001418 unsigned long last_switch_count;
1419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/* filesystem information */
1421 struct fs_struct *fs;
1422/* open file information */
1423 struct files_struct *files;
Serge E. Hallyn1651e142006-10-02 02:18:08 -07001424/* namespaces */
Serge E. Hallynab516012006-10-02 02:18:06 -07001425 struct nsproxy *nsproxy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/* signal handlers */
1427 struct signal_struct *signal;
1428 struct sighand_struct *sighand;
1429
1430 sigset_t blocked, real_blocked;
Roland McGrathf3de2722008-04-30 00:53:09 -07001431 sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct sigpending pending;
1433
1434 unsigned long sas_ss_sp;
1435 size_t sas_ss_size;
Stas Sergeev2a742132016-04-14 23:20:04 +03001436 unsigned sas_ss_flags;
Oleg Nesterov2e01fab2015-11-06 16:32:19 -08001437
Al Viro67d12142012-06-27 11:07:19 +04001438 struct callback_head *task_works;
Oleg Nesterove73f8952012-05-11 10:59:07 +10001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 struct audit_context *audit_context;
Al Virobfef93a2008-01-10 04:53:18 -05001441#ifdef CONFIG_AUDITSYSCALL
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001442 kuid_t loginuid;
Eric Paris4746ec52008-01-08 10:06:53 -05001443 unsigned int sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05001444#endif
Will Drewry932eceb2012-04-12 16:47:54 -05001445 struct seccomp seccomp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447/* Thread group tracking */
1448 u32 parent_exec_id;
1449 u32 self_exec_id;
Miao Xie58568d22009-06-16 15:31:49 -07001450/* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
1451 * mempolicy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 spinlock_t alloc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Ingo Molnarb29739f2006-06-27 02:54:51 -07001454 /* Protection of the PI data structures: */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001455 raw_spinlock_t pi_lock;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001456
Peter Zijlstra76751042015-05-01 08:27:50 -07001457 struct wake_q_node wake_q;
1458
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001459#ifdef CONFIG_RT_MUTEXES
1460 /* PI waiters blocked on a rt_mutex held by this task */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001461 struct rb_root pi_waiters;
1462 struct rb_node *pi_waiters_leftmost;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001463 /* Deadlock detection and priority inheritance handling */
1464 struct rt_mutex_waiter *pi_blocked_on;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001465#endif
1466
Ingo Molnar408894e2006-01-09 15:59:20 -08001467#ifdef CONFIG_DEBUG_MUTEXES
1468 /* mutex deadlock detection */
1469 struct mutex_waiter *blocked_on;
1470#endif
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001471#ifdef CONFIG_TRACE_IRQFLAGS
1472 unsigned int irq_events;
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001473 unsigned long hardirq_enable_ip;
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001474 unsigned long hardirq_disable_ip;
Hiroshi Shimamotofa1452e2009-11-30 14:59:44 +09001475 unsigned int hardirq_enable_event;
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001476 unsigned int hardirq_disable_event;
Hiroshi Shimamotofa1452e2009-11-30 14:59:44 +09001477 int hardirqs_enabled;
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001478 int hardirq_context;
Hiroshi Shimamotofa1452e2009-11-30 14:59:44 +09001479 unsigned long softirq_disable_ip;
1480 unsigned long softirq_enable_ip;
1481 unsigned int softirq_disable_event;
1482 unsigned int softirq_enable_event;
1483 int softirqs_enabled;
Ingo Molnarde30a2b2006-07-03 00:24:42 -07001484 int softirq_context;
1485#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001486#ifdef CONFIG_LOCKDEP
Peter Zijlstrabdb94412008-02-25 23:02:48 +01001487# define MAX_LOCK_DEPTH 48UL
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001488 u64 curr_chain_key;
1489 int lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001490 unsigned int lockdep_recursion;
Richard Kennedyc7aceab2008-05-15 12:09:15 +01001491 struct held_lock held_locks[MAX_LOCK_DEPTH];
Nick Piggincf40bd12009-01-21 08:12:39 +01001492 gfp_t lockdep_reclaim_gfp;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001493#endif
Andrey Ryabininc6d30852016-01-20 15:00:55 -08001494#ifdef CONFIG_UBSAN
1495 unsigned int in_ubsan;
1496#endif
Ingo Molnar408894e2006-01-09 15:59:20 -08001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/* journalling filesystem info */
1499 void *journal_info;
1500
Neil Brownd89d8792007-05-01 09:53:42 +02001501/* stacked block device info */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001502 struct bio_list *bio_list;
Neil Brownd89d8792007-05-01 09:53:42 +02001503
Jens Axboe73c10102011-03-08 13:19:51 +01001504#ifdef CONFIG_BLOCK
1505/* stack plugging */
1506 struct blk_plug *plug;
1507#endif
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* VM state */
1510 struct reclaim_state *reclaim_state;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct backing_dev_info *backing_dev_info;
1513
1514 struct io_context *io_context;
1515
1516 unsigned long ptrace_message;
1517 siginfo_t *last_siginfo; /* For ptrace use. */
Andrew Morton7c3ab7382006-12-10 02:19:19 -08001518 struct task_io_accounting ioac;
Jay Lan8f0ab512006-09-30 23:28:59 -07001519#if defined(CONFIG_TASK_XACCT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 u64 acct_rss_mem1; /* accumulated rss usage */
1521 u64 acct_vm_mem1; /* accumulated virtual memory usage */
Frederic Weisbecker605dc2b2017-01-31 04:09:30 +01001522 u64 acct_timexpd; /* stime + utime since last update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523#endif
1524#ifdef CONFIG_CPUSETS
Miao Xie58568d22009-06-16 15:31:49 -07001525 nodemask_t mems_allowed; /* Protected by alloc_lock */
Mel Gormancc9a6c82012-03-21 16:34:11 -07001526 seqcount_t mems_allowed_seq; /* Seqence no to catch updates */
Paul Jackson825a46a2006-03-24 03:16:03 -08001527 int cpuset_mem_spread_rotor;
Jack Steiner6adef3e2010-05-26 14:42:49 -07001528 int cpuset_slab_spread_rotor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530#ifdef CONFIG_CGROUPS
Paul Menage817929e2007-10-18 23:39:36 -07001531 /* Control Group info protected by css_set_lock */
Arnd Bergmann2c392b82010-02-24 19:41:39 +01001532 struct css_set __rcu *cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07001533 /* cg_list protected by css_set_lock and tsk->alloc_lock */
1534 struct list_head cg_list;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535#endif
Fenghua Yue02737d2016-10-28 15:04:46 -07001536#ifdef CONFIG_INTEL_RDT_A
1537 int closid;
1538#endif
Alexey Dobriyan42b2dd02007-10-16 23:27:30 -07001539#ifdef CONFIG_FUTEX
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001540 struct robust_list_head __user *robust_list;
Ingo Molnar34f192c2006-03-27 01:16:24 -08001541#ifdef CONFIG_COMPAT
1542 struct compat_robust_list_head __user *compat_robust_list;
1543#endif
Ingo Molnarc87e2832006-06-27 02:54:58 -07001544 struct list_head pi_state_list;
1545 struct futex_pi_state *pi_state_cache;
Alexey Dobriyan42b2dd02007-10-16 23:27:30 -07001546#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001547#ifdef CONFIG_PERF_EVENTS
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001548 struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549 struct mutex perf_event_mutex;
1550 struct list_head perf_event_list;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001551#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01001552#ifdef CONFIG_DEBUG_PREEMPT
1553 unsigned long preempt_disable_ip;
1554#endif
Richard Kennedyc7aceab2008-05-15 12:09:15 +01001555#ifdef CONFIG_NUMA
Miao Xie58568d22009-06-16 15:31:49 -07001556 struct mempolicy *mempolicy; /* Protected by alloc_lock */
Richard Kennedyc7aceab2008-05-15 12:09:15 +01001557 short il_next;
Eric Dumazet207205a2011-03-22 16:30:44 -07001558 short pref_node_fork;
Richard Kennedyc7aceab2008-05-15 12:09:15 +01001559#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001560#ifdef CONFIG_NUMA_BALANCING
1561 int numa_scan_seq;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001562 unsigned int numa_scan_period;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001563 unsigned int numa_scan_period_max;
Rik van Rielde1c9ce62013-10-07 11:29:39 +01001564 int numa_preferred_nid;
Mel Gorman6b9a7462013-10-07 11:29:11 +01001565 unsigned long numa_migrate_retry;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001566 u64 node_stamp; /* migration stamp */
Rik van Riel7e2703e2014-01-27 17:03:45 -05001567 u64 last_task_numa_placement;
1568 u64 last_sum_exec_runtime;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001569 struct callback_head numa_work;
Mel Gormanf809ca92013-10-07 11:28:57 +01001570
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001571 struct list_head numa_entry;
1572 struct numa_group *numa_group;
1573
Mel Gorman745d6142013-10-07 11:28:59 +01001574 /*
Iulia Manda44dba3d2014-10-31 02:13:31 +02001575 * numa_faults is an array split into four regions:
1576 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1577 * in this precise order.
1578 *
1579 * faults_memory: Exponential decaying average of faults on a per-node
1580 * basis. Scheduling placement decisions are made based on these
1581 * counts. The values remain static for the duration of a PTE scan.
1582 * faults_cpu: Track the nodes the process was running on when a NUMA
1583 * hinting fault was incurred.
1584 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1585 * during the current scan window. When the scan completes, the counts
1586 * in faults_memory and faults_cpu decay and these values are copied.
Mel Gorman745d6142013-10-07 11:28:59 +01001587 */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001588 unsigned long *numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001589 unsigned long total_numa_faults;
Mel Gorman745d6142013-10-07 11:28:59 +01001590
1591 /*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001592 * numa_faults_locality tracks if faults recorded during the last
Mel Gorman074c2382015-03-25 15:55:42 -07001593 * scan window were remote/local or failed to migrate. The task scan
1594 * period is adapted based on the locality of the faults with different
1595 * weights depending on whether they were shared or private faults
Rik van Riel04bb2f92013-10-07 11:29:36 +01001596 */
Mel Gorman074c2382015-03-25 15:55:42 -07001597 unsigned long numa_faults_locality[3];
Rik van Riel04bb2f92013-10-07 11:29:36 +01001598
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001599 unsigned long numa_pages_migrated;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001600#endif /* CONFIG_NUMA_BALANCING */
1601
Mel Gorman72b252a2015-09-04 15:47:32 -07001602#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1603 struct tlbflush_unmap_batch tlb_ubc;
1604#endif
1605
Ingo Molnare56d0902006-01-08 01:01:37 -08001606 struct rcu_head rcu;
Jens Axboeb92ce552006-04-11 13:52:07 +02001607
1608 /*
1609 * cache last used pipe for splice
1610 */
1611 struct pipe_inode_info *splice_pipe;
Eric Dumazet5640f762012-09-23 23:04:42 +00001612
1613 struct page_frag task_frag;
1614
Shailabh Nagarca74e922006-07-14 00:24:36 -07001615#ifdef CONFIG_TASK_DELAY_ACCT
1616 struct task_delay_info *delays;
1617#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001618#ifdef CONFIG_FAULT_INJECTION
1619 int make_it_fail;
1620#endif
Wu Fengguang9d823e82011-06-11 18:10:12 -06001621 /*
1622 * when (nr_dirtied >= nr_dirtied_pause), it's time to call
1623 * balance_dirty_pages() for some dirty throttling pause
1624 */
1625 int nr_dirtied;
1626 int nr_dirtied_pause;
Wu Fengguang83712352011-06-11 19:25:42 -06001627 unsigned long dirty_paused_when; /* start of a write-and-pause period */
Wu Fengguang9d823e82011-06-11 18:10:12 -06001628
Arjan van de Ven97455122008-01-25 21:08:34 +01001629#ifdef CONFIG_LATENCYTOP
1630 int latency_record_count;
1631 struct latency_record latency_record[LT_SAVECOUNT];
1632#endif
Arjan van de Ven69766752008-09-01 15:52:40 -07001633 /*
1634 * time slack values; these are used to round up poll() and
1635 * select() etc timeout values. These are in nanoseconds.
1636 */
John Stultzda8b44d2016-03-17 14:20:51 -07001637 u64 timer_slack_ns;
1638 u64 default_timer_slack_ns;
David Millerf8d570a2008-11-06 00:37:40 -08001639
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08001640#ifdef CONFIG_KASAN
1641 unsigned int kasan_depth;
1642#endif
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001643#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08001644 /* Index of current stored address in ret_stack */
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01001645 int curr_ret_stack;
1646 /* Stack of return addresses for return function tracing */
1647 struct ftrace_ret_stack *ret_stack;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04001648 /* time stamp for last schedule */
1649 unsigned long long ftrace_timestamp;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01001650 /*
1651 * Number of functions that haven't been traced
1652 * because of depth overrun.
1653 */
1654 atomic_t trace_overrun;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01001655 /* Pause for the tracing */
1656 atomic_t tracing_graph_pause;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01001657#endif
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001658#ifdef CONFIG_TRACING
1659 /* state flags for use by tracers */
1660 unsigned long trace;
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04001661 /* bitmask and counter of trace recursion */
Steven Rostedt261842b2009-04-16 21:41:52 -04001662 unsigned long trace_recursion;
1663#endif /* CONFIG_TRACING */
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -07001664#ifdef CONFIG_KCOV
1665 /* Coverage collection mode enabled for this task (0 if disabled). */
1666 enum kcov_mode kcov_mode;
1667 /* Size of the kcov_area. */
1668 unsigned kcov_size;
1669 /* Buffer for coverage collection. */
1670 void *kcov_area;
1671 /* kcov desciptor wired with this task or NULL. */
1672 struct kcov *kcov;
1673#endif
Vladimir Davydov6f185c22014-12-12 16:55:15 -08001674#ifdef CONFIG_MEMCG
Tejun Heo626ebc42015-11-05 18:46:09 -08001675 struct mem_cgroup *memcg_in_oom;
1676 gfp_t memcg_oom_gfp_mask;
1677 int memcg_oom_order;
Tejun Heob23afb92015-11-05 18:46:11 -08001678
1679 /* number of pages to reclaim on returning to userland */
1680 unsigned int memcg_nr_pages_over_high;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08001681#endif
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301682#ifdef CONFIG_UPROBES
1683 struct uprobe_task *utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301684#endif
Kent Overstreetcafe5632013-03-23 16:11:31 -07001685#if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1686 unsigned int sequential_io;
1687 unsigned int sequential_io_avg;
1688#endif
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02001689#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1690 unsigned long task_state_change;
1691#endif
David Hildenbrand8bcbde52015-05-11 17:52:06 +02001692 int pagefault_disabled;
Michal Hocko03049262016-03-25 14:20:33 -07001693#ifdef CONFIG_MMU
Vladimir Davydov29c696e2016-03-25 14:20:39 -07001694 struct task_struct *oom_reaper_list;
Michal Hocko03049262016-03-25 14:20:33 -07001695#endif
Andy Lutomirskiba14a192016-08-11 02:35:21 -07001696#ifdef CONFIG_VMAP_STACK
1697 struct vm_struct *stack_vm_area;
1698#endif
Andy Lutomirski68f24b082016-09-15 22:45:48 -07001699#ifdef CONFIG_THREAD_INFO_IN_TASK
1700 /* A live task holds one reference. */
1701 atomic_t stack_refcount;
1702#endif
Dave Hansen0c8c0f02015-07-17 12:28:11 +02001703/* CPU-specific state of this task */
1704 struct thread_struct thread;
1705/*
1706 * WARNING: on x86, 'thread_struct' contains a variable-sized
1707 * structure. It *MUST* be at the end of 'task_struct'.
1708 *
1709 * Do not put anything below here!
1710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711};
1712
Ingo Molnar5aaeb5c2015-07-17 12:28:12 +02001713#ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
1714extern int arch_task_struct_size __read_mostly;
1715#else
1716# define arch_task_struct_size (sizeof(struct task_struct))
1717#endif
Dave Hansen0c8c0f02015-07-17 12:28:11 +02001718
Andy Lutomirskiba14a192016-08-11 02:35:21 -07001719#ifdef CONFIG_VMAP_STACK
1720static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1721{
1722 return t->stack_vm_area;
1723}
1724#else
1725static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1726{
1727 return NULL;
1728}
1729#endif
1730
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001731#define TNF_MIGRATED 0x01
1732#define TNF_NO_GROUP 0x02
Rik van Rieldabe1d92013-10-07 11:29:34 +01001733#define TNF_SHARED 0x04
Rik van Riel04bb2f92013-10-07 11:29:36 +01001734#define TNF_FAULT_LOCAL 0x08
Mel Gorman074c2382015-03-25 15:55:42 -07001735#define TNF_MIGRATE_FAIL 0x10
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001736
Michal Hockob18dc5f2016-07-28 15:44:46 -07001737static inline bool in_vfork(struct task_struct *tsk)
1738{
1739 bool ret;
1740
1741 /*
1742 * need RCU to access ->real_parent if CLONE_VM was used along with
1743 * CLONE_PARENT.
1744 *
1745 * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
1746 * imply CLONE_VM
1747 *
1748 * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
1749 * ->real_parent is not necessarily the task doing vfork(), so in
1750 * theory we can't rely on task_lock() if we want to dereference it.
1751 *
1752 * And in this case we can't trust the real_parent->mm == tsk->mm
1753 * check, it can be false negative. But we do not care, if init or
1754 * another oom-unkillable task does this it should blame itself.
1755 */
1756 rcu_read_lock();
1757 ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
1758 rcu_read_unlock();
1759
1760 return ret;
1761}
1762
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001763#ifdef CONFIG_NUMA_BALANCING
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001764extern void task_numa_fault(int last_node, int node, int pages, int flags);
Mel Gormane29cf082013-10-07 11:29:22 +01001765extern pid_t task_numa_group_id(struct task_struct *p);
Mel Gorman1a687c22012-11-22 11:16:36 +00001766extern void set_numabalancing_state(bool enabled);
Rik van Riel82727012013-10-07 11:29:28 +01001767extern void task_numa_free(struct task_struct *p);
Rik van Riel10f39042014-01-27 17:03:44 -05001768extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
1769 int src_nid, int dst_cpu);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001770#else
Mel Gormanac8e8952013-10-07 11:29:03 +01001771static inline void task_numa_fault(int last_node, int node, int pages,
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001772 int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001773{
1774}
Mel Gormane29cf082013-10-07 11:29:22 +01001775static inline pid_t task_numa_group_id(struct task_struct *p)
1776{
1777 return 0;
1778}
Mel Gorman1a687c22012-11-22 11:16:36 +00001779static inline void set_numabalancing_state(bool enabled)
1780{
1781}
Rik van Riel82727012013-10-07 11:29:28 +01001782static inline void task_numa_free(struct task_struct *p)
1783{
1784}
Rik van Riel10f39042014-01-27 17:03:44 -05001785static inline bool should_numa_migrate_memory(struct task_struct *p,
1786 struct page *page, int src_nid, int dst_cpu)
1787{
1788 return true;
1789}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001790#endif
1791
Alexey Dobriyane8681712007-10-26 12:17:22 +04001792static inline struct pid *task_pid(struct task_struct *task)
Eric W. Biederman22c935f2006-10-02 02:17:09 -07001793{
1794 return task->pids[PIDTYPE_PID].pid;
1795}
1796
Alexey Dobriyane8681712007-10-26 12:17:22 +04001797static inline struct pid *task_tgid(struct task_struct *task)
Eric W. Biederman22c935f2006-10-02 02:17:09 -07001798{
1799 return task->group_leader->pids[PIDTYPE_PID].pid;
1800}
1801
Oleg Nesterov6dda81f2009-04-02 16:58:35 -07001802/*
1803 * Without tasklist or rcu lock it is not safe to dereference
1804 * the result of task_pgrp/task_session even if task == current,
1805 * we can race with another thread doing sys_setsid/sys_setpgid.
1806 */
Alexey Dobriyane8681712007-10-26 12:17:22 +04001807static inline struct pid *task_pgrp(struct task_struct *task)
Eric W. Biederman22c935f2006-10-02 02:17:09 -07001808{
1809 return task->group_leader->pids[PIDTYPE_PGID].pid;
1810}
1811
Alexey Dobriyane8681712007-10-26 12:17:22 +04001812static inline struct pid *task_session(struct task_struct *task)
Eric W. Biederman22c935f2006-10-02 02:17:09 -07001813{
1814 return task->group_leader->pids[PIDTYPE_SID].pid;
1815}
1816
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001817struct pid_namespace;
1818
1819/*
1820 * the helpers to get the task's different pids as they are seen
1821 * from various namespaces
1822 *
1823 * task_xid_nr() : global id, i.e. the id seen from the init namespace;
Eric W. Biederman44c4e1b2008-02-08 04:19:15 -08001824 * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
1825 * current.
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001826 * task_xid_nr_ns() : id seen from the ns specified;
1827 *
1828 * set_task_vxid() : assigns a virtual id to a task;
1829 *
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001830 * see also pid_nr() etc in include/linux/pid.h
1831 */
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001832pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
1833 struct pid_namespace *ns);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001834
Alexey Dobriyane8681712007-10-26 12:17:22 +04001835static inline pid_t task_pid_nr(struct task_struct *tsk)
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001836{
1837 return tsk->pid;
1838}
1839
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001840static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
1841 struct pid_namespace *ns)
1842{
1843 return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1844}
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001845
1846static inline pid_t task_pid_vnr(struct task_struct *tsk)
1847{
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001848 return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001849}
1850
1851
Alexey Dobriyane8681712007-10-26 12:17:22 +04001852static inline pid_t task_tgid_nr(struct task_struct *tsk)
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001853{
1854 return tsk->tgid;
1855}
1856
Pavel Emelyanov2f2a3a42007-10-18 23:40:19 -07001857pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001858
1859static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1860{
1861 return pid_vnr(task_tgid(tsk));
1862}
1863
1864
Richard Guy Briggs80e0b6e2014-03-16 14:00:19 -04001865static inline int pid_alive(const struct task_struct *p);
Richard Guy Briggsad36d282013-08-15 18:05:12 -04001866static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1867{
1868 pid_t pid = 0;
1869
1870 rcu_read_lock();
1871 if (pid_alive(tsk))
1872 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1873 rcu_read_unlock();
1874
1875 return pid;
1876}
1877
1878static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1879{
1880 return task_ppid_nr_ns(tsk, &init_pid_ns);
1881}
1882
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001883static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
1884 struct pid_namespace *ns)
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001885{
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001886 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001887}
1888
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001889static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
1890{
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001891 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001892}
1893
1894
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001895static inline pid_t task_session_nr_ns(struct task_struct *tsk,
1896 struct pid_namespace *ns)
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001897{
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001898 return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001899}
1900
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001901static inline pid_t task_session_vnr(struct task_struct *tsk)
1902{
Oleg Nesterov52ee2df2009-04-02 16:58:38 -07001903 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001904}
1905
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001906/* obsolete, do not use */
1907static inline pid_t task_pgrp_nr(struct task_struct *tsk)
1908{
1909 return task_pgrp_nr_ns(tsk, &init_pid_ns);
1910}
Pavel Emelyanov7af57292007-10-18 23:40:06 -07001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/**
1913 * pid_alive - check that a task structure is not stale
1914 * @p: Task structure to be checked.
1915 *
1916 * Test if a process is not yet dead (at most zombie state)
1917 * If pid_alive fails, then pointers within the task structure
1918 * can be stale and must not be dereferenced.
Yacine Belkadie69f6182013-07-12 20:45:47 +02001919 *
1920 * Return: 1 if the process is alive. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Richard Guy Briggsad36d282013-08-15 18:05:12 -04001922static inline int pid_alive(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Eric W. Biederman92476d72006-03-31 02:31:42 -08001924 return p->pids[PIDTYPE_PID].pid != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -07001927/**
Sergey Senozhatsky570f5242016-01-01 23:03:01 +09001928 * is_global_init - check if a task structure is init. Since init
1929 * is free to have sub-threads we need to check tgid.
Henne32602592006-10-06 00:44:01 -07001930 * @tsk: Task structure to be checked.
1931 *
1932 * Check if a task structure is the first user space task the kernel created.
Yacine Belkadie69f6182013-07-12 20:45:47 +02001933 *
1934 * Return: 1 if the task structure is init. 0 otherwise.
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -07001935 */
Alexey Dobriyane8681712007-10-26 12:17:22 +04001936static inline int is_global_init(struct task_struct *tsk)
Pavel Emelyanovb461cc02007-10-18 23:40:09 -07001937{
Sergey Senozhatsky570f5242016-01-01 23:03:01 +09001938 return task_tgid_nr(tsk) == 1;
Pavel Emelyanovb461cc02007-10-18 23:40:09 -07001939}
Serge E. Hallynb460cbc2007-10-18 23:39:52 -07001940
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001941extern struct pid *cad_pid;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943extern void free_task(struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944#define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
Ingo Molnare56d0902006-01-08 01:01:37 -08001945
Andrew Morton158d9eb2006-03-31 02:31:34 -08001946extern void __put_task_struct(struct task_struct *t);
Ingo Molnare56d0902006-01-08 01:01:37 -08001947
1948static inline void put_task_struct(struct task_struct *t)
1949{
1950 if (atomic_dec_and_test(&t->usage))
Eric W. Biederman8c7904a2006-03-31 02:31:37 -08001951 __put_task_struct(t);
Ingo Molnare56d0902006-01-08 01:01:37 -08001952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Oleg Nesterov150593b2016-05-18 19:02:18 +02001954struct task_struct *task_rcu_dereference(struct task_struct **ptask);
1955struct task_struct *try_get_task_struct(struct task_struct **ptask);
1956
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001957#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1958extern void task_cputime(struct task_struct *t,
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001959 u64 *utime, u64 *stime);
Frederic Weisbecker16a6d9b2017-01-31 04:09:21 +01001960extern u64 task_gtime(struct task_struct *t);
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001961#else
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001962static inline void task_cputime(struct task_struct *t,
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001963 u64 *utime, u64 *stime)
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001964{
Stanislaw Gruszka353c50e2016-11-15 03:06:52 +01001965 *utime = t->utime;
1966 *stime = t->stime;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001967}
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001968
Frederic Weisbecker16a6d9b2017-01-31 04:09:21 +01001969static inline u64 task_gtime(struct task_struct *t)
Frederic Weisbecker6a616712012-12-16 20:00:34 +01001970{
1971 return t->gtime;
1972}
1973#endif
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01001974
1975#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Jens Axboeb31dc662006-06-13 08:26:10 +02001976static inline void task_cputime_scaled(struct task_struct *t,
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001977 u64 *utimescaled,
1978 u64 *stimescaled)
Jens Axboeb31dc662006-06-13 08:26:10 +02001979{
Stanislaw Gruszka353c50e2016-11-15 03:06:52 +01001980 *utimescaled = t->utimescaled;
1981 *stimescaled = t->stimescaled;
Tejun Heo58a69cb2011-02-16 09:25:31 +01001982}
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01001983#else
1984static inline void task_cputime_scaled(struct task_struct *t,
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001985 u64 *utimescaled,
1986 u64 *stimescaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01001988 task_cputime(t, utimescaled, stimescaled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990#endif
Stanislaw Gruszka40565b52016-11-15 03:06:51 +01001991
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001992extern void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
1993extern void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995/*
1996 * Per process flags
1997 */
Peter Zijlstrac1de45c2016-11-28 23:03:05 -08001998#define PF_IDLE 0x00000002 /* I am an IDLE thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999#define PF_EXITING 0x00000004 /* getting shut down */
2000#define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
2001#define PF_VCPU 0x00000010 /* I'm a virtual CPU */
2002#define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
2003#define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
2004#define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
2005#define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
2006#define PF_DUMPCORE 0x00000200 /* dumped core */
2007#define PF_SIGNALED 0x00000400 /* killed by a signal */
2008#define PF_MEMALLOC 0x00000800 /* Allocating memory */
2009#define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
2010#define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
2011#define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
2012#define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
2013#define PF_FROZEN 0x00010000 /* frozen for system suspend */
2014#define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
2015#define PF_KSWAPD 0x00040000 /* I am kswapd */
Ming Lei21caf2f2013-02-22 16:34:08 -08002016#define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017#define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
2018#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
2019#define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
2020#define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002021#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022#define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
2024#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
Colin Cross2b44c4d2013-07-24 17:41:33 -07002025#define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027/*
2028 * Only the _current_ task can read/write to tsk->flags, but other
2029 * tasks can access tsk->flags in readonly mode for example
2030 * with tsk_used_math (like during threaded core dumping).
2031 * There is however an exception to this rule during ptrace
2032 * or during fork: the ptracer task is allowed to write to the
2033 * child->flags of its traced child (same goes for fork, the parent
2034 * can write to the child->flags), because we're guaranteed the
2035 * child is not running and in turn not changing child->flags
2036 * at the same time the parent does it.
2037 */
2038#define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
2039#define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
2040#define clear_used_math() clear_stopped_child_used_math(current)
2041#define set_used_math() set_stopped_child_used_math(current)
2042#define conditional_stopped_child_used_math(condition, child) \
2043 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
2044#define conditional_used_math(condition) \
2045 conditional_stopped_child_used_math(condition, current)
2046#define copy_to_stopped_child_used_math(child) \
2047 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
2048/* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
2049#define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
2050#define used_math() tsk_used_math(current)
2051
Junxiao Bi934f30722014-10-09 15:28:23 -07002052/* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
2053 * __GFP_FS is also cleared as it implies __GFP_IO.
2054 */
Ming Lei21caf2f2013-02-22 16:34:08 -08002055static inline gfp_t memalloc_noio_flags(gfp_t flags)
2056{
2057 if (unlikely(current->flags & PF_MEMALLOC_NOIO))
Junxiao Bi934f30722014-10-09 15:28:23 -07002058 flags &= ~(__GFP_IO | __GFP_FS);
Ming Lei21caf2f2013-02-22 16:34:08 -08002059 return flags;
2060}
2061
2062static inline unsigned int memalloc_noio_save(void)
2063{
2064 unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
2065 current->flags |= PF_MEMALLOC_NOIO;
2066 return flags;
2067}
2068
2069static inline void memalloc_noio_restore(unsigned int flags)
2070{
2071 current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
2072}
2073
Kees Cook1d4457f2014-05-21 15:23:46 -07002074/* Per-process atomic flags. */
Zefan Lia2b86f72014-09-25 09:40:17 +08002075#define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
Zefan Li2ad654b2014-09-25 09:41:02 +08002076#define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
2077#define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
Tetsuo Handa77ed2c52016-03-08 20:01:32 +09002078#define PFA_LMK_WAITING 3 /* Lowmemorykiller is waiting */
Kees Cook1d4457f2014-05-21 15:23:46 -07002079
Kees Cook1d4457f2014-05-21 15:23:46 -07002080
Zefan Lie0e50702014-09-25 09:40:40 +08002081#define TASK_PFA_TEST(name, func) \
2082 static inline bool task_##func(struct task_struct *p) \
2083 { return test_bit(PFA_##name, &p->atomic_flags); }
2084#define TASK_PFA_SET(name, func) \
2085 static inline void task_set_##func(struct task_struct *p) \
2086 { set_bit(PFA_##name, &p->atomic_flags); }
2087#define TASK_PFA_CLEAR(name, func) \
2088 static inline void task_clear_##func(struct task_struct *p) \
2089 { clear_bit(PFA_##name, &p->atomic_flags); }
Kees Cook1d4457f2014-05-21 15:23:46 -07002090
Zefan Lie0e50702014-09-25 09:40:40 +08002091TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
2092TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
Kees Cook1d4457f2014-05-21 15:23:46 -07002093
Zefan Li2ad654b2014-09-25 09:41:02 +08002094TASK_PFA_TEST(SPREAD_PAGE, spread_page)
2095TASK_PFA_SET(SPREAD_PAGE, spread_page)
2096TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
2097
2098TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
2099TASK_PFA_SET(SPREAD_SLAB, spread_slab)
2100TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
Tejun Heo544b2c92011-06-14 11:20:18 +02002101
Tetsuo Handa77ed2c52016-03-08 20:01:32 +09002102TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
2103TASK_PFA_SET(LMK_WAITING, lmk_waiting)
2104
Tejun Heo39efa3e2011-03-23 10:37:00 +01002105/*
Tejun Heoe5c1902e2011-03-23 10:37:00 +01002106 * task->jobctl flags
Tejun Heod79fdd62011-03-23 10:37:00 +01002107 */
Oleg Nesterovee77f072011-04-01 20:12:38 +02002108#define JOBCTL_STOP_SIGMASK 0xffff /* signr of the last group stop */
Tejun Heoe5c1902e2011-03-23 10:37:00 +01002109
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002110#define JOBCTL_STOP_DEQUEUED_BIT 16 /* stop signal dequeued */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002111#define JOBCTL_STOP_PENDING_BIT 17 /* task should stop for group stop */
2112#define JOBCTL_STOP_CONSUME_BIT 18 /* consume group stop count */
2113#define JOBCTL_TRAP_STOP_BIT 19 /* trap for STOP */
Tejun Heo73ddff22011-06-14 11:20:14 +02002114#define JOBCTL_TRAP_NOTIFY_BIT 20 /* trap for NOTIFY */
Tejun Heofb1d9102011-06-14 11:20:17 +02002115#define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002116#define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */
2117
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07002118#define JOBCTL_STOP_DEQUEUED (1UL << JOBCTL_STOP_DEQUEUED_BIT)
2119#define JOBCTL_STOP_PENDING (1UL << JOBCTL_STOP_PENDING_BIT)
2120#define JOBCTL_STOP_CONSUME (1UL << JOBCTL_STOP_CONSUME_BIT)
2121#define JOBCTL_TRAP_STOP (1UL << JOBCTL_TRAP_STOP_BIT)
2122#define JOBCTL_TRAP_NOTIFY (1UL << JOBCTL_TRAP_NOTIFY_BIT)
2123#define JOBCTL_TRAPPING (1UL << JOBCTL_TRAPPING_BIT)
2124#define JOBCTL_LISTENING (1UL << JOBCTL_LISTENING_BIT)
Tejun Heoa8f072c2011-06-02 11:13:59 +02002125
Tejun Heofb1d9102011-06-14 11:20:17 +02002126#define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
Tejun Heo73ddff22011-06-14 11:20:14 +02002127#define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
Tejun Heo3759a0d2011-06-02 11:14:00 +02002128
Tejun Heo7dd3db52011-06-02 11:14:00 +02002129extern bool task_set_jobctl_pending(struct task_struct *task,
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07002130 unsigned long mask);
Tejun Heo73ddff22011-06-14 11:20:14 +02002131extern void task_clear_jobctl_trapping(struct task_struct *task);
Tejun Heo3759a0d2011-06-02 11:14:00 +02002132extern void task_clear_jobctl_pending(struct task_struct *task,
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07002133 unsigned long mask);
Tejun Heo39efa3e2011-03-23 10:37:00 +01002134
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002135static inline void rcu_copy_process(struct task_struct *p)
2136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137#ifdef CONFIG_PREEMPT_RCU
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002138 p->rcu_read_lock_nesting = 0;
Paul E. McKenney1d082fd2014-08-14 16:01:53 -07002139 p->rcu_read_unlock_special.s = 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07002140 p->rcu_blocked_node = NULL;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002141 INIT_LIST_HEAD(&p->rcu_node_entry);
Paul E. McKenney8315f422014-06-27 13:42:20 -07002142#endif /* #ifdef CONFIG_PREEMPT_RCU */
2143#ifdef CONFIG_TASKS_RCU
2144 p->rcu_tasks_holdout = false;
2145 INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
Paul E. McKenney176f8f72014-08-04 17:43:50 -07002146 p->rcu_tasks_idle_cpu = -1;
Paul E. McKenney8315f422014-06-27 13:42:20 -07002147#endif /* #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07002148}
2149
Mel Gorman907aed42012-07-31 16:44:07 -07002150static inline void tsk_restore_flags(struct task_struct *task,
2151 unsigned long orig_flags, unsigned long flags)
2152{
2153 task->flags &= ~flags;
2154 task->flags |= orig_flags & flags;
2155}
2156
Juri Lellif82f8042014-10-07 09:52:11 +01002157extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
2158 const struct cpumask *trial);
Juri Lelli7f514122014-09-19 10:22:40 +01002159extern int task_can_attach(struct task_struct *p,
2160 const struct cpumask *cs_cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161#ifdef CONFIG_SMP
KOSAKI Motohiro1e1b6c512011-05-19 15:08:58 +09002162extern void do_set_cpus_allowed(struct task_struct *p,
2163 const struct cpumask *new_mask);
2164
Mike Traviscd8ba7c2008-03-26 14:23:49 -07002165extern int set_cpus_allowed_ptr(struct task_struct *p,
Rusty Russell96f874e22008-11-25 02:35:14 +10302166 const struct cpumask *new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#else
KOSAKI Motohiro1e1b6c512011-05-19 15:08:58 +09002168static inline void do_set_cpus_allowed(struct task_struct *p,
2169 const struct cpumask *new_mask)
2170{
2171}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07002172static inline int set_cpus_allowed_ptr(struct task_struct *p,
Rusty Russell96f874e22008-11-25 02:35:14 +10302173 const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Rusty Russell96f874e22008-11-25 02:35:14 +10302175 if (!cpumask_test_cpu(0, new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return -EINVAL;
2177 return 0;
2178}
2179#endif
Rusty Russelle0ad9552009-09-24 09:34:38 -06002180
Frederic Weisbecker3451d022011-08-10 23:21:01 +02002181#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra5167e8d2012-06-22 15:52:09 +02002182void calc_load_enter_idle(void);
2183void calc_load_exit_idle(void);
2184#else
2185static inline void calc_load_enter_idle(void) { }
2186static inline void calc_load_exit_idle(void) { }
Frederic Weisbecker3451d022011-08-10 23:21:01 +02002187#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra5167e8d2012-06-22 15:52:09 +02002188
Christian Borntraeger6d0d2872016-11-16 13:23:05 +01002189#ifndef cpu_relax_yield
2190#define cpu_relax_yield() cpu_relax()
2191#endif
2192
Ingo Molnarb3425012009-02-26 20:20:29 +01002193/*
Peter Zijlstrac6763292010-05-25 10:48:51 +02002194 * Do not use outside of architecture code which knows its limitations.
2195 *
2196 * sched_clock() has no promise of monotonicity or bounded drift between
2197 * CPUs, use (which you should not) requires disabling IRQs.
2198 *
2199 * Please use one of the three interfaces below.
Ingo Molnarb3425012009-02-26 20:20:29 +01002200 */
Mike Frysinger1bbfa6f2009-12-09 20:07:03 -05002201extern unsigned long long notrace sched_clock(void);
Peter Zijlstrac6763292010-05-25 10:48:51 +02002202/*
Hiroshi Shimamoto489a71b2012-04-02 17:00:44 +09002203 * See the comment in kernel/sched/clock.c
Peter Zijlstrac6763292010-05-25 10:48:51 +02002204 */
Cyril Bur545a2bf2015-02-12 15:01:24 -08002205extern u64 running_clock(void);
Peter Zijlstrac6763292010-05-25 10:48:51 +02002206extern u64 sched_clock_cpu(int cpu);
2207
Ingo Molnare436d802007-07-19 21:28:35 +02002208
Peter Zijlstrac1955a32008-08-11 08:59:03 +02002209extern void sched_clock_init(void);
Peter Zijlstrac1955a32008-08-11 08:59:03 +02002210
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002211#ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
Peter Zijlstra9881b022016-12-15 13:35:52 +01002212static inline void sched_clock_init_late(void)
2213{
2214}
2215
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002216static inline void sched_clock_tick(void)
2217{
2218}
2219
Paul Gortmaker733ce722017-01-27 12:38:16 -05002220static inline void clear_sched_clock_stable(void)
2221{
2222}
2223
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002224static inline void sched_clock_idle_sleep_event(void)
2225{
2226}
2227
2228static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
2229{
2230}
Daniel Lezcano2c923e92016-04-11 16:38:34 +02002231
2232static inline u64 cpu_clock(int cpu)
2233{
2234 return sched_clock();
2235}
2236
2237static inline u64 local_clock(void)
2238{
2239 return sched_clock();
2240}
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002241#else
Peter Zijlstra9881b022016-12-15 13:35:52 +01002242extern void sched_clock_init_late(void);
Peter Zijlstrac6763292010-05-25 10:48:51 +02002243/*
2244 * Architectures can set this to 1 if they have specified
2245 * CONFIG_HAVE_UNSTABLE_SCHED_CLOCK in their arch Kconfig,
2246 * but then during bootup it turns out that sched_clock()
2247 * is reliable after all:
2248 */
Peter Zijlstra35af99e2013-11-28 19:38:42 +01002249extern int sched_clock_stable(void);
Peter Zijlstra35af99e2013-11-28 19:38:42 +01002250extern void clear_sched_clock_stable(void);
Peter Zijlstrac6763292010-05-25 10:48:51 +02002251
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002252extern void sched_clock_tick(void);
2253extern void sched_clock_idle_sleep_event(void);
2254extern void sched_clock_idle_wakeup_event(u64 delta_ns);
Daniel Lezcano2c923e92016-04-11 16:38:34 +02002255
2256/*
2257 * As outlined in clock.c, provides a fast, high resolution, nanosecond
2258 * time source that is monotonic per cpu argument and has bounded drift
2259 * between cpus.
2260 *
2261 * ######################### BIG FAT WARNING ##########################
2262 * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can #
2263 * # go backwards !! #
2264 * ####################################################################
2265 */
2266static inline u64 cpu_clock(int cpu)
2267{
2268 return sched_clock_cpu(cpu);
2269}
2270
2271static inline u64 local_clock(void)
2272{
2273 return sched_clock_cpu(raw_smp_processor_id());
2274}
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002275#endif
2276
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07002277#ifdef CONFIG_IRQ_TIME_ACCOUNTING
2278/*
2279 * An i/f to runtime opt-in for irq time accounting based off of sched_clock.
2280 * The reason for this explicit opt-in is not to have perf penalty with
2281 * slow sched_clocks.
2282 */
2283extern void enable_sched_clock_irqtime(void);
2284extern void disable_sched_clock_irqtime(void);
2285#else
2286static inline void enable_sched_clock_irqtime(void) {}
2287static inline void disable_sched_clock_irqtime(void) {}
2288#endif
2289
Ingo Molnar36c8b582006-07-03 00:25:41 -07002290extern unsigned long long
Ingo Molnar41b86e92007-07-09 18:51:58 +02002291task_sched_runtime(struct task_struct *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293/* sched_exec is called by processes performing an exec */
2294#ifdef CONFIG_SMP
2295extern void sched_exec(void);
2296#else
2297#define sched_exec() {}
2298#endif
2299
Ingo Molnar2aa44d02007-08-23 15:18:02 +02002300extern void sched_clock_idle_sleep_event(void);
2301extern void sched_clock_idle_wakeup_event(u64 delta_ns);
Ingo Molnarbb29ab22007-07-09 18:51:59 +02002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#ifdef CONFIG_HOTPLUG_CPU
2304extern void idle_task_exit(void);
2305#else
2306static inline void idle_task_exit(void) {}
2307#endif
2308
Frederic Weisbecker3451d022011-08-10 23:21:01 +02002309#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
Frederic Weisbecker1c200912011-08-10 23:21:01 +02002310extern void wake_up_nohz_cpu(int cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +01002311#else
Frederic Weisbecker1c200912011-08-10 23:21:01 +02002312static inline void wake_up_nohz_cpu(int cpu) { }
Thomas Gleixner06d83082008-03-22 09:20:24 +01002313#endif
2314
Frederic Weisbeckerce831b32013-04-20 15:15:35 +02002315#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002316extern u64 scheduler_tick_max_deferment(void);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002317#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002318
Mike Galbraith5091faa2010-11-30 14:18:03 +01002319#ifdef CONFIG_SCHED_AUTOGROUP
Mike Galbraith5091faa2010-11-30 14:18:03 +01002320extern void sched_autogroup_create_attach(struct task_struct *p);
2321extern void sched_autogroup_detach(struct task_struct *p);
2322extern void sched_autogroup_fork(struct signal_struct *sig);
2323extern void sched_autogroup_exit(struct signal_struct *sig);
Oleg Nesterov8e5bfa82016-11-14 19:46:12 +01002324extern void sched_autogroup_exit_task(struct task_struct *p);
Mike Galbraith5091faa2010-11-30 14:18:03 +01002325#ifdef CONFIG_PROC_FS
2326extern void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m);
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09002327extern int proc_sched_autogroup_set_nice(struct task_struct *p, int nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01002328#endif
2329#else
2330static inline void sched_autogroup_create_attach(struct task_struct *p) { }
2331static inline void sched_autogroup_detach(struct task_struct *p) { }
2332static inline void sched_autogroup_fork(struct signal_struct *sig) { }
2333static inline void sched_autogroup_exit(struct signal_struct *sig) { }
Oleg Nesterov8e5bfa82016-11-14 19:46:12 +01002334static inline void sched_autogroup_exit_task(struct task_struct *p) { }
Mike Galbraith5091faa2010-11-30 14:18:03 +01002335#endif
2336
Dan Carpenterfa933842014-05-23 13:20:42 +03002337extern int yield_to(struct task_struct *p, bool preempt);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002338extern void set_user_nice(struct task_struct *p, long nice);
2339extern int task_prio(const struct task_struct *p);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05002340/**
2341 * task_nice - return the nice value of a given task.
2342 * @p: the task in question.
2343 *
2344 * Return: The nice value [ -20 ... 0 ... 19 ].
2345 */
2346static inline int task_nice(const struct task_struct *p)
2347{
2348 return PRIO_TO_NICE((p)->static_prio);
2349}
Ingo Molnar36c8b582006-07-03 00:25:41 -07002350extern int can_nice(const struct task_struct *p, const int nice);
2351extern int task_curr(const struct task_struct *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352extern int idle_cpu(int cpu);
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07002353extern int sched_setscheduler(struct task_struct *, int,
2354 const struct sched_param *);
Rusty Russell961ccdd2008-06-23 13:55:38 +10002355extern int sched_setscheduler_nocheck(struct task_struct *, int,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07002356 const struct sched_param *);
Dario Faggiolid50dde52013-11-07 14:43:36 +01002357extern int sched_setattr(struct task_struct *,
2358 const struct sched_attr *);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002359extern struct task_struct *idle_task(int cpu);
Paul E. McKenneyc4f30602011-11-10 12:41:56 -08002360/**
2361 * is_idle_task - is the specified task an idle task?
Randy Dunlapfa757282012-01-21 11:03:13 -08002362 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002363 *
2364 * Return: 1 if @p is an idle task. 0 otherwise.
Paul E. McKenneyc4f30602011-11-10 12:41:56 -08002365 */
Paul E. McKenney7061ca32011-12-20 08:20:46 -08002366static inline bool is_idle_task(const struct task_struct *p)
Paul E. McKenneyc4f30602011-11-10 12:41:56 -08002367{
Peter Zijlstrac1de45c2016-11-28 23:03:05 -08002368 return !!(p->flags & PF_IDLE);
Paul E. McKenneyc4f30602011-11-10 12:41:56 -08002369}
Ingo Molnar36c8b582006-07-03 00:25:41 -07002370extern struct task_struct *curr_task(int cpu);
Peter Zijlstraa458ae22016-09-20 20:29:40 +02002371extern void ia64_set_curr_task(int cpu, struct task_struct *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373void yield(void);
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375union thread_union {
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002376#ifndef CONFIG_THREAD_INFO_IN_TASK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct thread_info thread_info;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 unsigned long stack[THREAD_SIZE/sizeof(long)];
2380};
2381
2382#ifndef __HAVE_ARCH_KSTACK_END
2383static inline int kstack_end(void *addr)
2384{
2385 /* Reliable end of stack detection:
2386 * Some APM bios versions misalign the stack
2387 */
2388 return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
2389}
2390#endif
2391
2392extern union thread_union init_thread_union;
2393extern struct task_struct init_task;
2394
2395extern struct mm_struct init_mm;
2396
Pavel Emelyanov198fe212007-10-18 23:40:06 -07002397extern struct pid_namespace init_pid_ns;
2398
2399/*
2400 * find a task by one of its numerical ids
2401 *
Pavel Emelyanov198fe212007-10-18 23:40:06 -07002402 * find_task_by_pid_ns():
2403 * finds a task by its pid in the specified namespace
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002404 * find_task_by_vpid():
2405 * finds a task by its virtual pid
Pavel Emelyanov198fe212007-10-18 23:40:06 -07002406 *
Pavel Emelyanove49859e2008-07-25 01:48:36 -07002407 * see also find_vpid() etc in include/linux/pid.h
Pavel Emelyanov198fe212007-10-18 23:40:06 -07002408 */
2409
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002410extern struct task_struct *find_task_by_vpid(pid_t nr);
2411extern struct task_struct *find_task_by_pid_ns(pid_t nr,
2412 struct pid_namespace *ns);
Pavel Emelyanov198fe212007-10-18 23:40:06 -07002413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/* per-UID process charging. */
Eric W. Biederman7b44ab92011-11-16 23:20:58 -08002415extern struct user_struct * alloc_uid(kuid_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416static inline struct user_struct *get_uid(struct user_struct *u)
2417{
2418 atomic_inc(&u->__count);
2419 return u;
2420}
2421extern void free_uid(struct user_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423#include <asm/current.h>
2424
Torben Hohnf0af911a92011-01-27 15:59:10 +01002425extern void xtime_update(unsigned long ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Harvey Harrisonb3c97522008-02-13 15:03:15 -08002427extern int wake_up_state(struct task_struct *tsk, unsigned int state);
2428extern int wake_up_process(struct task_struct *tsk);
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002429extern void wake_up_new_task(struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430#ifdef CONFIG_SMP
2431 extern void kick_process(struct task_struct *tsk);
2432#else
2433 static inline void kick_process(struct task_struct *tsk) { }
2434#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +01002435extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
Ingo Molnarad46c2c2007-07-09 18:52:00 +02002436extern void sched_dead(struct task_struct *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438extern void proc_caches_init(void);
2439extern void flush_signals(struct task_struct *);
Oleg Nesterov10ab8252007-05-09 02:34:37 -07002440extern void ignore_signals(struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441extern void flush_signal_handlers(struct task_struct *, int force_default);
2442extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
2443
Oleg Nesterovbe0e6f22015-11-06 16:32:22 -08002444static inline int kernel_dequeue_signal(siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Oleg Nesterovbe0e6f22015-11-06 16:32:22 -08002446 struct task_struct *tsk = current;
2447 siginfo_t __info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 int ret;
2449
Oleg Nesterovbe0e6f22015-11-06 16:32:22 -08002450 spin_lock_irq(&tsk->sighand->siglock);
2451 ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
2452 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 return ret;
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Oleg Nesterov9a130492015-11-06 16:32:25 -08002457static inline void kernel_signal_stop(void)
2458{
2459 spin_lock_irq(&current->sighand->siglock);
2460 if (current->jobctl & JOBCTL_STOP_DEQUEUED)
2461 __set_current_state(TASK_STOPPED);
2462 spin_unlock_irq(&current->sighand->siglock);
2463
2464 schedule();
2465}
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467extern void release_task(struct task_struct * p);
2468extern int send_sig_info(int, struct siginfo *, struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469extern int force_sigsegv(int, struct task_struct *);
2470extern int force_sig_info(int, struct siginfo *, struct task_struct *);
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07002471extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07002472extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
Serge Hallynd178bc32011-09-26 10:45:18 -05002473extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
2474 const struct cred *, u32);
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07002475extern int kill_pgrp(struct pid *pid, int sig, int priv);
2476extern int kill_pid(struct pid *pid, int sig, int priv);
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07002477extern int kill_proc_info(int, struct siginfo *, pid_t);
Oleg Nesterov86773472011-06-22 23:09:09 +02002478extern __must_check bool do_notify_parent(struct task_struct *, int);
Oleg Nesterova7f07652009-09-23 15:56:44 -07002479extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480extern void force_sig(int, struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481extern int send_sig(int, struct task_struct *, int);
Oleg Nesterov09faef12010-05-26 14:43:11 -07002482extern int zap_other_threads(struct task_struct *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483extern struct sigqueue *sigqueue_alloc(void);
2484extern void sigqueue_free(struct sigqueue *);
Oleg Nesterovac5c2152008-04-30 00:52:57 -07002485extern int send_sigqueue(struct sigqueue *, struct task_struct *, int group);
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002486extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Andy Lutomirski7e781412016-08-02 14:05:36 -07002488#ifdef TIF_RESTORE_SIGMASK
2489/*
2490 * Legacy restore_sigmask accessors. These are inefficient on
2491 * SMP architectures because they require atomic operations.
2492 */
2493
2494/**
2495 * set_restore_sigmask() - make sure saved_sigmask processing gets done
2496 *
2497 * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
2498 * will run before returning to user mode, to process the flag. For
2499 * all callers, TIF_SIGPENDING is already set or it's no harm to set
2500 * it. TIF_RESTORE_SIGMASK need not be in the set of bits that the
2501 * arch code will notice on return to user mode, in case those bits
2502 * are scarce. We set TIF_SIGPENDING here to ensure that the arch
2503 * signal code always gets run when TIF_RESTORE_SIGMASK is set.
2504 */
2505static inline void set_restore_sigmask(void)
2506{
2507 set_thread_flag(TIF_RESTORE_SIGMASK);
2508 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
2509}
2510static inline void clear_restore_sigmask(void)
2511{
2512 clear_thread_flag(TIF_RESTORE_SIGMASK);
2513}
2514static inline bool test_restore_sigmask(void)
2515{
2516 return test_thread_flag(TIF_RESTORE_SIGMASK);
2517}
2518static inline bool test_and_clear_restore_sigmask(void)
2519{
2520 return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
2521}
2522
2523#else /* TIF_RESTORE_SIGMASK */
2524
2525/* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
2526static inline void set_restore_sigmask(void)
2527{
2528 current->restore_sigmask = true;
2529 WARN_ON(!test_thread_flag(TIF_SIGPENDING));
2530}
2531static inline void clear_restore_sigmask(void)
2532{
2533 current->restore_sigmask = false;
2534}
2535static inline bool test_restore_sigmask(void)
2536{
2537 return current->restore_sigmask;
2538}
2539static inline bool test_and_clear_restore_sigmask(void)
2540{
2541 if (!current->restore_sigmask)
2542 return false;
2543 current->restore_sigmask = false;
2544 return true;
2545}
2546#endif
2547
Al Viro51a7b442012-05-21 23:33:55 -04002548static inline void restore_saved_sigmask(void)
2549{
2550 if (test_and_clear_restore_sigmask())
Al Viro77097ae2012-04-27 13:58:59 -04002551 __set_current_blocked(&current->saved_sigmask);
Al Viro51a7b442012-05-21 23:33:55 -04002552}
2553
Al Virob7f9a112012-05-02 09:59:21 -04002554static inline sigset_t *sigmask_to_save(void)
2555{
2556 sigset_t *res = &current->blocked;
2557 if (unlikely(test_restore_sigmask()))
2558 res = &current->saved_sigmask;
2559 return res;
2560}
2561
Cedric Le Goater9ec52092006-10-02 02:19:00 -07002562static inline int kill_cad_pid(int sig, int priv)
2563{
2564 return kill_pid(cad_pid, sig, priv);
2565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567/* These can be the second arg to send_sig_info/send_group_sig_info. */
2568#define SEND_SIG_NOINFO ((struct siginfo *) 0)
2569#define SEND_SIG_PRIV ((struct siginfo *) 1)
2570#define SEND_SIG_FORCED ((struct siginfo *) 2)
2571
Sebastian Andrzej Siewior2a855dd2009-10-25 15:37:58 +01002572/*
2573 * True if we are on the alternate signal stack.
2574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575static inline int on_sig_stack(unsigned long sp)
2576{
Andy Lutomirskic876eea2016-05-03 10:31:49 -07002577 /*
2578 * If the signal stack is SS_AUTODISARM then, by construction, we
2579 * can't be on the signal stack unless user code deliberately set
2580 * SS_AUTODISARM when we were already on it.
2581 *
2582 * This improves reliability: if user state gets corrupted such that
2583 * the stack pointer points very close to the end of the signal stack,
2584 * then this check will enable the signal to be handled anyway.
2585 */
2586 if (current->sas_ss_flags & SS_AUTODISARM)
2587 return 0;
2588
Sebastian Andrzej Siewior2a855dd2009-10-25 15:37:58 +01002589#ifdef CONFIG_STACK_GROWSUP
2590 return sp >= current->sas_ss_sp &&
2591 sp - current->sas_ss_sp < current->sas_ss_size;
2592#else
2593 return sp > current->sas_ss_sp &&
2594 sp - current->sas_ss_sp <= current->sas_ss_size;
2595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598static inline int sas_ss_flags(unsigned long sp)
2599{
Richard Weinberger72f15c02014-03-05 15:15:22 +01002600 if (!current->sas_ss_size)
2601 return SS_DISABLE;
2602
2603 return on_sig_stack(sp) ? SS_ONSTACK : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Stas Sergeev2a742132016-04-14 23:20:04 +03002606static inline void sas_ss_reset(struct task_struct *p)
2607{
2608 p->sas_ss_sp = 0;
2609 p->sas_ss_size = 0;
2610 p->sas_ss_flags = SS_DISABLE;
2611}
2612
Al Viro5a1b98d2012-11-06 13:28:21 -05002613static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
2614{
2615 if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
2616#ifdef CONFIG_STACK_GROWSUP
2617 return current->sas_ss_sp;
2618#else
2619 return current->sas_ss_sp + current->sas_ss_size;
2620#endif
2621 return sp;
2622}
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624/*
2625 * Routines for handling mm_structs
2626 */
2627extern struct mm_struct * mm_alloc(void);
2628
Vegard Nossumf1f10072017-02-27 14:30:07 -08002629/**
2630 * mmgrab() - Pin a &struct mm_struct.
2631 * @mm: The &struct mm_struct to pin.
2632 *
2633 * Make sure that @mm will not get freed even after the owning task
2634 * exits. This doesn't guarantee that the associated address space
2635 * will still exist later on and mmget_not_zero() has to be used before
2636 * accessing it.
2637 *
2638 * This is a preferred way to to pin @mm for a longer/unbounded amount
2639 * of time.
2640 *
2641 * Use mmdrop() to release the reference acquired by mmgrab().
2642 *
2643 * See also <Documentation/vm/active_mm.txt> for an in-depth explanation
2644 * of &mm_struct.mm_count vs &mm_struct.mm_users.
2645 */
2646static inline void mmgrab(struct mm_struct *mm)
2647{
2648 atomic_inc(&mm->mm_count);
2649}
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651/* mmdrop drops the mm and the page tables */
Harvey Harrisonb3c97522008-02-13 15:03:15 -08002652extern void __mmdrop(struct mm_struct *);
Oleg Nesterovd2005e32016-05-20 16:58:36 -07002653static inline void mmdrop(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Ingo Molnar6fb43d72007-07-09 18:52:01 +02002655 if (unlikely(atomic_dec_and_test(&mm->mm_count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 __mmdrop(mm);
2657}
2658
Michal Hocko7283094ec2016-10-07 16:58:54 -07002659static inline void mmdrop_async_fn(struct work_struct *work)
2660{
2661 struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work);
2662 __mmdrop(mm);
2663}
2664
2665static inline void mmdrop_async(struct mm_struct *mm)
2666{
2667 if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
2668 INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
2669 schedule_work(&mm->async_put_work);
2670 }
2671}
2672
Vegard Nossum3fce3712017-02-27 14:30:10 -08002673/**
2674 * mmget() - Pin the address space associated with a &struct mm_struct.
2675 * @mm: The address space to pin.
2676 *
2677 * Make sure that the address space of the given &struct mm_struct doesn't
2678 * go away. This does not protect against parts of the address space being
2679 * modified or freed, however.
2680 *
2681 * Never use this function to pin this address space for an
2682 * unbounded/indefinite amount of time.
2683 *
2684 * Use mmput() to release the reference acquired by mmget().
2685 *
2686 * See also <Documentation/vm/active_mm.txt> for an in-depth explanation
2687 * of &mm_struct.mm_count vs &mm_struct.mm_users.
2688 */
2689static inline void mmget(struct mm_struct *mm)
2690{
2691 atomic_inc(&mm->mm_users);
2692}
2693
Oleg Nesterovd2005e32016-05-20 16:58:36 -07002694static inline bool mmget_not_zero(struct mm_struct *mm)
2695{
2696 return atomic_inc_not_zero(&mm->mm_users);
2697}
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699/* mmput gets rid of the mappings and all user-space */
2700extern void mmput(struct mm_struct *);
Michal Hocko7ef949d72016-05-26 15:16:22 -07002701#ifdef CONFIG_MMU
2702/* same as above but performs the slow path from the async context. Can
Michal Hockoec8d7c12016-05-20 16:57:21 -07002703 * be called from the atomic context as well
2704 */
2705extern void mmput_async(struct mm_struct *);
Michal Hocko7ef949d72016-05-26 15:16:22 -07002706#endif
Michal Hockoec8d7c12016-05-20 16:57:21 -07002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708/* Grab a reference to a task's mm, if it is not already going away */
2709extern struct mm_struct *get_task_mm(struct task_struct *task);
Christopher Yeoh8cdb8782012-02-02 11:34:09 +10302710/*
2711 * Grab a reference to a task's mm, if it is not already going away
2712 * and ptrace_may_access with the mode parameter passed to it
2713 * succeeds.
2714 */
2715extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/* Remove the current tasks stale references to the old mm_struct */
2717extern void mm_release(struct task_struct *, struct mm_struct *);
2718
Josh Triplett3033f14a2015-06-25 15:01:19 -07002719#ifdef CONFIG_HAVE_COPY_THREAD_TLS
2720extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
2721 struct task_struct *, unsigned long);
2722#else
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -07002723extern int copy_thread(unsigned long, unsigned long, unsigned long,
Al Viroafa86fc2012-10-22 22:51:14 -04002724 struct task_struct *);
Josh Triplett3033f14a2015-06-25 15:01:19 -07002725
2726/* Architectures that haven't opted into copy_thread_tls get the tls argument
2727 * via pt_regs, so ignore the tls argument passed via C. */
2728static inline int copy_thread_tls(
2729 unsigned long clone_flags, unsigned long sp, unsigned long arg,
2730 struct task_struct *p, unsigned long tls)
2731{
2732 return copy_thread(clone_flags, sp, arg, p);
2733}
2734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735extern void flush_thread(void);
Jiri Slaby5f56a5d2016-05-20 17:00:16 -07002736
2737#ifdef CONFIG_HAVE_EXIT_THREAD
Jiri Slabye6464692016-05-20 17:00:20 -07002738extern void exit_thread(struct task_struct *tsk);
Jiri Slaby5f56a5d2016-05-20 17:00:16 -07002739#else
Jiri Slabye6464692016-05-20 17:00:20 -07002740static inline void exit_thread(struct task_struct *tsk)
Jiri Slaby5f56a5d2016-05-20 17:00:16 -07002741{
2742}
2743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745extern void exit_files(struct task_struct *);
Oleg Nesterova7e53282006-03-28 16:11:27 -08002746extern void __cleanup_sighand(struct sighand_struct *);
Oleg Nesterovcbaffba2008-05-26 20:55:42 +04002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748extern void exit_itimers(struct signal_struct *);
Oleg Nesterovcbaffba2008-05-26 20:55:42 +04002749extern void flush_itimer_signals(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Joe Perches9402c952012-01-12 17:17:17 -08002751extern void do_group_exit(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -08002753extern int do_execve(struct filename *,
David Howellsd7627462010-08-17 23:52:56 +01002754 const char __user * const __user *,
Al Viroda3d4c52012-10-20 21:49:33 -04002755 const char __user * const __user *);
David Drysdale51f39a12014-12-12 16:57:29 -08002756extern int do_execveat(int, struct filename *,
2757 const char __user * const __user *,
2758 const char __user * const __user *,
2759 int);
Josh Triplett3033f14a2015-06-25 15:01:19 -07002760extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
Al Viroe80d6662012-10-22 23:10:08 -04002761extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002762struct task_struct *fork_idle(int);
Al Viro2aa3a7f2012-09-21 19:55:31 -04002763extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Adrian Hunter82b89772014-05-28 11:45:04 +03002765extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
2766static inline void set_task_comm(struct task_struct *tsk, const char *from)
2767{
2768 __set_task_comm(tsk, from, false);
2769}
Andrew Morton59714d62008-02-04 22:27:21 -08002770extern char *get_task_comm(char *to, struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772#ifdef CONFIG_SMP
Peter Zijlstra317f3942011-04-05 17:23:58 +02002773void scheduler_ipi(void);
Roland McGrath85ba2d82008-07-25 19:45:58 -07002774extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775#else
Peter Zijlstra184748c2011-04-05 17:23:39 +02002776static inline void scheduler_ipi(void) { }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002777static inline unsigned long wait_task_inactive(struct task_struct *p,
2778 long match_state)
2779{
2780 return 1;
2781}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782#endif
2783
Frederic Weisbeckerfafe8702015-05-06 18:04:24 +02002784#define tasklist_empty() \
2785 list_empty(&init_task.tasks)
2786
Jiri Pirko05725f72009-04-14 20:17:16 +02002787#define next_task(p) \
2788 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790#define for_each_process(p) \
2791 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
2792
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02002793extern bool current_is_single_threaded(void);
David Howellsd84f4f92008-11-14 10:39:23 +11002794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795/*
2796 * Careful: do_each_thread/while_each_thread is a double loop so
2797 * 'break' will not work as expected - use goto instead.
2798 */
2799#define do_each_thread(g, t) \
2800 for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
2801
2802#define while_each_thread(g, t) \
2803 while ((t = next_thread(t)) != g)
2804
Oleg Nesterov0c740d02014-01-21 15:49:56 -08002805#define __for_each_thread(signal, t) \
2806 list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
2807
2808#define for_each_thread(p, t) \
2809 __for_each_thread((p)->signal, t)
2810
2811/* Careful: this is a double loop, 'break' won't work as expected. */
2812#define for_each_process_thread(p, t) \
2813 for_each_process(p) for_each_thread(p, t)
2814
Oleg Nesterov0f1b92c2017-01-30 18:06:11 +03002815typedef int (*proc_visitor)(struct task_struct *p, void *data);
2816void walk_process_tree(struct task_struct *top, proc_visitor, void *);
2817
Oleg Nesterov7e498272010-05-26 14:43:22 -07002818static inline int get_nr_threads(struct task_struct *tsk)
2819{
Oleg Nesterovb3ac0222010-05-26 14:43:24 -07002820 return tsk->signal->nr_threads;
Oleg Nesterov7e498272010-05-26 14:43:22 -07002821}
2822
Oleg Nesterov087806b2011-06-22 23:10:26 +02002823static inline bool thread_group_leader(struct task_struct *p)
2824{
2825 return p->exit_signal >= 0;
2826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002828/* Do to the insanities of de_thread it is possible for a process
2829 * to have the pid of the thread group leader without actually being
2830 * the thread group leader. For iteration through the pids in proc
2831 * all we care about is that we have a task with the appropriate
2832 * pid, we don't actually care if we have the right task.
2833 */
Oleg Nesterove1403b82013-09-11 14:20:06 -07002834static inline bool has_group_leader_pid(struct task_struct *p)
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002835{
Oleg Nesterove1403b82013-09-11 14:20:06 -07002836 return task_pid(p) == p->signal->leader_pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002837}
2838
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -07002839static inline
Oleg Nesterove1403b82013-09-11 14:20:06 -07002840bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -07002841{
Oleg Nesterove1403b82013-09-11 14:20:06 -07002842 return p1->signal == p2->signal;
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -07002843}
2844
Ingo Molnar36c8b582006-07-03 00:25:41 -07002845static inline struct task_struct *next_thread(const struct task_struct *p)
Oleg Nesterov47e65322006-03-28 16:11:25 -08002846{
Jiri Pirko05725f72009-04-14 20:17:16 +02002847 return list_entry_rcu(p->thread_group.next,
2848 struct task_struct, thread_group);
Oleg Nesterov47e65322006-03-28 16:11:25 -08002849}
2850
Alexey Dobriyane8681712007-10-26 12:17:22 +04002851static inline int thread_group_empty(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Oleg Nesterov47e65322006-03-28 16:11:25 -08002853 return list_empty(&p->thread_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
2856#define delay_group_leader(p) \
2857 (thread_group_leader(p) && !thread_group_empty(p))
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859/*
Eric W. Biederman260ea102006-06-23 02:05:18 -07002860 * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
Jens Axboe22e2c502005-06-27 10:55:12 +02002861 * subscriptions and synchronises with wait4(). Also used in procfs. Also
Paul Menageddbcc7e2007-10-18 23:39:30 -07002862 * pins the final release of task.io_context. Also protects ->cpuset and
Oleg Nesterovd68b46f2012-03-05 14:59:13 -08002863 * ->cgroup.subsys[]. And ->vfork_done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 *
2865 * Nests both inside and outside of read_lock(&tasklist_lock).
2866 * It must not be nested with write_lock_irq(&tasklist_lock),
2867 * neither inside nor outside.
2868 */
2869static inline void task_lock(struct task_struct *p)
2870{
2871 spin_lock(&p->alloc_lock);
2872}
2873
2874static inline void task_unlock(struct task_struct *p)
2875{
2876 spin_unlock(&p->alloc_lock);
2877}
2878
Namhyung Kimb8ed3742010-10-27 15:34:06 -07002879extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
Oleg Nesterovf63ee722006-03-28 16:11:13 -08002880 unsigned long *flags);
2881
Anton Vorontsov9388dc32012-02-09 20:45:19 +04002882static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
2883 unsigned long *flags)
2884{
2885 struct sighand_struct *ret;
2886
2887 ret = __lock_task_sighand(tsk, flags);
2888 (void)__cond_lock(&tsk->sighand->siglock, ret);
2889 return ret;
2890}
Namhyung Kimb8ed3742010-10-27 15:34:06 -07002891
Oleg Nesterovf63ee722006-03-28 16:11:13 -08002892static inline void unlock_task_sighand(struct task_struct *tsk,
2893 unsigned long *flags)
2894{
2895 spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
2896}
2897
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002898#ifdef CONFIG_THREAD_INFO_IN_TASK
2899
2900static inline struct thread_info *task_thread_info(struct task_struct *task)
2901{
2902 return &task->thread_info;
2903}
Andy Lutomirskic6c314a2016-09-15 22:45:43 -07002904
2905/*
2906 * When accessing the stack of a non-current task that might exit, use
2907 * try_get_task_stack() instead. task_stack_page will return a pointer
2908 * that could get freed out from under you.
2909 */
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002910static inline void *task_stack_page(const struct task_struct *task)
2911{
2912 return task->stack;
2913}
Andy Lutomirskic6c314a2016-09-15 22:45:43 -07002914
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002915#define setup_thread_stack(new,old) do { } while(0)
Andy Lutomirskic6c314a2016-09-15 22:45:43 -07002916
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002917static inline unsigned long *end_of_stack(const struct task_struct *task)
2918{
2919 return task->stack;
2920}
2921
2922#elif !defined(__HAVE_THREAD_FUNCTIONS)
Al Virof0373602005-11-13 16:06:57 -08002923
Roman Zippelf7e42172007-05-09 02:35:17 -07002924#define task_thread_info(task) ((struct thread_info *)(task)->stack)
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07002925#define task_stack_page(task) ((void *)(task)->stack)
Al Viroa1261f542005-11-13 16:06:55 -08002926
Al Viro10ebffd2005-11-13 16:06:56 -08002927static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
2928{
2929 *task_thread_info(p) = *task_thread_info(org);
2930 task_thread_info(p)->task = p;
2931}
2932
Chuck Ebbert6a402812014-09-20 10:17:51 -05002933/*
2934 * Return the address of the last usable long on the stack.
2935 *
2936 * When the stack grows down, this is just above the thread
2937 * info struct. Going any lower will corrupt the threadinfo.
2938 *
2939 * When the stack grows up, this is the highest address.
2940 * Beyond that position, we corrupt data on the next page.
2941 */
Al Viro10ebffd2005-11-13 16:06:56 -08002942static inline unsigned long *end_of_stack(struct task_struct *p)
2943{
Chuck Ebbert6a402812014-09-20 10:17:51 -05002944#ifdef CONFIG_STACK_GROWSUP
2945 return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
2946#else
Roman Zippelf7e42172007-05-09 02:35:17 -07002947 return (unsigned long *)(task_thread_info(p) + 1);
Chuck Ebbert6a402812014-09-20 10:17:51 -05002948#endif
Al Viro10ebffd2005-11-13 16:06:56 -08002949}
2950
Al Virof0373602005-11-13 16:06:57 -08002951#endif
Andy Lutomirskic6c314a2016-09-15 22:45:43 -07002952
Andy Lutomirski68f24b082016-09-15 22:45:48 -07002953#ifdef CONFIG_THREAD_INFO_IN_TASK
2954static inline void *try_get_task_stack(struct task_struct *tsk)
2955{
2956 return atomic_inc_not_zero(&tsk->stack_refcount) ?
2957 task_stack_page(tsk) : NULL;
2958}
2959
2960extern void put_task_stack(struct task_struct *tsk);
2961#else
Andy Lutomirskic6c314a2016-09-15 22:45:43 -07002962static inline void *try_get_task_stack(struct task_struct *tsk)
2963{
2964 return task_stack_page(tsk);
2965}
2966
2967static inline void put_task_stack(struct task_struct *tsk) {}
Andy Lutomirski68f24b082016-09-15 22:45:48 -07002968#endif
Andy Lutomirskic6c314a2016-09-15 22:45:43 -07002969
Aaron Tomlina70857e2014-09-12 14:16:18 +01002970#define task_stack_end_corrupted(task) \
2971 (*(end_of_stack(task)) != STACK_END_MAGIC)
Al Virof0373602005-11-13 16:06:57 -08002972
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -07002973static inline int object_is_on_stack(void *obj)
2974{
2975 void *stack = task_stack_page(current);
2976
2977 return (obj >= stack) && (obj < (stack + THREAD_SIZE));
2978}
2979
Linus Torvaldsb235bee2016-06-24 15:09:37 -07002980extern void thread_stack_cache_init(void);
Benjamin Herrenschmidt8c9843e2008-04-18 16:56:15 +10002981
Eric Sandeen7c9f8862008-04-22 16:38:23 -05002982#ifdef CONFIG_DEBUG_STACK_USAGE
2983static inline unsigned long stack_not_used(struct task_struct *p)
2984{
2985 unsigned long *n = end_of_stack(p);
2986
2987 do { /* Skip over canary */
Helge Deller6c31da32016-03-19 17:54:10 +01002988# ifdef CONFIG_STACK_GROWSUP
2989 n--;
2990# else
Eric Sandeen7c9f8862008-04-22 16:38:23 -05002991 n++;
Helge Deller6c31da32016-03-19 17:54:10 +01002992# endif
Eric Sandeen7c9f8862008-04-22 16:38:23 -05002993 } while (!*n);
2994
Helge Deller6c31da32016-03-19 17:54:10 +01002995# ifdef CONFIG_STACK_GROWSUP
2996 return (unsigned long)end_of_stack(p) - (unsigned long)n;
2997# else
Eric Sandeen7c9f8862008-04-22 16:38:23 -05002998 return (unsigned long)n - (unsigned long)end_of_stack(p);
Helge Deller6c31da32016-03-19 17:54:10 +01002999# endif
Eric Sandeen7c9f8862008-04-22 16:38:23 -05003000}
3001#endif
Aaron Tomlind4311ff2014-09-12 14:16:17 +01003002extern void set_task_stack_end_magic(struct task_struct *tsk);
Eric Sandeen7c9f8862008-04-22 16:38:23 -05003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004/* set thread flags in other task's structures
3005 * - see asm/thread_info.h for TIF_xxxx flags available
3006 */
3007static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
3008{
Al Viroa1261f542005-11-13 16:06:55 -08003009 set_ti_thread_flag(task_thread_info(tsk), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
3012static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
3013{
Al Viroa1261f542005-11-13 16:06:55 -08003014 clear_ti_thread_flag(task_thread_info(tsk), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
3018{
Al Viroa1261f542005-11-13 16:06:55 -08003019 return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
3022static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
3023{
Al Viroa1261f542005-11-13 16:06:55 -08003024 return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
3028{
Al Viroa1261f542005-11-13 16:06:55 -08003029 return test_ti_thread_flag(task_thread_info(tsk), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
3032static inline void set_tsk_need_resched(struct task_struct *tsk)
3033{
3034 set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
3035}
3036
3037static inline void clear_tsk_need_resched(struct task_struct *tsk)
3038{
3039 clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
3040}
3041
Gregory Haskins8ae121a2008-04-23 07:13:29 -04003042static inline int test_tsk_need_resched(struct task_struct *tsk)
3043{
3044 return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
3045}
3046
Eric W. Biederman690cc3f2009-05-13 16:55:10 +00003047static inline int restart_syscall(void)
3048{
3049 set_tsk_thread_flag(current, TIF_SIGPENDING);
3050 return -ERESTARTNOINTR;
3051}
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053static inline int signal_pending(struct task_struct *p)
3054{
3055 return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
3056}
Matthew Wilcoxf776d122007-12-06 11:15:50 -05003057
Roland McGrathd9588722009-09-23 15:57:04 -07003058static inline int __fatal_signal_pending(struct task_struct *p)
3059{
3060 return unlikely(sigismember(&p->pending.signal, SIGKILL));
3061}
Matthew Wilcoxf776d122007-12-06 11:15:50 -05003062
3063static inline int fatal_signal_pending(struct task_struct *p)
3064{
3065 return signal_pending(p) && __fatal_signal_pending(p);
3066}
3067
Oleg Nesterov16882c12008-06-08 21:20:41 +04003068static inline int signal_pending_state(long state, struct task_struct *p)
3069{
3070 if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
3071 return 0;
3072 if (!signal_pending(p))
3073 return 0;
3074
Oleg Nesterov16882c12008-06-08 21:20:41 +04003075 return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
3076}
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078/*
3079 * cond_resched() and cond_resched_lock(): latency reduction via
3080 * explicit rescheduling in places that are safe. The return
3081 * value indicates whether a reschedule was done in fact.
3082 * cond_resched_lock() will drop the spinlock before scheduling,
3083 * cond_resched_softirq() will enable bhs before scheduling.
3084 */
Peter Zijlstra35a773a2016-09-19 12:57:53 +02003085#ifndef CONFIG_PREEMPT
Linus Torvaldsc3921ab2008-05-11 16:04:48 -07003086extern int _cond_resched(void);
Peter Zijlstra35a773a2016-09-19 12:57:53 +02003087#else
3088static inline int _cond_resched(void) { return 0; }
3089#endif
Frederic Weisbecker6f80bd92009-07-16 15:44:29 +02003090
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02003091#define cond_resched() ({ \
Peter Zijlstra34274452014-09-24 10:18:56 +02003092 ___might_sleep(__FILE__, __LINE__, 0); \
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02003093 _cond_resched(); \
3094})
Frederic Weisbecker6f80bd92009-07-16 15:44:29 +02003095
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02003096extern int __cond_resched_lock(spinlock_t *lock);
3097
3098#define cond_resched_lock(lock) ({ \
Peter Zijlstra34274452014-09-24 10:18:56 +02003099 ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02003100 __cond_resched_lock(lock); \
3101})
3102
3103extern int __cond_resched_softirq(void);
3104
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -07003105#define cond_resched_softirq() ({ \
Peter Zijlstra34274452014-09-24 10:18:56 +02003106 ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -07003107 __cond_resched_softirq(); \
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02003108})
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Simon Hormanf6f3c432013-05-22 14:50:31 +09003110static inline void cond_resched_rcu(void)
3111{
3112#if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
3113 rcu_read_unlock();
3114 cond_resched();
3115 rcu_read_lock();
3116#endif
3117}
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119/*
3120 * Does a critical section need to be broken due to another
Nick Piggin95c354f2008-01-30 13:31:20 +01003121 * task waiting?: (technically does not depend on CONFIG_PREEMPT,
3122 * but a general need for low latency)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 */
Nick Piggin95c354f2008-01-30 13:31:20 +01003124static inline int spin_needbreak(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
Nick Piggin95c354f2008-01-30 13:31:20 +01003126#ifdef CONFIG_PREEMPT
3127 return spin_is_contended(lock);
3128#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return 0;
Nick Piggin95c354f2008-01-30 13:31:20 +01003130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Peter Zijlstra75f93fe2013-09-27 17:30:03 +02003133static __always_inline bool need_resched(void)
3134{
3135 return unlikely(tif_need_resched());
3136}
3137
Thomas Gleixneree761f62013-03-21 22:49:32 +01003138/*
Frank Mayharf06febc2008-09-12 09:54:39 -07003139 * Thread group CPU time accounting.
3140 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01003141void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
Peter Zijlstra4da94d492009-02-11 11:30:27 +01003142void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
Frank Mayharf06febc2008-09-12 09:54:39 -07003143
Frank Mayharf06febc2008-09-12 09:54:39 -07003144/*
Roland McGrath7bb44ad2007-05-23 13:57:44 -07003145 * Reevaluate whether the task has signals pending delivery.
3146 * Wake the task if so.
3147 * This is required every time the blocked sigset_t changes.
3148 * callers must hold sighand->siglock.
3149 */
3150extern void recalc_sigpending_and_wake(struct task_struct *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151extern void recalc_sigpending(void);
3152
Oleg Nesterov910ffdb2013-01-21 20:47:41 +01003153extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
3154
3155static inline void signal_wake_up(struct task_struct *t, bool resume)
3156{
3157 signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
3158}
3159static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
3160{
3161 signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
3162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164/*
3165 * Wrappers for p->thread_info->cpu access. No-op on UP.
3166 */
3167#ifdef CONFIG_SMP
3168
3169static inline unsigned int task_cpu(const struct task_struct *p)
3170{
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07003171#ifdef CONFIG_THREAD_INFO_IN_TASK
3172 return p->cpu;
3173#else
Al Viroa1261f542005-11-13 16:06:55 -08003174 return task_thread_info(p)->cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07003175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
Ingo Molnarb32e86b2013-10-07 11:29:30 +01003178static inline int task_node(const struct task_struct *p)
3179{
3180 return cpu_to_node(task_cpu(p));
3181}
3182
Ingo Molnarc65cc872007-07-09 18:51:58 +02003183extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185#else
3186
3187static inline unsigned int task_cpu(const struct task_struct *p)
3188{
3189 return 0;
3190}
3191
3192static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
3193{
3194}
3195
3196#endif /* CONFIG_SMP */
3197
Pan Xinhuid9345c62016-11-02 05:08:28 -04003198/*
3199 * In order to reduce various lock holder preemption latencies provide an
3200 * interface to see if a vCPU is currently running or not.
3201 *
3202 * This allows us to terminate optimistic spin loops and block, analogous to
3203 * the native optimistic spin heuristic of testing if the lock owner task is
3204 * running or not.
3205 */
3206#ifndef vcpu_is_preempted
3207# define vcpu_is_preempted(cpu) false
3208#endif
3209
Rusty Russell96f874e22008-11-25 02:35:14 +10303210extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
3211extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003212
Dhaval Giani7c941432010-01-20 13:26:18 +01003213#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08003214extern struct task_group root_task_group;
Peter Zijlstra8323f262012-06-22 13:36:05 +02003215#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02003216
Dhaval Giani54e99122009-02-27 15:13:54 +05303217extern int task_can_switch_user(struct user_struct *up,
3218 struct task_struct *tsk);
3219
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08003220#ifdef CONFIG_TASK_XACCT
3221static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
3222{
Andrea Righi940389b2008-07-28 00:48:12 +02003223 tsk->ioac.rchar += amt;
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08003224}
3225
3226static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
3227{
Andrea Righi940389b2008-07-28 00:48:12 +02003228 tsk->ioac.wchar += amt;
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08003229}
3230
3231static inline void inc_syscr(struct task_struct *tsk)
3232{
Andrea Righi940389b2008-07-28 00:48:12 +02003233 tsk->ioac.syscr++;
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08003234}
3235
3236static inline void inc_syscw(struct task_struct *tsk)
3237{
Andrea Righi940389b2008-07-28 00:48:12 +02003238 tsk->ioac.syscw++;
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08003239}
3240#else
3241static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
3242{
3243}
3244
3245static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
3246{
3247}
3248
3249static inline void inc_syscr(struct task_struct *tsk)
3250{
3251}
3252
3253static inline void inc_syscw(struct task_struct *tsk)
3254{
3255}
3256#endif
3257
Dave Hansen82455252008-02-04 22:28:59 -08003258#ifndef TASK_SIZE_OF
3259#define TASK_SIZE_OF(tsk) TASK_SIZE
3260#endif
3261
Oleg Nesterovf98bafa2014-06-04 16:07:34 -07003262#ifdef CONFIG_MEMCG
Balbir Singhcf475ad22008-04-29 01:00:16 -07003263extern void mm_update_next_owner(struct mm_struct *mm);
Balbir Singhcf475ad22008-04-29 01:00:16 -07003264#else
3265static inline void mm_update_next_owner(struct mm_struct *mm)
3266{
3267}
Oleg Nesterovf98bafa2014-06-04 16:07:34 -07003268#endif /* CONFIG_MEMCG */
Balbir Singhcf475ad22008-04-29 01:00:16 -07003269
Jiri Slaby3e10e712009-11-19 17:16:37 +01003270static inline unsigned long task_rlimit(const struct task_struct *tsk,
3271 unsigned int limit)
3272{
Jason Low316c1608d2015-04-28 13:00:20 -07003273 return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
Jiri Slaby3e10e712009-11-19 17:16:37 +01003274}
3275
3276static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
3277 unsigned int limit)
3278{
Jason Low316c1608d2015-04-28 13:00:20 -07003279 return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
Jiri Slaby3e10e712009-11-19 17:16:37 +01003280}
3281
3282static inline unsigned long rlimit(unsigned int limit)
3283{
3284 return task_rlimit(current, limit);
3285}
3286
3287static inline unsigned long rlimit_max(unsigned int limit)
3288{
3289 return task_rlimit_max(current, limit);
3290}
3291
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003292#define SCHED_CPUFREQ_RT (1U << 0)
3293#define SCHED_CPUFREQ_DL (1U << 1)
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02003294#define SCHED_CPUFREQ_IOWAIT (1U << 2)
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003295
3296#define SCHED_CPUFREQ_RT_DL (SCHED_CPUFREQ_RT | SCHED_CPUFREQ_DL)
3297
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01003298#ifdef CONFIG_CPU_FREQ
3299struct update_util_data {
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003300 void (*func)(struct update_util_data *data, u64 time, unsigned int flags);
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01003301};
3302
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02003303void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003304 void (*func)(struct update_util_data *data, u64 time,
3305 unsigned int flags));
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02003306void cpufreq_remove_update_util_hook(int cpu);
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01003307#endif /* CONFIG_CPU_FREQ */
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309#endif