Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.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
9 struct sched_param {
10         int sched_priority;
11 };
12
13 #include <asm/param.h>  /* for HZ */
14
15 #include <linux/capability.h>
16 #include <linux/threads.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/timex.h>
20 #include <linux/jiffies.h>
21 #include <linux/plist.h>
22 #include <linux/rbtree.h>
23 #include <linux/thread_info.h>
24 #include <linux/cpumask.h>
25 #include <linux/errno.h>
26 #include <linux/nodemask.h>
27 #include <linux/mm_types.h>
28 #include <linux/preempt.h>
29
30 #include <asm/page.h>
31 #include <asm/ptrace.h>
32 #include <linux/cputime.h>
33
34 #include <linux/smp.h>
35 #include <linux/sem.h>
36 #include <linux/shm.h>
37 #include <linux/signal.h>
38 #include <linux/compiler.h>
39 #include <linux/completion.h>
40 #include <linux/pid.h>
41 #include <linux/percpu.h>
42 #include <linux/topology.h>
43 #include <linux/proportions.h>
44 #include <linux/seccomp.h>
45 #include <linux/rcupdate.h>
46 #include <linux/rculist.h>
47 #include <linux/rtmutex.h>
48
49 #include <linux/time.h>
50 #include <linux/param.h>
51 #include <linux/resource.h>
52 #include <linux/timer.h>
53 #include <linux/hrtimer.h>
54 #include <linux/task_io_accounting.h>
55 #include <linux/latencytop.h>
56 #include <linux/cred.h>
57 #include <linux/llist.h>
58 #include <linux/uidgid.h>
59 #include <linux/gfp.h>
60 #include <linux/magic.h>
61 #include <linux/cgroup-defs.h>
62
63 #include <asm/processor.h>
64
65 #define SCHED_ATTR_SIZE_VER0    48      /* sizeof first published struct */
66
67 /*
68  * Extended scheduling parameters data structure.
69  *
70  * This is needed because the original struct sched_param can not be
71  * altered without introducing ABI issues with legacy applications
72  * (e.g., in sched_getparam()).
73  *
74  * However, the possibility of specifying more than just a priority for
75  * the tasks may be useful for a wide variety of application fields, e.g.,
76  * multimedia, streaming, automation and control, and many others.
77  *
78  * This variant (sched_attr) is meant at describing a so-called
79  * sporadic time-constrained task. In such model a task is specified by:
80  *  - the activation period or minimum instance inter-arrival time;
81  *  - the maximum (or average, depending on the actual scheduling
82  *    discipline) computation time of all instances, a.k.a. runtime;
83  *  - the deadline (relative to the actual activation time) of each
84  *    instance.
85  * Very briefly, a periodic (sporadic) task asks for the execution of
86  * some specific computation --which is typically called an instance--
87  * (at most) every period. Moreover, each instance typically lasts no more
88  * than the runtime and must be completed by time instant t equal to
89  * the instance activation time + the deadline.
90  *
91  * This is reflected by the actual fields of the sched_attr structure:
92  *
93  *  @size               size of the structure, for fwd/bwd compat.
94  *
95  *  @sched_policy       task's scheduling policy
96  *  @sched_flags        for customizing the scheduler behaviour
97  *  @sched_nice         task's nice value      (SCHED_NORMAL/BATCH)
98  *  @sched_priority     task's static priority (SCHED_FIFO/RR)
99  *  @sched_deadline     representative of the task's deadline
100  *  @sched_runtime      representative of the task's runtime
101  *  @sched_period       representative of the task's period
102  *
103  * Given this task model, there are a multiplicity of scheduling algorithms
104  * and policies, that can be used to ensure all the tasks will make their
105  * timing constraints.
106  *
107  * As of now, the SCHED_DEADLINE policy (sched_dl scheduling class) is the
108  * only user of this new interface. More information about the algorithm
109  * available in the scheduling class file or in Documentation/.
110  */
111 struct sched_attr {
112         u32 size;
113
114         u32 sched_policy;
115         u64 sched_flags;
116
117         /* SCHED_NORMAL, SCHED_BATCH */
118         s32 sched_nice;
119
120         /* SCHED_FIFO, SCHED_RR */
121         u32 sched_priority;
122
123         /* SCHED_DEADLINE */
124         u64 sched_runtime;
125         u64 sched_deadline;
126         u64 sched_period;
127 };
128
129 struct futex_pi_state;
130 struct robust_list_head;
131 struct bio_list;
132 struct fs_struct;
133 struct perf_event_context;
134 struct blk_plug;
135 struct filename;
136 struct nameidata;
137
138 #define VMACACHE_BITS 2
139 #define VMACACHE_SIZE (1U << VMACACHE_BITS)
140 #define VMACACHE_MASK (VMACACHE_SIZE - 1)
141
142 /*
143  * These are the constant used to fake the fixed-point load-average
144  * counting. Some notes:
145  *  - 11 bit fractions expand to 22 bits by the multiplies: this gives
146  *    a load-average precision of 10 bits integer + 11 bits fractional
147  *  - if you want to count load-averages more often, you need more
148  *    precision, or rounding will get you. With 2-second counting freq,
149  *    the EXP_n values would be 1981, 2034 and 2043 if still using only
150  *    11 bit fractions.
151  */
152 extern unsigned long avenrun[];         /* Load averages */
153 extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
154
155 #define FSHIFT          11              /* nr of bits of precision */
156 #define FIXED_1         (1<<FSHIFT)     /* 1.0 as fixed-point */
157 #define LOAD_FREQ       (5*HZ+1)        /* 5 sec intervals */
158 #define EXP_1           1884            /* 1/exp(5sec/1min) as fixed-point */
159 #define EXP_5           2014            /* 1/exp(5sec/5min) */
160 #define EXP_15          2037            /* 1/exp(5sec/15min) */
161
162 #define CALC_LOAD(load,exp,n) \
163         load *= exp; \
164         load += n*(FIXED_1-exp); \
165         load >>= FSHIFT;
166
167 extern unsigned long total_forks;
168 extern int nr_threads;
169 DECLARE_PER_CPU(unsigned long, process_counts);
170 extern int nr_processes(void);
171 extern unsigned long nr_running(void);
172 extern bool single_task_running(void);
173 extern unsigned long nr_iowait(void);
174 extern unsigned long nr_iowait_cpu(int cpu);
175 extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
176 #ifdef CONFIG_CPU_QUIET
177 extern u64 nr_running_integral(unsigned int cpu);
178 #endif
179
180 extern void calc_global_load(unsigned long ticks);
181
182 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
183 extern void update_cpu_load_nohz(void);
184 #else
185 static inline void update_cpu_load_nohz(void) { }
186 #endif
187
188 extern unsigned long get_parent_ip(unsigned long addr);
189
190 extern void dump_cpu_task(int cpu);
191
192 struct seq_file;
193 struct cfs_rq;
194 struct task_group;
195 #ifdef CONFIG_SCHED_DEBUG
196 extern void proc_sched_show_task(struct task_struct *p, struct seq_file *m);
197 extern void proc_sched_set_task(struct task_struct *p);
198 #endif
199
200 /*
201  * Task state bitmask. NOTE! These bits are also
202  * encoded in fs/proc/array.c: get_task_state().
203  *
204  * We have two separate sets of flags: task->state
205  * is about runnability, while task->exit_state are
206  * about the task exiting. Confusing, but this way
207  * modifying one set can't modify the other one by
208  * mistake.
209  */
210 #define TASK_RUNNING            0
211 #define TASK_INTERRUPTIBLE      1
212 #define TASK_UNINTERRUPTIBLE    2
213 #define __TASK_STOPPED          4
214 #define __TASK_TRACED           8
215 /* in tsk->exit_state */
216 #define EXIT_DEAD               16
217 #define EXIT_ZOMBIE             32
218 #define EXIT_TRACE              (EXIT_ZOMBIE | EXIT_DEAD)
219 /* in tsk->state again */
220 #define TASK_DEAD               64
221 #define TASK_WAKEKILL           128
222 #define TASK_WAKING             256
223 #define TASK_PARKED             512
224 #define TASK_NOLOAD             1024
225 #define TASK_STATE_MAX          2048
226
227 #define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPN"
228
229 extern char ___assert_task_state[1 - 2*!!(
230                 sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
231
232 /* Convenience macros for the sake of set_task_state */
233 #define TASK_KILLABLE           (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
234 #define TASK_STOPPED            (TASK_WAKEKILL | __TASK_STOPPED)
235 #define TASK_TRACED             (TASK_WAKEKILL | __TASK_TRACED)
236
237 #define TASK_IDLE               (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
238
239 /* Convenience macros for the sake of wake_up */
240 #define TASK_NORMAL             (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
241 #define TASK_ALL                (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
242
243 /* get_task_state() */
244 #define TASK_REPORT             (TASK_RUNNING | TASK_INTERRUPTIBLE | \
245                                  TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
246                                  __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
247
248 #define task_is_traced(task)    ((task->state & __TASK_TRACED) != 0)
249 #define task_is_stopped(task)   ((task->state & __TASK_STOPPED) != 0)
250 #define task_is_stopped_or_traced(task) \
251                         ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
252 #define task_contributes_to_load(task)  \
253                                 ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
254                                  (task->flags & PF_FROZEN) == 0 && \
255                                  (task->state & TASK_NOLOAD) == 0)
256
257 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
258
259 #define __set_task_state(tsk, state_value)                      \
260         do {                                                    \
261                 (tsk)->task_state_change = _THIS_IP_;           \
262                 (tsk)->state = (state_value);                   \
263         } while (0)
264 #define set_task_state(tsk, state_value)                        \
265         do {                                                    \
266                 (tsk)->task_state_change = _THIS_IP_;           \
267                 smp_store_mb((tsk)->state, (state_value));              \
268         } while (0)
269
270 /*
271  * set_current_state() includes a barrier so that the write of current->state
272  * is correctly serialised wrt the caller's subsequent test of whether to
273  * actually sleep:
274  *
275  *      set_current_state(TASK_UNINTERRUPTIBLE);
276  *      if (do_i_need_to_sleep())
277  *              schedule();
278  *
279  * If the caller does not need such serialisation then use __set_current_state()
280  */
281 #define __set_current_state(state_value)                        \
282         do {                                                    \
283                 current->task_state_change = _THIS_IP_;         \
284                 current->state = (state_value);                 \
285         } while (0)
286 #define set_current_state(state_value)                          \
287         do {                                                    \
288                 current->task_state_change = _THIS_IP_;         \
289                 smp_store_mb(current->state, (state_value));            \
290         } while (0)
291
292 #else
293
294 #define __set_task_state(tsk, state_value)              \
295         do { (tsk)->state = (state_value); } while (0)
296 #define set_task_state(tsk, state_value)                \
297         smp_store_mb((tsk)->state, (state_value))
298
299 /*
300  * set_current_state() includes a barrier so that the write of current->state
301  * is correctly serialised wrt the caller's subsequent test of whether to
302  * actually sleep:
303  *
304  *      set_current_state(TASK_UNINTERRUPTIBLE);
305  *      if (do_i_need_to_sleep())
306  *              schedule();
307  *
308  * If the caller does not need such serialisation then use __set_current_state()
309  */
310 #define __set_current_state(state_value)                \
311         do { current->state = (state_value); } while (0)
312 #define set_current_state(state_value)                  \
313         smp_store_mb(current->state, (state_value))
314
315 #endif
316
317 /* Task command name length */
318 #define TASK_COMM_LEN 16
319
320 enum task_event {
321         PUT_PREV_TASK   = 0,
322         PICK_NEXT_TASK  = 1,
323         TASK_WAKE       = 2,
324         TASK_MIGRATE    = 3,
325         TASK_UPDATE     = 4,
326         IRQ_UPDATE      = 5,
327 };
328
329 #include <linux/spinlock.h>
330
331 /*
332  * This serializes "schedule()" and also protects
333  * the run-queue from deletions/modifications (but
334  * _adding_ to the beginning of the run-queue has
335  * a separate lock).
336  */
337 extern rwlock_t tasklist_lock;
338 extern spinlock_t mmlist_lock;
339
340 struct task_struct;
341
342 #ifdef CONFIG_PROVE_RCU
343 extern int lockdep_tasklist_lock_is_held(void);
344 #endif /* #ifdef CONFIG_PROVE_RCU */
345
346 extern void sched_init(void);
347 extern void sched_init_smp(void);
348 extern asmlinkage void schedule_tail(struct task_struct *prev);
349 extern void init_idle(struct task_struct *idle, int cpu);
350 extern void init_idle_bootup_task(struct task_struct *idle);
351
352 extern cpumask_var_t cpu_isolated_map;
353
354 extern int runqueue_is_locked(int cpu);
355
356 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
357 extern void nohz_balance_enter_idle(int cpu);
358 extern void set_cpu_sd_state_idle(void);
359 extern int get_nohz_timer_target(void);
360 #else
361 static inline void nohz_balance_enter_idle(int cpu) { }
362 static inline void set_cpu_sd_state_idle(void) { }
363 #endif
364
365 /*
366  * Only dump TASK_* tasks. (0 for all tasks)
367  */
368 extern void show_state_filter(unsigned long state_filter);
369
370 static inline void show_state(void)
371 {
372         show_state_filter(0);
373 }
374
375 extern void show_regs(struct pt_regs *);
376
377 /*
378  * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
379  * task), SP is the stack pointer of the first frame that should be shown in the back
380  * trace (or NULL if the entire call-chain of the task should be shown).
381  */
382 extern void show_stack(struct task_struct *task, unsigned long *sp);
383
384 extern void cpu_init (void);
385 extern void trap_init(void);
386 extern void update_process_times(int user);
387 extern void scheduler_tick(void);
388
389 extern void sched_show_task(struct task_struct *p);
390
391 #ifdef CONFIG_LOCKUP_DETECTOR
392 extern void touch_softlockup_watchdog(void);
393 extern void touch_softlockup_watchdog_sync(void);
394 extern void touch_all_softlockup_watchdogs(void);
395 extern int proc_dowatchdog_thresh(struct ctl_table *table, int write,
396                                   void __user *buffer,
397                                   size_t *lenp, loff_t *ppos);
398 extern unsigned int  softlockup_panic;
399 extern unsigned int  hardlockup_panic;
400 void lockup_detector_init(void);
401 #else
402 static inline void touch_softlockup_watchdog(void)
403 {
404 }
405 static inline void touch_softlockup_watchdog_sync(void)
406 {
407 }
408 static inline void touch_all_softlockup_watchdogs(void)
409 {
410 }
411 static inline void lockup_detector_init(void)
412 {
413 }
414 #endif
415
416 #ifdef CONFIG_DETECT_HUNG_TASK
417 void reset_hung_task_detector(void);
418 #else
419 static inline void reset_hung_task_detector(void)
420 {
421 }
422 #endif
423
424 /* Attach to any functions which should be ignored in wchan output. */
425 #define __sched         __attribute__((__section__(".sched.text")))
426
427 /* Linker adds these: start and end of __sched functions */
428 extern char __sched_text_start[], __sched_text_end[];
429
430 /* Is this address in the __sched functions? */
431 extern int in_sched_functions(unsigned long addr);
432
433 #define MAX_SCHEDULE_TIMEOUT    LONG_MAX
434 extern signed long schedule_timeout(signed long timeout);
435 extern signed long schedule_timeout_interruptible(signed long timeout);
436 extern signed long schedule_timeout_killable(signed long timeout);
437 extern signed long schedule_timeout_uninterruptible(signed long timeout);
438 asmlinkage void schedule(void);
439 extern void schedule_preempt_disabled(void);
440
441 extern long io_schedule_timeout(long timeout);
442
443 static inline void io_schedule(void)
444 {
445         io_schedule_timeout(MAX_SCHEDULE_TIMEOUT);
446 }
447
448 struct nsproxy;
449 struct user_namespace;
450
451 #ifdef CONFIG_MMU
452 extern void arch_pick_mmap_layout(struct mm_struct *mm);
453 extern unsigned long
454 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
455                        unsigned long, unsigned long);
456 extern unsigned long
457 arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
458                           unsigned long len, unsigned long pgoff,
459                           unsigned long flags);
460 #else
461 static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
462 #endif
463
464 #define SUID_DUMP_DISABLE       0       /* No setuid dumping */
465 #define SUID_DUMP_USER          1       /* Dump as user of process */
466 #define SUID_DUMP_ROOT          2       /* Dump as root */
467
468 /* mm flags */
469
470 /* for SUID_DUMP_* above */
471 #define MMF_DUMPABLE_BITS 2
472 #define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
473
474 extern void set_dumpable(struct mm_struct *mm, int value);
475 /*
476  * This returns the actual value of the suid_dumpable flag. For things
477  * that are using this for checking for privilege transitions, it must
478  * test against SUID_DUMP_USER rather than treating it as a boolean
479  * value.
480  */
481 static inline int __get_dumpable(unsigned long mm_flags)
482 {
483         return mm_flags & MMF_DUMPABLE_MASK;
484 }
485
486 static inline int get_dumpable(struct mm_struct *mm)
487 {
488         return __get_dumpable(mm->flags);
489 }
490
491 /* coredump filter bits */
492 #define MMF_DUMP_ANON_PRIVATE   2
493 #define MMF_DUMP_ANON_SHARED    3
494 #define MMF_DUMP_MAPPED_PRIVATE 4
495 #define MMF_DUMP_MAPPED_SHARED  5
496 #define MMF_DUMP_ELF_HEADERS    6
497 #define MMF_DUMP_HUGETLB_PRIVATE 7
498 #define MMF_DUMP_HUGETLB_SHARED  8
499 #define MMF_DUMP_DAX_PRIVATE    9
500 #define MMF_DUMP_DAX_SHARED     10
501
502 #define MMF_DUMP_FILTER_SHIFT   MMF_DUMPABLE_BITS
503 #define MMF_DUMP_FILTER_BITS    9
504 #define MMF_DUMP_FILTER_MASK \
505         (((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
506 #define MMF_DUMP_FILTER_DEFAULT \
507         ((1 << MMF_DUMP_ANON_PRIVATE) | (1 << MMF_DUMP_ANON_SHARED) |\
508          (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
509
510 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
511 # define MMF_DUMP_MASK_DEFAULT_ELF      (1 << MMF_DUMP_ELF_HEADERS)
512 #else
513 # define MMF_DUMP_MASK_DEFAULT_ELF      0
514 #endif
515                                         /* leave room for more dump flags */
516 #define MMF_VM_MERGEABLE        16      /* KSM may merge identical pages */
517 #define MMF_VM_HUGEPAGE         17      /* set when VM_HUGEPAGE is set on vma */
518 #define MMF_EXE_FILE_CHANGED    18      /* see prctl_set_mm_exe_file() */
519
520 #define MMF_HAS_UPROBES         19      /* has uprobes */
521 #define MMF_RECALC_UPROBES      20      /* MMF_HAS_UPROBES can be wrong */
522
523 #define MMF_INIT_MASK           (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK)
524
525 struct sighand_struct {
526         atomic_t                count;
527         struct k_sigaction      action[_NSIG];
528         spinlock_t              siglock;
529         wait_queue_head_t       signalfd_wqh;
530 };
531
532 struct pacct_struct {
533         int                     ac_flag;
534         long                    ac_exitcode;
535         unsigned long           ac_mem;
536         cputime_t               ac_utime, ac_stime;
537         unsigned long           ac_minflt, ac_majflt;
538 };
539
540 struct cpu_itimer {
541         cputime_t expires;
542         cputime_t incr;
543         u32 error;
544         u32 incr_error;
545 };
546
547 /**
548  * struct prev_cputime - snaphsot of system and user cputime
549  * @utime: time spent in user mode
550  * @stime: time spent in system mode
551  * @lock: protects the above two fields
552  *
553  * Stores previous user/system time values such that we can guarantee
554  * monotonicity.
555  */
556 struct prev_cputime {
557 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
558         cputime_t utime;
559         cputime_t stime;
560         raw_spinlock_t lock;
561 #endif
562 };
563
564 static inline void prev_cputime_init(struct prev_cputime *prev)
565 {
566 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
567         prev->utime = prev->stime = 0;
568         raw_spin_lock_init(&prev->lock);
569 #endif
570 }
571
572 /**
573  * struct task_cputime - collected CPU time counts
574  * @utime:              time spent in user mode, in &cputime_t units
575  * @stime:              time spent in kernel mode, in &cputime_t units
576  * @sum_exec_runtime:   total time spent on the CPU, in nanoseconds
577  *
578  * This structure groups together three kinds of CPU time that are tracked for
579  * threads and thread groups.  Most things considering CPU time want to group
580  * these counts together and treat all three of them in parallel.
581  */
582 struct task_cputime {
583         cputime_t utime;
584         cputime_t stime;
585         unsigned long long sum_exec_runtime;
586 };
587
588 /* Alternate field names when used to cache expirations. */
589 #define virt_exp        utime
590 #define prof_exp        stime
591 #define sched_exp       sum_exec_runtime
592
593 #define INIT_CPUTIME    \
594         (struct task_cputime) {                                 \
595                 .utime = 0,                                     \
596                 .stime = 0,                                     \
597                 .sum_exec_runtime = 0,                          \
598         }
599
600 /*
601  * This is the atomic variant of task_cputime, which can be used for
602  * storing and updating task_cputime statistics without locking.
603  */
604 struct task_cputime_atomic {
605         atomic64_t utime;
606         atomic64_t stime;
607         atomic64_t sum_exec_runtime;
608 };
609
610 #define INIT_CPUTIME_ATOMIC \
611         (struct task_cputime_atomic) {                          \
612                 .utime = ATOMIC64_INIT(0),                      \
613                 .stime = ATOMIC64_INIT(0),                      \
614                 .sum_exec_runtime = ATOMIC64_INIT(0),           \
615         }
616
617 #define PREEMPT_DISABLED        (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
618
619 /*
620  * Disable preemption until the scheduler is running -- use an unconditional
621  * value so that it also works on !PREEMPT_COUNT kernels.
622  *
623  * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
624  */
625 #define INIT_PREEMPT_COUNT      PREEMPT_OFFSET
626
627 /*
628  * Initial preempt_count value; reflects the preempt_count schedule invariant
629  * which states that during context switches:
630  *
631  *    preempt_count() == 2*PREEMPT_DISABLE_OFFSET
632  *
633  * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
634  * Note: See finish_task_switch().
635  */
636 #define FORK_PREEMPT_COUNT      (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
637
638 /**
639  * struct thread_group_cputimer - thread group interval timer counts
640  * @cputime_atomic:     atomic thread group interval timers.
641  * @running:            true when there are timers running and
642  *                      @cputime_atomic receives updates.
643  * @checking_timer:     true when a thread in the group is in the
644  *                      process of checking for thread group timers.
645  *
646  * This structure contains the version of task_cputime, above, that is
647  * used for thread group CPU timer calculations.
648  */
649 struct thread_group_cputimer {
650         struct task_cputime_atomic cputime_atomic;
651         bool running;
652         bool checking_timer;
653 };
654
655 #include <linux/rwsem.h>
656 struct autogroup;
657
658 /*
659  * NOTE! "signal_struct" does not have its own
660  * locking, because a shared signal_struct always
661  * implies a shared sighand_struct, so locking
662  * sighand_struct is always a proper superset of
663  * the locking of signal_struct.
664  */
665 struct signal_struct {
666         atomic_t                sigcnt;
667         atomic_t                live;
668         int                     nr_threads;
669         struct list_head        thread_head;
670
671         wait_queue_head_t       wait_chldexit;  /* for wait4() */
672
673         /* current thread group signal load-balancing target: */
674         struct task_struct      *curr_target;
675
676         /* shared signal handling: */
677         struct sigpending       shared_pending;
678
679         /* thread group exit support */
680         int                     group_exit_code;
681         /* overloaded:
682          * - notify group_exit_task when ->count is equal to notify_count
683          * - everyone except group_exit_task is stopped during signal delivery
684          *   of fatal signals, group_exit_task processes the signal.
685          */
686         int                     notify_count;
687         struct task_struct      *group_exit_task;
688
689         /* thread group stop support, overloads group_exit_code too */
690         int                     group_stop_count;
691         unsigned int            flags; /* see SIGNAL_* flags below */
692
693         /*
694          * PR_SET_CHILD_SUBREAPER marks a process, like a service
695          * manager, to re-parent orphan (double-forking) child processes
696          * to this process instead of 'init'. The service manager is
697          * able to receive SIGCHLD signals and is able to investigate
698          * the process until it calls wait(). All children of this
699          * process will inherit a flag if they should look for a
700          * child_subreaper process at exit.
701          */
702         unsigned int            is_child_subreaper:1;
703         unsigned int            has_child_subreaper:1;
704
705         /* POSIX.1b Interval Timers */
706         int                     posix_timer_id;
707         struct list_head        posix_timers;
708
709         /* ITIMER_REAL timer for the process */
710         struct hrtimer real_timer;
711         struct pid *leader_pid;
712         ktime_t it_real_incr;
713
714         /*
715          * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
716          * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
717          * values are defined to 0 and 1 respectively
718          */
719         struct cpu_itimer it[2];
720
721         /*
722          * Thread group totals for process CPU timers.
723          * See thread_group_cputimer(), et al, for details.
724          */
725         struct thread_group_cputimer cputimer;
726
727         /* Earliest-expiration cache. */
728         struct task_cputime cputime_expires;
729
730         struct list_head cpu_timers[3];
731
732         struct pid *tty_old_pgrp;
733
734         /* boolean value for session group leader */
735         int leader;
736
737         struct tty_struct *tty; /* NULL if no tty */
738
739 #ifdef CONFIG_SCHED_AUTOGROUP
740         struct autogroup *autogroup;
741 #endif
742         /*
743          * Cumulative resource counters for dead threads in the group,
744          * and for reaped dead child processes forked by this group.
745          * Live threads maintain their own counters and add to these
746          * in __exit_signal, except for the group leader.
747          */
748         seqlock_t stats_lock;
749         cputime_t utime, stime, cutime, cstime;
750         cputime_t gtime;
751         cputime_t cgtime;
752         struct prev_cputime prev_cputime;
753         unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
754         unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
755         unsigned long inblock, oublock, cinblock, coublock;
756         unsigned long maxrss, cmaxrss;
757         struct task_io_accounting ioac;
758
759         /*
760          * Cumulative ns of schedule CPU time fo dead threads in the
761          * group, not including a zombie group leader, (This only differs
762          * from jiffies_to_ns(utime + stime) if sched_clock uses something
763          * other than jiffies.)
764          */
765         unsigned long long sum_sched_runtime;
766
767         /*
768          * We don't bother to synchronize most readers of this at all,
769          * because there is no reader checking a limit that actually needs
770          * to get both rlim_cur and rlim_max atomically, and either one
771          * alone is a single word that can safely be read normally.
772          * getrlimit/setrlimit use task_lock(current->group_leader) to
773          * protect this instead of the siglock, because they really
774          * have no need to disable irqs.
775          */
776         struct rlimit rlim[RLIM_NLIMITS];
777
778 #ifdef CONFIG_BSD_PROCESS_ACCT
779         struct pacct_struct pacct;      /* per-process accounting information */
780 #endif
781 #ifdef CONFIG_TASKSTATS
782         struct taskstats *stats;
783 #endif
784 #ifdef CONFIG_AUDIT
785         unsigned audit_tty;
786         unsigned audit_tty_log_passwd;
787         struct tty_audit_buf *tty_audit_buf;
788 #endif
789
790         oom_flags_t oom_flags;
791         short oom_score_adj;            /* OOM kill score adjustment */
792         short oom_score_adj_min;        /* OOM kill score adjustment min value.
793                                          * Only settable by CAP_SYS_RESOURCE. */
794
795         struct mutex cred_guard_mutex;  /* guard against foreign influences on
796                                          * credential calculations
797                                          * (notably. ptrace) */
798 };
799
800 /*
801  * Bits in flags field of signal_struct.
802  */
803 #define SIGNAL_STOP_STOPPED     0x00000001 /* job control stop in effect */
804 #define SIGNAL_STOP_CONTINUED   0x00000002 /* SIGCONT since WCONTINUED reap */
805 #define SIGNAL_GROUP_EXIT       0x00000004 /* group exit in progress */
806 #define SIGNAL_GROUP_COREDUMP   0x00000008 /* coredump in progress */
807 /*
808  * Pending notifications to parent.
809  */
810 #define SIGNAL_CLD_STOPPED      0x00000010
811 #define SIGNAL_CLD_CONTINUED    0x00000020
812 #define SIGNAL_CLD_MASK         (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
813
814 #define SIGNAL_UNKILLABLE       0x00000040 /* for init: ignore fatal signals */
815
816 /* If true, all threads except ->group_exit_task have pending SIGKILL */
817 static inline int signal_group_exit(const struct signal_struct *sig)
818 {
819         return  (sig->flags & SIGNAL_GROUP_EXIT) ||
820                 (sig->group_exit_task != NULL);
821 }
822
823 /*
824  * Some day this will be a full-fledged user tracking system..
825  */
826 struct user_struct {
827         atomic_t __count;       /* reference count */
828         atomic_t processes;     /* How many processes does this user have? */
829         atomic_t sigpending;    /* How many pending signals does this user have? */
830 #ifdef CONFIG_INOTIFY_USER
831         atomic_t inotify_watches; /* How many inotify watches does this user have? */
832         atomic_t inotify_devs;  /* How many inotify devs does this user have opened? */
833 #endif
834 #ifdef CONFIG_FANOTIFY
835         atomic_t fanotify_listeners;
836 #endif
837 #ifdef CONFIG_EPOLL
838         atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
839 #endif
840 #ifdef CONFIG_POSIX_MQUEUE
841         /* protected by mq_lock */
842         unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
843 #endif
844         unsigned long locked_shm; /* How many pages of mlocked shm ? */
845         unsigned long unix_inflight;    /* How many files in flight in unix sockets */
846         atomic_long_t pipe_bufs;  /* how many pages are allocated in pipe buffers */
847
848 #ifdef CONFIG_KEYS
849         struct key *uid_keyring;        /* UID specific keyring */
850         struct key *session_keyring;    /* UID's default session keyring */
851 #endif
852
853         /* Hash table maintenance information */
854         struct hlist_node uidhash_node;
855         kuid_t uid;
856
857 #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL)
858         atomic_long_t locked_vm;
859 #endif
860 };
861
862 extern int uids_sysfs_init(void);
863
864 extern struct user_struct *find_user(kuid_t);
865
866 extern struct user_struct root_user;
867 #define INIT_USER (&root_user)
868
869
870 struct backing_dev_info;
871 struct reclaim_state;
872
873 #ifdef CONFIG_SCHED_INFO
874 struct sched_info {
875         /* cumulative counters */
876         unsigned long pcount;         /* # of times run on this cpu */
877         unsigned long long run_delay; /* time spent waiting on a runqueue */
878
879         /* timestamps */
880         unsigned long long last_arrival,/* when we last ran on a cpu */
881                            last_queued; /* when we were last queued to run */
882 };
883 #endif /* CONFIG_SCHED_INFO */
884
885 #ifdef CONFIG_TASK_DELAY_ACCT
886 struct task_delay_info {
887         spinlock_t      lock;
888         unsigned int    flags;  /* Private per-task flags */
889
890         /* For each stat XXX, add following, aligned appropriately
891          *
892          * struct timespec XXX_start, XXX_end;
893          * u64 XXX_delay;
894          * u32 XXX_count;
895          *
896          * Atomicity of updates to XXX_delay, XXX_count protected by
897          * single lock above (split into XXX_lock if contention is an issue).
898          */
899
900         /*
901          * XXX_count is incremented on every XXX operation, the delay
902          * associated with the operation is added to XXX_delay.
903          * XXX_delay contains the accumulated delay time in nanoseconds.
904          */
905         u64 blkio_start;        /* Shared by blkio, swapin */
906         u64 blkio_delay;        /* wait for sync block io completion */
907         u64 swapin_delay;       /* wait for swapin block io completion */
908         u32 blkio_count;        /* total count of the number of sync block */
909                                 /* io operations performed */
910         u32 swapin_count;       /* total count of the number of swapin block */
911                                 /* io operations performed */
912
913         u64 freepages_start;
914         u64 freepages_delay;    /* wait for memory reclaim */
915         u32 freepages_count;    /* total count of memory reclaim */
916 };
917 #endif  /* CONFIG_TASK_DELAY_ACCT */
918
919 static inline int sched_info_on(void)
920 {
921 #ifdef CONFIG_SCHEDSTATS
922         return 1;
923 #elif defined(CONFIG_TASK_DELAY_ACCT)
924         extern int delayacct_on;
925         return delayacct_on;
926 #else
927         return 0;
928 #endif
929 }
930
931 enum cpu_idle_type {
932         CPU_IDLE,
933         CPU_NOT_IDLE,
934         CPU_NEWLY_IDLE,
935         CPU_MAX_IDLE_TYPES
936 };
937
938 /*
939  * Increase resolution of cpu_capacity calculations
940  */
941 #define SCHED_CAPACITY_SHIFT    10
942 #define SCHED_CAPACITY_SCALE    (1L << SCHED_CAPACITY_SHIFT)
943
944 struct sched_capacity_reqs {
945         unsigned long cfs;
946         unsigned long rt;
947         unsigned long dl;
948
949         unsigned long total;
950 };
951
952 /*
953  * Wake-queues are lists of tasks with a pending wakeup, whose
954  * callers have already marked the task as woken internally,
955  * and can thus carry on. A common use case is being able to
956  * do the wakeups once the corresponding user lock as been
957  * released.
958  *
959  * We hold reference to each task in the list across the wakeup,
960  * thus guaranteeing that the memory is still valid by the time
961  * the actual wakeups are performed in wake_up_q().
962  *
963  * One per task suffices, because there's never a need for a task to be
964  * in two wake queues simultaneously; it is forbidden to abandon a task
965  * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
966  * already in a wake queue, the wakeup will happen soon and the second
967  * waker can just skip it.
968  *
969  * The WAKE_Q macro declares and initializes the list head.
970  * wake_up_q() does NOT reinitialize the list; it's expected to be
971  * called near the end of a function, where the fact that the queue is
972  * not used again will be easy to see by inspection.
973  *
974  * Note that this can cause spurious wakeups. schedule() callers
975  * must ensure the call is done inside a loop, confirming that the
976  * wakeup condition has in fact occurred.
977  */
978 struct wake_q_node {
979         struct wake_q_node *next;
980 };
981
982 struct wake_q_head {
983         struct wake_q_node *first;
984         struct wake_q_node **lastp;
985 };
986
987 #define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
988
989 #define WAKE_Q(name)                                    \
990         struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
991
992 extern void wake_q_add(struct wake_q_head *head,
993                        struct task_struct *task);
994 extern void wake_up_q(struct wake_q_head *head);
995
996 /*
997  * sched-domains (multiprocessor balancing) declarations:
998  */
999 #ifdef CONFIG_SMP
1000 #define SD_LOAD_BALANCE         0x0001  /* Do load balancing on this domain. */
1001 #define SD_BALANCE_NEWIDLE      0x0002  /* Balance when about to become idle */
1002 #define SD_BALANCE_EXEC         0x0004  /* Balance on exec */
1003 #define SD_BALANCE_FORK         0x0008  /* Balance on fork, clone */
1004 #define SD_BALANCE_WAKE         0x0010  /* Balance on wakeup */
1005 #define SD_WAKE_AFFINE          0x0020  /* Wake task to waking CPU */
1006 #define SD_SHARE_CPUCAPACITY    0x0080  /* Domain members share cpu power */
1007 #define SD_SHARE_POWERDOMAIN    0x0100  /* Domain members share power domain */
1008 #define SD_SHARE_PKG_RESOURCES  0x0200  /* Domain members share cpu pkg resources */
1009 #define SD_SERIALIZE            0x0400  /* Only a single load balancing instance */
1010 #define SD_ASYM_PACKING         0x0800  /* Place busy groups earlier in the domain */
1011 #define SD_PREFER_SIBLING       0x1000  /* Prefer to place tasks in a sibling domain */
1012 #define SD_OVERLAP              0x2000  /* sched_domains of this level overlap */
1013 #define SD_NUMA                 0x4000  /* cross-node balancing */
1014 #define SD_SHARE_CAP_STATES     0x8000  /* Domain members share capacity state */
1015
1016 #ifdef CONFIG_SCHED_SMT
1017 static inline int cpu_smt_flags(void)
1018 {
1019         return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
1020 }
1021 #endif
1022
1023 #ifdef CONFIG_SCHED_MC
1024 static inline int cpu_core_flags(void)
1025 {
1026         return SD_SHARE_PKG_RESOURCES;
1027 }
1028 #endif
1029
1030 #ifdef CONFIG_NUMA
1031 static inline int cpu_numa_flags(void)
1032 {
1033         return SD_NUMA;
1034 }
1035 #endif
1036
1037 struct sched_domain_attr {
1038         int relax_domain_level;
1039 };
1040
1041 #define SD_ATTR_INIT    (struct sched_domain_attr) {    \
1042         .relax_domain_level = -1,                       \
1043 }
1044
1045 extern int sched_domain_level_max;
1046
1047 struct capacity_state {
1048         unsigned long cap;      /* compute capacity */
1049         unsigned long power;    /* power consumption at this compute capacity */
1050 };
1051
1052 struct idle_state {
1053         unsigned long power;     /* power consumption in this idle state */
1054 };
1055
1056 struct sched_group_energy {
1057         unsigned int nr_idle_states;    /* number of idle states */
1058         struct idle_state *idle_states; /* ptr to idle state array */
1059         unsigned int nr_cap_states;     /* number of capacity states */
1060         struct capacity_state *cap_states; /* ptr to capacity state array */
1061 };
1062
1063 unsigned long capacity_curr_of(int cpu);
1064
1065 struct sched_group;
1066
1067 struct sched_domain {
1068         /* These fields must be setup */
1069         struct sched_domain *parent;    /* top domain must be null terminated */
1070         struct sched_domain *child;     /* bottom domain must be null terminated */
1071         struct sched_group *groups;     /* the balancing groups of the domain */
1072         unsigned long min_interval;     /* Minimum balance interval ms */
1073         unsigned long max_interval;     /* Maximum balance interval ms */
1074         unsigned int busy_factor;       /* less balancing by factor if busy */
1075         unsigned int imbalance_pct;     /* No balance until over watermark */
1076         unsigned int cache_nice_tries;  /* Leave cache hot tasks for # tries */
1077         unsigned int busy_idx;
1078         unsigned int idle_idx;
1079         unsigned int newidle_idx;
1080         unsigned int wake_idx;
1081         unsigned int forkexec_idx;
1082         unsigned int smt_gain;
1083
1084         int nohz_idle;                  /* NOHZ IDLE status */
1085         int flags;                      /* See SD_* */
1086         int level;
1087
1088         /* Runtime fields. */
1089         unsigned long last_balance;     /* init to jiffies. units in jiffies */
1090         unsigned int balance_interval;  /* initialise to 1. units in ms. */
1091         unsigned int nr_balance_failed; /* initialise to 0 */
1092
1093         /* idle_balance() stats */
1094         u64 max_newidle_lb_cost;
1095         unsigned long next_decay_max_lb_cost;
1096
1097 #ifdef CONFIG_SCHEDSTATS
1098         /* load_balance() stats */
1099         unsigned int lb_count[CPU_MAX_IDLE_TYPES];
1100         unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
1101         unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
1102         unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
1103         unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
1104         unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
1105         unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
1106         unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
1107
1108         /* Active load balancing */
1109         unsigned int alb_count;
1110         unsigned int alb_failed;
1111         unsigned int alb_pushed;
1112
1113         /* SD_BALANCE_EXEC stats */
1114         unsigned int sbe_count;
1115         unsigned int sbe_balanced;
1116         unsigned int sbe_pushed;
1117
1118         /* SD_BALANCE_FORK stats */
1119         unsigned int sbf_count;
1120         unsigned int sbf_balanced;
1121         unsigned int sbf_pushed;
1122
1123         /* try_to_wake_up() stats */
1124         unsigned int ttwu_wake_remote;
1125         unsigned int ttwu_move_affine;
1126         unsigned int ttwu_move_balance;
1127 #endif
1128 #ifdef CONFIG_SCHED_DEBUG
1129         char *name;
1130 #endif
1131         union {
1132                 void *private;          /* used during construction */
1133                 struct rcu_head rcu;    /* used during destruction */
1134         };
1135
1136         unsigned int span_weight;
1137         /*
1138          * Span of all CPUs in this domain.
1139          *
1140          * NOTE: this field is variable length. (Allocated dynamically
1141          * by attaching extra space to the end of the structure,
1142          * depending on how many CPUs the kernel has booted up with)
1143          */
1144         unsigned long span[0];
1145 };
1146
1147 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
1148 {
1149         return to_cpumask(sd->span);
1150 }
1151
1152 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
1153                                     struct sched_domain_attr *dattr_new);
1154
1155 /* Allocate an array of sched domains, for partition_sched_domains(). */
1156 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
1157 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
1158
1159 bool cpus_share_cache(int this_cpu, int that_cpu);
1160
1161 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
1162 typedef int (*sched_domain_flags_f)(void);
1163 typedef
1164 const struct sched_group_energy * const(*sched_domain_energy_f)(int cpu);
1165
1166 #define SDTL_OVERLAP    0x01
1167
1168 struct sd_data {
1169         struct sched_domain **__percpu sd;
1170         struct sched_group **__percpu sg;
1171         struct sched_group_capacity **__percpu sgc;
1172 };
1173
1174 struct sched_domain_topology_level {
1175         sched_domain_mask_f mask;
1176         sched_domain_flags_f sd_flags;
1177         sched_domain_energy_f energy;
1178         int                 flags;
1179         int                 numa_level;
1180         struct sd_data      data;
1181 #ifdef CONFIG_SCHED_DEBUG
1182         char                *name;
1183 #endif
1184 };
1185
1186 extern void set_sched_topology(struct sched_domain_topology_level *tl);
1187 extern void wake_up_if_idle(int cpu);
1188
1189 #ifdef CONFIG_SCHED_DEBUG
1190 # define SD_INIT_NAME(type)             .name = #type
1191 #else
1192 # define SD_INIT_NAME(type)
1193 #endif
1194
1195 #else /* CONFIG_SMP */
1196
1197 struct sched_domain_attr;
1198
1199 static inline void
1200 partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
1201                         struct sched_domain_attr *dattr_new)
1202 {
1203 }
1204
1205 static inline bool cpus_share_cache(int this_cpu, int that_cpu)
1206 {
1207         return true;
1208 }
1209
1210 #endif  /* !CONFIG_SMP */
1211
1212
1213 struct io_context;                      /* See blkdev.h */
1214
1215
1216 #ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
1217 extern void prefetch_stack(struct task_struct *t);
1218 #else
1219 static inline void prefetch_stack(struct task_struct *t) { }
1220 #endif
1221
1222 struct audit_context;           /* See audit.c */
1223 struct mempolicy;
1224 struct pipe_inode_info;
1225 struct uts_namespace;
1226
1227 struct load_weight {
1228         unsigned long weight;
1229         u32 inv_weight;
1230 };
1231
1232 /*
1233  * The load_avg/util_avg accumulates an infinite geometric series.
1234  * 1) load_avg factors frequency scaling into the amount of time that a
1235  * sched_entity is runnable on a rq into its weight. For cfs_rq, it is the
1236  * aggregated such weights of all runnable and blocked sched_entities.
1237  * 2) util_avg factors frequency and cpu scaling into the amount of time
1238  * that a sched_entity is running on a CPU, in the range [0..SCHED_LOAD_SCALE].
1239  * For cfs_rq, it is the aggregated such times of all runnable and
1240  * blocked sched_entities.
1241  * The 64 bit load_sum can:
1242  * 1) for cfs_rq, afford 4353082796 (=2^64/47742/88761) entities with
1243  * the highest weight (=88761) always runnable, we should not overflow
1244  * 2) for entity, support any load.weight always runnable
1245  */
1246 struct sched_avg {
1247         u64 last_update_time, load_sum;
1248         u32 util_sum, period_contrib;
1249         unsigned long load_avg, util_avg;
1250 };
1251
1252 #ifdef CONFIG_SCHEDSTATS
1253 struct sched_statistics {
1254         u64                     wait_start;
1255         u64                     wait_max;
1256         u64                     wait_count;
1257         u64                     wait_sum;
1258         u64                     iowait_count;
1259         u64                     iowait_sum;
1260
1261         u64                     sleep_start;
1262         u64                     sleep_max;
1263         s64                     sum_sleep_runtime;
1264
1265         u64                     block_start;
1266         u64                     block_max;
1267         u64                     exec_max;
1268         u64                     slice_max;
1269
1270         u64                     nr_migrations_cold;
1271         u64                     nr_failed_migrations_affine;
1272         u64                     nr_failed_migrations_running;
1273         u64                     nr_failed_migrations_hot;
1274         u64                     nr_forced_migrations;
1275
1276         u64                     nr_wakeups;
1277         u64                     nr_wakeups_sync;
1278         u64                     nr_wakeups_migrate;
1279         u64                     nr_wakeups_local;
1280         u64                     nr_wakeups_remote;
1281         u64                     nr_wakeups_affine;
1282         u64                     nr_wakeups_affine_attempts;
1283         u64                     nr_wakeups_passive;
1284         u64                     nr_wakeups_idle;
1285 };
1286 #endif
1287
1288 #ifdef CONFIG_SCHED_WALT
1289 #define RAVG_HIST_SIZE_MAX  5
1290
1291 /* ravg represents frequency scaled cpu-demand of tasks */
1292 struct ravg {
1293         /*
1294          * 'mark_start' marks the beginning of an event (task waking up, task
1295          * starting to execute, task being preempted) within a window
1296          *
1297          * 'sum' represents how runnable a task has been within current
1298          * window. It incorporates both running time and wait time and is
1299          * frequency scaled.
1300          *
1301          * 'sum_history' keeps track of history of 'sum' seen over previous
1302          * RAVG_HIST_SIZE windows. Windows where task was entirely sleeping are
1303          * ignored.
1304          *
1305          * 'demand' represents maximum sum seen over previous
1306          * sysctl_sched_ravg_hist_size windows. 'demand' could drive frequency
1307          * demand for tasks.
1308          *
1309          * 'curr_window' represents task's contribution to cpu busy time
1310          * statistics (rq->curr_runnable_sum) in current window
1311          *
1312          * 'prev_window' represents task's contribution to cpu busy time
1313          * statistics (rq->prev_runnable_sum) in previous window
1314          */
1315         u64 mark_start;
1316         u32 sum, demand;
1317         u32 sum_history[RAVG_HIST_SIZE_MAX];
1318         u32 curr_window, prev_window;
1319         u16 active_windows;
1320 };
1321 #endif
1322
1323 struct sched_entity {
1324         struct load_weight      load;           /* for load-balancing */
1325         struct rb_node          run_node;
1326         struct list_head        group_node;
1327         unsigned int            on_rq;
1328
1329         u64                     exec_start;
1330         u64                     sum_exec_runtime;
1331         u64                     vruntime;
1332         u64                     prev_sum_exec_runtime;
1333
1334         u64                     nr_migrations;
1335
1336 #ifdef CONFIG_SCHEDSTATS
1337         struct sched_statistics statistics;
1338 #endif
1339
1340 #ifdef CONFIG_FAIR_GROUP_SCHED
1341         int                     depth;
1342         struct sched_entity     *parent;
1343         /* rq on which this entity is (to be) queued: */
1344         struct cfs_rq           *cfs_rq;
1345         /* rq "owned" by this entity/group: */
1346         struct cfs_rq           *my_q;
1347 #endif
1348
1349 #ifdef CONFIG_SMP
1350         /* Per entity load average tracking */
1351         struct sched_avg        avg;
1352 #endif
1353 };
1354
1355 struct sched_rt_entity {
1356         struct list_head run_list;
1357         unsigned long timeout;
1358         unsigned long watchdog_stamp;
1359         unsigned int time_slice;
1360
1361         struct sched_rt_entity *back;
1362 #ifdef CONFIG_RT_GROUP_SCHED
1363         struct sched_rt_entity  *parent;
1364         /* rq on which this entity is (to be) queued: */
1365         struct rt_rq            *rt_rq;
1366         /* rq "owned" by this entity/group: */
1367         struct rt_rq            *my_q;
1368 #endif
1369 };
1370
1371 struct sched_dl_entity {
1372         struct rb_node  rb_node;
1373
1374         /*
1375          * Original scheduling parameters. Copied here from sched_attr
1376          * during sched_setattr(), they will remain the same until
1377          * the next sched_setattr().
1378          */
1379         u64 dl_runtime;         /* maximum runtime for each instance    */
1380         u64 dl_deadline;        /* relative deadline of each instance   */
1381         u64 dl_period;          /* separation of two instances (period) */
1382         u64 dl_bw;              /* dl_runtime / dl_deadline             */
1383
1384         /*
1385          * Actual scheduling parameters. Initialized with the values above,
1386          * they are continously updated during task execution. Note that
1387          * the remaining runtime could be < 0 in case we are in overrun.
1388          */
1389         s64 runtime;            /* remaining runtime for this instance  */
1390         u64 deadline;           /* absolute deadline for this instance  */
1391         unsigned int flags;     /* specifying the scheduler behaviour   */
1392
1393         /*
1394          * Some bool flags:
1395          *
1396          * @dl_throttled tells if we exhausted the runtime. If so, the
1397          * task has to wait for a replenishment to be performed at the
1398          * next firing of dl_timer.
1399          *
1400          * @dl_new tells if a new instance arrived. If so we must
1401          * start executing it with full runtime and reset its absolute
1402          * deadline;
1403          *
1404          * @dl_boosted tells if we are boosted due to DI. If so we are
1405          * outside bandwidth enforcement mechanism (but only until we
1406          * exit the critical section);
1407          *
1408          * @dl_yielded tells if task gave up the cpu before consuming
1409          * all its available runtime during the last job.
1410          */
1411         int dl_throttled, dl_new, dl_boosted, dl_yielded;
1412
1413         /*
1414          * Bandwidth enforcement timer. Each -deadline task has its
1415          * own bandwidth to be enforced, thus we need one timer per task.
1416          */
1417         struct hrtimer dl_timer;
1418 };
1419
1420 union rcu_special {
1421         struct {
1422                 u8 blocked;
1423                 u8 need_qs;
1424                 u8 exp_need_qs;
1425                 u8 pad; /* Otherwise the compiler can store garbage here. */
1426         } b; /* Bits. */
1427         u32 s; /* Set of bits. */
1428 };
1429 struct rcu_node;
1430
1431 enum perf_event_task_context {
1432         perf_invalid_context = -1,
1433         perf_hw_context = 0,
1434         perf_sw_context,
1435         perf_nr_task_contexts,
1436 };
1437
1438 /* Track pages that require TLB flushes */
1439 struct tlbflush_unmap_batch {
1440         /*
1441          * Each bit set is a CPU that potentially has a TLB entry for one of
1442          * the PFNs being flushed. See set_tlb_ubc_flush_pending().
1443          */
1444         struct cpumask cpumask;
1445
1446         /* True if any bit in cpumask is set */
1447         bool flush_required;
1448
1449         /*
1450          * If true then the PTE was dirty when unmapped. The entry must be
1451          * flushed before IO is initiated or a stale TLB entry potentially
1452          * allows an update without redirtying the page.
1453          */
1454         bool writable;
1455 };
1456
1457 struct task_struct {
1458         volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
1459         void *stack;
1460         atomic_t usage;
1461         unsigned int flags;     /* per process flags, defined below */
1462         unsigned int ptrace;
1463
1464 #ifdef CONFIG_SMP
1465         struct llist_node wake_entry;
1466         int on_cpu;
1467         unsigned int wakee_flips;
1468         unsigned long wakee_flip_decay_ts;
1469         struct task_struct *last_wakee;
1470
1471         int wake_cpu;
1472 #endif
1473         int on_rq;
1474
1475         int prio, static_prio, normal_prio;
1476         unsigned int rt_priority;
1477         const struct sched_class *sched_class;
1478         struct sched_entity se;
1479         struct sched_rt_entity rt;
1480 #ifdef CONFIG_SCHED_WALT
1481         struct ravg ravg;
1482         /*
1483          * 'init_load_pct' represents the initial task load assigned to children
1484          * of this task
1485          */
1486         u32 init_load_pct;
1487 #endif
1488
1489 #ifdef CONFIG_CGROUP_SCHED
1490         struct task_group *sched_task_group;
1491 #endif
1492         struct sched_dl_entity dl;
1493
1494 #ifdef CONFIG_PREEMPT_NOTIFIERS
1495         /* list of struct preempt_notifier: */
1496         struct hlist_head preempt_notifiers;
1497 #endif
1498
1499 #ifdef CONFIG_BLK_DEV_IO_TRACE
1500         unsigned int btrace_seq;
1501 #endif
1502
1503         unsigned int policy;
1504         int nr_cpus_allowed;
1505         cpumask_t cpus_allowed;
1506
1507 #ifdef CONFIG_PREEMPT_RCU
1508         int rcu_read_lock_nesting;
1509         union rcu_special rcu_read_unlock_special;
1510         struct list_head rcu_node_entry;
1511         struct rcu_node *rcu_blocked_node;
1512 #endif /* #ifdef CONFIG_PREEMPT_RCU */
1513 #ifdef CONFIG_TASKS_RCU
1514         unsigned long rcu_tasks_nvcsw;
1515         bool rcu_tasks_holdout;
1516         struct list_head rcu_tasks_holdout_list;
1517         int rcu_tasks_idle_cpu;
1518 #endif /* #ifdef CONFIG_TASKS_RCU */
1519
1520 #ifdef CONFIG_SCHED_INFO
1521         struct sched_info sched_info;
1522 #endif
1523
1524         struct list_head tasks;
1525 #ifdef CONFIG_SMP
1526         struct plist_node pushable_tasks;
1527         struct rb_node pushable_dl_tasks;
1528 #endif
1529
1530         struct mm_struct *mm, *active_mm;
1531         /* per-thread vma caching */
1532         u32 vmacache_seqnum;
1533         struct vm_area_struct *vmacache[VMACACHE_SIZE];
1534 #if defined(SPLIT_RSS_COUNTING)
1535         struct task_rss_stat    rss_stat;
1536 #endif
1537 /* task state */
1538         int exit_state;
1539         int exit_code, exit_signal;
1540         int pdeath_signal;  /*  The signal sent when the parent dies  */
1541         unsigned long jobctl;   /* JOBCTL_*, siglock protected */
1542
1543         /* Used for emulating ABI behavior of previous Linux versions */
1544         unsigned int personality;
1545
1546         /* scheduler bits, serialized by scheduler locks */
1547         unsigned sched_reset_on_fork:1;
1548         unsigned sched_contributes_to_load:1;
1549         unsigned sched_migrated:1;
1550         unsigned :0; /* force alignment to the next boundary */
1551
1552         /* unserialized, strictly 'current' */
1553         unsigned in_execve:1; /* bit to tell LSMs we're in execve */
1554         unsigned in_iowait:1;
1555 #ifdef CONFIG_MEMCG
1556         unsigned memcg_may_oom:1;
1557 #endif
1558 #ifdef CONFIG_MEMCG_KMEM
1559         unsigned memcg_kmem_skip_account:1;
1560 #endif
1561 #ifdef CONFIG_COMPAT_BRK
1562         unsigned brk_randomized:1;
1563 #endif
1564
1565         unsigned long atomic_flags; /* Flags needing atomic access. */
1566
1567         struct restart_block restart_block;
1568
1569         pid_t pid;
1570         pid_t tgid;
1571
1572 #ifdef CONFIG_CC_STACKPROTECTOR
1573         /* Canary value for the -fstack-protector gcc feature */
1574         unsigned long stack_canary;
1575 #endif
1576         /*
1577          * pointers to (original) parent process, youngest child, younger sibling,
1578          * older sibling, respectively.  (p->father can be replaced with
1579          * p->real_parent->pid)
1580          */
1581         struct task_struct __rcu *real_parent; /* real parent process */
1582         struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
1583         /*
1584          * children/sibling forms the list of my natural children
1585          */
1586         struct list_head children;      /* list of my children */
1587         struct list_head sibling;       /* linkage in my parent's children list */
1588         struct task_struct *group_leader;       /* threadgroup leader */
1589
1590         /*
1591          * ptraced is the list of tasks this task is using ptrace on.
1592          * This includes both natural children and PTRACE_ATTACH targets.
1593          * p->ptrace_entry is p's link on the p->parent->ptraced list.
1594          */
1595         struct list_head ptraced;
1596         struct list_head ptrace_entry;
1597
1598         /* PID/PID hash table linkage. */
1599         struct pid_link pids[PIDTYPE_MAX];
1600         struct list_head thread_group;
1601         struct list_head thread_node;
1602
1603         struct completion *vfork_done;          /* for vfork() */
1604         int __user *set_child_tid;              /* CLONE_CHILD_SETTID */
1605         int __user *clear_child_tid;            /* CLONE_CHILD_CLEARTID */
1606
1607         cputime_t utime, stime, utimescaled, stimescaled;
1608         cputime_t gtime;
1609         struct prev_cputime prev_cputime;
1610 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1611         seqlock_t vtime_seqlock;
1612         unsigned long long vtime_snap;
1613         enum {
1614                 VTIME_SLEEPING = 0,
1615                 VTIME_USER,
1616                 VTIME_SYS,
1617         } vtime_snap_whence;
1618 #endif
1619         unsigned long nvcsw, nivcsw; /* context switch counts */
1620         u64 start_time;         /* monotonic time in nsec */
1621         u64 real_start_time;    /* boot based time in nsec */
1622 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
1623         unsigned long min_flt, maj_flt;
1624
1625         struct task_cputime cputime_expires;
1626         struct list_head cpu_timers[3];
1627
1628 /* process credentials */
1629         const struct cred __rcu *real_cred; /* objective and real subjective task
1630                                          * credentials (COW) */
1631         const struct cred __rcu *cred;  /* effective (overridable) subjective task
1632                                          * credentials (COW) */
1633         char comm[TASK_COMM_LEN]; /* executable name excluding path
1634                                      - access with [gs]et_task_comm (which lock
1635                                        it with task_lock())
1636                                      - initialized normally by setup_new_exec */
1637 /* file system info */
1638         struct nameidata *nameidata;
1639 #ifdef CONFIG_SYSVIPC
1640 /* ipc stuff */
1641         struct sysv_sem sysvsem;
1642         struct sysv_shm sysvshm;
1643 #endif
1644 #ifdef CONFIG_DETECT_HUNG_TASK
1645 /* hung task detection */
1646         unsigned long last_switch_count;
1647 #endif
1648 /* filesystem information */
1649         struct fs_struct *fs;
1650 /* open file information */
1651         struct files_struct *files;
1652 /* namespaces */
1653         struct nsproxy *nsproxy;
1654 /* signal handlers */
1655         struct signal_struct *signal;
1656         struct sighand_struct *sighand;
1657
1658         sigset_t blocked, real_blocked;
1659         sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
1660         struct sigpending pending;
1661
1662         unsigned long sas_ss_sp;
1663         size_t sas_ss_size;
1664
1665         struct callback_head *task_works;
1666
1667         struct audit_context *audit_context;
1668 #ifdef CONFIG_AUDITSYSCALL
1669         kuid_t loginuid;
1670         unsigned int sessionid;
1671 #endif
1672         struct seccomp seccomp;
1673
1674 /* Thread group tracking */
1675         u32 parent_exec_id;
1676         u32 self_exec_id;
1677 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
1678  * mempolicy */
1679         spinlock_t alloc_lock;
1680
1681         /* Protection of the PI data structures: */
1682         raw_spinlock_t pi_lock;
1683
1684         struct wake_q_node wake_q;
1685
1686 #ifdef CONFIG_RT_MUTEXES
1687         /* PI waiters blocked on a rt_mutex held by this task */
1688         struct rb_root pi_waiters;
1689         struct rb_node *pi_waiters_leftmost;
1690         /* Deadlock detection and priority inheritance handling */
1691         struct rt_mutex_waiter *pi_blocked_on;
1692 #endif
1693
1694 #ifdef CONFIG_DEBUG_MUTEXES
1695         /* mutex deadlock detection */
1696         struct mutex_waiter *blocked_on;
1697 #endif
1698 #ifdef CONFIG_TRACE_IRQFLAGS
1699         unsigned int irq_events;
1700         unsigned long hardirq_enable_ip;
1701         unsigned long hardirq_disable_ip;
1702         unsigned int hardirq_enable_event;
1703         unsigned int hardirq_disable_event;
1704         int hardirqs_enabled;
1705         int hardirq_context;
1706         unsigned long softirq_disable_ip;
1707         unsigned long softirq_enable_ip;
1708         unsigned int softirq_disable_event;
1709         unsigned int softirq_enable_event;
1710         int softirqs_enabled;
1711         int softirq_context;
1712 #endif
1713 #ifdef CONFIG_LOCKDEP
1714 # define MAX_LOCK_DEPTH 48UL
1715         u64 curr_chain_key;
1716         int lockdep_depth;
1717         unsigned int lockdep_recursion;
1718         struct held_lock held_locks[MAX_LOCK_DEPTH];
1719         gfp_t lockdep_reclaim_gfp;
1720 #endif
1721
1722 /* journalling filesystem info */
1723         void *journal_info;
1724
1725 /* stacked block device info */
1726         struct bio_list *bio_list;
1727
1728 #ifdef CONFIG_BLOCK
1729 /* stack plugging */
1730         struct blk_plug *plug;
1731 #endif
1732
1733 /* VM state */
1734         struct reclaim_state *reclaim_state;
1735
1736         struct backing_dev_info *backing_dev_info;
1737
1738         struct io_context *io_context;
1739
1740         unsigned long ptrace_message;
1741         siginfo_t *last_siginfo; /* For ptrace use.  */
1742         struct task_io_accounting ioac;
1743 #if defined(CONFIG_TASK_XACCT)
1744         u64 acct_rss_mem1;      /* accumulated rss usage */
1745         u64 acct_vm_mem1;       /* accumulated virtual memory usage */
1746         cputime_t acct_timexpd; /* stime + utime since last update */
1747 #endif
1748 #ifdef CONFIG_CPUSETS
1749         nodemask_t mems_allowed;        /* Protected by alloc_lock */
1750         seqcount_t mems_allowed_seq;    /* Seqence no to catch updates */
1751         int cpuset_mem_spread_rotor;
1752         int cpuset_slab_spread_rotor;
1753 #endif
1754 #ifdef CONFIG_CGROUPS
1755         /* Control Group info protected by css_set_lock */
1756         struct css_set __rcu *cgroups;
1757         /* cg_list protected by css_set_lock and tsk->alloc_lock */
1758         struct list_head cg_list;
1759 #endif
1760 #ifdef CONFIG_FUTEX
1761         struct robust_list_head __user *robust_list;
1762 #ifdef CONFIG_COMPAT
1763         struct compat_robust_list_head __user *compat_robust_list;
1764 #endif
1765         struct list_head pi_state_list;
1766         struct futex_pi_state *pi_state_cache;
1767 #endif
1768 #ifdef CONFIG_PERF_EVENTS
1769         struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1770         struct mutex perf_event_mutex;
1771         struct list_head perf_event_list;
1772 #endif
1773 #ifdef CONFIG_DEBUG_PREEMPT
1774         unsigned long preempt_disable_ip;
1775 #endif
1776 #ifdef CONFIG_NUMA
1777         struct mempolicy *mempolicy;    /* Protected by alloc_lock */
1778         short il_next;
1779         short pref_node_fork;
1780 #endif
1781 #ifdef CONFIG_NUMA_BALANCING
1782         int numa_scan_seq;
1783         unsigned int numa_scan_period;
1784         unsigned int numa_scan_period_max;
1785         int numa_preferred_nid;
1786         unsigned long numa_migrate_retry;
1787         u64 node_stamp;                 /* migration stamp  */
1788         u64 last_task_numa_placement;
1789         u64 last_sum_exec_runtime;
1790         struct callback_head numa_work;
1791
1792         struct list_head numa_entry;
1793         struct numa_group *numa_group;
1794
1795         /*
1796          * numa_faults is an array split into four regions:
1797          * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1798          * in this precise order.
1799          *
1800          * faults_memory: Exponential decaying average of faults on a per-node
1801          * basis. Scheduling placement decisions are made based on these
1802          * counts. The values remain static for the duration of a PTE scan.
1803          * faults_cpu: Track the nodes the process was running on when a NUMA
1804          * hinting fault was incurred.
1805          * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1806          * during the current scan window. When the scan completes, the counts
1807          * in faults_memory and faults_cpu decay and these values are copied.
1808          */
1809         unsigned long *numa_faults;
1810         unsigned long total_numa_faults;
1811
1812         /*
1813          * numa_faults_locality tracks if faults recorded during the last
1814          * scan window were remote/local or failed to migrate. The task scan
1815          * period is adapted based on the locality of the faults with different
1816          * weights depending on whether they were shared or private faults
1817          */
1818         unsigned long numa_faults_locality[3];
1819
1820         unsigned long numa_pages_migrated;
1821 #endif /* CONFIG_NUMA_BALANCING */
1822
1823 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1824         struct tlbflush_unmap_batch tlb_ubc;
1825 #endif
1826
1827         struct rcu_head rcu;
1828
1829         /*
1830          * cache last used pipe for splice
1831          */
1832         struct pipe_inode_info *splice_pipe;
1833
1834         struct page_frag task_frag;
1835
1836 #ifdef  CONFIG_TASK_DELAY_ACCT
1837         struct task_delay_info *delays;
1838 #endif
1839 #ifdef CONFIG_FAULT_INJECTION
1840         int make_it_fail;
1841 #endif
1842         /*
1843          * when (nr_dirtied >= nr_dirtied_pause), it's time to call
1844          * balance_dirty_pages() for some dirty throttling pause
1845          */
1846         int nr_dirtied;
1847         int nr_dirtied_pause;
1848         unsigned long dirty_paused_when; /* start of a write-and-pause period */
1849
1850 #ifdef CONFIG_LATENCYTOP
1851         int latency_record_count;
1852         struct latency_record latency_record[LT_SAVECOUNT];
1853 #endif
1854         /*
1855          * time slack values; these are used to round up poll() and
1856          * select() etc timeout values. These are in nanoseconds.
1857          */
1858         u64 timer_slack_ns;
1859         u64 default_timer_slack_ns;
1860
1861 #ifdef CONFIG_KASAN
1862         unsigned int kasan_depth;
1863 #endif
1864 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1865         /* Index of current stored address in ret_stack */
1866         int curr_ret_stack;
1867         /* Stack of return addresses for return function tracing */
1868         struct ftrace_ret_stack *ret_stack;
1869         /* time stamp for last schedule */
1870         unsigned long long ftrace_timestamp;
1871         /*
1872          * Number of functions that haven't been traced
1873          * because of depth overrun.
1874          */
1875         atomic_t trace_overrun;
1876         /* Pause for the tracing */
1877         atomic_t tracing_graph_pause;
1878 #endif
1879 #ifdef CONFIG_TRACING
1880         /* state flags for use by tracers */
1881         unsigned long trace;
1882         /* bitmask and counter of trace recursion */
1883         unsigned long trace_recursion;
1884 #endif /* CONFIG_TRACING */
1885 #ifdef CONFIG_MEMCG
1886         struct mem_cgroup *memcg_in_oom;
1887         gfp_t memcg_oom_gfp_mask;
1888         int memcg_oom_order;
1889
1890         /* number of pages to reclaim on returning to userland */
1891         unsigned int memcg_nr_pages_over_high;
1892 #endif
1893 #ifdef CONFIG_UPROBES
1894         struct uprobe_task *utask;
1895 #endif
1896 #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1897         unsigned int    sequential_io;
1898         unsigned int    sequential_io_avg;
1899 #endif
1900 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1901         unsigned long   task_state_change;
1902 #endif
1903         int pagefault_disabled;
1904 /* CPU-specific state of this task */
1905         struct thread_struct thread;
1906 /*
1907  * WARNING: on x86, 'thread_struct' contains a variable-sized
1908  * structure.  It *MUST* be at the end of 'task_struct'.
1909  *
1910  * Do not put anything below here!
1911  */
1912 };
1913
1914 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
1915 extern int arch_task_struct_size __read_mostly;
1916 #else
1917 # define arch_task_struct_size (sizeof(struct task_struct))
1918 #endif
1919
1920 /* Future-safe accessor for struct task_struct's cpus_allowed. */
1921 #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
1922
1923 #define TNF_MIGRATED    0x01
1924 #define TNF_NO_GROUP    0x02
1925 #define TNF_SHARED      0x04
1926 #define TNF_FAULT_LOCAL 0x08
1927 #define TNF_MIGRATE_FAIL 0x10
1928
1929 #ifdef CONFIG_NUMA_BALANCING
1930 extern void task_numa_fault(int last_node, int node, int pages, int flags);
1931 extern pid_t task_numa_group_id(struct task_struct *p);
1932 extern void set_numabalancing_state(bool enabled);
1933 extern void task_numa_free(struct task_struct *p);
1934 extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
1935                                         int src_nid, int dst_cpu);
1936 #else
1937 static inline void task_numa_fault(int last_node, int node, int pages,
1938                                    int flags)
1939 {
1940 }
1941 static inline pid_t task_numa_group_id(struct task_struct *p)
1942 {
1943         return 0;
1944 }
1945 static inline void set_numabalancing_state(bool enabled)
1946 {
1947 }
1948 static inline void task_numa_free(struct task_struct *p)
1949 {
1950 }
1951 static inline bool should_numa_migrate_memory(struct task_struct *p,
1952                                 struct page *page, int src_nid, int dst_cpu)
1953 {
1954         return true;
1955 }
1956 #endif
1957
1958 static inline struct pid *task_pid(struct task_struct *task)
1959 {
1960         return task->pids[PIDTYPE_PID].pid;
1961 }
1962
1963 static inline struct pid *task_tgid(struct task_struct *task)
1964 {
1965         return task->group_leader->pids[PIDTYPE_PID].pid;
1966 }
1967
1968 /*
1969  * Without tasklist or rcu lock it is not safe to dereference
1970  * the result of task_pgrp/task_session even if task == current,
1971  * we can race with another thread doing sys_setsid/sys_setpgid.
1972  */
1973 static inline struct pid *task_pgrp(struct task_struct *task)
1974 {
1975         return task->group_leader->pids[PIDTYPE_PGID].pid;
1976 }
1977
1978 static inline struct pid *task_session(struct task_struct *task)
1979 {
1980         return task->group_leader->pids[PIDTYPE_SID].pid;
1981 }
1982
1983 struct pid_namespace;
1984
1985 /*
1986  * the helpers to get the task's different pids as they are seen
1987  * from various namespaces
1988  *
1989  * task_xid_nr()     : global id, i.e. the id seen from the init namespace;
1990  * task_xid_vnr()    : virtual id, i.e. the id seen from the pid namespace of
1991  *                     current.
1992  * task_xid_nr_ns()  : id seen from the ns specified;
1993  *
1994  * set_task_vxid()   : assigns a virtual id to a task;
1995  *
1996  * see also pid_nr() etc in include/linux/pid.h
1997  */
1998 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
1999                         struct pid_namespace *ns);
2000
2001 static inline pid_t task_pid_nr(struct task_struct *tsk)
2002 {
2003         return tsk->pid;
2004 }
2005
2006 static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
2007                                         struct pid_namespace *ns)
2008 {
2009         return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
2010 }
2011
2012 static inline pid_t task_pid_vnr(struct task_struct *tsk)
2013 {
2014         return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
2015 }
2016
2017
2018 static inline pid_t task_tgid_nr(struct task_struct *tsk)
2019 {
2020         return tsk->tgid;
2021 }
2022
2023 pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
2024
2025 static inline pid_t task_tgid_vnr(struct task_struct *tsk)
2026 {
2027         return pid_vnr(task_tgid(tsk));
2028 }
2029
2030
2031 static inline int pid_alive(const struct task_struct *p);
2032 static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
2033 {
2034         pid_t pid = 0;
2035
2036         rcu_read_lock();
2037         if (pid_alive(tsk))
2038                 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
2039         rcu_read_unlock();
2040
2041         return pid;
2042 }
2043
2044 static inline pid_t task_ppid_nr(const struct task_struct *tsk)
2045 {
2046         return task_ppid_nr_ns(tsk, &init_pid_ns);
2047 }
2048
2049 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
2050                                         struct pid_namespace *ns)
2051 {
2052         return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
2053 }
2054
2055 static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
2056 {
2057         return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
2058 }
2059
2060
2061 static inline pid_t task_session_nr_ns(struct task_struct *tsk,
2062                                         struct pid_namespace *ns)
2063 {
2064         return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
2065 }
2066
2067 static inline pid_t task_session_vnr(struct task_struct *tsk)
2068 {
2069         return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
2070 }
2071
2072 /* obsolete, do not use */
2073 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
2074 {
2075         return task_pgrp_nr_ns(tsk, &init_pid_ns);
2076 }
2077
2078 /**
2079  * pid_alive - check that a task structure is not stale
2080  * @p: Task structure to be checked.
2081  *
2082  * Test if a process is not yet dead (at most zombie state)
2083  * If pid_alive fails, then pointers within the task structure
2084  * can be stale and must not be dereferenced.
2085  *
2086  * Return: 1 if the process is alive. 0 otherwise.
2087  */
2088 static inline int pid_alive(const struct task_struct *p)
2089 {
2090         return p->pids[PIDTYPE_PID].pid != NULL;
2091 }
2092
2093 /**
2094  * is_global_init - check if a task structure is init. Since init
2095  * is free to have sub-threads we need to check tgid.
2096  * @tsk: Task structure to be checked.
2097  *
2098  * Check if a task structure is the first user space task the kernel created.
2099  *
2100  * Return: 1 if the task structure is init. 0 otherwise.
2101  */
2102 static inline int is_global_init(struct task_struct *tsk)
2103 {
2104         return task_tgid_nr(tsk) == 1;
2105 }
2106
2107 extern struct pid *cad_pid;
2108
2109 extern void free_task(struct task_struct *tsk);
2110 #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
2111
2112 extern void __put_task_struct(struct task_struct *t);
2113
2114 static inline void put_task_struct(struct task_struct *t)
2115 {
2116         if (atomic_dec_and_test(&t->usage))
2117                 __put_task_struct(t);
2118 }
2119
2120 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
2121 extern void task_cputime(struct task_struct *t,
2122                          cputime_t *utime, cputime_t *stime);
2123 extern void task_cputime_scaled(struct task_struct *t,
2124                                 cputime_t *utimescaled, cputime_t *stimescaled);
2125 extern cputime_t task_gtime(struct task_struct *t);
2126 #else
2127 static inline void task_cputime(struct task_struct *t,
2128                                 cputime_t *utime, cputime_t *stime)
2129 {
2130         if (utime)
2131                 *utime = t->utime;
2132         if (stime)
2133                 *stime = t->stime;
2134 }
2135
2136 static inline void task_cputime_scaled(struct task_struct *t,
2137                                        cputime_t *utimescaled,
2138                                        cputime_t *stimescaled)
2139 {
2140         if (utimescaled)
2141                 *utimescaled = t->utimescaled;
2142         if (stimescaled)
2143                 *stimescaled = t->stimescaled;
2144 }
2145
2146 static inline cputime_t task_gtime(struct task_struct *t)
2147 {
2148         return t->gtime;
2149 }
2150 #endif
2151 extern void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
2152 extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
2153
2154 /*
2155  * Per process flags
2156  */
2157 #define PF_EXITING      0x00000004      /* getting shut down */
2158 #define PF_EXITPIDONE   0x00000008      /* pi exit done on shut down */
2159 #define PF_VCPU         0x00000010      /* I'm a virtual CPU */
2160 #define PF_WQ_WORKER    0x00000020      /* I'm a workqueue worker */
2161 #define PF_FORKNOEXEC   0x00000040      /* forked but didn't exec */
2162 #define PF_MCE_PROCESS  0x00000080      /* process policy on mce errors */
2163 #define PF_SUPERPRIV    0x00000100      /* used super-user privileges */
2164 #define PF_DUMPCORE     0x00000200      /* dumped core */
2165 #define PF_SIGNALED     0x00000400      /* killed by a signal */
2166 #define PF_MEMALLOC     0x00000800      /* Allocating memory */
2167 #define PF_NPROC_EXCEEDED 0x00001000    /* set_user noticed that RLIMIT_NPROC was exceeded */
2168 #define PF_USED_MATH    0x00002000      /* if unset the fpu must be initialized before use */
2169 #define PF_USED_ASYNC   0x00004000      /* used async_schedule*(), used by module init */
2170 #define PF_NOFREEZE     0x00008000      /* this thread should not be frozen */
2171 #define PF_FROZEN       0x00010000      /* frozen for system suspend */
2172 #define PF_FSTRANS      0x00020000      /* inside a filesystem transaction */
2173 #define PF_KSWAPD       0x00040000      /* I am kswapd */
2174 #define PF_MEMALLOC_NOIO 0x00080000     /* Allocating memory without IO involved */
2175 #define PF_LESS_THROTTLE 0x00100000     /* Throttle me less: I clean memory */
2176 #define PF_KTHREAD      0x00200000      /* I am a kernel thread */
2177 #define PF_RANDOMIZE    0x00400000      /* randomize virtual address space */
2178 #define PF_SWAPWRITE    0x00800000      /* Allowed to write to swap */
2179 #define PF_NO_SETAFFINITY 0x04000000    /* Userland is not allowed to meddle with cpus_allowed */
2180 #define PF_MCE_EARLY    0x08000000      /* Early kill for mce process policy */
2181 #define PF_MUTEX_TESTER 0x20000000      /* Thread belongs to the rt mutex tester */
2182 #define PF_FREEZER_SKIP 0x40000000      /* Freezer should not count it as freezable */
2183 #define PF_SUSPEND_TASK 0x80000000      /* this thread called freeze_processes and should not be frozen */
2184
2185 /*
2186  * Only the _current_ task can read/write to tsk->flags, but other
2187  * tasks can access tsk->flags in readonly mode for example
2188  * with tsk_used_math (like during threaded core dumping).
2189  * There is however an exception to this rule during ptrace
2190  * or during fork: the ptracer task is allowed to write to the
2191  * child->flags of its traced child (same goes for fork, the parent
2192  * can write to the child->flags), because we're guaranteed the
2193  * child is not running and in turn not changing child->flags
2194  * at the same time the parent does it.
2195  */
2196 #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
2197 #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
2198 #define clear_used_math() clear_stopped_child_used_math(current)
2199 #define set_used_math() set_stopped_child_used_math(current)
2200 #define conditional_stopped_child_used_math(condition, child) \
2201         do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
2202 #define conditional_used_math(condition) \
2203         conditional_stopped_child_used_math(condition, current)
2204 #define copy_to_stopped_child_used_math(child) \
2205         do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
2206 /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
2207 #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
2208 #define used_math() tsk_used_math(current)
2209
2210 /* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
2211  * __GFP_FS is also cleared as it implies __GFP_IO.
2212  */
2213 static inline gfp_t memalloc_noio_flags(gfp_t flags)
2214 {
2215         if (unlikely(current->flags & PF_MEMALLOC_NOIO))
2216                 flags &= ~(__GFP_IO | __GFP_FS);
2217         return flags;
2218 }
2219
2220 static inline unsigned int memalloc_noio_save(void)
2221 {
2222         unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
2223         current->flags |= PF_MEMALLOC_NOIO;
2224         return flags;
2225 }
2226
2227 static inline void memalloc_noio_restore(unsigned int flags)
2228 {
2229         current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
2230 }
2231
2232 /* Per-process atomic flags. */
2233 #define PFA_NO_NEW_PRIVS 0      /* May not gain new privileges. */
2234 #define PFA_SPREAD_PAGE  1      /* Spread page cache over cpuset */
2235 #define PFA_SPREAD_SLAB  2      /* Spread some slab caches over cpuset */
2236
2237
2238 #define TASK_PFA_TEST(name, func)                                       \
2239         static inline bool task_##func(struct task_struct *p)           \
2240         { return test_bit(PFA_##name, &p->atomic_flags); }
2241 #define TASK_PFA_SET(name, func)                                        \
2242         static inline void task_set_##func(struct task_struct *p)       \
2243         { set_bit(PFA_##name, &p->atomic_flags); }
2244 #define TASK_PFA_CLEAR(name, func)                                      \
2245         static inline void task_clear_##func(struct task_struct *p)     \
2246         { clear_bit(PFA_##name, &p->atomic_flags); }
2247
2248 TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
2249 TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
2250
2251 TASK_PFA_TEST(SPREAD_PAGE, spread_page)
2252 TASK_PFA_SET(SPREAD_PAGE, spread_page)
2253 TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
2254
2255 TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
2256 TASK_PFA_SET(SPREAD_SLAB, spread_slab)
2257 TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
2258
2259 /*
2260  * task->jobctl flags
2261  */
2262 #define JOBCTL_STOP_SIGMASK     0xffff  /* signr of the last group stop */
2263
2264 #define JOBCTL_STOP_DEQUEUED_BIT 16     /* stop signal dequeued */
2265 #define JOBCTL_STOP_PENDING_BIT 17      /* task should stop for group stop */
2266 #define JOBCTL_STOP_CONSUME_BIT 18      /* consume group stop count */
2267 #define JOBCTL_TRAP_STOP_BIT    19      /* trap for STOP */
2268 #define JOBCTL_TRAP_NOTIFY_BIT  20      /* trap for NOTIFY */
2269 #define JOBCTL_TRAPPING_BIT     21      /* switching to TRACED */
2270 #define JOBCTL_LISTENING_BIT    22      /* ptracer is listening for events */
2271
2272 #define JOBCTL_STOP_DEQUEUED    (1UL << JOBCTL_STOP_DEQUEUED_BIT)
2273 #define JOBCTL_STOP_PENDING     (1UL << JOBCTL_STOP_PENDING_BIT)
2274 #define JOBCTL_STOP_CONSUME     (1UL << JOBCTL_STOP_CONSUME_BIT)
2275 #define JOBCTL_TRAP_STOP        (1UL << JOBCTL_TRAP_STOP_BIT)
2276 #define JOBCTL_TRAP_NOTIFY      (1UL << JOBCTL_TRAP_NOTIFY_BIT)
2277 #define JOBCTL_TRAPPING         (1UL << JOBCTL_TRAPPING_BIT)
2278 #define JOBCTL_LISTENING        (1UL << JOBCTL_LISTENING_BIT)
2279
2280 #define JOBCTL_TRAP_MASK        (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
2281 #define JOBCTL_PENDING_MASK     (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
2282
2283 extern bool task_set_jobctl_pending(struct task_struct *task,
2284                                     unsigned long mask);
2285 extern void task_clear_jobctl_trapping(struct task_struct *task);
2286 extern void task_clear_jobctl_pending(struct task_struct *task,
2287                                       unsigned long mask);
2288
2289 static inline void rcu_copy_process(struct task_struct *p)
2290 {
2291 #ifdef CONFIG_PREEMPT_RCU
2292         p->rcu_read_lock_nesting = 0;
2293         p->rcu_read_unlock_special.s = 0;
2294         p->rcu_blocked_node = NULL;
2295         INIT_LIST_HEAD(&p->rcu_node_entry);
2296 #endif /* #ifdef CONFIG_PREEMPT_RCU */
2297 #ifdef CONFIG_TASKS_RCU
2298         p->rcu_tasks_holdout = false;
2299         INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
2300         p->rcu_tasks_idle_cpu = -1;
2301 #endif /* #ifdef CONFIG_TASKS_RCU */
2302 }
2303
2304 static inline void tsk_restore_flags(struct task_struct *task,
2305                                 unsigned long orig_flags, unsigned long flags)
2306 {
2307         task->flags &= ~flags;
2308         task->flags |= orig_flags & flags;
2309 }
2310
2311 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
2312                                      const struct cpumask *trial);
2313 extern int task_can_attach(struct task_struct *p,
2314                            const struct cpumask *cs_cpus_allowed);
2315 #ifdef CONFIG_SMP
2316 extern void do_set_cpus_allowed(struct task_struct *p,
2317                                const struct cpumask *new_mask);
2318
2319 extern int set_cpus_allowed_ptr(struct task_struct *p,
2320                                 const struct cpumask *new_mask);
2321 #else
2322 static inline void do_set_cpus_allowed(struct task_struct *p,
2323                                       const struct cpumask *new_mask)
2324 {
2325 }
2326 static inline int set_cpus_allowed_ptr(struct task_struct *p,
2327                                        const struct cpumask *new_mask)
2328 {
2329         if (!cpumask_test_cpu(0, new_mask))
2330                 return -EINVAL;
2331         return 0;
2332 }
2333 #endif
2334
2335 #ifdef CONFIG_NO_HZ_COMMON
2336 void calc_load_enter_idle(void);
2337 void calc_load_exit_idle(void);
2338 #else
2339 static inline void calc_load_enter_idle(void) { }
2340 static inline void calc_load_exit_idle(void) { }
2341 #endif /* CONFIG_NO_HZ_COMMON */
2342
2343 /*
2344  * Do not use outside of architecture code which knows its limitations.
2345  *
2346  * sched_clock() has no promise of monotonicity or bounded drift between
2347  * CPUs, use (which you should not) requires disabling IRQs.
2348  *
2349  * Please use one of the three interfaces below.
2350  */
2351 extern unsigned long long notrace sched_clock(void);
2352 /*
2353  * See the comment in kernel/sched/clock.c
2354  */
2355 extern u64 cpu_clock(int cpu);
2356 extern u64 local_clock(void);
2357 extern u64 running_clock(void);
2358 extern u64 sched_clock_cpu(int cpu);
2359
2360
2361 extern void sched_clock_init(void);
2362
2363 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2364 static inline void sched_clock_tick(void)
2365 {
2366 }
2367
2368 static inline void sched_clock_idle_sleep_event(void)
2369 {
2370 }
2371
2372 static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
2373 {
2374 }
2375 #else
2376 /*
2377  * Architectures can set this to 1 if they have specified
2378  * CONFIG_HAVE_UNSTABLE_SCHED_CLOCK in their arch Kconfig,
2379  * but then during bootup it turns out that sched_clock()
2380  * is reliable after all:
2381  */
2382 extern int sched_clock_stable(void);
2383 extern void set_sched_clock_stable(void);
2384 extern void clear_sched_clock_stable(void);
2385
2386 extern void sched_clock_tick(void);
2387 extern void sched_clock_idle_sleep_event(void);
2388 extern void sched_clock_idle_wakeup_event(u64 delta_ns);
2389 #endif
2390
2391 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
2392 /*
2393  * An i/f to runtime opt-in for irq time accounting based off of sched_clock.
2394  * The reason for this explicit opt-in is not to have perf penalty with
2395  * slow sched_clocks.
2396  */
2397 extern void enable_sched_clock_irqtime(void);
2398 extern void disable_sched_clock_irqtime(void);
2399 #else
2400 static inline void enable_sched_clock_irqtime(void) {}
2401 static inline void disable_sched_clock_irqtime(void) {}
2402 #endif
2403
2404 extern unsigned long long
2405 task_sched_runtime(struct task_struct *task);
2406
2407 /* sched_exec is called by processes performing an exec */
2408 #ifdef CONFIG_SMP
2409 extern void sched_exec(void);
2410 #else
2411 #define sched_exec()   {}
2412 #endif
2413
2414 extern void sched_clock_idle_sleep_event(void);
2415 extern void sched_clock_idle_wakeup_event(u64 delta_ns);
2416
2417 #ifdef CONFIG_HOTPLUG_CPU
2418 extern void idle_task_exit(void);
2419 #else
2420 static inline void idle_task_exit(void) {}
2421 #endif
2422
2423 #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
2424 extern void wake_up_nohz_cpu(int cpu);
2425 #else
2426 static inline void wake_up_nohz_cpu(int cpu) { }
2427 #endif
2428
2429 #ifdef CONFIG_NO_HZ_FULL
2430 extern bool sched_can_stop_tick(void);
2431 extern u64 scheduler_tick_max_deferment(void);
2432 #else
2433 static inline bool sched_can_stop_tick(void) { return false; }
2434 #endif
2435
2436 #ifdef CONFIG_SCHED_AUTOGROUP
2437 extern void sched_autogroup_create_attach(struct task_struct *p);
2438 extern void sched_autogroup_detach(struct task_struct *p);
2439 extern void sched_autogroup_fork(struct signal_struct *sig);
2440 extern void sched_autogroup_exit(struct signal_struct *sig);
2441 #ifdef CONFIG_PROC_FS
2442 extern void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m);
2443 extern int proc_sched_autogroup_set_nice(struct task_struct *p, int nice);
2444 #endif
2445 #else
2446 static inline void sched_autogroup_create_attach(struct task_struct *p) { }
2447 static inline void sched_autogroup_detach(struct task_struct *p) { }
2448 static inline void sched_autogroup_fork(struct signal_struct *sig) { }
2449 static inline void sched_autogroup_exit(struct signal_struct *sig) { }
2450 #endif
2451
2452 extern int yield_to(struct task_struct *p, bool preempt);
2453 extern void set_user_nice(struct task_struct *p, long nice);
2454 extern int task_prio(const struct task_struct *p);
2455 /**
2456  * task_nice - return the nice value of a given task.
2457  * @p: the task in question.
2458  *
2459  * Return: The nice value [ -20 ... 0 ... 19 ].
2460  */
2461 static inline int task_nice(const struct task_struct *p)
2462 {
2463         return PRIO_TO_NICE((p)->static_prio);
2464 }
2465 extern int can_nice(const struct task_struct *p, const int nice);
2466 extern int task_curr(const struct task_struct *p);
2467 extern int idle_cpu(int cpu);
2468 extern int sched_setscheduler(struct task_struct *, int,
2469                               const struct sched_param *);
2470 extern int sched_setscheduler_nocheck(struct task_struct *, int,
2471                                       const struct sched_param *);
2472 extern int sched_setattr(struct task_struct *,
2473                          const struct sched_attr *);
2474 extern struct task_struct *idle_task(int cpu);
2475 /**
2476  * is_idle_task - is the specified task an idle task?
2477  * @p: the task in question.
2478  *
2479  * Return: 1 if @p is an idle task. 0 otherwise.
2480  */
2481 static inline bool is_idle_task(const struct task_struct *p)
2482 {
2483         return p->pid == 0;
2484 }
2485 extern struct task_struct *curr_task(int cpu);
2486 extern void set_curr_task(int cpu, struct task_struct *p);
2487
2488 void yield(void);
2489
2490 union thread_union {
2491         struct thread_info thread_info;
2492         unsigned long stack[THREAD_SIZE/sizeof(long)];
2493 };
2494
2495 #ifndef __HAVE_ARCH_KSTACK_END
2496 static inline int kstack_end(void *addr)
2497 {
2498         /* Reliable end of stack detection:
2499          * Some APM bios versions misalign the stack
2500          */
2501         return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
2502 }
2503 #endif
2504
2505 extern union thread_union init_thread_union;
2506 extern struct task_struct init_task;
2507
2508 extern struct   mm_struct init_mm;
2509
2510 extern struct pid_namespace init_pid_ns;
2511
2512 /*
2513  * find a task by one of its numerical ids
2514  *
2515  * find_task_by_pid_ns():
2516  *      finds a task by its pid in the specified namespace
2517  * find_task_by_vpid():
2518  *      finds a task by its virtual pid
2519  *
2520  * see also find_vpid() etc in include/linux/pid.h
2521  */
2522
2523 extern struct task_struct *find_task_by_vpid(pid_t nr);
2524 extern struct task_struct *find_task_by_pid_ns(pid_t nr,
2525                 struct pid_namespace *ns);
2526
2527 /* per-UID process charging. */
2528 extern struct user_struct * alloc_uid(kuid_t);
2529 static inline struct user_struct *get_uid(struct user_struct *u)
2530 {
2531         atomic_inc(&u->__count);
2532         return u;
2533 }
2534 extern void free_uid(struct user_struct *);
2535
2536 #include <asm/current.h>
2537
2538 extern void xtime_update(unsigned long ticks);
2539
2540 extern int wake_up_state(struct task_struct *tsk, unsigned int state);
2541 extern int wake_up_process(struct task_struct *tsk);
2542 extern void wake_up_new_task(struct task_struct *tsk);
2543 #ifdef CONFIG_SMP
2544  extern void kick_process(struct task_struct *tsk);
2545 #else
2546  static inline void kick_process(struct task_struct *tsk) { }
2547 #endif
2548 extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
2549 extern void sched_dead(struct task_struct *p);
2550
2551 extern void proc_caches_init(void);
2552 extern void flush_signals(struct task_struct *);
2553 extern void ignore_signals(struct task_struct *);
2554 extern void flush_signal_handlers(struct task_struct *, int force_default);
2555 extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
2556
2557 static inline int kernel_dequeue_signal(siginfo_t *info)
2558 {
2559         struct task_struct *tsk = current;
2560         siginfo_t __info;
2561         int ret;
2562
2563         spin_lock_irq(&tsk->sighand->siglock);
2564         ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
2565         spin_unlock_irq(&tsk->sighand->siglock);
2566
2567         return ret;
2568 }
2569
2570 static inline void kernel_signal_stop(void)
2571 {
2572         spin_lock_irq(&current->sighand->siglock);
2573         if (current->jobctl & JOBCTL_STOP_DEQUEUED)
2574                 __set_current_state(TASK_STOPPED);
2575         spin_unlock_irq(&current->sighand->siglock);
2576
2577         schedule();
2578 }
2579
2580 extern void release_task(struct task_struct * p);
2581 extern int send_sig_info(int, struct siginfo *, struct task_struct *);
2582 extern int force_sigsegv(int, struct task_struct *);
2583 extern int force_sig_info(int, struct siginfo *, struct task_struct *);
2584 extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
2585 extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
2586 extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
2587                                 const struct cred *, u32);
2588 extern int kill_pgrp(struct pid *pid, int sig, int priv);
2589 extern int kill_pid(struct pid *pid, int sig, int priv);
2590 extern int kill_proc_info(int, struct siginfo *, pid_t);
2591 extern __must_check bool do_notify_parent(struct task_struct *, int);
2592 extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
2593 extern void force_sig(int, struct task_struct *);
2594 extern int send_sig(int, struct task_struct *, int);
2595 extern int zap_other_threads(struct task_struct *p);
2596 extern struct sigqueue *sigqueue_alloc(void);
2597 extern void sigqueue_free(struct sigqueue *);
2598 extern int send_sigqueue(struct sigqueue *,  struct task_struct *, int group);
2599 extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
2600
2601 static inline void restore_saved_sigmask(void)
2602 {
2603         if (test_and_clear_restore_sigmask())
2604                 __set_current_blocked(&current->saved_sigmask);
2605 }
2606
2607 static inline sigset_t *sigmask_to_save(void)
2608 {
2609         sigset_t *res = &current->blocked;
2610         if (unlikely(test_restore_sigmask()))
2611                 res = &current->saved_sigmask;
2612         return res;
2613 }
2614
2615 static inline int kill_cad_pid(int sig, int priv)
2616 {
2617         return kill_pid(cad_pid, sig, priv);
2618 }
2619
2620 /* These can be the second arg to send_sig_info/send_group_sig_info.  */
2621 #define SEND_SIG_NOINFO ((struct siginfo *) 0)
2622 #define SEND_SIG_PRIV   ((struct siginfo *) 1)
2623 #define SEND_SIG_FORCED ((struct siginfo *) 2)
2624
2625 /*
2626  * True if we are on the alternate signal stack.
2627  */
2628 static inline int on_sig_stack(unsigned long sp)
2629 {
2630 #ifdef CONFIG_STACK_GROWSUP
2631         return sp >= current->sas_ss_sp &&
2632                 sp - current->sas_ss_sp < current->sas_ss_size;
2633 #else
2634         return sp > current->sas_ss_sp &&
2635                 sp - current->sas_ss_sp <= current->sas_ss_size;
2636 #endif
2637 }
2638
2639 static inline int sas_ss_flags(unsigned long sp)
2640 {
2641         if (!current->sas_ss_size)
2642                 return SS_DISABLE;
2643
2644         return on_sig_stack(sp) ? SS_ONSTACK : 0;
2645 }
2646
2647 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
2648 {
2649         if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
2650 #ifdef CONFIG_STACK_GROWSUP
2651                 return current->sas_ss_sp;
2652 #else
2653                 return current->sas_ss_sp + current->sas_ss_size;
2654 #endif
2655         return sp;
2656 }
2657
2658 /*
2659  * Routines for handling mm_structs
2660  */
2661 extern struct mm_struct * mm_alloc(void);
2662
2663 /* mmdrop drops the mm and the page tables */
2664 extern void __mmdrop(struct mm_struct *);
2665 static inline void mmdrop(struct mm_struct * mm)
2666 {
2667         if (unlikely(atomic_dec_and_test(&mm->mm_count)))
2668                 __mmdrop(mm);
2669 }
2670
2671 /* mmput gets rid of the mappings and all user-space */
2672 extern void mmput(struct mm_struct *);
2673 /* Grab a reference to a task's mm, if it is not already going away */
2674 extern struct mm_struct *get_task_mm(struct task_struct *task);
2675 /*
2676  * Grab a reference to a task's mm, if it is not already going away
2677  * and ptrace_may_access with the mode parameter passed to it
2678  * succeeds.
2679  */
2680 extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
2681 /* Remove the current tasks stale references to the old mm_struct */
2682 extern void mm_release(struct task_struct *, struct mm_struct *);
2683
2684 #ifdef CONFIG_HAVE_COPY_THREAD_TLS
2685 extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
2686                         struct task_struct *, unsigned long);
2687 #else
2688 extern int copy_thread(unsigned long, unsigned long, unsigned long,
2689                         struct task_struct *);
2690
2691 /* Architectures that haven't opted into copy_thread_tls get the tls argument
2692  * via pt_regs, so ignore the tls argument passed via C. */
2693 static inline int copy_thread_tls(
2694                 unsigned long clone_flags, unsigned long sp, unsigned long arg,
2695                 struct task_struct *p, unsigned long tls)
2696 {
2697         return copy_thread(clone_flags, sp, arg, p);
2698 }
2699 #endif
2700 extern void flush_thread(void);
2701 extern void exit_thread(void);
2702
2703 extern void exit_files(struct task_struct *);
2704 extern void __cleanup_sighand(struct sighand_struct *);
2705
2706 extern void exit_itimers(struct signal_struct *);
2707 extern void flush_itimer_signals(void);
2708
2709 extern void do_group_exit(int);
2710
2711 extern int do_execve(struct filename *,
2712                      const char __user * const __user *,
2713                      const char __user * const __user *);
2714 extern int do_execveat(int, struct filename *,
2715                        const char __user * const __user *,
2716                        const char __user * const __user *,
2717                        int);
2718 extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
2719 extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
2720 struct task_struct *fork_idle(int);
2721 extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
2722
2723 extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
2724 static inline void set_task_comm(struct task_struct *tsk, const char *from)
2725 {
2726         __set_task_comm(tsk, from, false);
2727 }
2728 extern char *get_task_comm(char *to, struct task_struct *tsk);
2729
2730 #ifdef CONFIG_SMP
2731 void scheduler_ipi(void);
2732 extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
2733 #else
2734 static inline void scheduler_ipi(void) { }
2735 static inline unsigned long wait_task_inactive(struct task_struct *p,
2736                                                long match_state)
2737 {
2738         return 1;
2739 }
2740 #endif
2741
2742 #define tasklist_empty() \
2743         list_empty(&init_task.tasks)
2744
2745 #define next_task(p) \
2746         list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
2747
2748 #define for_each_process(p) \
2749         for (p = &init_task ; (p = next_task(p)) != &init_task ; )
2750
2751 extern bool current_is_single_threaded(void);
2752
2753 /*
2754  * Careful: do_each_thread/while_each_thread is a double loop so
2755  *          'break' will not work as expected - use goto instead.
2756  */
2757 #define do_each_thread(g, t) \
2758         for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
2759
2760 #define while_each_thread(g, t) \
2761         while ((t = next_thread(t)) != g)
2762
2763 #define __for_each_thread(signal, t)    \
2764         list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
2765
2766 #define for_each_thread(p, t)           \
2767         __for_each_thread((p)->signal, t)
2768
2769 /* Careful: this is a double loop, 'break' won't work as expected. */
2770 #define for_each_process_thread(p, t)   \
2771         for_each_process(p) for_each_thread(p, t)
2772
2773 static inline int get_nr_threads(struct task_struct *tsk)
2774 {
2775         return tsk->signal->nr_threads;
2776 }
2777
2778 static inline bool thread_group_leader(struct task_struct *p)
2779 {
2780         return p->exit_signal >= 0;
2781 }
2782
2783 /* Do to the insanities of de_thread it is possible for a process
2784  * to have the pid of the thread group leader without actually being
2785  * the thread group leader.  For iteration through the pids in proc
2786  * all we care about is that we have a task with the appropriate
2787  * pid, we don't actually care if we have the right task.
2788  */
2789 static inline bool has_group_leader_pid(struct task_struct *p)
2790 {
2791         return task_pid(p) == p->signal->leader_pid;
2792 }
2793
2794 static inline
2795 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
2796 {
2797         return p1->signal == p2->signal;
2798 }
2799
2800 static inline struct task_struct *next_thread(const struct task_struct *p)
2801 {
2802         return list_entry_rcu(p->thread_group.next,
2803                               struct task_struct, thread_group);
2804 }
2805
2806 static inline int thread_group_empty(struct task_struct *p)
2807 {
2808         return list_empty(&p->thread_group);
2809 }
2810
2811 #define delay_group_leader(p) \
2812                 (thread_group_leader(p) && !thread_group_empty(p))
2813
2814 /*
2815  * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
2816  * subscriptions and synchronises with wait4().  Also used in procfs.  Also
2817  * pins the final release of task.io_context.  Also protects ->cpuset and
2818  * ->cgroup.subsys[]. And ->vfork_done.
2819  *
2820  * Nests both inside and outside of read_lock(&tasklist_lock).
2821  * It must not be nested with write_lock_irq(&tasklist_lock),
2822  * neither inside nor outside.
2823  */
2824 static inline void task_lock(struct task_struct *p)
2825 {
2826         spin_lock(&p->alloc_lock);
2827 }
2828
2829 static inline void task_unlock(struct task_struct *p)
2830 {
2831         spin_unlock(&p->alloc_lock);
2832 }
2833
2834 extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
2835                                                         unsigned long *flags);
2836
2837 static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
2838                                                        unsigned long *flags)
2839 {
2840         struct sighand_struct *ret;
2841
2842         ret = __lock_task_sighand(tsk, flags);
2843         (void)__cond_lock(&tsk->sighand->siglock, ret);
2844         return ret;
2845 }
2846
2847 static inline void unlock_task_sighand(struct task_struct *tsk,
2848                                                 unsigned long *flags)
2849 {
2850         spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
2851 }
2852
2853 /**
2854  * threadgroup_change_begin - mark the beginning of changes to a threadgroup
2855  * @tsk: task causing the changes
2856  *
2857  * All operations which modify a threadgroup - a new thread joining the
2858  * group, death of a member thread (the assertion of PF_EXITING) and
2859  * exec(2) dethreading the process and replacing the leader - are wrapped
2860  * by threadgroup_change_{begin|end}().  This is to provide a place which
2861  * subsystems needing threadgroup stability can hook into for
2862  * synchronization.
2863  */
2864 static inline void threadgroup_change_begin(struct task_struct *tsk)
2865 {
2866         might_sleep();
2867         cgroup_threadgroup_change_begin(tsk);
2868 }
2869
2870 /**
2871  * threadgroup_change_end - mark the end of changes to a threadgroup
2872  * @tsk: task causing the changes
2873  *
2874  * See threadgroup_change_begin().
2875  */
2876 static inline void threadgroup_change_end(struct task_struct *tsk)
2877 {
2878         cgroup_threadgroup_change_end(tsk);
2879 }
2880
2881 #ifndef __HAVE_THREAD_FUNCTIONS
2882
2883 #define task_thread_info(task)  ((struct thread_info *)(task)->stack)
2884 #define task_stack_page(task)   ((task)->stack)
2885
2886 static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
2887 {
2888         *task_thread_info(p) = *task_thread_info(org);
2889         task_thread_info(p)->task = p;
2890 }
2891
2892 /*
2893  * Return the address of the last usable long on the stack.
2894  *
2895  * When the stack grows down, this is just above the thread
2896  * info struct. Going any lower will corrupt the threadinfo.
2897  *
2898  * When the stack grows up, this is the highest address.
2899  * Beyond that position, we corrupt data on the next page.
2900  */
2901 static inline unsigned long *end_of_stack(struct task_struct *p)
2902 {
2903 #ifdef CONFIG_STACK_GROWSUP
2904         return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
2905 #else
2906         return (unsigned long *)(task_thread_info(p) + 1);
2907 #endif
2908 }
2909
2910 #endif
2911 #define task_stack_end_corrupted(task) \
2912                 (*(end_of_stack(task)) != STACK_END_MAGIC)
2913
2914 static inline int object_is_on_stack(void *obj)
2915 {
2916         void *stack = task_stack_page(current);
2917
2918         return (obj >= stack) && (obj < (stack + THREAD_SIZE));
2919 }
2920
2921 extern void thread_info_cache_init(void);
2922
2923 #ifdef CONFIG_DEBUG_STACK_USAGE
2924 static inline unsigned long stack_not_used(struct task_struct *p)
2925 {
2926         unsigned long *n = end_of_stack(p);
2927
2928         do {    /* Skip over canary */
2929                 n++;
2930         } while (!*n);
2931
2932         return (unsigned long)n - (unsigned long)end_of_stack(p);
2933 }
2934 #endif
2935 extern void set_task_stack_end_magic(struct task_struct *tsk);
2936
2937 /* set thread flags in other task's structures
2938  * - see asm/thread_info.h for TIF_xxxx flags available
2939  */
2940 static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
2941 {
2942         set_ti_thread_flag(task_thread_info(tsk), flag);
2943 }
2944
2945 static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
2946 {
2947         clear_ti_thread_flag(task_thread_info(tsk), flag);
2948 }
2949
2950 static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
2951 {
2952         return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
2953 }
2954
2955 static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
2956 {
2957         return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
2958 }
2959
2960 static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
2961 {
2962         return test_ti_thread_flag(task_thread_info(tsk), flag);
2963 }
2964
2965 static inline void set_tsk_need_resched(struct task_struct *tsk)
2966 {
2967         set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
2968 }
2969
2970 static inline void clear_tsk_need_resched(struct task_struct *tsk)
2971 {
2972         clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
2973 }
2974
2975 static inline int test_tsk_need_resched(struct task_struct *tsk)
2976 {
2977         return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
2978 }
2979
2980 static inline int restart_syscall(void)
2981 {
2982         set_tsk_thread_flag(current, TIF_SIGPENDING);
2983         return -ERESTARTNOINTR;
2984 }
2985
2986 static inline int signal_pending(struct task_struct *p)
2987 {
2988         return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
2989 }
2990
2991 static inline int __fatal_signal_pending(struct task_struct *p)
2992 {
2993         return unlikely(sigismember(&p->pending.signal, SIGKILL));
2994 }
2995
2996 static inline int fatal_signal_pending(struct task_struct *p)
2997 {
2998         return signal_pending(p) && __fatal_signal_pending(p);
2999 }
3000
3001 static inline int signal_pending_state(long state, struct task_struct *p)
3002 {
3003         if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
3004                 return 0;
3005         if (!signal_pending(p))
3006                 return 0;
3007
3008         return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
3009 }
3010
3011 /*
3012  * cond_resched() and cond_resched_lock(): latency reduction via
3013  * explicit rescheduling in places that are safe. The return
3014  * value indicates whether a reschedule was done in fact.
3015  * cond_resched_lock() will drop the spinlock before scheduling,
3016  * cond_resched_softirq() will enable bhs before scheduling.
3017  */
3018 extern int _cond_resched(void);
3019
3020 #define cond_resched() ({                       \
3021         ___might_sleep(__FILE__, __LINE__, 0);  \
3022         _cond_resched();                        \
3023 })
3024
3025 extern int __cond_resched_lock(spinlock_t *lock);
3026
3027 #define cond_resched_lock(lock) ({                              \
3028         ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
3029         __cond_resched_lock(lock);                              \
3030 })
3031
3032 extern int __cond_resched_softirq(void);
3033
3034 #define cond_resched_softirq() ({                                       \
3035         ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET);     \
3036         __cond_resched_softirq();                                       \
3037 })
3038
3039 static inline void cond_resched_rcu(void)
3040 {
3041 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
3042         rcu_read_unlock();
3043         cond_resched();
3044         rcu_read_lock();
3045 #endif
3046 }
3047
3048 /*
3049  * Does a critical section need to be broken due to another
3050  * task waiting?: (technically does not depend on CONFIG_PREEMPT,
3051  * but a general need for low latency)
3052  */
3053 static inline int spin_needbreak(spinlock_t *lock)
3054 {
3055 #ifdef CONFIG_PREEMPT
3056         return spin_is_contended(lock);
3057 #else
3058         return 0;
3059 #endif
3060 }
3061
3062 /*
3063  * Idle thread specific functions to determine the need_resched
3064  * polling state.
3065  */
3066 #ifdef TIF_POLLING_NRFLAG
3067 static inline int tsk_is_polling(struct task_struct *p)
3068 {
3069         return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG);
3070 }
3071
3072 static inline void __current_set_polling(void)
3073 {
3074         set_thread_flag(TIF_POLLING_NRFLAG);
3075 }
3076
3077 static inline bool __must_check current_set_polling_and_test(void)
3078 {
3079         __current_set_polling();
3080
3081         /*
3082          * Polling state must be visible before we test NEED_RESCHED,
3083          * paired by resched_curr()
3084          */
3085         smp_mb__after_atomic();
3086
3087         return unlikely(tif_need_resched());
3088 }
3089
3090 static inline void __current_clr_polling(void)
3091 {
3092         clear_thread_flag(TIF_POLLING_NRFLAG);
3093 }
3094
3095 static inline bool __must_check current_clr_polling_and_test(void)
3096 {
3097         __current_clr_polling();
3098
3099         /*
3100          * Polling state must be visible before we test NEED_RESCHED,
3101          * paired by resched_curr()
3102          */
3103         smp_mb__after_atomic();
3104
3105         return unlikely(tif_need_resched());
3106 }
3107
3108 #else
3109 static inline int tsk_is_polling(struct task_struct *p) { return 0; }
3110 static inline void __current_set_polling(void) { }
3111 static inline void __current_clr_polling(void) { }
3112
3113 static inline bool __must_check current_set_polling_and_test(void)
3114 {
3115         return unlikely(tif_need_resched());
3116 }
3117 static inline bool __must_check current_clr_polling_and_test(void)
3118 {
3119         return unlikely(tif_need_resched());
3120 }
3121 #endif
3122
3123 static inline void current_clr_polling(void)
3124 {
3125         __current_clr_polling();
3126
3127         /*
3128          * Ensure we check TIF_NEED_RESCHED after we clear the polling bit.
3129          * Once the bit is cleared, we'll get IPIs with every new
3130          * TIF_NEED_RESCHED and the IPI handler, scheduler_ipi(), will also
3131          * fold.
3132          */
3133         smp_mb(); /* paired with resched_curr() */
3134
3135         preempt_fold_need_resched();
3136 }
3137
3138 static __always_inline bool need_resched(void)
3139 {
3140         return unlikely(tif_need_resched());
3141 }
3142
3143 /*
3144  * Thread group CPU time accounting.
3145  */
3146 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
3147 void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
3148
3149 /*
3150  * Reevaluate whether the task has signals pending delivery.
3151  * Wake the task if so.
3152  * This is required every time the blocked sigset_t changes.
3153  * callers must hold sighand->siglock.
3154  */
3155 extern void recalc_sigpending_and_wake(struct task_struct *t);
3156 extern void recalc_sigpending(void);
3157
3158 extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
3159
3160 static inline void signal_wake_up(struct task_struct *t, bool resume)
3161 {
3162         signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
3163 }
3164 static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
3165 {
3166         signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
3167 }
3168
3169 /*
3170  * Wrappers for p->thread_info->cpu access. No-op on UP.
3171  */
3172 #ifdef CONFIG_SMP
3173
3174 static inline unsigned int task_cpu(const struct task_struct *p)
3175 {
3176         return task_thread_info(p)->cpu;
3177 }
3178
3179 static inline int task_node(const struct task_struct *p)
3180 {
3181         return cpu_to_node(task_cpu(p));
3182 }
3183
3184 extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
3185
3186 #else
3187
3188 static inline unsigned int task_cpu(const struct task_struct *p)
3189 {
3190         return 0;
3191 }
3192
3193 static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
3194 {
3195 }
3196
3197 #endif /* CONFIG_SMP */
3198
3199 extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
3200 extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
3201
3202 #ifdef CONFIG_CGROUP_SCHED
3203 extern struct task_group root_task_group;
3204 #endif /* CONFIG_CGROUP_SCHED */
3205
3206 extern int task_can_switch_user(struct user_struct *up,
3207                                         struct task_struct *tsk);
3208
3209 #ifdef CONFIG_TASK_XACCT
3210 static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
3211 {
3212         tsk->ioac.rchar += amt;
3213 }
3214
3215 static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
3216 {
3217         tsk->ioac.wchar += amt;
3218 }
3219
3220 static inline void inc_syscr(struct task_struct *tsk)
3221 {
3222         tsk->ioac.syscr++;
3223 }
3224
3225 static inline void inc_syscw(struct task_struct *tsk)
3226 {
3227         tsk->ioac.syscw++;
3228 }
3229 #else
3230 static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
3231 {
3232 }
3233
3234 static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
3235 {
3236 }
3237
3238 static inline void inc_syscr(struct task_struct *tsk)
3239 {
3240 }
3241
3242 static inline void inc_syscw(struct task_struct *tsk)
3243 {
3244 }
3245 #endif
3246
3247 #ifndef TASK_SIZE_OF
3248 #define TASK_SIZE_OF(tsk)       TASK_SIZE
3249 #endif
3250
3251 #ifdef CONFIG_MEMCG
3252 extern void mm_update_next_owner(struct mm_struct *mm);
3253 #else
3254 static inline void mm_update_next_owner(struct mm_struct *mm)
3255 {
3256 }
3257 #endif /* CONFIG_MEMCG */
3258
3259 static inline unsigned long task_rlimit(const struct task_struct *tsk,
3260                 unsigned int limit)
3261 {
3262         return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
3263 }
3264
3265 static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
3266                 unsigned int limit)
3267 {
3268         return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
3269 }
3270
3271 static inline unsigned long rlimit(unsigned int limit)
3272 {
3273         return task_rlimit(current, limit);
3274 }
3275
3276 static inline unsigned long rlimit_max(unsigned int limit)
3277 {
3278         return task_rlimit_max(current, limit);
3279 }
3280
3281 #endif