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