sched/headers: Move task_struct::signal and task_struct::sighand types and accessors...
[linux-block.git] / include / linux / sched.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_SCHED_H
2#define _LINUX_SCHED_H
3
607ca46e 4#include <uapi/linux/sched.h>
b7b3c76a 5
5c228079
DY
6#include <linux/sched/prio.h>
7
1da177e4
LT
8#include <asm/param.h> /* for HZ */
9
1da177e4
LT
10#include <linux/capability.h>
11#include <linux/threads.h>
12#include <linux/kernel.h>
13#include <linux/types.h>
14#include <linux/timex.h>
15#include <linux/jiffies.h>
b69339ba 16#include <linux/mutex.h>
fb00aca4 17#include <linux/plist.h>
1da177e4
LT
18#include <linux/rbtree.h>
19#include <linux/thread_info.h>
20#include <linux/cpumask.h>
21#include <linux/errno.h>
22#include <linux/nodemask.h>
c92ff1bd 23#include <linux/mm_types.h>
92cf2118 24#include <linux/preempt.h>
1da177e4 25
1da177e4
LT
26#include <asm/page.h>
27#include <asm/ptrace.h>
1da177e4
LT
28
29#include <linux/smp.h>
30#include <linux/sem.h>
ab602f79 31#include <linux/shm.h>
1da177e4 32#include <linux/signal.h>
1da177e4
LT
33#include <linux/compiler.h>
34#include <linux/completion.h>
f361bf4a 35#include <linux/signal_types.h>
1da177e4
LT
36#include <linux/pid.h>
37#include <linux/percpu.h>
38#include <linux/topology.h>
39#include <linux/seccomp.h>
e56d0903 40#include <linux/rcupdate.h>
05725f7e 41#include <linux/rculist.h>
23f78d4a 42#include <linux/rtmutex.h>
1da177e4 43
a3b6714e
DW
44#include <linux/time.h>
45#include <linux/param.h>
46#include <linux/resource.h>
47#include <linux/timer.h>
48#include <linux/hrtimer.h>
5c9a8750 49#include <linux/kcov.h>
7c3ab738 50#include <linux/task_io_accounting.h>
9745512c 51#include <linux/latencytop.h>
9e2b2dc4 52#include <linux/cred.h>
fa14ff4a 53#include <linux/llist.h>
7b44ab97 54#include <linux/uidgid.h>
21caf2fc 55#include <linux/gfp.h>
fd771233 56#include <linux/topology.h>
d4311ff1 57#include <linux/magic.h>
7d7efec3 58#include <linux/cgroup-defs.h>
a3b6714e
DW
59
60#include <asm/processor.h>
36d57ac4 61
e2d1e2ae
IM
62struct sched_attr;
63struct sched_param;
d50dde5a 64
c87e2837 65struct futex_pi_state;
286100a6 66struct robust_list_head;
bddd87c7 67struct bio_list;
5ad4e53b 68struct fs_struct;
cdd6c482 69struct perf_event_context;
73c10101 70struct blk_plug;
c4ad8f98 71struct filename;
89076bc3 72struct nameidata;
1da177e4 73
c3edc401
IM
74struct signal_struct;
75struct sighand_struct;
76
1da177e4
LT
77extern unsigned long total_forks;
78extern int nr_threads;
1da177e4
LT
79DECLARE_PER_CPU(unsigned long, process_counts);
80extern int nr_processes(void);
81extern unsigned long nr_running(void);
2ee507c4 82extern bool single_task_running(void);
1da177e4 83extern unsigned long nr_iowait(void);
8c215bd3 84extern unsigned long nr_iowait_cpu(int cpu);
372ba8cb 85extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
69d25870 86
3289bdb4 87#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
1f41906a
FW
88extern void cpu_load_update_nohz_start(void);
89extern void cpu_load_update_nohz_stop(void);
3289bdb4 90#else
1f41906a
FW
91static inline void cpu_load_update_nohz_start(void) { }
92static inline void cpu_load_update_nohz_stop(void) { }
3289bdb4 93#endif
1da177e4 94
b637a328
PM
95extern void dump_cpu_task(int cpu);
96
43ae34cb
IM
97struct seq_file;
98struct cfs_rq;
4cf86d77 99struct task_group;
43ae34cb
IM
100#ifdef CONFIG_SCHED_DEBUG
101extern void proc_sched_show_task(struct task_struct *p, struct seq_file *m);
102extern void proc_sched_set_task(struct task_struct *p);
43ae34cb 103#endif
1da177e4 104
4a8342d2
LT
105/*
106 * Task state bitmask. NOTE! These bits are also
107 * encoded in fs/proc/array.c: get_task_state().
108 *
109 * We have two separate sets of flags: task->state
110 * is about runnability, while task->exit_state are
111 * about the task exiting. Confusing, but this way
112 * modifying one set can't modify the other one by
113 * mistake.
114 */
1da177e4
LT
115#define TASK_RUNNING 0
116#define TASK_INTERRUPTIBLE 1
117#define TASK_UNINTERRUPTIBLE 2
f021a3c2
MW
118#define __TASK_STOPPED 4
119#define __TASK_TRACED 8
4a8342d2 120/* in tsk->exit_state */
ad86622b
ON
121#define EXIT_DEAD 16
122#define EXIT_ZOMBIE 32
abd50b39 123#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
4a8342d2 124/* in tsk->state again */
af927232 125#define TASK_DEAD 64
f021a3c2 126#define TASK_WAKEKILL 128
e9c84311 127#define TASK_WAKING 256
f2530dc7 128#define TASK_PARKED 512
80ed87c8 129#define TASK_NOLOAD 1024
7dc603c9
PZ
130#define TASK_NEW 2048
131#define TASK_STATE_MAX 4096
f021a3c2 132
7dc603c9 133#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
73342151 134
642fa448 135/* Convenience macros for the sake of set_current_state */
f021a3c2
MW
136#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
137#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
138#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
1da177e4 139
80ed87c8
PZ
140#define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
141
92a1f4bc
MW
142/* Convenience macros for the sake of wake_up */
143#define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
f021a3c2 144#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
92a1f4bc
MW
145
146/* get_task_state() */
147#define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
f021a3c2 148 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
74e37200 149 __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
92a1f4bc 150
f021a3c2
MW
151#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
152#define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
92a1f4bc 153#define task_is_stopped_or_traced(task) \
f021a3c2 154 ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
92a1f4bc 155#define task_contributes_to_load(task) \
e3c8ca83 156 ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
80ed87c8
PZ
157 (task->flags & PF_FROZEN) == 0 && \
158 (task->state & TASK_NOLOAD) == 0)
1da177e4 159
8eb23b9f
PZ
160#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
161
8eb23b9f
PZ
162#define __set_current_state(state_value) \
163 do { \
164 current->task_state_change = _THIS_IP_; \
165 current->state = (state_value); \
166 } while (0)
167#define set_current_state(state_value) \
168 do { \
169 current->task_state_change = _THIS_IP_; \
a2250238 170 smp_store_mb(current->state, (state_value)); \
8eb23b9f
PZ
171 } while (0)
172
173#else
498d0c57
AM
174/*
175 * set_current_state() includes a barrier so that the write of current->state
176 * is correctly serialised wrt the caller's subsequent test of whether to
177 * actually sleep:
178 *
a2250238 179 * for (;;) {
498d0c57 180 * set_current_state(TASK_UNINTERRUPTIBLE);
a2250238
PZ
181 * if (!need_sleep)
182 * break;
183 *
184 * schedule();
185 * }
186 * __set_current_state(TASK_RUNNING);
187 *
188 * If the caller does not need such serialisation (because, for instance, the
189 * condition test and condition change and wakeup are under the same lock) then
190 * use __set_current_state().
191 *
192 * The above is typically ordered against the wakeup, which does:
193 *
194 * need_sleep = false;
195 * wake_up_state(p, TASK_UNINTERRUPTIBLE);
196 *
197 * Where wake_up_state() (and all other wakeup primitives) imply enough
198 * barriers to order the store of the variable against wakeup.
199 *
200 * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
201 * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
202 * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
498d0c57 203 *
a2250238 204 * This is obviously fine, since they both store the exact same value.
498d0c57 205 *
a2250238 206 * Also see the comments of try_to_wake_up().
498d0c57 207 */
8eb23b9f 208#define __set_current_state(state_value) \
1da177e4 209 do { current->state = (state_value); } while (0)
8eb23b9f 210#define set_current_state(state_value) \
b92b8b35 211 smp_store_mb(current->state, (state_value))
1da177e4 212
8eb23b9f
PZ
213#endif
214
1da177e4
LT
215/* Task command name length */
216#define TASK_COMM_LEN 16
217
1da177e4
LT
218#include <linux/spinlock.h>
219
220/*
221 * This serializes "schedule()" and also protects
222 * the run-queue from deletions/modifications (but
223 * _adding_ to the beginning of the run-queue has
224 * a separate lock).
225 */
226extern rwlock_t tasklist_lock;
227extern spinlock_t mmlist_lock;
228
36c8b586 229struct task_struct;
1da177e4 230
db1466b3
PM
231#ifdef CONFIG_PROVE_RCU
232extern int lockdep_tasklist_lock_is_held(void);
233#endif /* #ifdef CONFIG_PROVE_RCU */
234
1da177e4
LT
235extern void sched_init(void);
236extern void sched_init_smp(void);
2d07b255 237extern asmlinkage void schedule_tail(struct task_struct *prev);
36c8b586 238extern void init_idle(struct task_struct *idle, int cpu);
1df21055 239extern void init_idle_bootup_task(struct task_struct *idle);
1da177e4 240
3fa0818b
RR
241extern cpumask_var_t cpu_isolated_map;
242
89f19f04 243extern int runqueue_is_locked(int cpu);
017730c1 244
3451d024 245#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
c1cc017c 246extern void nohz_balance_enter_idle(int cpu);
69e1e811 247extern void set_cpu_sd_state_idle(void);
bc7a34b8 248extern int get_nohz_timer_target(void);
46cb4b7c 249#else
c1cc017c 250static inline void nohz_balance_enter_idle(int cpu) { }
fdaabd80 251static inline void set_cpu_sd_state_idle(void) { }
46cb4b7c 252#endif
1da177e4 253
e59e2ae2 254/*
39bc89fd 255 * Only dump TASK_* tasks. (0 for all tasks)
e59e2ae2
IM
256 */
257extern void show_state_filter(unsigned long state_filter);
258
259static inline void show_state(void)
260{
39bc89fd 261 show_state_filter(0);
e59e2ae2
IM
262}
263
1da177e4
LT
264extern void show_regs(struct pt_regs *);
265
266/*
267 * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
268 * task), SP is the stack pointer of the first frame that should be shown in the back
269 * trace (or NULL if the entire call-chain of the task should be shown).
270 */
271extern void show_stack(struct task_struct *task, unsigned long *sp);
272
1da177e4
LT
273extern void cpu_init (void);
274extern void trap_init(void);
275extern void update_process_times(int user);
276extern void scheduler_tick(void);
9cf7243d 277extern int sched_cpu_starting(unsigned int cpu);
40190a78
TG
278extern int sched_cpu_activate(unsigned int cpu);
279extern int sched_cpu_deactivate(unsigned int cpu);
1da177e4 280
f2785ddb
TG
281#ifdef CONFIG_HOTPLUG_CPU
282extern int sched_cpu_dying(unsigned int cpu);
283#else
284# define sched_cpu_dying NULL
285#endif
1da177e4 286
82a1fcb9
IM
287extern void sched_show_task(struct task_struct *p);
288
19cc36c0 289#ifdef CONFIG_LOCKUP_DETECTOR
03e0d461 290extern void touch_softlockup_watchdog_sched(void);
8446f1d3 291extern void touch_softlockup_watchdog(void);
d6ad3e28 292extern void touch_softlockup_watchdog_sync(void);
04c9167f 293extern void touch_all_softlockup_watchdogs(void);
332fbdbc
DZ
294extern int proc_dowatchdog_thresh(struct ctl_table *table, int write,
295 void __user *buffer,
296 size_t *lenp, loff_t *ppos);
9c44bc03 297extern unsigned int softlockup_panic;
ac1f5912 298extern unsigned int hardlockup_panic;
004417a6 299void lockup_detector_init(void);
8446f1d3 300#else
03e0d461
TH
301static inline void touch_softlockup_watchdog_sched(void)
302{
303}
8446f1d3
IM
304static inline void touch_softlockup_watchdog(void)
305{
306}
d6ad3e28
JW
307static inline void touch_softlockup_watchdog_sync(void)
308{
309}
04c9167f
JF
310static inline void touch_all_softlockup_watchdogs(void)
311{
312}
004417a6
PZ
313static inline void lockup_detector_init(void)
314{
315}
8446f1d3
IM
316#endif
317
8b414521
MT
318#ifdef CONFIG_DETECT_HUNG_TASK
319void reset_hung_task_detector(void);
320#else
321static inline void reset_hung_task_detector(void)
322{
323}
324#endif
325
1da177e4
LT
326/* Attach to any functions which should be ignored in wchan output. */
327#define __sched __attribute__((__section__(".sched.text")))
deaf2227
IM
328
329/* Linker adds these: start and end of __sched functions */
330extern char __sched_text_start[], __sched_text_end[];
331
1da177e4
LT
332/* Is this address in the __sched functions? */
333extern int in_sched_functions(unsigned long addr);
334
335#define MAX_SCHEDULE_TIMEOUT LONG_MAX
b3c97528 336extern signed long schedule_timeout(signed long timeout);
64ed93a2 337extern signed long schedule_timeout_interruptible(signed long timeout);
294d5cc2 338extern signed long schedule_timeout_killable(signed long timeout);
64ed93a2 339extern signed long schedule_timeout_uninterruptible(signed long timeout);
69b27baf 340extern signed long schedule_timeout_idle(signed long timeout);
1da177e4 341asmlinkage void schedule(void);
c5491ea7 342extern void schedule_preempt_disabled(void);
1da177e4 343
10ab5643
TH
344extern int __must_check io_schedule_prepare(void);
345extern void io_schedule_finish(int token);
9cff8ade 346extern long io_schedule_timeout(long timeout);
10ab5643 347extern void io_schedule(void);
9cff8ade 348
9af6528e
PZ
349void __noreturn do_task_dead(void);
350
ab516013 351struct nsproxy;
acce292c 352struct user_namespace;
1da177e4 353
efc1a3b1
DH
354#ifdef CONFIG_MMU
355extern void arch_pick_mmap_layout(struct mm_struct *mm);
1da177e4
LT
356extern unsigned long
357arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
358 unsigned long, unsigned long);
359extern unsigned long
360arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
361 unsigned long len, unsigned long pgoff,
362 unsigned long flags);
efc1a3b1
DH
363#else
364static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
365#endif
1da177e4 366
0e464814 367struct pacct_struct {
f6ec29a4
KK
368 int ac_flag;
369 long ac_exitcode;
0e464814 370 unsigned long ac_mem;
d4bc42af 371 u64 ac_utime, ac_stime;
77787bfb 372 unsigned long ac_minflt, ac_majflt;
0e464814
KK
373};
374
42c4ab41 375struct cpu_itimer {
858cf3a8
FW
376 u64 expires;
377 u64 incr;
42c4ab41
SG
378};
379
d37f761d 380/**
9d7fb042 381 * struct prev_cputime - snaphsot of system and user cputime
d37f761d
FW
382 * @utime: time spent in user mode
383 * @stime: time spent in system mode
9d7fb042 384 * @lock: protects the above two fields
d37f761d 385 *
9d7fb042
PZ
386 * Stores previous user/system time values such that we can guarantee
387 * monotonicity.
d37f761d 388 */
9d7fb042
PZ
389struct prev_cputime {
390#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
5613fda9
FW
391 u64 utime;
392 u64 stime;
9d7fb042
PZ
393 raw_spinlock_t lock;
394#endif
d37f761d
FW
395};
396
9d7fb042
PZ
397static inline void prev_cputime_init(struct prev_cputime *prev)
398{
399#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
400 prev->utime = prev->stime = 0;
401 raw_spin_lock_init(&prev->lock);
402#endif
403}
404
f06febc9
FM
405/**
406 * struct task_cputime - collected CPU time counts
5613fda9
FW
407 * @utime: time spent in user mode, in nanoseconds
408 * @stime: time spent in kernel mode, in nanoseconds
f06febc9 409 * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
5ce73a4a 410 *
9d7fb042
PZ
411 * This structure groups together three kinds of CPU time that are tracked for
412 * threads and thread groups. Most things considering CPU time want to group
413 * these counts together and treat all three of them in parallel.
f06febc9
FM
414 */
415struct task_cputime {
5613fda9
FW
416 u64 utime;
417 u64 stime;
f06febc9
FM
418 unsigned long long sum_exec_runtime;
419};
9d7fb042 420
f06febc9 421/* Alternate field names when used to cache expirations. */
f06febc9 422#define virt_exp utime
9d7fb042 423#define prof_exp stime
f06febc9
FM
424#define sched_exp sum_exec_runtime
425
971e8a98
JL
426/*
427 * This is the atomic variant of task_cputime, which can be used for
428 * storing and updating task_cputime statistics without locking.
429 */
430struct task_cputime_atomic {
431 atomic64_t utime;
432 atomic64_t stime;
433 atomic64_t sum_exec_runtime;
434};
435
436#define INIT_CPUTIME_ATOMIC \
437 (struct task_cputime_atomic) { \
438 .utime = ATOMIC64_INIT(0), \
439 .stime = ATOMIC64_INIT(0), \
440 .sum_exec_runtime = ATOMIC64_INIT(0), \
441 }
442
609ca066 443#define PREEMPT_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
a233f112 444
c99e6efe 445/*
87dcbc06
PZ
446 * Disable preemption until the scheduler is running -- use an unconditional
447 * value so that it also works on !PREEMPT_COUNT kernels.
d86ee480 448 *
87dcbc06 449 * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
c99e6efe 450 */
87dcbc06 451#define INIT_PREEMPT_COUNT PREEMPT_OFFSET
a233f112 452
c99e6efe 453/*
609ca066
PZ
454 * Initial preempt_count value; reflects the preempt_count schedule invariant
455 * which states that during context switches:
d86ee480 456 *
609ca066
PZ
457 * preempt_count() == 2*PREEMPT_DISABLE_OFFSET
458 *
459 * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
460 * Note: See finish_task_switch().
c99e6efe 461 */
609ca066 462#define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
c99e6efe 463
f06febc9 464/**
4cd4c1b4 465 * struct thread_group_cputimer - thread group interval timer counts
920ce39f 466 * @cputime_atomic: atomic thread group interval timers.
d5c373eb
JL
467 * @running: true when there are timers running and
468 * @cputime_atomic receives updates.
c8d75aa4
JL
469 * @checking_timer: true when a thread in the group is in the
470 * process of checking for thread group timers.
f06febc9
FM
471 *
472 * This structure contains the version of task_cputime, above, that is
4cd4c1b4 473 * used for thread group CPU timer calculations.
f06febc9 474 */
4cd4c1b4 475struct thread_group_cputimer {
71107445 476 struct task_cputime_atomic cputime_atomic;
d5c373eb 477 bool running;
c8d75aa4 478 bool checking_timer;
f06febc9 479};
f06febc9 480
4714d1d3 481#include <linux/rwsem.h>
5091faa4
MG
482struct autogroup;
483
1da177e4
LT
484/*
485 * Some day this will be a full-fledged user tracking system..
486 */
487struct user_struct {
488 atomic_t __count; /* reference count */
489 atomic_t processes; /* How many processes does this user have? */
1da177e4 490 atomic_t sigpending; /* How many pending signals does this user have? */
4afeff85
EP
491#ifdef CONFIG_FANOTIFY
492 atomic_t fanotify_listeners;
493#endif
7ef9964e 494#ifdef CONFIG_EPOLL
52bd19f7 495 atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
7ef9964e 496#endif
970a8645 497#ifdef CONFIG_POSIX_MQUEUE
1da177e4
LT
498 /* protected by mq_lock */
499 unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
970a8645 500#endif
1da177e4 501 unsigned long locked_shm; /* How many pages of mlocked shm ? */
712f4aad 502 unsigned long unix_inflight; /* How many files in flight in unix sockets */
759c0114 503 atomic_long_t pipe_bufs; /* how many pages are allocated in pipe buffers */
1da177e4
LT
504
505#ifdef CONFIG_KEYS
506 struct key *uid_keyring; /* UID specific keyring */
507 struct key *session_keyring; /* UID's default session keyring */
508#endif
509
510 /* Hash table maintenance information */
735de223 511 struct hlist_node uidhash_node;
7b44ab97 512 kuid_t uid;
24e377a8 513
aaac3ba9 514#if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL)
789f90fc
PZ
515 atomic_long_t locked_vm;
516#endif
1da177e4
LT
517};
518
eb41d946 519extern int uids_sysfs_init(void);
5cb350ba 520
7b44ab97 521extern struct user_struct *find_user(kuid_t);
1da177e4
LT
522
523extern struct user_struct root_user;
524#define INIT_USER (&root_user)
525
b6dff3ec 526
1da177e4
LT
527struct backing_dev_info;
528struct reclaim_state;
529
f6db8347 530#ifdef CONFIG_SCHED_INFO
1da177e4
LT
531struct sched_info {
532 /* cumulative counters */
2d72376b 533 unsigned long pcount; /* # of times run on this cpu */
9c2c4802 534 unsigned long long run_delay; /* time spent waiting on a runqueue */
1da177e4
LT
535
536 /* timestamps */
172ba844
BS
537 unsigned long long last_arrival,/* when we last ran on a cpu */
538 last_queued; /* when we were last queued to run */
1da177e4 539};
f6db8347 540#endif /* CONFIG_SCHED_INFO */
1da177e4 541
47913d4e 542struct task_delay_info;
52f17b6c
CS
543
544static inline int sched_info_on(void)
545{
546#ifdef CONFIG_SCHEDSTATS
547 return 1;
548#elif defined(CONFIG_TASK_DELAY_ACCT)
549 extern int delayacct_on;
550 return delayacct_on;
551#else
552 return 0;
ca74e92b 553#endif
52f17b6c 554}
ca74e92b 555
cb251765
MG
556#ifdef CONFIG_SCHEDSTATS
557void force_schedstat_enabled(void);
558#endif
559
6ecdd749
YD
560/*
561 * Integer metrics need fixed point arithmetic, e.g., sched/fair
562 * has a few: load, load_avg, util_avg, freq, and capacity.
563 *
564 * We define a basic fixed point arithmetic range, and then formalize
565 * all these metrics based on that basic range.
566 */
567# define SCHED_FIXEDPOINT_SHIFT 10
568# define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
569
1da177e4 570struct io_context; /* See blkdev.h */
1da177e4 571
1da177e4 572
383f2835 573#ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
36c8b586 574extern void prefetch_stack(struct task_struct *t);
383f2835
CK
575#else
576static inline void prefetch_stack(struct task_struct *t) { }
577#endif
1da177e4
LT
578
579struct audit_context; /* See audit.c */
580struct mempolicy;
b92ce558 581struct pipe_inode_info;
4865ecf1 582struct uts_namespace;
1da177e4 583
20b8a59f 584struct load_weight {
9dbdb155
PZ
585 unsigned long weight;
586 u32 inv_weight;
20b8a59f
IM
587};
588
9d89c257 589/*
7b595334
YD
590 * The load_avg/util_avg accumulates an infinite geometric series
591 * (see __update_load_avg() in kernel/sched/fair.c).
592 *
593 * [load_avg definition]
594 *
595 * load_avg = runnable% * scale_load_down(load)
596 *
597 * where runnable% is the time ratio that a sched_entity is runnable.
598 * For cfs_rq, it is the aggregated load_avg of all runnable and
9d89c257 599 * blocked sched_entities.
7b595334
YD
600 *
601 * load_avg may also take frequency scaling into account:
602 *
603 * load_avg = runnable% * scale_load_down(load) * freq%
604 *
605 * where freq% is the CPU frequency normalized to the highest frequency.
606 *
607 * [util_avg definition]
608 *
609 * util_avg = running% * SCHED_CAPACITY_SCALE
610 *
611 * where running% is the time ratio that a sched_entity is running on
612 * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
613 * and blocked sched_entities.
614 *
615 * util_avg may also factor frequency scaling and CPU capacity scaling:
616 *
617 * util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
618 *
619 * where freq% is the same as above, and capacity% is the CPU capacity
620 * normalized to the greatest capacity (due to uarch differences, etc).
621 *
622 * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
623 * themselves are in the range of [0, 1]. To do fixed point arithmetics,
624 * we therefore scale them to as large a range as necessary. This is for
625 * example reflected by util_avg's SCHED_CAPACITY_SCALE.
626 *
627 * [Overflow issue]
628 *
629 * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
630 * with the highest load (=88761), always runnable on a single cfs_rq,
631 * and should not overflow as the number already hits PID_MAX_LIMIT.
632 *
633 * For all other cases (including 32-bit kernels), struct load_weight's
634 * weight will overflow first before we do, because:
635 *
636 * Max(load_avg) <= Max(load.weight)
637 *
638 * Then it is the load_weight's responsibility to consider overflow
639 * issues.
9d89c257 640 */
9d85f21c 641struct sched_avg {
9d89c257
YD
642 u64 last_update_time, load_sum;
643 u32 util_sum, period_contrib;
644 unsigned long load_avg, util_avg;
9d85f21c
PT
645};
646
94c18227 647#ifdef CONFIG_SCHEDSTATS
41acab88 648struct sched_statistics {
20b8a59f 649 u64 wait_start;
94c18227 650 u64 wait_max;
6d082592
AV
651 u64 wait_count;
652 u64 wait_sum;
8f0dfc34
AV
653 u64 iowait_count;
654 u64 iowait_sum;
94c18227 655
20b8a59f 656 u64 sleep_start;
20b8a59f 657 u64 sleep_max;
94c18227
IM
658 s64 sum_sleep_runtime;
659
660 u64 block_start;
20b8a59f
IM
661 u64 block_max;
662 u64 exec_max;
eba1ed4b 663 u64 slice_max;
cc367732 664
cc367732
IM
665 u64 nr_migrations_cold;
666 u64 nr_failed_migrations_affine;
667 u64 nr_failed_migrations_running;
668 u64 nr_failed_migrations_hot;
669 u64 nr_forced_migrations;
cc367732
IM
670
671 u64 nr_wakeups;
672 u64 nr_wakeups_sync;
673 u64 nr_wakeups_migrate;
674 u64 nr_wakeups_local;
675 u64 nr_wakeups_remote;
676 u64 nr_wakeups_affine;
677 u64 nr_wakeups_affine_attempts;
678 u64 nr_wakeups_passive;
679 u64 nr_wakeups_idle;
41acab88
LDM
680};
681#endif
682
683struct sched_entity {
684 struct load_weight load; /* for load-balancing */
685 struct rb_node run_node;
686 struct list_head group_node;
687 unsigned int on_rq;
688
689 u64 exec_start;
690 u64 sum_exec_runtime;
691 u64 vruntime;
692 u64 prev_sum_exec_runtime;
693
41acab88
LDM
694 u64 nr_migrations;
695
41acab88
LDM
696#ifdef CONFIG_SCHEDSTATS
697 struct sched_statistics statistics;
94c18227
IM
698#endif
699
20b8a59f 700#ifdef CONFIG_FAIR_GROUP_SCHED
fed14d45 701 int depth;
20b8a59f
IM
702 struct sched_entity *parent;
703 /* rq on which this entity is (to be) queued: */
704 struct cfs_rq *cfs_rq;
705 /* rq "owned" by this entity/group: */
706 struct cfs_rq *my_q;
707#endif
8bd75c77 708
141965c7 709#ifdef CONFIG_SMP
5a107804
JO
710 /*
711 * Per entity load average tracking.
712 *
713 * Put into separate cache line so it does not
714 * collide with read-mostly values above.
715 */
716 struct sched_avg avg ____cacheline_aligned_in_smp;
9d85f21c 717#endif
20b8a59f 718};
70b97a7f 719
fa717060
PZ
720struct sched_rt_entity {
721 struct list_head run_list;
78f2c7db 722 unsigned long timeout;
57d2aa00 723 unsigned long watchdog_stamp;
bee367ed 724 unsigned int time_slice;
ff77e468
PZ
725 unsigned short on_rq;
726 unsigned short on_list;
6f505b16 727
58d6c2d7 728 struct sched_rt_entity *back;
052f1dc7 729#ifdef CONFIG_RT_GROUP_SCHED
6f505b16
PZ
730 struct sched_rt_entity *parent;
731 /* rq on which this entity is (to be) queued: */
732 struct rt_rq *rt_rq;
733 /* rq "owned" by this entity/group: */
734 struct rt_rq *my_q;
735#endif
fa717060
PZ
736};
737
aab03e05
DF
738struct sched_dl_entity {
739 struct rb_node rb_node;
740
741 /*
742 * Original scheduling parameters. Copied here from sched_attr
4027d080 743 * during sched_setattr(), they will remain the same until
744 * the next sched_setattr().
aab03e05
DF
745 */
746 u64 dl_runtime; /* maximum runtime for each instance */
747 u64 dl_deadline; /* relative deadline of each instance */
755378a4 748 u64 dl_period; /* separation of two instances (period) */
332ac17e 749 u64 dl_bw; /* dl_runtime / dl_deadline */
aab03e05
DF
750
751 /*
752 * Actual scheduling parameters. Initialized with the values above,
753 * they are continously updated during task execution. Note that
754 * the remaining runtime could be < 0 in case we are in overrun.
755 */
756 s64 runtime; /* remaining runtime for this instance */
757 u64 deadline; /* absolute deadline for this instance */
758 unsigned int flags; /* specifying the scheduler behaviour */
759
760 /*
761 * Some bool flags:
762 *
763 * @dl_throttled tells if we exhausted the runtime. If so, the
764 * task has to wait for a replenishment to be performed at the
765 * next firing of dl_timer.
766 *
2d3d891d
DF
767 * @dl_boosted tells if we are boosted due to DI. If so we are
768 * outside bandwidth enforcement mechanism (but only until we
5bfd126e
JL
769 * exit the critical section);
770 *
771 * @dl_yielded tells if task gave up the cpu before consuming
772 * all its available runtime during the last job.
aab03e05 773 */
72f9f3fd 774 int dl_throttled, dl_boosted, dl_yielded;
aab03e05
DF
775
776 /*
777 * Bandwidth enforcement timer. Each -deadline task has its
778 * own bandwidth to be enforced, thus we need one timer per task.
779 */
780 struct hrtimer dl_timer;
781};
8bd75c77 782
1d082fd0
PM
783union rcu_special {
784 struct {
8203d6d0
PM
785 u8 blocked;
786 u8 need_qs;
787 u8 exp_need_qs;
788 u8 pad; /* Otherwise the compiler can store garbage here. */
789 } b; /* Bits. */
790 u32 s; /* Set of bits. */
1d082fd0 791};
86848966
PM
792struct rcu_node;
793
8dc85d54
PZ
794enum perf_event_task_context {
795 perf_invalid_context = -1,
796 perf_hw_context = 0,
89a1e187 797 perf_sw_context,
8dc85d54
PZ
798 perf_nr_task_contexts,
799};
800
eb61baf6
IM
801struct wake_q_node {
802 struct wake_q_node *next;
803};
804
72b252ae
MG
805/* Track pages that require TLB flushes */
806struct tlbflush_unmap_batch {
807 /*
808 * Each bit set is a CPU that potentially has a TLB entry for one of
809 * the PFNs being flushed. See set_tlb_ubc_flush_pending().
810 */
811 struct cpumask cpumask;
812
813 /* True if any bit in cpumask is set */
814 bool flush_required;
d950c947
MG
815
816 /*
817 * If true then the PTE was dirty when unmapped. The entry must be
818 * flushed before IO is initiated or a stale TLB entry potentially
819 * allows an update without redirtying the page.
820 */
821 bool writable;
72b252ae
MG
822};
823
1da177e4 824struct task_struct {
c65eacbe
AL
825#ifdef CONFIG_THREAD_INFO_IN_TASK
826 /*
827 * For reasons of header soup (see current_thread_info()), this
828 * must be the first element of task_struct.
829 */
830 struct thread_info thread_info;
831#endif
1da177e4 832 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
f7e4217b 833 void *stack;
1da177e4 834 atomic_t usage;
97dc32cd
WC
835 unsigned int flags; /* per process flags, defined below */
836 unsigned int ptrace;
1da177e4 837
2dd73a4f 838#ifdef CONFIG_SMP
fa14ff4a 839 struct llist_node wake_entry;
3ca7a440 840 int on_cpu;
c65eacbe
AL
841#ifdef CONFIG_THREAD_INFO_IN_TASK
842 unsigned int cpu; /* current CPU */
843#endif
63b0e9ed 844 unsigned int wakee_flips;
62470419 845 unsigned long wakee_flip_decay_ts;
63b0e9ed 846 struct task_struct *last_wakee;
ac66f547
PZ
847
848 int wake_cpu;
2dd73a4f 849#endif
fd2f4419 850 int on_rq;
50e645a8 851
b29739f9 852 int prio, static_prio, normal_prio;
c7aceaba 853 unsigned int rt_priority;
5522d5d5 854 const struct sched_class *sched_class;
20b8a59f 855 struct sched_entity se;
fa717060 856 struct sched_rt_entity rt;
8323f26c
PZ
857#ifdef CONFIG_CGROUP_SCHED
858 struct task_group *sched_task_group;
859#endif
aab03e05 860 struct sched_dl_entity dl;
1da177e4 861
e107be36
AK
862#ifdef CONFIG_PREEMPT_NOTIFIERS
863 /* list of struct preempt_notifier: */
864 struct hlist_head preempt_notifiers;
865#endif
866
6c5c9341 867#ifdef CONFIG_BLK_DEV_IO_TRACE
2056a782 868 unsigned int btrace_seq;
6c5c9341 869#endif
1da177e4 870
97dc32cd 871 unsigned int policy;
29baa747 872 int nr_cpus_allowed;
1da177e4 873 cpumask_t cpus_allowed;
1da177e4 874
a57eb940 875#ifdef CONFIG_PREEMPT_RCU
e260be67 876 int rcu_read_lock_nesting;
1d082fd0 877 union rcu_special rcu_read_unlock_special;
f41d911f 878 struct list_head rcu_node_entry;
a57eb940 879 struct rcu_node *rcu_blocked_node;
28f6569a 880#endif /* #ifdef CONFIG_PREEMPT_RCU */
8315f422
PM
881#ifdef CONFIG_TASKS_RCU
882 unsigned long rcu_tasks_nvcsw;
883 bool rcu_tasks_holdout;
884 struct list_head rcu_tasks_holdout_list;
176f8f7a 885 int rcu_tasks_idle_cpu;
8315f422 886#endif /* #ifdef CONFIG_TASKS_RCU */
e260be67 887
f6db8347 888#ifdef CONFIG_SCHED_INFO
1da177e4
LT
889 struct sched_info sched_info;
890#endif
891
892 struct list_head tasks;
806c09a7 893#ifdef CONFIG_SMP
917b627d 894 struct plist_node pushable_tasks;
1baca4ce 895 struct rb_node pushable_dl_tasks;
806c09a7 896#endif
1da177e4
LT
897
898 struct mm_struct *mm, *active_mm;
314ff785
IM
899
900 /* Per-thread vma caching: */
901 struct vmacache vmacache;
902
34e55232
KH
903#if defined(SPLIT_RSS_COUNTING)
904 struct task_rss_stat rss_stat;
905#endif
1da177e4 906/* task state */
97dc32cd 907 int exit_state;
1da177e4
LT
908 int exit_code, exit_signal;
909 int pdeath_signal; /* The signal sent when the parent dies */
e7cc4173 910 unsigned long jobctl; /* JOBCTL_*, siglock protected */
9b89f6ba
AE
911
912 /* Used for emulating ABI behavior of previous Linux versions */
97dc32cd 913 unsigned int personality;
9b89f6ba 914
be958bdc 915 /* scheduler bits, serialized by scheduler locks */
ca94c442 916 unsigned sched_reset_on_fork:1;
a8e4f2ea 917 unsigned sched_contributes_to_load:1;
ff303e66 918 unsigned sched_migrated:1;
b7e7ade3 919 unsigned sched_remote_wakeup:1;
be958bdc
PZ
920 unsigned :0; /* force alignment to the next boundary */
921
922 /* unserialized, strictly 'current' */
923 unsigned in_execve:1; /* bit to tell LSMs we're in execve */
924 unsigned in_iowait:1;
7e781418
AL
925#if !defined(TIF_RESTORE_SIGMASK)
926 unsigned restore_sigmask:1;
927#endif
626ebc41
TH
928#ifdef CONFIG_MEMCG
929 unsigned memcg_may_oom:1;
127424c8 930#ifndef CONFIG_SLOB
6f185c29
VD
931 unsigned memcg_kmem_skip_account:1;
932#endif
127424c8 933#endif
ff303e66
PZ
934#ifdef CONFIG_COMPAT_BRK
935 unsigned brk_randomized:1;
936#endif
6f185c29 937
1d4457f9
KC
938 unsigned long atomic_flags; /* Flags needing atomic access. */
939
f56141e3
AL
940 struct restart_block restart_block;
941
1da177e4
LT
942 pid_t pid;
943 pid_t tgid;
0a425405 944
1314562a 945#ifdef CONFIG_CC_STACKPROTECTOR
0a425405
AV
946 /* Canary value for the -fstack-protector gcc feature */
947 unsigned long stack_canary;
1314562a 948#endif
4d1d61a6 949 /*
1da177e4 950 * pointers to (original) parent process, youngest child, younger sibling,
4d1d61a6 951 * older sibling, respectively. (p->father can be replaced with
f470021a 952 * p->real_parent->pid)
1da177e4 953 */
abd63bc3
KC
954 struct task_struct __rcu *real_parent; /* real parent process */
955 struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
1da177e4 956 /*
f470021a 957 * children/sibling forms the list of my natural children
1da177e4
LT
958 */
959 struct list_head children; /* list of my children */
960 struct list_head sibling; /* linkage in my parent's children list */
961 struct task_struct *group_leader; /* threadgroup leader */
962
f470021a
RM
963 /*
964 * ptraced is the list of tasks this task is using ptrace on.
965 * This includes both natural children and PTRACE_ATTACH targets.
966 * p->ptrace_entry is p's link on the p->parent->ptraced list.
967 */
968 struct list_head ptraced;
969 struct list_head ptrace_entry;
970
1da177e4 971 /* PID/PID hash table linkage. */
92476d7f 972 struct pid_link pids[PIDTYPE_MAX];
47e65328 973 struct list_head thread_group;
0c740d0a 974 struct list_head thread_node;
1da177e4
LT
975
976 struct completion *vfork_done; /* for vfork() */
977 int __user *set_child_tid; /* CLONE_CHILD_SETTID */
978 int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
979
5613fda9 980 u64 utime, stime;
40565b5a 981#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
5613fda9 982 u64 utimescaled, stimescaled;
40565b5a 983#endif
16a6d9be 984 u64 gtime;
9d7fb042 985 struct prev_cputime prev_cputime;
6a61671b 986#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
b7ce2277 987 seqcount_t vtime_seqcount;
6a61671b
FW
988 unsigned long long vtime_snap;
989 enum {
7098c1ea
FW
990 /* Task is sleeping or running in a CPU with VTIME inactive */
991 VTIME_INACTIVE = 0,
992 /* Task runs in userspace in a CPU with VTIME active */
6a61671b 993 VTIME_USER,
7098c1ea 994 /* Task runs in kernelspace in a CPU with VTIME active */
6a61671b
FW
995 VTIME_SYS,
996 } vtime_snap_whence;
d99ca3b9 997#endif
d027d45d
FW
998
999#ifdef CONFIG_NO_HZ_FULL
f009a7a7 1000 atomic_t tick_dep_mask;
d027d45d 1001#endif
1da177e4 1002 unsigned long nvcsw, nivcsw; /* context switch counts */
ccbf62d8 1003 u64 start_time; /* monotonic time in nsec */
57e0be04 1004 u64 real_start_time; /* boot based time in nsec */
1da177e4
LT
1005/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
1006 unsigned long min_flt, maj_flt;
1007
b18b6a9c 1008#ifdef CONFIG_POSIX_TIMERS
f06febc9 1009 struct task_cputime cputime_expires;
1da177e4 1010 struct list_head cpu_timers[3];
b18b6a9c 1011#endif
1da177e4
LT
1012
1013/* process credentials */
64b875f7 1014 const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
1b0ba1c9 1015 const struct cred __rcu *real_cred; /* objective and real subjective task
3b11a1de 1016 * credentials (COW) */
1b0ba1c9 1017 const struct cred __rcu *cred; /* effective (overridable) subjective task
3b11a1de 1018 * credentials (COW) */
36772092
PBG
1019 char comm[TASK_COMM_LEN]; /* executable name excluding path
1020 - access with [gs]et_task_comm (which lock
1021 it with task_lock())
221af7f8 1022 - initialized normally by setup_new_exec */
1da177e4 1023/* file system info */
756daf26 1024 struct nameidata *nameidata;
3d5b6fcc 1025#ifdef CONFIG_SYSVIPC
1da177e4
LT
1026/* ipc stuff */
1027 struct sysv_sem sysvsem;
ab602f79 1028 struct sysv_shm sysvshm;
3d5b6fcc 1029#endif
e162b39a 1030#ifdef CONFIG_DETECT_HUNG_TASK
82a1fcb9 1031/* hung task detection */
82a1fcb9
IM
1032 unsigned long last_switch_count;
1033#endif
1da177e4
LT
1034/* filesystem information */
1035 struct fs_struct *fs;
1036/* open file information */
1037 struct files_struct *files;
1651e14e 1038/* namespaces */
ab516013 1039 struct nsproxy *nsproxy;
1da177e4
LT
1040/* signal handlers */
1041 struct signal_struct *signal;
1042 struct sighand_struct *sighand;
1043
1044 sigset_t blocked, real_blocked;
f3de272b 1045 sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
1da177e4
LT
1046 struct sigpending pending;
1047
1048 unsigned long sas_ss_sp;
1049 size_t sas_ss_size;
2a742138 1050 unsigned sas_ss_flags;
2e01fabe 1051
67d12145 1052 struct callback_head *task_works;
e73f8959 1053
1da177e4 1054 struct audit_context *audit_context;
bfef93a5 1055#ifdef CONFIG_AUDITSYSCALL
e1760bd5 1056 kuid_t loginuid;
4746ec5b 1057 unsigned int sessionid;
bfef93a5 1058#endif
932ecebb 1059 struct seccomp seccomp;
1da177e4
LT
1060
1061/* Thread group tracking */
1062 u32 parent_exec_id;
1063 u32 self_exec_id;
58568d2a
MX
1064/* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
1065 * mempolicy */
1da177e4 1066 spinlock_t alloc_lock;
1da177e4 1067
b29739f9 1068 /* Protection of the PI data structures: */
1d615482 1069 raw_spinlock_t pi_lock;
b29739f9 1070
76751049
PZ
1071 struct wake_q_node wake_q;
1072
23f78d4a
IM
1073#ifdef CONFIG_RT_MUTEXES
1074 /* PI waiters blocked on a rt_mutex held by this task */
fb00aca4
PZ
1075 struct rb_root pi_waiters;
1076 struct rb_node *pi_waiters_leftmost;
23f78d4a
IM
1077 /* Deadlock detection and priority inheritance handling */
1078 struct rt_mutex_waiter *pi_blocked_on;
23f78d4a
IM
1079#endif
1080
408894ee
IM
1081#ifdef CONFIG_DEBUG_MUTEXES
1082 /* mutex deadlock detection */
1083 struct mutex_waiter *blocked_on;
1084#endif
de30a2b3
IM
1085#ifdef CONFIG_TRACE_IRQFLAGS
1086 unsigned int irq_events;
de30a2b3 1087 unsigned long hardirq_enable_ip;
de30a2b3 1088 unsigned long hardirq_disable_ip;
fa1452e8 1089 unsigned int hardirq_enable_event;
de30a2b3 1090 unsigned int hardirq_disable_event;
fa1452e8
HS
1091 int hardirqs_enabled;
1092 int hardirq_context;
de30a2b3 1093 unsigned long softirq_disable_ip;
de30a2b3 1094 unsigned long softirq_enable_ip;
fa1452e8 1095 unsigned int softirq_disable_event;
de30a2b3 1096 unsigned int softirq_enable_event;
fa1452e8 1097 int softirqs_enabled;
de30a2b3
IM
1098 int softirq_context;
1099#endif
fbb9ce95 1100#ifdef CONFIG_LOCKDEP
bdb9441e 1101# define MAX_LOCK_DEPTH 48UL
fbb9ce95
IM
1102 u64 curr_chain_key;
1103 int lockdep_depth;
fbb9ce95 1104 unsigned int lockdep_recursion;
c7aceaba 1105 struct held_lock held_locks[MAX_LOCK_DEPTH];
cf40bd16 1106 gfp_t lockdep_reclaim_gfp;
fbb9ce95 1107#endif
c6d30853
AR
1108#ifdef CONFIG_UBSAN
1109 unsigned int in_ubsan;
1110#endif
408894ee 1111
1da177e4
LT
1112/* journalling filesystem info */
1113 void *journal_info;
1114
d89d8796 1115/* stacked block device info */
bddd87c7 1116 struct bio_list *bio_list;
d89d8796 1117
73c10101
JA
1118#ifdef CONFIG_BLOCK
1119/* stack plugging */
1120 struct blk_plug *plug;
1121#endif
1122
1da177e4
LT
1123/* VM state */
1124 struct reclaim_state *reclaim_state;
1125
1da177e4
LT
1126 struct backing_dev_info *backing_dev_info;
1127
1128 struct io_context *io_context;
1129
1130 unsigned long ptrace_message;
1131 siginfo_t *last_siginfo; /* For ptrace use. */
7c3ab738 1132 struct task_io_accounting ioac;
8f0ab514 1133#if defined(CONFIG_TASK_XACCT)
1da177e4
LT
1134 u64 acct_rss_mem1; /* accumulated rss usage */
1135 u64 acct_vm_mem1; /* accumulated virtual memory usage */
605dc2b3 1136 u64 acct_timexpd; /* stime + utime since last update */
1da177e4
LT
1137#endif
1138#ifdef CONFIG_CPUSETS
58568d2a 1139 nodemask_t mems_allowed; /* Protected by alloc_lock */
cc9a6c87 1140 seqcount_t mems_allowed_seq; /* Seqence no to catch updates */
825a46af 1141 int cpuset_mem_spread_rotor;
6adef3eb 1142 int cpuset_slab_spread_rotor;
1da177e4 1143#endif
ddbcc7e8 1144#ifdef CONFIG_CGROUPS
817929ec 1145 /* Control Group info protected by css_set_lock */
2c392b8c 1146 struct css_set __rcu *cgroups;
817929ec
PM
1147 /* cg_list protected by css_set_lock and tsk->alloc_lock */
1148 struct list_head cg_list;
ddbcc7e8 1149#endif
e02737d5
FY
1150#ifdef CONFIG_INTEL_RDT_A
1151 int closid;
1152#endif
42b2dd0a 1153#ifdef CONFIG_FUTEX
0771dfef 1154 struct robust_list_head __user *robust_list;
34f192c6
IM
1155#ifdef CONFIG_COMPAT
1156 struct compat_robust_list_head __user *compat_robust_list;
1157#endif
c87e2837
IM
1158 struct list_head pi_state_list;
1159 struct futex_pi_state *pi_state_cache;
c7aceaba 1160#endif
cdd6c482 1161#ifdef CONFIG_PERF_EVENTS
8dc85d54 1162 struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
cdd6c482
IM
1163 struct mutex perf_event_mutex;
1164 struct list_head perf_event_list;
a63eaf34 1165#endif
8f47b187
TG
1166#ifdef CONFIG_DEBUG_PREEMPT
1167 unsigned long preempt_disable_ip;
1168#endif
c7aceaba 1169#ifdef CONFIG_NUMA
58568d2a 1170 struct mempolicy *mempolicy; /* Protected by alloc_lock */
c7aceaba 1171 short il_next;
207205a2 1172 short pref_node_fork;
42b2dd0a 1173#endif
cbee9f88
PZ
1174#ifdef CONFIG_NUMA_BALANCING
1175 int numa_scan_seq;
cbee9f88 1176 unsigned int numa_scan_period;
598f0ec0 1177 unsigned int numa_scan_period_max;
de1c9ce6 1178 int numa_preferred_nid;
6b9a7460 1179 unsigned long numa_migrate_retry;
cbee9f88 1180 u64 node_stamp; /* migration stamp */
7e2703e6
RR
1181 u64 last_task_numa_placement;
1182 u64 last_sum_exec_runtime;
cbee9f88 1183 struct callback_head numa_work;
f809ca9a 1184
8c8a743c
PZ
1185 struct list_head numa_entry;
1186 struct numa_group *numa_group;
1187
745d6147 1188 /*
44dba3d5
IM
1189 * numa_faults is an array split into four regions:
1190 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1191 * in this precise order.
1192 *
1193 * faults_memory: Exponential decaying average of faults on a per-node
1194 * basis. Scheduling placement decisions are made based on these
1195 * counts. The values remain static for the duration of a PTE scan.
1196 * faults_cpu: Track the nodes the process was running on when a NUMA
1197 * hinting fault was incurred.
1198 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1199 * during the current scan window. When the scan completes, the counts
1200 * in faults_memory and faults_cpu decay and these values are copied.
745d6147 1201 */
44dba3d5 1202 unsigned long *numa_faults;
83e1d2cd 1203 unsigned long total_numa_faults;
745d6147 1204
04bb2f94
RR
1205 /*
1206 * numa_faults_locality tracks if faults recorded during the last
074c2381
MG
1207 * scan window were remote/local or failed to migrate. The task scan
1208 * period is adapted based on the locality of the faults with different
1209 * weights depending on whether they were shared or private faults
04bb2f94 1210 */
074c2381 1211 unsigned long numa_faults_locality[3];
04bb2f94 1212
b32e86b4 1213 unsigned long numa_pages_migrated;
cbee9f88
PZ
1214#endif /* CONFIG_NUMA_BALANCING */
1215
72b252ae
MG
1216#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1217 struct tlbflush_unmap_batch tlb_ubc;
1218#endif
1219
e56d0903 1220 struct rcu_head rcu;
b92ce558
JA
1221
1222 /*
1223 * cache last used pipe for splice
1224 */
1225 struct pipe_inode_info *splice_pipe;
5640f768
ED
1226
1227 struct page_frag task_frag;
1228
47913d4e
IM
1229#ifdef CONFIG_TASK_DELAY_ACCT
1230 struct task_delay_info *delays;
f4f154fd 1231#endif
47913d4e 1232
f4f154fd
AM
1233#ifdef CONFIG_FAULT_INJECTION
1234 int make_it_fail;
ca74e92b 1235#endif
9d823e8f
WF
1236 /*
1237 * when (nr_dirtied >= nr_dirtied_pause), it's time to call
1238 * balance_dirty_pages() for some dirty throttling pause
1239 */
1240 int nr_dirtied;
1241 int nr_dirtied_pause;
83712358 1242 unsigned long dirty_paused_when; /* start of a write-and-pause period */
9d823e8f 1243
9745512c
AV
1244#ifdef CONFIG_LATENCYTOP
1245 int latency_record_count;
1246 struct latency_record latency_record[LT_SAVECOUNT];
1247#endif
6976675d
AV
1248 /*
1249 * time slack values; these are used to round up poll() and
1250 * select() etc timeout values. These are in nanoseconds.
1251 */
da8b44d5
JS
1252 u64 timer_slack_ns;
1253 u64 default_timer_slack_ns;
f8d570a4 1254
0b24becc
AR
1255#ifdef CONFIG_KASAN
1256 unsigned int kasan_depth;
1257#endif
fb52607a 1258#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3ad2f3fb 1259 /* Index of current stored address in ret_stack */
f201ae23
FW
1260 int curr_ret_stack;
1261 /* Stack of return addresses for return function tracing */
1262 struct ftrace_ret_stack *ret_stack;
8aef2d28
SR
1263 /* time stamp for last schedule */
1264 unsigned long long ftrace_timestamp;
f201ae23
FW
1265 /*
1266 * Number of functions that haven't been traced
1267 * because of depth overrun.
1268 */
1269 atomic_t trace_overrun;
380c4b14
FW
1270 /* Pause for the tracing */
1271 atomic_t tracing_graph_pause;
f201ae23 1272#endif
ea4e2bc4
SR
1273#ifdef CONFIG_TRACING
1274 /* state flags for use by tracers */
1275 unsigned long trace;
b1cff0ad 1276 /* bitmask and counter of trace recursion */
261842b7
SR
1277 unsigned long trace_recursion;
1278#endif /* CONFIG_TRACING */
5c9a8750
DV
1279#ifdef CONFIG_KCOV
1280 /* Coverage collection mode enabled for this task (0 if disabled). */
1281 enum kcov_mode kcov_mode;
1282 /* Size of the kcov_area. */
1283 unsigned kcov_size;
1284 /* Buffer for coverage collection. */
1285 void *kcov_area;
1286 /* kcov desciptor wired with this task or NULL. */
1287 struct kcov *kcov;
1288#endif
6f185c29 1289#ifdef CONFIG_MEMCG
626ebc41
TH
1290 struct mem_cgroup *memcg_in_oom;
1291 gfp_t memcg_oom_gfp_mask;
1292 int memcg_oom_order;
b23afb93
TH
1293
1294 /* number of pages to reclaim on returning to userland */
1295 unsigned int memcg_nr_pages_over_high;
569b846d 1296#endif
0326f5a9
SD
1297#ifdef CONFIG_UPROBES
1298 struct uprobe_task *utask;
0326f5a9 1299#endif
cafe5635
KO
1300#if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1301 unsigned int sequential_io;
1302 unsigned int sequential_io_avg;
1303#endif
8eb23b9f
PZ
1304#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1305 unsigned long task_state_change;
1306#endif
8bcbde54 1307 int pagefault_disabled;
03049269 1308#ifdef CONFIG_MMU
29c696e1 1309 struct task_struct *oom_reaper_list;
03049269 1310#endif
ba14a194
AL
1311#ifdef CONFIG_VMAP_STACK
1312 struct vm_struct *stack_vm_area;
1313#endif
68f24b08
AL
1314#ifdef CONFIG_THREAD_INFO_IN_TASK
1315 /* A live task holds one reference. */
1316 atomic_t stack_refcount;
1317#endif
0c8c0f03
DH
1318/* CPU-specific state of this task */
1319 struct thread_struct thread;
1320/*
1321 * WARNING: on x86, 'thread_struct' contains a variable-sized
1322 * structure. It *MUST* be at the end of 'task_struct'.
1323 *
1324 * Do not put anything below here!
1325 */
1da177e4
LT
1326};
1327
5aaeb5c0
IM
1328#ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
1329extern int arch_task_struct_size __read_mostly;
1330#else
1331# define arch_task_struct_size (sizeof(struct task_struct))
1332#endif
0c8c0f03 1333
ba14a194
AL
1334#ifdef CONFIG_VMAP_STACK
1335static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1336{
1337 return t->stack_vm_area;
1338}
1339#else
1340static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1341{
1342 return NULL;
1343}
1344#endif
1345
6688cc05
PZ
1346#define TNF_MIGRATED 0x01
1347#define TNF_NO_GROUP 0x02
dabe1d99 1348#define TNF_SHARED 0x04
04bb2f94 1349#define TNF_FAULT_LOCAL 0x08
074c2381 1350#define TNF_MIGRATE_FAIL 0x10
6688cc05 1351
b18dc5f2
MH
1352static inline bool in_vfork(struct task_struct *tsk)
1353{
1354 bool ret;
1355
1356 /*
1357 * need RCU to access ->real_parent if CLONE_VM was used along with
1358 * CLONE_PARENT.
1359 *
1360 * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
1361 * imply CLONE_VM
1362 *
1363 * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
1364 * ->real_parent is not necessarily the task doing vfork(), so in
1365 * theory we can't rely on task_lock() if we want to dereference it.
1366 *
1367 * And in this case we can't trust the real_parent->mm == tsk->mm
1368 * check, it can be false negative. But we do not care, if init or
1369 * another oom-unkillable task does this it should blame itself.
1370 */
1371 rcu_read_lock();
1372 ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
1373 rcu_read_unlock();
1374
1375 return ret;
1376}
1377
cbee9f88 1378#ifdef CONFIG_NUMA_BALANCING
6688cc05 1379extern void task_numa_fault(int last_node, int node, int pages, int flags);
e29cf08b 1380extern pid_t task_numa_group_id(struct task_struct *p);
1a687c2e 1381extern void set_numabalancing_state(bool enabled);
82727018 1382extern void task_numa_free(struct task_struct *p);
10f39042
RR
1383extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
1384 int src_nid, int dst_cpu);
cbee9f88 1385#else
ac8e895b 1386static inline void task_numa_fault(int last_node, int node, int pages,
6688cc05 1387 int flags)
cbee9f88
PZ
1388{
1389}
e29cf08b
MG
1390static inline pid_t task_numa_group_id(struct task_struct *p)
1391{
1392 return 0;
1393}
1a687c2e
MG
1394static inline void set_numabalancing_state(bool enabled)
1395{
1396}
82727018
RR
1397static inline void task_numa_free(struct task_struct *p)
1398{
1399}
10f39042
RR
1400static inline bool should_numa_migrate_memory(struct task_struct *p,
1401 struct page *page, int src_nid, int dst_cpu)
1402{
1403 return true;
1404}
cbee9f88
PZ
1405#endif
1406
e868171a 1407static inline struct pid *task_pid(struct task_struct *task)
22c935f4
EB
1408{
1409 return task->pids[PIDTYPE_PID].pid;
1410}
1411
e868171a 1412static inline struct pid *task_tgid(struct task_struct *task)
22c935f4
EB
1413{
1414 return task->group_leader->pids[PIDTYPE_PID].pid;
1415}
1416
6dda81f4
ON
1417/*
1418 * Without tasklist or rcu lock it is not safe to dereference
1419 * the result of task_pgrp/task_session even if task == current,
1420 * we can race with another thread doing sys_setsid/sys_setpgid.
1421 */
e868171a 1422static inline struct pid *task_pgrp(struct task_struct *task)
22c935f4
EB
1423{
1424 return task->group_leader->pids[PIDTYPE_PGID].pid;
1425}
1426
e868171a 1427static inline struct pid *task_session(struct task_struct *task)
22c935f4
EB
1428{
1429 return task->group_leader->pids[PIDTYPE_SID].pid;
1430}
1431
7af57294
PE
1432struct pid_namespace;
1433
1434/*
1435 * the helpers to get the task's different pids as they are seen
1436 * from various namespaces
1437 *
1438 * task_xid_nr() : global id, i.e. the id seen from the init namespace;
44c4e1b2
EB
1439 * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
1440 * current.
7af57294
PE
1441 * task_xid_nr_ns() : id seen from the ns specified;
1442 *
1443 * set_task_vxid() : assigns a virtual id to a task;
1444 *
7af57294
PE
1445 * see also pid_nr() etc in include/linux/pid.h
1446 */
52ee2dfd
ON
1447pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
1448 struct pid_namespace *ns);
7af57294 1449
e868171a 1450static inline pid_t task_pid_nr(struct task_struct *tsk)
7af57294
PE
1451{
1452 return tsk->pid;
1453}
1454
52ee2dfd
ON
1455static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
1456 struct pid_namespace *ns)
1457{
1458 return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1459}
7af57294
PE
1460
1461static inline pid_t task_pid_vnr(struct task_struct *tsk)
1462{
52ee2dfd 1463 return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
7af57294
PE
1464}
1465
1466
e868171a 1467static inline pid_t task_tgid_nr(struct task_struct *tsk)
7af57294
PE
1468{
1469 return tsk->tgid;
1470}
1471
2f2a3a46 1472pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
7af57294
PE
1473
1474static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1475{
1476 return pid_vnr(task_tgid(tsk));
1477}
1478
1479
80e0b6e8 1480static inline int pid_alive(const struct task_struct *p);
ad36d282
RGB
1481static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1482{
1483 pid_t pid = 0;
1484
1485 rcu_read_lock();
1486 if (pid_alive(tsk))
1487 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1488 rcu_read_unlock();
1489
1490 return pid;
1491}
1492
1493static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1494{
1495 return task_ppid_nr_ns(tsk, &init_pid_ns);
1496}
1497
52ee2dfd
ON
1498static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
1499 struct pid_namespace *ns)
7af57294 1500{
52ee2dfd 1501 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
7af57294
PE
1502}
1503
7af57294
PE
1504static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
1505{
52ee2dfd 1506 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
7af57294
PE
1507}
1508
1509
52ee2dfd
ON
1510static inline pid_t task_session_nr_ns(struct task_struct *tsk,
1511 struct pid_namespace *ns)
7af57294 1512{
52ee2dfd 1513 return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
7af57294
PE
1514}
1515
7af57294
PE
1516static inline pid_t task_session_vnr(struct task_struct *tsk)
1517{
52ee2dfd 1518 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
7af57294
PE
1519}
1520
1b0f7ffd
ON
1521/* obsolete, do not use */
1522static inline pid_t task_pgrp_nr(struct task_struct *tsk)
1523{
1524 return task_pgrp_nr_ns(tsk, &init_pid_ns);
1525}
7af57294 1526
1da177e4
LT
1527/**
1528 * pid_alive - check that a task structure is not stale
1529 * @p: Task structure to be checked.
1530 *
1531 * Test if a process is not yet dead (at most zombie state)
1532 * If pid_alive fails, then pointers within the task structure
1533 * can be stale and must not be dereferenced.
e69f6186
YB
1534 *
1535 * Return: 1 if the process is alive. 0 otherwise.
1da177e4 1536 */
ad36d282 1537static inline int pid_alive(const struct task_struct *p)
1da177e4 1538{
92476d7f 1539 return p->pids[PIDTYPE_PID].pid != NULL;
1da177e4
LT
1540}
1541
f400e198 1542/**
570f5241
SS
1543 * is_global_init - check if a task structure is init. Since init
1544 * is free to have sub-threads we need to check tgid.
3260259f
H
1545 * @tsk: Task structure to be checked.
1546 *
1547 * Check if a task structure is the first user space task the kernel created.
e69f6186
YB
1548 *
1549 * Return: 1 if the task structure is init. 0 otherwise.
b460cbc5 1550 */
e868171a 1551static inline int is_global_init(struct task_struct *tsk)
b461cc03 1552{
570f5241 1553 return task_tgid_nr(tsk) == 1;
b461cc03 1554}
b460cbc5 1555
9ec52099
CLG
1556extern struct pid *cad_pid;
1557
1da177e4 1558extern void free_task(struct task_struct *tsk);
1da177e4 1559#define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
e56d0903 1560
158d9ebd 1561extern void __put_task_struct(struct task_struct *t);
e56d0903
IM
1562
1563static inline void put_task_struct(struct task_struct *t)
1564{
1565 if (atomic_dec_and_test(&t->usage))
8c7904a0 1566 __put_task_struct(t);
e56d0903 1567}
1da177e4 1568
150593bf
ON
1569struct task_struct *task_rcu_dereference(struct task_struct **ptask);
1570struct task_struct *try_get_task_struct(struct task_struct **ptask);
1571
6a61671b
FW
1572#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1573extern void task_cputime(struct task_struct *t,
5613fda9 1574 u64 *utime, u64 *stime);
16a6d9be 1575extern u64 task_gtime(struct task_struct *t);
6a61671b 1576#else
6fac4829 1577static inline void task_cputime(struct task_struct *t,
5613fda9 1578 u64 *utime, u64 *stime)
6fac4829 1579{
353c50eb
SG
1580 *utime = t->utime;
1581 *stime = t->stime;
6fac4829
FW
1582}
1583
16a6d9be 1584static inline u64 task_gtime(struct task_struct *t)
40565b5a
SG
1585{
1586 return t->gtime;
1587}
1588#endif
1589
1590#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
6fac4829 1591static inline void task_cputime_scaled(struct task_struct *t,
5613fda9
FW
1592 u64 *utimescaled,
1593 u64 *stimescaled)
6fac4829 1594{
353c50eb
SG
1595 *utimescaled = t->utimescaled;
1596 *stimescaled = t->stimescaled;
6fac4829 1597}
40565b5a
SG
1598#else
1599static inline void task_cputime_scaled(struct task_struct *t,
5613fda9
FW
1600 u64 *utimescaled,
1601 u64 *stimescaled)
6a61671b 1602{
40565b5a 1603 task_cputime(t, utimescaled, stimescaled);
6a61671b
FW
1604}
1605#endif
40565b5a 1606
5613fda9
FW
1607extern void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
1608extern void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
49048622 1609
1da177e4
LT
1610/*
1611 * Per process flags
1612 */
c1de45ca 1613#define PF_IDLE 0x00000002 /* I am an IDLE thread */
1da177e4 1614#define PF_EXITING 0x00000004 /* getting shut down */
778e9a9c 1615#define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
94886b84 1616#define PF_VCPU 0x00000010 /* I'm a virtual CPU */
21aa9af0 1617#define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
1da177e4 1618#define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
4db96cf0 1619#define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
1da177e4
LT
1620#define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
1621#define PF_DUMPCORE 0x00000200 /* dumped core */
1622#define PF_SIGNALED 0x00000400 /* killed by a signal */
1623#define PF_MEMALLOC 0x00000800 /* Allocating memory */
72fa5997 1624#define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
1da177e4 1625#define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
774a1221 1626#define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
1da177e4
LT
1627#define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
1628#define PF_FROZEN 0x00010000 /* frozen for system suspend */
1629#define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
1630#define PF_KSWAPD 0x00040000 /* I am kswapd */
21caf2fc 1631#define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
1da177e4 1632#define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
246bb0b1 1633#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
b31dc66a
JA
1634#define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
1635#define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
14a40ffc 1636#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
4db96cf0 1637#define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
61a87122 1638#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
58a69cb4 1639#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
2b44c4db 1640#define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
1da177e4
LT
1641
1642/*
1643 * Only the _current_ task can read/write to tsk->flags, but other
1644 * tasks can access tsk->flags in readonly mode for example
1645 * with tsk_used_math (like during threaded core dumping).
1646 * There is however an exception to this rule during ptrace
1647 * or during fork: the ptracer task is allowed to write to the
1648 * child->flags of its traced child (same goes for fork, the parent
1649 * can write to the child->flags), because we're guaranteed the
1650 * child is not running and in turn not changing child->flags
1651 * at the same time the parent does it.
1652 */
1653#define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
1654#define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
1655#define clear_used_math() clear_stopped_child_used_math(current)
1656#define set_used_math() set_stopped_child_used_math(current)
1657#define conditional_stopped_child_used_math(condition, child) \
1658 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
1659#define conditional_used_math(condition) \
1660 conditional_stopped_child_used_math(condition, current)
1661#define copy_to_stopped_child_used_math(child) \
1662 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
1663/* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
1664#define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
1665#define used_math() tsk_used_math(current)
1666
934f3072
JB
1667/* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
1668 * __GFP_FS is also cleared as it implies __GFP_IO.
1669 */
21caf2fc
ML
1670static inline gfp_t memalloc_noio_flags(gfp_t flags)
1671{
1672 if (unlikely(current->flags & PF_MEMALLOC_NOIO))
934f3072 1673 flags &= ~(__GFP_IO | __GFP_FS);
21caf2fc
ML
1674 return flags;
1675}
1676
1677static inline unsigned int memalloc_noio_save(void)
1678{
1679 unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
1680 current->flags |= PF_MEMALLOC_NOIO;
1681 return flags;
1682}
1683
1684static inline void memalloc_noio_restore(unsigned int flags)
1685{
1686 current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
1687}
1688
1d4457f9 1689/* Per-process atomic flags. */
a2b86f77 1690#define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
2ad654bc
ZL
1691#define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
1692#define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
77ed2c57 1693#define PFA_LMK_WAITING 3 /* Lowmemorykiller is waiting */
2ad654bc 1694
1d4457f9 1695
e0e5070b
ZL
1696#define TASK_PFA_TEST(name, func) \
1697 static inline bool task_##func(struct task_struct *p) \
1698 { return test_bit(PFA_##name, &p->atomic_flags); }
1699#define TASK_PFA_SET(name, func) \
1700 static inline void task_set_##func(struct task_struct *p) \
1701 { set_bit(PFA_##name, &p->atomic_flags); }
1702#define TASK_PFA_CLEAR(name, func) \
1703 static inline void task_clear_##func(struct task_struct *p) \
1704 { clear_bit(PFA_##name, &p->atomic_flags); }
1705
1706TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
1707TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
1d4457f9 1708
2ad654bc
ZL
1709TASK_PFA_TEST(SPREAD_PAGE, spread_page)
1710TASK_PFA_SET(SPREAD_PAGE, spread_page)
1711TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
1712
1713TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
1714TASK_PFA_SET(SPREAD_SLAB, spread_slab)
1715TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
1d4457f9 1716
77ed2c57
TH
1717TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
1718TASK_PFA_SET(LMK_WAITING, lmk_waiting)
1719
e5c1902e 1720/*
a8f072c1 1721 * task->jobctl flags
e5c1902e 1722 */
a8f072c1 1723#define JOBCTL_STOP_SIGMASK 0xffff /* signr of the last group stop */
e5c1902e 1724
a8f072c1
TH
1725#define JOBCTL_STOP_DEQUEUED_BIT 16 /* stop signal dequeued */
1726#define JOBCTL_STOP_PENDING_BIT 17 /* task should stop for group stop */
1727#define JOBCTL_STOP_CONSUME_BIT 18 /* consume group stop count */
73ddff2b 1728#define JOBCTL_TRAP_STOP_BIT 19 /* trap for STOP */
fb1d910c 1729#define JOBCTL_TRAP_NOTIFY_BIT 20 /* trap for NOTIFY */
a8f072c1 1730#define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */
544b2c91 1731#define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */
a8f072c1 1732
b76808e6
PD
1733#define JOBCTL_STOP_DEQUEUED (1UL << JOBCTL_STOP_DEQUEUED_BIT)
1734#define JOBCTL_STOP_PENDING (1UL << JOBCTL_STOP_PENDING_BIT)
1735#define JOBCTL_STOP_CONSUME (1UL << JOBCTL_STOP_CONSUME_BIT)
1736#define JOBCTL_TRAP_STOP (1UL << JOBCTL_TRAP_STOP_BIT)
1737#define JOBCTL_TRAP_NOTIFY (1UL << JOBCTL_TRAP_NOTIFY_BIT)
1738#define JOBCTL_TRAPPING (1UL << JOBCTL_TRAPPING_BIT)
1739#define JOBCTL_LISTENING (1UL << JOBCTL_LISTENING_BIT)
a8f072c1 1740
fb1d910c 1741#define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
73ddff2b 1742#define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
3759a0d9 1743
7dd3db54 1744extern bool task_set_jobctl_pending(struct task_struct *task,
b76808e6 1745 unsigned long mask);
73ddff2b 1746extern void task_clear_jobctl_trapping(struct task_struct *task);
3759a0d9 1747extern void task_clear_jobctl_pending(struct task_struct *task,
b76808e6 1748 unsigned long mask);
39efa3ef 1749
f41d911f
PM
1750static inline void rcu_copy_process(struct task_struct *p)
1751{
8315f422 1752#ifdef CONFIG_PREEMPT_RCU
f41d911f 1753 p->rcu_read_lock_nesting = 0;
1d082fd0 1754 p->rcu_read_unlock_special.s = 0;
dd5d19ba 1755 p->rcu_blocked_node = NULL;
f41d911f 1756 INIT_LIST_HEAD(&p->rcu_node_entry);
8315f422
PM
1757#endif /* #ifdef CONFIG_PREEMPT_RCU */
1758#ifdef CONFIG_TASKS_RCU
1759 p->rcu_tasks_holdout = false;
1760 INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
176f8f7a 1761 p->rcu_tasks_idle_cpu = -1;
8315f422 1762#endif /* #ifdef CONFIG_TASKS_RCU */
f41d911f
PM
1763}
1764
907aed48
MG
1765static inline void tsk_restore_flags(struct task_struct *task,
1766 unsigned long orig_flags, unsigned long flags)
1767{
1768 task->flags &= ~flags;
1769 task->flags |= orig_flags & flags;
1770}
1771
f82f8042
JL
1772extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
1773 const struct cpumask *trial);
7f51412a
JL
1774extern int task_can_attach(struct task_struct *p,
1775 const struct cpumask *cs_cpus_allowed);
1da177e4 1776#ifdef CONFIG_SMP
1e1b6c51
KM
1777extern void do_set_cpus_allowed(struct task_struct *p,
1778 const struct cpumask *new_mask);
1779
cd8ba7cd 1780extern int set_cpus_allowed_ptr(struct task_struct *p,
96f874e2 1781 const struct cpumask *new_mask);
1da177e4 1782#else
1e1b6c51
KM
1783static inline void do_set_cpus_allowed(struct task_struct *p,
1784 const struct cpumask *new_mask)
1785{
1786}
cd8ba7cd 1787static inline int set_cpus_allowed_ptr(struct task_struct *p,
96f874e2 1788 const struct cpumask *new_mask)
1da177e4 1789{
96f874e2 1790 if (!cpumask_test_cpu(0, new_mask))
1da177e4
LT
1791 return -EINVAL;
1792 return 0;
1793}
1794#endif
e0ad9556 1795
3451d024 1796#ifdef CONFIG_NO_HZ_COMMON
5167e8d5
PZ
1797void calc_load_enter_idle(void);
1798void calc_load_exit_idle(void);
1799#else
1800static inline void calc_load_enter_idle(void) { }
1801static inline void calc_load_exit_idle(void) { }
3451d024 1802#endif /* CONFIG_NO_HZ_COMMON */
5167e8d5 1803
6d0d2878
CB
1804#ifndef cpu_relax_yield
1805#define cpu_relax_yield() cpu_relax()
1806#endif
1807
36c8b586 1808extern unsigned long long
41b86e9c 1809task_sched_runtime(struct task_struct *task);
1da177e4
LT
1810
1811/* sched_exec is called by processes performing an exec */
1812#ifdef CONFIG_SMP
1813extern void sched_exec(void);
1814#else
1815#define sched_exec() {}
1816#endif
1817
1818#ifdef CONFIG_HOTPLUG_CPU
1819extern void idle_task_exit(void);
1820#else
1821static inline void idle_task_exit(void) {}
1822#endif
1823
3451d024 1824#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
1c20091e 1825extern void wake_up_nohz_cpu(int cpu);
06d8308c 1826#else
1c20091e 1827static inline void wake_up_nohz_cpu(int cpu) { }
06d8308c
TG
1828#endif
1829
ce831b38 1830#ifdef CONFIG_NO_HZ_FULL
265f22a9 1831extern u64 scheduler_tick_max_deferment(void);
06d8308c
TG
1832#endif
1833
fa93384f 1834extern int yield_to(struct task_struct *p, bool preempt);
36c8b586
IM
1835extern void set_user_nice(struct task_struct *p, long nice);
1836extern int task_prio(const struct task_struct *p);
d0ea0268
DY
1837/**
1838 * task_nice - return the nice value of a given task.
1839 * @p: the task in question.
1840 *
1841 * Return: The nice value [ -20 ... 0 ... 19 ].
1842 */
1843static inline int task_nice(const struct task_struct *p)
1844{
1845 return PRIO_TO_NICE((p)->static_prio);
1846}
36c8b586
IM
1847extern int can_nice(const struct task_struct *p, const int nice);
1848extern int task_curr(const struct task_struct *p);
1da177e4 1849extern int idle_cpu(int cpu);
fe7de49f
KM
1850extern int sched_setscheduler(struct task_struct *, int,
1851 const struct sched_param *);
961ccddd 1852extern int sched_setscheduler_nocheck(struct task_struct *, int,
fe7de49f 1853 const struct sched_param *);
d50dde5a
DF
1854extern int sched_setattr(struct task_struct *,
1855 const struct sched_attr *);
36c8b586 1856extern struct task_struct *idle_task(int cpu);
c4f30608
PM
1857/**
1858 * is_idle_task - is the specified task an idle task?
fa757281 1859 * @p: the task in question.
e69f6186
YB
1860 *
1861 * Return: 1 if @p is an idle task. 0 otherwise.
c4f30608 1862 */
7061ca3b 1863static inline bool is_idle_task(const struct task_struct *p)
c4f30608 1864{
c1de45ca 1865 return !!(p->flags & PF_IDLE);
c4f30608 1866}
36c8b586 1867extern struct task_struct *curr_task(int cpu);
a458ae2e 1868extern void ia64_set_curr_task(int cpu, struct task_struct *p);
1da177e4
LT
1869
1870void yield(void);
1871
1da177e4 1872union thread_union {
c65eacbe 1873#ifndef CONFIG_THREAD_INFO_IN_TASK
1da177e4 1874 struct thread_info thread_info;
c65eacbe 1875#endif
1da177e4
LT
1876 unsigned long stack[THREAD_SIZE/sizeof(long)];
1877};
1878
1879#ifndef __HAVE_ARCH_KSTACK_END
1880static inline int kstack_end(void *addr)
1881{
1882 /* Reliable end of stack detection:
1883 * Some APM bios versions misalign the stack
1884 */
1885 return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
1886}
1887#endif
1888
1889extern union thread_union init_thread_union;
1890extern struct task_struct init_task;
1891
1892extern struct mm_struct init_mm;
1893
198fe21b
PE
1894extern struct pid_namespace init_pid_ns;
1895
1896/*
1897 * find a task by one of its numerical ids
1898 *
198fe21b
PE
1899 * find_task_by_pid_ns():
1900 * finds a task by its pid in the specified namespace
228ebcbe
PE
1901 * find_task_by_vpid():
1902 * finds a task by its virtual pid
198fe21b 1903 *
e49859e7 1904 * see also find_vpid() etc in include/linux/pid.h
198fe21b
PE
1905 */
1906
228ebcbe
PE
1907extern struct task_struct *find_task_by_vpid(pid_t nr);
1908extern struct task_struct *find_task_by_pid_ns(pid_t nr,
1909 struct pid_namespace *ns);
198fe21b 1910
1da177e4 1911/* per-UID process charging. */
7b44ab97 1912extern struct user_struct * alloc_uid(kuid_t);
1da177e4
LT
1913static inline struct user_struct *get_uid(struct user_struct *u)
1914{
1915 atomic_inc(&u->__count);
1916 return u;
1917}
1918extern void free_uid(struct user_struct *);
1da177e4
LT
1919
1920#include <asm/current.h>
1921
f0af911a 1922extern void xtime_update(unsigned long ticks);
1da177e4 1923
b3c97528
HH
1924extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1925extern int wake_up_process(struct task_struct *tsk);
3e51e3ed 1926extern void wake_up_new_task(struct task_struct *tsk);
1da177e4
LT
1927#ifdef CONFIG_SMP
1928 extern void kick_process(struct task_struct *tsk);
1929#else
1930 static inline void kick_process(struct task_struct *tsk) { }
1931#endif
aab03e05 1932extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
ad46c2c4 1933extern void sched_dead(struct task_struct *p);
1da177e4 1934
1da177e4 1935extern void proc_caches_init(void);
9a13049e 1936
1da177e4 1937extern void release_task(struct task_struct * p);
5a1b98d3 1938
3033f14a
JT
1939#ifdef CONFIG_HAVE_COPY_THREAD_TLS
1940extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
1941 struct task_struct *, unsigned long);
1942#else
6f2c55b8 1943extern int copy_thread(unsigned long, unsigned long, unsigned long,
afa86fc4 1944 struct task_struct *);
3033f14a
JT
1945
1946/* Architectures that haven't opted into copy_thread_tls get the tls argument
1947 * via pt_regs, so ignore the tls argument passed via C. */
1948static inline int copy_thread_tls(
1949 unsigned long clone_flags, unsigned long sp, unsigned long arg,
1950 struct task_struct *p, unsigned long tls)
1951{
1952 return copy_thread(clone_flags, sp, arg, p);
1953}
1954#endif
1da177e4 1955extern void flush_thread(void);
5f56a5df
JS
1956
1957#ifdef CONFIG_HAVE_EXIT_THREAD
e6464694 1958extern void exit_thread(struct task_struct *tsk);
5f56a5df 1959#else
e6464694 1960static inline void exit_thread(struct task_struct *tsk)
5f56a5df
JS
1961{
1962}
1963#endif
1da177e4 1964
1da177e4 1965extern void exit_files(struct task_struct *);
cbaffba1 1966
1da177e4
LT
1967extern void exit_itimers(struct signal_struct *);
1968
9402c95f 1969extern void do_group_exit(int);
1da177e4 1970
c4ad8f98 1971extern int do_execve(struct filename *,
d7627467 1972 const char __user * const __user *,
da3d4c5f 1973 const char __user * const __user *);
51f39a1f
DD
1974extern int do_execveat(int, struct filename *,
1975 const char __user * const __user *,
1976 const char __user * const __user *,
1977 int);
3033f14a 1978extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
e80d6661 1979extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
36c8b586 1980struct task_struct *fork_idle(int);
2aa3a7f8 1981extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
1da177e4 1982
82b89778
AH
1983extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
1984static inline void set_task_comm(struct task_struct *tsk, const char *from)
1985{
1986 __set_task_comm(tsk, from, false);
1987}
59714d65 1988extern char *get_task_comm(char *to, struct task_struct *tsk);
1da177e4
LT
1989
1990#ifdef CONFIG_SMP
317f3941 1991void scheduler_ipi(void);
85ba2d86 1992extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
1da177e4 1993#else
184748cc 1994static inline void scheduler_ipi(void) { }
85ba2d86
RM
1995static inline unsigned long wait_task_inactive(struct task_struct *p,
1996 long match_state)
1997{
1998 return 1;
1999}
1da177e4
LT
2000#endif
2001
1da177e4 2002/*
260ea101 2003 * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
22e2c507 2004 * subscriptions and synchronises with wait4(). Also used in procfs. Also
ddbcc7e8 2005 * pins the final release of task.io_context. Also protects ->cpuset and
d68b46fe 2006 * ->cgroup.subsys[]. And ->vfork_done.
1da177e4
LT
2007 *
2008 * Nests both inside and outside of read_lock(&tasklist_lock).
2009 * It must not be nested with write_lock_irq(&tasklist_lock),
2010 * neither inside nor outside.
2011 */
2012static inline void task_lock(struct task_struct *p)
2013{
2014 spin_lock(&p->alloc_lock);
2015}
2016
2017static inline void task_unlock(struct task_struct *p)
2018{
2019 spin_unlock(&p->alloc_lock);
2020}
2021
c65eacbe
AL
2022#ifdef CONFIG_THREAD_INFO_IN_TASK
2023
2024static inline struct thread_info *task_thread_info(struct task_struct *task)
2025{
2026 return &task->thread_info;
2027}
c6c314a6
AL
2028
2029/*
2030 * When accessing the stack of a non-current task that might exit, use
2031 * try_get_task_stack() instead. task_stack_page will return a pointer
2032 * that could get freed out from under you.
2033 */
c65eacbe
AL
2034static inline void *task_stack_page(const struct task_struct *task)
2035{
2036 return task->stack;
2037}
c6c314a6 2038
c65eacbe 2039#define setup_thread_stack(new,old) do { } while(0)
c6c314a6 2040
c65eacbe
AL
2041static inline unsigned long *end_of_stack(const struct task_struct *task)
2042{
2043 return task->stack;
2044}
2045
2046#elif !defined(__HAVE_THREAD_FUNCTIONS)
f037360f 2047
f7e4217b 2048#define task_thread_info(task) ((struct thread_info *)(task)->stack)
c65eacbe 2049#define task_stack_page(task) ((void *)(task)->stack)
a1261f54 2050
10ebffde
AV
2051static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
2052{
2053 *task_thread_info(p) = *task_thread_info(org);
2054 task_thread_info(p)->task = p;
2055}
2056
6a40281a
CE
2057/*
2058 * Return the address of the last usable long on the stack.
2059 *
2060 * When the stack grows down, this is just above the thread
2061 * info struct. Going any lower will corrupt the threadinfo.
2062 *
2063 * When the stack grows up, this is the highest address.
2064 * Beyond that position, we corrupt data on the next page.
2065 */
10ebffde
AV
2066static inline unsigned long *end_of_stack(struct task_struct *p)
2067{
6a40281a
CE
2068#ifdef CONFIG_STACK_GROWSUP
2069 return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
2070#else
f7e4217b 2071 return (unsigned long *)(task_thread_info(p) + 1);
6a40281a 2072#endif
10ebffde
AV
2073}
2074
f037360f 2075#endif
c6c314a6 2076
68f24b08
AL
2077#ifdef CONFIG_THREAD_INFO_IN_TASK
2078static inline void *try_get_task_stack(struct task_struct *tsk)
2079{
2080 return atomic_inc_not_zero(&tsk->stack_refcount) ?
2081 task_stack_page(tsk) : NULL;
2082}
2083
2084extern void put_task_stack(struct task_struct *tsk);
2085#else
c6c314a6
AL
2086static inline void *try_get_task_stack(struct task_struct *tsk)
2087{
2088 return task_stack_page(tsk);
2089}
2090
2091static inline void put_task_stack(struct task_struct *tsk) {}
68f24b08 2092#endif
c6c314a6 2093
a70857e4
AT
2094#define task_stack_end_corrupted(task) \
2095 (*(end_of_stack(task)) != STACK_END_MAGIC)
f037360f 2096
8b05c7e6
FT
2097static inline int object_is_on_stack(void *obj)
2098{
2099 void *stack = task_stack_page(current);
2100
2101 return (obj >= stack) && (obj < (stack + THREAD_SIZE));
2102}
2103
b235beea 2104extern void thread_stack_cache_init(void);
8c9843e5 2105
7c9f8861
ES
2106#ifdef CONFIG_DEBUG_STACK_USAGE
2107static inline unsigned long stack_not_used(struct task_struct *p)
2108{
2109 unsigned long *n = end_of_stack(p);
2110
2111 do { /* Skip over canary */
6c31da34
HD
2112# ifdef CONFIG_STACK_GROWSUP
2113 n--;
2114# else
7c9f8861 2115 n++;
6c31da34 2116# endif
7c9f8861
ES
2117 } while (!*n);
2118
6c31da34
HD
2119# ifdef CONFIG_STACK_GROWSUP
2120 return (unsigned long)end_of_stack(p) - (unsigned long)n;
2121# else
7c9f8861 2122 return (unsigned long)n - (unsigned long)end_of_stack(p);
6c31da34 2123# endif
7c9f8861
ES
2124}
2125#endif
d4311ff1 2126extern void set_task_stack_end_magic(struct task_struct *tsk);
7c9f8861 2127
1da177e4
LT
2128/* set thread flags in other task's structures
2129 * - see asm/thread_info.h for TIF_xxxx flags available
2130 */
2131static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
2132{
a1261f54 2133 set_ti_thread_flag(task_thread_info(tsk), flag);
1da177e4
LT
2134}
2135
2136static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
2137{
a1261f54 2138 clear_ti_thread_flag(task_thread_info(tsk), flag);
1da177e4
LT
2139}
2140
2141static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
2142{
a1261f54 2143 return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
1da177e4
LT
2144}
2145
2146static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
2147{
a1261f54 2148 return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
1da177e4
LT
2149}
2150
2151static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
2152{
a1261f54 2153 return test_ti_thread_flag(task_thread_info(tsk), flag);
1da177e4
LT
2154}
2155
2156static inline void set_tsk_need_resched(struct task_struct *tsk)
2157{
2158 set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
2159}
2160
2161static inline void clear_tsk_need_resched(struct task_struct *tsk)
2162{
2163 clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
2164}
2165
8ae121ac
GH
2166static inline int test_tsk_need_resched(struct task_struct *tsk)
2167{
2168 return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
2169}
2170
690cc3ff
EB
2171static inline int restart_syscall(void)
2172{
2173 set_tsk_thread_flag(current, TIF_SIGPENDING);
2174 return -ERESTARTNOINTR;
2175}
2176
1da177e4
LT
2177static inline int signal_pending(struct task_struct *p)
2178{
2179 return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
2180}
f776d12d 2181
d9588725
RM
2182static inline int __fatal_signal_pending(struct task_struct *p)
2183{
2184 return unlikely(sigismember(&p->pending.signal, SIGKILL));
2185}
f776d12d
MW
2186
2187static inline int fatal_signal_pending(struct task_struct *p)
2188{
2189 return signal_pending(p) && __fatal_signal_pending(p);
2190}
2191
16882c1e
ON
2192static inline int signal_pending_state(long state, struct task_struct *p)
2193{
2194 if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
2195 return 0;
2196 if (!signal_pending(p))
2197 return 0;
2198
16882c1e
ON
2199 return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
2200}
2201
1da177e4
LT
2202/*
2203 * cond_resched() and cond_resched_lock(): latency reduction via
2204 * explicit rescheduling in places that are safe. The return
2205 * value indicates whether a reschedule was done in fact.
2206 * cond_resched_lock() will drop the spinlock before scheduling,
2207 * cond_resched_softirq() will enable bhs before scheduling.
2208 */
35a773a0 2209#ifndef CONFIG_PREEMPT
c3921ab7 2210extern int _cond_resched(void);
35a773a0
PZ
2211#else
2212static inline int _cond_resched(void) { return 0; }
2213#endif
6f80bd98 2214
613afbf8 2215#define cond_resched() ({ \
3427445a 2216 ___might_sleep(__FILE__, __LINE__, 0); \
613afbf8
FW
2217 _cond_resched(); \
2218})
6f80bd98 2219
613afbf8
FW
2220extern int __cond_resched_lock(spinlock_t *lock);
2221
2222#define cond_resched_lock(lock) ({ \
3427445a 2223 ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
613afbf8
FW
2224 __cond_resched_lock(lock); \
2225})
2226
2227extern int __cond_resched_softirq(void);
2228
75e1056f 2229#define cond_resched_softirq() ({ \
3427445a 2230 ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
75e1056f 2231 __cond_resched_softirq(); \
613afbf8 2232})
1da177e4 2233
f6f3c437
SH
2234static inline void cond_resched_rcu(void)
2235{
2236#if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
2237 rcu_read_unlock();
2238 cond_resched();
2239 rcu_read_lock();
2240#endif
2241}
2242
1da177e4
LT
2243/*
2244 * Does a critical section need to be broken due to another
95c354fe
NP
2245 * task waiting?: (technically does not depend on CONFIG_PREEMPT,
2246 * but a general need for low latency)
1da177e4 2247 */
95c354fe 2248static inline int spin_needbreak(spinlock_t *lock)
1da177e4 2249{
95c354fe
NP
2250#ifdef CONFIG_PREEMPT
2251 return spin_is_contended(lock);
2252#else
1da177e4 2253 return 0;
95c354fe 2254#endif
1da177e4
LT
2255}
2256
75f93fed
PZ
2257static __always_inline bool need_resched(void)
2258{
2259 return unlikely(tif_need_resched());
2260}
2261
f06febc9
FM
2262/*
2263 * Thread group CPU time accounting.
2264 */
4cd4c1b4 2265void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
4da94d49 2266void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
f06febc9 2267
7bb44ade
RM
2268/*
2269 * Reevaluate whether the task has signals pending delivery.
2270 * Wake the task if so.
2271 * This is required every time the blocked sigset_t changes.
2272 * callers must hold sighand->siglock.
2273 */
2274extern void recalc_sigpending_and_wake(struct task_struct *t);
1da177e4
LT
2275extern void recalc_sigpending(void);
2276
910ffdb1
ON
2277extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
2278
2279static inline void signal_wake_up(struct task_struct *t, bool resume)
2280{
2281 signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
2282}
2283static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
2284{
2285 signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
2286}
1da177e4
LT
2287
2288/*
2289 * Wrappers for p->thread_info->cpu access. No-op on UP.
2290 */
2291#ifdef CONFIG_SMP
2292
2293static inline unsigned int task_cpu(const struct task_struct *p)
2294{
c65eacbe
AL
2295#ifdef CONFIG_THREAD_INFO_IN_TASK
2296 return p->cpu;
2297#else
a1261f54 2298 return task_thread_info(p)->cpu;
c65eacbe 2299#endif
1da177e4
LT
2300}
2301
b32e86b4
IM
2302static inline int task_node(const struct task_struct *p)
2303{
2304 return cpu_to_node(task_cpu(p));
2305}
2306
c65cc870 2307extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
1da177e4
LT
2308
2309#else
2310
2311static inline unsigned int task_cpu(const struct task_struct *p)
2312{
2313 return 0;
2314}
2315
2316static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
2317{
2318}
2319
2320#endif /* CONFIG_SMP */
2321
d9345c65
PX
2322/*
2323 * In order to reduce various lock holder preemption latencies provide an
2324 * interface to see if a vCPU is currently running or not.
2325 *
2326 * This allows us to terminate optimistic spin loops and block, analogous to
2327 * the native optimistic spin heuristic of testing if the lock owner task is
2328 * running or not.
2329 */
2330#ifndef vcpu_is_preempted
2331# define vcpu_is_preempted(cpu) false
2332#endif
2333
96f874e2
RR
2334extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
2335extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
5c45bf27 2336
7c941438 2337#ifdef CONFIG_CGROUP_SCHED
07e06b01 2338extern struct task_group root_task_group;
8323f26c 2339#endif /* CONFIG_CGROUP_SCHED */
9b5b7751 2340
54e99124
DG
2341extern int task_can_switch_user(struct user_struct *up,
2342 struct task_struct *tsk);
2343
4b98d11b
AD
2344#ifdef CONFIG_TASK_XACCT
2345static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
2346{
940389b8 2347 tsk->ioac.rchar += amt;
4b98d11b
AD
2348}
2349
2350static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
2351{
940389b8 2352 tsk->ioac.wchar += amt;
4b98d11b
AD
2353}
2354
2355static inline void inc_syscr(struct task_struct *tsk)
2356{
940389b8 2357 tsk->ioac.syscr++;
4b98d11b
AD
2358}
2359
2360static inline void inc_syscw(struct task_struct *tsk)
2361{
940389b8 2362 tsk->ioac.syscw++;
4b98d11b
AD
2363}
2364#else
2365static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
2366{
2367}
2368
2369static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
2370{
2371}
2372
2373static inline void inc_syscr(struct task_struct *tsk)
2374{
2375}
2376
2377static inline void inc_syscw(struct task_struct *tsk)
2378{
2379}
2380#endif
2381
82455257
DH
2382#ifndef TASK_SIZE_OF
2383#define TASK_SIZE_OF(tsk) TASK_SIZE
2384#endif
2385
f98bafa0 2386#ifdef CONFIG_MEMCG
cf475ad2 2387extern void mm_update_next_owner(struct mm_struct *mm);
cf475ad2
BS
2388#else
2389static inline void mm_update_next_owner(struct mm_struct *mm)
2390{
2391}
f98bafa0 2392#endif /* CONFIG_MEMCG */
cf475ad2 2393
58919e83
RW
2394#define SCHED_CPUFREQ_RT (1U << 0)
2395#define SCHED_CPUFREQ_DL (1U << 1)
8c34ab19 2396#define SCHED_CPUFREQ_IOWAIT (1U << 2)
58919e83
RW
2397
2398#define SCHED_CPUFREQ_RT_DL (SCHED_CPUFREQ_RT | SCHED_CPUFREQ_DL)
2399
adaf9fcd
RW
2400#ifdef CONFIG_CPU_FREQ
2401struct update_util_data {
58919e83 2402 void (*func)(struct update_util_data *data, u64 time, unsigned int flags);
adaf9fcd
RW
2403};
2404
0bed612b 2405void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
58919e83
RW
2406 void (*func)(struct update_util_data *data, u64 time,
2407 unsigned int flags));
0bed612b 2408void cpufreq_remove_update_util_hook(int cpu);
adaf9fcd
RW
2409#endif /* CONFIG_CPU_FREQ */
2410
1da177e4 2411#endif