OSDN Git Service

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