4 #include <uapi/linux/sched.h>
6 #include <linux/sched/prio.h>
8 #include <linux/capability.h>
9 #include <linux/mutex.h>
10 #include <linux/plist.h>
11 #include <linux/mm_types.h>
12 #include <asm/ptrace.h>
14 #include <linux/sem.h>
15 #include <linux/shm.h>
16 #include <linux/signal.h>
17 #include <linux/signal_types.h>
18 #include <linux/pid.h>
19 #include <linux/seccomp.h>
20 #include <linux/rculist.h>
21 #include <linux/rtmutex.h>
23 #include <linux/resource.h>
24 #include <linux/hrtimer.h>
25 #include <linux/kcov.h>
26 #include <linux/task_io_accounting.h>
27 #include <linux/latencytop.h>
28 #include <linux/cred.h>
29 #include <linux/gfp.h>
30 #include <linux/topology.h>
31 #include <linux/magic.h>
32 #include <linux/cgroup-defs.h>
37 struct futex_pi_state;
38 struct robust_list_head;
41 struct perf_event_context;
47 struct sighand_struct;
49 extern void dump_cpu_task(int cpu);
54 #ifdef CONFIG_SCHED_DEBUG
55 extern void proc_sched_show_task(struct task_struct *p, struct seq_file *m);
56 extern void proc_sched_set_task(struct task_struct *p);
60 * Task state bitmask. NOTE! These bits are also
61 * encoded in fs/proc/array.c: get_task_state().
63 * We have two separate sets of flags: task->state
64 * is about runnability, while task->exit_state are
65 * about the task exiting. Confusing, but this way
66 * modifying one set can't modify the other one by
69 #define TASK_RUNNING 0
70 #define TASK_INTERRUPTIBLE 1
71 #define TASK_UNINTERRUPTIBLE 2
72 #define __TASK_STOPPED 4
73 #define __TASK_TRACED 8
74 /* in tsk->exit_state */
76 #define EXIT_ZOMBIE 32
77 #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
78 /* in tsk->state again */
80 #define TASK_WAKEKILL 128
81 #define TASK_WAKING 256
82 #define TASK_PARKED 512
83 #define TASK_NOLOAD 1024
85 #define TASK_STATE_MAX 4096
87 #define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
89 /* Convenience macros for the sake of set_current_state */
90 #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
91 #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
92 #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
94 #define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
96 /* Convenience macros for the sake of wake_up */
97 #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
98 #define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
100 /* get_task_state() */
101 #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
102 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
103 __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
105 #define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
106 #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
107 #define task_is_stopped_or_traced(task) \
108 ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
109 #define task_contributes_to_load(task) \
110 ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
111 (task->flags & PF_FROZEN) == 0 && \
112 (task->state & TASK_NOLOAD) == 0)
114 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
116 #define __set_current_state(state_value) \
118 current->task_state_change = _THIS_IP_; \
119 current->state = (state_value); \
121 #define set_current_state(state_value) \
123 current->task_state_change = _THIS_IP_; \
124 smp_store_mb(current->state, (state_value)); \
129 * set_current_state() includes a barrier so that the write of current->state
130 * is correctly serialised wrt the caller's subsequent test of whether to
134 * set_current_state(TASK_UNINTERRUPTIBLE);
140 * __set_current_state(TASK_RUNNING);
142 * If the caller does not need such serialisation (because, for instance, the
143 * condition test and condition change and wakeup are under the same lock) then
144 * use __set_current_state().
146 * The above is typically ordered against the wakeup, which does:
148 * need_sleep = false;
149 * wake_up_state(p, TASK_UNINTERRUPTIBLE);
151 * Where wake_up_state() (and all other wakeup primitives) imply enough
152 * barriers to order the store of the variable against wakeup.
154 * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
155 * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
156 * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
158 * This is obviously fine, since they both store the exact same value.
160 * Also see the comments of try_to_wake_up().
162 #define __set_current_state(state_value) \
163 do { current->state = (state_value); } while (0)
164 #define set_current_state(state_value) \
165 smp_store_mb(current->state, (state_value))
169 /* Task command name length */
170 #define TASK_COMM_LEN 16
172 #include <linux/spinlock.h>
175 * This serializes "schedule()" and also protects
176 * the run-queue from deletions/modifications (but
177 * _adding_ to the beginning of the run-queue has
180 extern rwlock_t tasklist_lock;
181 extern spinlock_t mmlist_lock;
185 #ifdef CONFIG_PROVE_RCU
186 extern int lockdep_tasklist_lock_is_held(void);
187 #endif /* #ifdef CONFIG_PROVE_RCU */
189 extern void sched_init(void);
190 extern void sched_init_smp(void);
191 extern asmlinkage void schedule_tail(struct task_struct *prev);
192 extern void init_idle(struct task_struct *idle, int cpu);
193 extern void init_idle_bootup_task(struct task_struct *idle);
195 extern cpumask_var_t cpu_isolated_map;
197 extern int runqueue_is_locked(int cpu);
200 * Only dump TASK_* tasks. (0 for all tasks)
202 extern void show_state_filter(unsigned long state_filter);
204 static inline void show_state(void)
206 show_state_filter(0);
209 extern void show_regs(struct pt_regs *);
212 * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
213 * task), SP is the stack pointer of the first frame that should be shown in the back
214 * trace (or NULL if the entire call-chain of the task should be shown).
216 extern void show_stack(struct task_struct *task, unsigned long *sp);
218 extern void cpu_init (void);
219 extern void trap_init(void);
220 extern void update_process_times(int user);
221 extern void scheduler_tick(void);
222 extern int sched_cpu_starting(unsigned int cpu);
223 extern int sched_cpu_activate(unsigned int cpu);
224 extern int sched_cpu_deactivate(unsigned int cpu);
226 #ifdef CONFIG_HOTPLUG_CPU
227 extern int sched_cpu_dying(unsigned int cpu);
229 # define sched_cpu_dying NULL
232 extern void sched_show_task(struct task_struct *p);
234 /* Attach to any functions which should be ignored in wchan output. */
235 #define __sched __attribute__((__section__(".sched.text")))
237 /* Linker adds these: start and end of __sched functions */
238 extern char __sched_text_start[], __sched_text_end[];
240 /* Is this address in the __sched functions? */
241 extern int in_sched_functions(unsigned long addr);
243 #define MAX_SCHEDULE_TIMEOUT LONG_MAX
244 extern signed long schedule_timeout(signed long timeout);
245 extern signed long schedule_timeout_interruptible(signed long timeout);
246 extern signed long schedule_timeout_killable(signed long timeout);
247 extern signed long schedule_timeout_uninterruptible(signed long timeout);
248 extern signed long schedule_timeout_idle(signed long timeout);
249 asmlinkage void schedule(void);
250 extern void schedule_preempt_disabled(void);
252 extern int __must_check io_schedule_prepare(void);
253 extern void io_schedule_finish(int token);
254 extern long io_schedule_timeout(long timeout);
255 extern void io_schedule(void);
257 void __noreturn do_task_dead(void);
262 * struct prev_cputime - snaphsot of system and user cputime
263 * @utime: time spent in user mode
264 * @stime: time spent in system mode
265 * @lock: protects the above two fields
267 * Stores previous user/system time values such that we can guarantee
270 struct prev_cputime {
271 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
278 static inline void prev_cputime_init(struct prev_cputime *prev)
280 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
281 prev->utime = prev->stime = 0;
282 raw_spin_lock_init(&prev->lock);
287 * struct task_cputime - collected CPU time counts
288 * @utime: time spent in user mode, in nanoseconds
289 * @stime: time spent in kernel mode, in nanoseconds
290 * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
292 * This structure groups together three kinds of CPU time that are tracked for
293 * threads and thread groups. Most things considering CPU time want to group
294 * these counts together and treat all three of them in parallel.
296 struct task_cputime {
299 unsigned long long sum_exec_runtime;
302 /* Alternate field names when used to cache expirations. */
303 #define virt_exp utime
304 #define prof_exp stime
305 #define sched_exp sum_exec_runtime
308 * This is the atomic variant of task_cputime, which can be used for
309 * storing and updating task_cputime statistics without locking.
311 struct task_cputime_atomic {
314 atomic64_t sum_exec_runtime;
317 #define INIT_CPUTIME_ATOMIC \
318 (struct task_cputime_atomic) { \
319 .utime = ATOMIC64_INIT(0), \
320 .stime = ATOMIC64_INIT(0), \
321 .sum_exec_runtime = ATOMIC64_INIT(0), \
324 #define PREEMPT_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
327 * Disable preemption until the scheduler is running -- use an unconditional
328 * value so that it also works on !PREEMPT_COUNT kernels.
330 * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
332 #define INIT_PREEMPT_COUNT PREEMPT_OFFSET
335 * Initial preempt_count value; reflects the preempt_count schedule invariant
336 * which states that during context switches:
338 * preempt_count() == 2*PREEMPT_DISABLE_OFFSET
340 * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
341 * Note: See finish_task_switch().
343 #define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
346 * struct thread_group_cputimer - thread group interval timer counts
347 * @cputime_atomic: atomic thread group interval timers.
348 * @running: true when there are timers running and
349 * @cputime_atomic receives updates.
350 * @checking_timer: true when a thread in the group is in the
351 * process of checking for thread group timers.
353 * This structure contains the version of task_cputime, above, that is
354 * used for thread group CPU timer calculations.
356 struct thread_group_cputimer {
357 struct task_cputime_atomic cputime_atomic;
362 #include <linux/rwsem.h>
365 struct backing_dev_info;
366 struct reclaim_state;
368 #ifdef CONFIG_SCHED_INFO
370 /* cumulative counters */
371 unsigned long pcount; /* # of times run on this cpu */
372 unsigned long long run_delay; /* time spent waiting on a runqueue */
375 unsigned long long last_arrival,/* when we last ran on a cpu */
376 last_queued; /* when we were last queued to run */
378 #endif /* CONFIG_SCHED_INFO */
380 struct task_delay_info;
382 static inline int sched_info_on(void)
384 #ifdef CONFIG_SCHEDSTATS
386 #elif defined(CONFIG_TASK_DELAY_ACCT)
387 extern int delayacct_on;
394 #ifdef CONFIG_SCHEDSTATS
395 void force_schedstat_enabled(void);
399 * Integer metrics need fixed point arithmetic, e.g., sched/fair
400 * has a few: load, load_avg, util_avg, freq, and capacity.
402 * We define a basic fixed point arithmetic range, and then formalize
403 * all these metrics based on that basic range.
405 # define SCHED_FIXEDPOINT_SHIFT 10
406 # define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
408 struct io_context; /* See blkdev.h */
411 #ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
412 extern void prefetch_stack(struct task_struct *t);
414 static inline void prefetch_stack(struct task_struct *t) { }
417 struct audit_context; /* See audit.c */
419 struct pipe_inode_info;
420 struct uts_namespace;
423 unsigned long weight;
428 * The load_avg/util_avg accumulates an infinite geometric series
429 * (see __update_load_avg() in kernel/sched/fair.c).
431 * [load_avg definition]
433 * load_avg = runnable% * scale_load_down(load)
435 * where runnable% is the time ratio that a sched_entity is runnable.
436 * For cfs_rq, it is the aggregated load_avg of all runnable and
437 * blocked sched_entities.
439 * load_avg may also take frequency scaling into account:
441 * load_avg = runnable% * scale_load_down(load) * freq%
443 * where freq% is the CPU frequency normalized to the highest frequency.
445 * [util_avg definition]
447 * util_avg = running% * SCHED_CAPACITY_SCALE
449 * where running% is the time ratio that a sched_entity is running on
450 * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
451 * and blocked sched_entities.
453 * util_avg may also factor frequency scaling and CPU capacity scaling:
455 * util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
457 * where freq% is the same as above, and capacity% is the CPU capacity
458 * normalized to the greatest capacity (due to uarch differences, etc).
460 * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
461 * themselves are in the range of [0, 1]. To do fixed point arithmetics,
462 * we therefore scale them to as large a range as necessary. This is for
463 * example reflected by util_avg's SCHED_CAPACITY_SCALE.
467 * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
468 * with the highest load (=88761), always runnable on a single cfs_rq,
469 * and should not overflow as the number already hits PID_MAX_LIMIT.
471 * For all other cases (including 32-bit kernels), struct load_weight's
472 * weight will overflow first before we do, because:
474 * Max(load_avg) <= Max(load.weight)
476 * Then it is the load_weight's responsibility to consider overflow
480 u64 last_update_time, load_sum;
481 u32 util_sum, period_contrib;
482 unsigned long load_avg, util_avg;
485 #ifdef CONFIG_SCHEDSTATS
486 struct sched_statistics {
496 s64 sum_sleep_runtime;
503 u64 nr_migrations_cold;
504 u64 nr_failed_migrations_affine;
505 u64 nr_failed_migrations_running;
506 u64 nr_failed_migrations_hot;
507 u64 nr_forced_migrations;
511 u64 nr_wakeups_migrate;
512 u64 nr_wakeups_local;
513 u64 nr_wakeups_remote;
514 u64 nr_wakeups_affine;
515 u64 nr_wakeups_affine_attempts;
516 u64 nr_wakeups_passive;
521 struct sched_entity {
522 struct load_weight load; /* for load-balancing */
523 struct rb_node run_node;
524 struct list_head group_node;
528 u64 sum_exec_runtime;
530 u64 prev_sum_exec_runtime;
534 #ifdef CONFIG_SCHEDSTATS
535 struct sched_statistics statistics;
538 #ifdef CONFIG_FAIR_GROUP_SCHED
540 struct sched_entity *parent;
541 /* rq on which this entity is (to be) queued: */
542 struct cfs_rq *cfs_rq;
543 /* rq "owned" by this entity/group: */
549 * Per entity load average tracking.
551 * Put into separate cache line so it does not
552 * collide with read-mostly values above.
554 struct sched_avg avg ____cacheline_aligned_in_smp;
558 struct sched_rt_entity {
559 struct list_head run_list;
560 unsigned long timeout;
561 unsigned long watchdog_stamp;
562 unsigned int time_slice;
563 unsigned short on_rq;
564 unsigned short on_list;
566 struct sched_rt_entity *back;
567 #ifdef CONFIG_RT_GROUP_SCHED
568 struct sched_rt_entity *parent;
569 /* rq on which this entity is (to be) queued: */
571 /* rq "owned" by this entity/group: */
576 struct sched_dl_entity {
577 struct rb_node rb_node;
580 * Original scheduling parameters. Copied here from sched_attr
581 * during sched_setattr(), they will remain the same until
582 * the next sched_setattr().
584 u64 dl_runtime; /* maximum runtime for each instance */
585 u64 dl_deadline; /* relative deadline of each instance */
586 u64 dl_period; /* separation of two instances (period) */
587 u64 dl_bw; /* dl_runtime / dl_deadline */
590 * Actual scheduling parameters. Initialized with the values above,
591 * they are continously updated during task execution. Note that
592 * the remaining runtime could be < 0 in case we are in overrun.
594 s64 runtime; /* remaining runtime for this instance */
595 u64 deadline; /* absolute deadline for this instance */
596 unsigned int flags; /* specifying the scheduler behaviour */
601 * @dl_throttled tells if we exhausted the runtime. If so, the
602 * task has to wait for a replenishment to be performed at the
603 * next firing of dl_timer.
605 * @dl_boosted tells if we are boosted due to DI. If so we are
606 * outside bandwidth enforcement mechanism (but only until we
607 * exit the critical section);
609 * @dl_yielded tells if task gave up the cpu before consuming
610 * all its available runtime during the last job.
612 int dl_throttled, dl_boosted, dl_yielded;
615 * Bandwidth enforcement timer. Each -deadline task has its
616 * own bandwidth to be enforced, thus we need one timer per task.
618 struct hrtimer dl_timer;
626 u8 pad; /* Otherwise the compiler can store garbage here. */
628 u32 s; /* Set of bits. */
632 enum perf_event_task_context {
633 perf_invalid_context = -1,
636 perf_nr_task_contexts,
640 struct wake_q_node *next;
643 /* Track pages that require TLB flushes */
644 struct tlbflush_unmap_batch {
646 * Each bit set is a CPU that potentially has a TLB entry for one of
647 * the PFNs being flushed. See set_tlb_ubc_flush_pending().
649 struct cpumask cpumask;
651 /* True if any bit in cpumask is set */
655 * If true then the PTE was dirty when unmapped. The entry must be
656 * flushed before IO is initiated or a stale TLB entry potentially
657 * allows an update without redirtying the page.
663 #ifdef CONFIG_THREAD_INFO_IN_TASK
665 * For reasons of header soup (see current_thread_info()), this
666 * must be the first element of task_struct.
668 struct thread_info thread_info;
670 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
673 unsigned int flags; /* per process flags, defined below */
677 struct llist_node wake_entry;
679 #ifdef CONFIG_THREAD_INFO_IN_TASK
680 unsigned int cpu; /* current CPU */
682 unsigned int wakee_flips;
683 unsigned long wakee_flip_decay_ts;
684 struct task_struct *last_wakee;
690 int prio, static_prio, normal_prio;
691 unsigned int rt_priority;
692 const struct sched_class *sched_class;
693 struct sched_entity se;
694 struct sched_rt_entity rt;
695 #ifdef CONFIG_CGROUP_SCHED
696 struct task_group *sched_task_group;
698 struct sched_dl_entity dl;
700 #ifdef CONFIG_PREEMPT_NOTIFIERS
701 /* list of struct preempt_notifier: */
702 struct hlist_head preempt_notifiers;
705 #ifdef CONFIG_BLK_DEV_IO_TRACE
706 unsigned int btrace_seq;
711 cpumask_t cpus_allowed;
713 #ifdef CONFIG_PREEMPT_RCU
714 int rcu_read_lock_nesting;
715 union rcu_special rcu_read_unlock_special;
716 struct list_head rcu_node_entry;
717 struct rcu_node *rcu_blocked_node;
718 #endif /* #ifdef CONFIG_PREEMPT_RCU */
719 #ifdef CONFIG_TASKS_RCU
720 unsigned long rcu_tasks_nvcsw;
721 bool rcu_tasks_holdout;
722 struct list_head rcu_tasks_holdout_list;
723 int rcu_tasks_idle_cpu;
724 #endif /* #ifdef CONFIG_TASKS_RCU */
726 #ifdef CONFIG_SCHED_INFO
727 struct sched_info sched_info;
730 struct list_head tasks;
732 struct plist_node pushable_tasks;
733 struct rb_node pushable_dl_tasks;
736 struct mm_struct *mm, *active_mm;
738 /* Per-thread vma caching: */
739 struct vmacache vmacache;
741 #if defined(SPLIT_RSS_COUNTING)
742 struct task_rss_stat rss_stat;
746 int exit_code, exit_signal;
747 int pdeath_signal; /* The signal sent when the parent dies */
748 unsigned long jobctl; /* JOBCTL_*, siglock protected */
750 /* Used for emulating ABI behavior of previous Linux versions */
751 unsigned int personality;
753 /* scheduler bits, serialized by scheduler locks */
754 unsigned sched_reset_on_fork:1;
755 unsigned sched_contributes_to_load:1;
756 unsigned sched_migrated:1;
757 unsigned sched_remote_wakeup:1;
758 unsigned :0; /* force alignment to the next boundary */
760 /* unserialized, strictly 'current' */
761 unsigned in_execve:1; /* bit to tell LSMs we're in execve */
762 unsigned in_iowait:1;
763 #if !defined(TIF_RESTORE_SIGMASK)
764 unsigned restore_sigmask:1;
767 unsigned memcg_may_oom:1;
769 unsigned memcg_kmem_skip_account:1;
772 #ifdef CONFIG_COMPAT_BRK
773 unsigned brk_randomized:1;
776 unsigned long atomic_flags; /* Flags needing atomic access. */
778 struct restart_block restart_block;
783 #ifdef CONFIG_CC_STACKPROTECTOR
784 /* Canary value for the -fstack-protector gcc feature */
785 unsigned long stack_canary;
788 * pointers to (original) parent process, youngest child, younger sibling,
789 * older sibling, respectively. (p->father can be replaced with
790 * p->real_parent->pid)
792 struct task_struct __rcu *real_parent; /* real parent process */
793 struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
795 * children/sibling forms the list of my natural children
797 struct list_head children; /* list of my children */
798 struct list_head sibling; /* linkage in my parent's children list */
799 struct task_struct *group_leader; /* threadgroup leader */
802 * ptraced is the list of tasks this task is using ptrace on.
803 * This includes both natural children and PTRACE_ATTACH targets.
804 * p->ptrace_entry is p's link on the p->parent->ptraced list.
806 struct list_head ptraced;
807 struct list_head ptrace_entry;
809 /* PID/PID hash table linkage. */
810 struct pid_link pids[PIDTYPE_MAX];
811 struct list_head thread_group;
812 struct list_head thread_node;
814 struct completion *vfork_done; /* for vfork() */
815 int __user *set_child_tid; /* CLONE_CHILD_SETTID */
816 int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
819 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
820 u64 utimescaled, stimescaled;
823 struct prev_cputime prev_cputime;
824 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
825 seqcount_t vtime_seqcount;
826 unsigned long long vtime_snap;
828 /* Task is sleeping or running in a CPU with VTIME inactive */
830 /* Task runs in userspace in a CPU with VTIME active */
832 /* Task runs in kernelspace in a CPU with VTIME active */
837 #ifdef CONFIG_NO_HZ_FULL
838 atomic_t tick_dep_mask;
840 unsigned long nvcsw, nivcsw; /* context switch counts */
841 u64 start_time; /* monotonic time in nsec */
842 u64 real_start_time; /* boot based time in nsec */
843 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
844 unsigned long min_flt, maj_flt;
846 #ifdef CONFIG_POSIX_TIMERS
847 struct task_cputime cputime_expires;
848 struct list_head cpu_timers[3];
851 /* process credentials */
852 const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
853 const struct cred __rcu *real_cred; /* objective and real subjective task
854 * credentials (COW) */
855 const struct cred __rcu *cred; /* effective (overridable) subjective task
856 * credentials (COW) */
857 char comm[TASK_COMM_LEN]; /* executable name excluding path
858 - access with [gs]et_task_comm (which lock
860 - initialized normally by setup_new_exec */
861 /* file system info */
862 struct nameidata *nameidata;
863 #ifdef CONFIG_SYSVIPC
865 struct sysv_sem sysvsem;
866 struct sysv_shm sysvshm;
868 #ifdef CONFIG_DETECT_HUNG_TASK
869 /* hung task detection */
870 unsigned long last_switch_count;
872 /* filesystem information */
873 struct fs_struct *fs;
874 /* open file information */
875 struct files_struct *files;
877 struct nsproxy *nsproxy;
878 /* signal handlers */
879 struct signal_struct *signal;
880 struct sighand_struct *sighand;
882 sigset_t blocked, real_blocked;
883 sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
884 struct sigpending pending;
886 unsigned long sas_ss_sp;
888 unsigned sas_ss_flags;
890 struct callback_head *task_works;
892 struct audit_context *audit_context;
893 #ifdef CONFIG_AUDITSYSCALL
895 unsigned int sessionid;
897 struct seccomp seccomp;
899 /* Thread group tracking */
902 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
904 spinlock_t alloc_lock;
906 /* Protection of the PI data structures: */
907 raw_spinlock_t pi_lock;
909 struct wake_q_node wake_q;
911 #ifdef CONFIG_RT_MUTEXES
912 /* PI waiters blocked on a rt_mutex held by this task */
913 struct rb_root pi_waiters;
914 struct rb_node *pi_waiters_leftmost;
915 /* Deadlock detection and priority inheritance handling */
916 struct rt_mutex_waiter *pi_blocked_on;
919 #ifdef CONFIG_DEBUG_MUTEXES
920 /* mutex deadlock detection */
921 struct mutex_waiter *blocked_on;
923 #ifdef CONFIG_TRACE_IRQFLAGS
924 unsigned int irq_events;
925 unsigned long hardirq_enable_ip;
926 unsigned long hardirq_disable_ip;
927 unsigned int hardirq_enable_event;
928 unsigned int hardirq_disable_event;
929 int hardirqs_enabled;
931 unsigned long softirq_disable_ip;
932 unsigned long softirq_enable_ip;
933 unsigned int softirq_disable_event;
934 unsigned int softirq_enable_event;
935 int softirqs_enabled;
938 #ifdef CONFIG_LOCKDEP
939 # define MAX_LOCK_DEPTH 48UL
942 unsigned int lockdep_recursion;
943 struct held_lock held_locks[MAX_LOCK_DEPTH];
944 gfp_t lockdep_reclaim_gfp;
947 unsigned int in_ubsan;
950 /* journalling filesystem info */
953 /* stacked block device info */
954 struct bio_list *bio_list;
958 struct blk_plug *plug;
962 struct reclaim_state *reclaim_state;
964 struct backing_dev_info *backing_dev_info;
966 struct io_context *io_context;
968 unsigned long ptrace_message;
969 siginfo_t *last_siginfo; /* For ptrace use. */
970 struct task_io_accounting ioac;
971 #if defined(CONFIG_TASK_XACCT)
972 u64 acct_rss_mem1; /* accumulated rss usage */
973 u64 acct_vm_mem1; /* accumulated virtual memory usage */
974 u64 acct_timexpd; /* stime + utime since last update */
976 #ifdef CONFIG_CPUSETS
977 nodemask_t mems_allowed; /* Protected by alloc_lock */
978 seqcount_t mems_allowed_seq; /* Seqence no to catch updates */
979 int cpuset_mem_spread_rotor;
980 int cpuset_slab_spread_rotor;
982 #ifdef CONFIG_CGROUPS
983 /* Control Group info protected by css_set_lock */
984 struct css_set __rcu *cgroups;
985 /* cg_list protected by css_set_lock and tsk->alloc_lock */
986 struct list_head cg_list;
988 #ifdef CONFIG_INTEL_RDT_A
992 struct robust_list_head __user *robust_list;
994 struct compat_robust_list_head __user *compat_robust_list;
996 struct list_head pi_state_list;
997 struct futex_pi_state *pi_state_cache;
999 #ifdef CONFIG_PERF_EVENTS
1000 struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1001 struct mutex perf_event_mutex;
1002 struct list_head perf_event_list;
1004 #ifdef CONFIG_DEBUG_PREEMPT
1005 unsigned long preempt_disable_ip;
1008 struct mempolicy *mempolicy; /* Protected by alloc_lock */
1010 short pref_node_fork;
1012 #ifdef CONFIG_NUMA_BALANCING
1014 unsigned int numa_scan_period;
1015 unsigned int numa_scan_period_max;
1016 int numa_preferred_nid;
1017 unsigned long numa_migrate_retry;
1018 u64 node_stamp; /* migration stamp */
1019 u64 last_task_numa_placement;
1020 u64 last_sum_exec_runtime;
1021 struct callback_head numa_work;
1023 struct list_head numa_entry;
1024 struct numa_group *numa_group;
1027 * numa_faults is an array split into four regions:
1028 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1029 * in this precise order.
1031 * faults_memory: Exponential decaying average of faults on a per-node
1032 * basis. Scheduling placement decisions are made based on these
1033 * counts. The values remain static for the duration of a PTE scan.
1034 * faults_cpu: Track the nodes the process was running on when a NUMA
1035 * hinting fault was incurred.
1036 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1037 * during the current scan window. When the scan completes, the counts
1038 * in faults_memory and faults_cpu decay and these values are copied.
1040 unsigned long *numa_faults;
1041 unsigned long total_numa_faults;
1044 * numa_faults_locality tracks if faults recorded during the last
1045 * scan window were remote/local or failed to migrate. The task scan
1046 * period is adapted based on the locality of the faults with different
1047 * weights depending on whether they were shared or private faults
1049 unsigned long numa_faults_locality[3];
1051 unsigned long numa_pages_migrated;
1052 #endif /* CONFIG_NUMA_BALANCING */
1054 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1055 struct tlbflush_unmap_batch tlb_ubc;
1058 struct rcu_head rcu;
1061 * cache last used pipe for splice
1063 struct pipe_inode_info *splice_pipe;
1065 struct page_frag task_frag;
1067 #ifdef CONFIG_TASK_DELAY_ACCT
1068 struct task_delay_info *delays;
1071 #ifdef CONFIG_FAULT_INJECTION
1075 * when (nr_dirtied >= nr_dirtied_pause), it's time to call
1076 * balance_dirty_pages() for some dirty throttling pause
1079 int nr_dirtied_pause;
1080 unsigned long dirty_paused_when; /* start of a write-and-pause period */
1082 #ifdef CONFIG_LATENCYTOP
1083 int latency_record_count;
1084 struct latency_record latency_record[LT_SAVECOUNT];
1087 * time slack values; these are used to round up poll() and
1088 * select() etc timeout values. These are in nanoseconds.
1091 u64 default_timer_slack_ns;
1094 unsigned int kasan_depth;
1096 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1097 /* Index of current stored address in ret_stack */
1099 /* Stack of return addresses for return function tracing */
1100 struct ftrace_ret_stack *ret_stack;
1101 /* time stamp for last schedule */
1102 unsigned long long ftrace_timestamp;
1104 * Number of functions that haven't been traced
1105 * because of depth overrun.
1107 atomic_t trace_overrun;
1108 /* Pause for the tracing */
1109 atomic_t tracing_graph_pause;
1111 #ifdef CONFIG_TRACING
1112 /* state flags for use by tracers */
1113 unsigned long trace;
1114 /* bitmask and counter of trace recursion */
1115 unsigned long trace_recursion;
1116 #endif /* CONFIG_TRACING */
1118 /* Coverage collection mode enabled for this task (0 if disabled). */
1119 enum kcov_mode kcov_mode;
1120 /* Size of the kcov_area. */
1122 /* Buffer for coverage collection. */
1124 /* kcov desciptor wired with this task or NULL. */
1128 struct mem_cgroup *memcg_in_oom;
1129 gfp_t memcg_oom_gfp_mask;
1130 int memcg_oom_order;
1132 /* number of pages to reclaim on returning to userland */
1133 unsigned int memcg_nr_pages_over_high;
1135 #ifdef CONFIG_UPROBES
1136 struct uprobe_task *utask;
1138 #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1139 unsigned int sequential_io;
1140 unsigned int sequential_io_avg;
1142 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1143 unsigned long task_state_change;
1145 int pagefault_disabled;
1147 struct task_struct *oom_reaper_list;
1149 #ifdef CONFIG_VMAP_STACK
1150 struct vm_struct *stack_vm_area;
1152 #ifdef CONFIG_THREAD_INFO_IN_TASK
1153 /* A live task holds one reference. */
1154 atomic_t stack_refcount;
1156 /* CPU-specific state of this task */
1157 struct thread_struct thread;
1159 * WARNING: on x86, 'thread_struct' contains a variable-sized
1160 * structure. It *MUST* be at the end of 'task_struct'.
1162 * Do not put anything below here!
1166 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
1167 extern int arch_task_struct_size __read_mostly;
1169 # define arch_task_struct_size (sizeof(struct task_struct))
1172 #ifdef CONFIG_VMAP_STACK
1173 static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1175 return t->stack_vm_area;
1178 static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1184 static inline struct pid *task_pid(struct task_struct *task)
1186 return task->pids[PIDTYPE_PID].pid;
1189 static inline struct pid *task_tgid(struct task_struct *task)
1191 return task->group_leader->pids[PIDTYPE_PID].pid;
1195 * Without tasklist or rcu lock it is not safe to dereference
1196 * the result of task_pgrp/task_session even if task == current,
1197 * we can race with another thread doing sys_setsid/sys_setpgid.
1199 static inline struct pid *task_pgrp(struct task_struct *task)
1201 return task->group_leader->pids[PIDTYPE_PGID].pid;
1204 static inline struct pid *task_session(struct task_struct *task)
1206 return task->group_leader->pids[PIDTYPE_SID].pid;
1209 struct pid_namespace;
1212 * the helpers to get the task's different pids as they are seen
1213 * from various namespaces
1215 * task_xid_nr() : global id, i.e. the id seen from the init namespace;
1216 * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
1218 * task_xid_nr_ns() : id seen from the ns specified;
1220 * set_task_vxid() : assigns a virtual id to a task;
1222 * see also pid_nr() etc in include/linux/pid.h
1224 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
1225 struct pid_namespace *ns);
1227 static inline pid_t task_pid_nr(struct task_struct *tsk)
1232 static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
1233 struct pid_namespace *ns)
1235 return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1238 static inline pid_t task_pid_vnr(struct task_struct *tsk)
1240 return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
1244 static inline pid_t task_tgid_nr(struct task_struct *tsk)
1249 pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
1251 static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1253 return pid_vnr(task_tgid(tsk));
1257 static inline int pid_alive(const struct task_struct *p);
1258 static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1264 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1270 static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1272 return task_ppid_nr_ns(tsk, &init_pid_ns);
1275 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
1276 struct pid_namespace *ns)
1278 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
1281 static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
1283 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
1287 static inline pid_t task_session_nr_ns(struct task_struct *tsk,
1288 struct pid_namespace *ns)
1290 return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
1293 static inline pid_t task_session_vnr(struct task_struct *tsk)
1295 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
1298 /* obsolete, do not use */
1299 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
1301 return task_pgrp_nr_ns(tsk, &init_pid_ns);
1305 * pid_alive - check that a task structure is not stale
1306 * @p: Task structure to be checked.
1308 * Test if a process is not yet dead (at most zombie state)
1309 * If pid_alive fails, then pointers within the task structure
1310 * can be stale and must not be dereferenced.
1312 * Return: 1 if the process is alive. 0 otherwise.
1314 static inline int pid_alive(const struct task_struct *p)
1316 return p->pids[PIDTYPE_PID].pid != NULL;
1320 * is_global_init - check if a task structure is init. Since init
1321 * is free to have sub-threads we need to check tgid.
1322 * @tsk: Task structure to be checked.
1324 * Check if a task structure is the first user space task the kernel created.
1326 * Return: 1 if the task structure is init. 0 otherwise.
1328 static inline int is_global_init(struct task_struct *tsk)
1330 return task_tgid_nr(tsk) == 1;
1333 extern struct pid *cad_pid;
1335 extern void free_task(struct task_struct *tsk);
1336 #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
1338 extern void __put_task_struct(struct task_struct *t);
1340 static inline void put_task_struct(struct task_struct *t)
1342 if (atomic_dec_and_test(&t->usage))
1343 __put_task_struct(t);
1346 struct task_struct *task_rcu_dereference(struct task_struct **ptask);
1347 struct task_struct *try_get_task_struct(struct task_struct **ptask);
1349 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1350 extern void task_cputime(struct task_struct *t,
1351 u64 *utime, u64 *stime);
1352 extern u64 task_gtime(struct task_struct *t);
1354 static inline void task_cputime(struct task_struct *t,
1355 u64 *utime, u64 *stime)
1361 static inline u64 task_gtime(struct task_struct *t)
1367 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
1368 static inline void task_cputime_scaled(struct task_struct *t,
1372 *utimescaled = t->utimescaled;
1373 *stimescaled = t->stimescaled;
1376 static inline void task_cputime_scaled(struct task_struct *t,
1380 task_cputime(t, utimescaled, stimescaled);
1384 extern void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
1385 extern void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
1390 #define PF_IDLE 0x00000002 /* I am an IDLE thread */
1391 #define PF_EXITING 0x00000004 /* getting shut down */
1392 #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
1393 #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
1394 #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
1395 #define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
1396 #define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
1397 #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
1398 #define PF_DUMPCORE 0x00000200 /* dumped core */
1399 #define PF_SIGNALED 0x00000400 /* killed by a signal */
1400 #define PF_MEMALLOC 0x00000800 /* Allocating memory */
1401 #define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
1402 #define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
1403 #define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
1404 #define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
1405 #define PF_FROZEN 0x00010000 /* frozen for system suspend */
1406 #define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
1407 #define PF_KSWAPD 0x00040000 /* I am kswapd */
1408 #define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
1409 #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
1410 #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
1411 #define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
1412 #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
1413 #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
1414 #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
1415 #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
1416 #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
1417 #define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
1420 * Only the _current_ task can read/write to tsk->flags, but other
1421 * tasks can access tsk->flags in readonly mode for example
1422 * with tsk_used_math (like during threaded core dumping).
1423 * There is however an exception to this rule during ptrace
1424 * or during fork: the ptracer task is allowed to write to the
1425 * child->flags of its traced child (same goes for fork, the parent
1426 * can write to the child->flags), because we're guaranteed the
1427 * child is not running and in turn not changing child->flags
1428 * at the same time the parent does it.
1430 #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
1431 #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
1432 #define clear_used_math() clear_stopped_child_used_math(current)
1433 #define set_used_math() set_stopped_child_used_math(current)
1434 #define conditional_stopped_child_used_math(condition, child) \
1435 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
1436 #define conditional_used_math(condition) \
1437 conditional_stopped_child_used_math(condition, current)
1438 #define copy_to_stopped_child_used_math(child) \
1439 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
1440 /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
1441 #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
1442 #define used_math() tsk_used_math(current)
1444 /* Per-process atomic flags. */
1445 #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
1446 #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
1447 #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
1448 #define PFA_LMK_WAITING 3 /* Lowmemorykiller is waiting */
1451 #define TASK_PFA_TEST(name, func) \
1452 static inline bool task_##func(struct task_struct *p) \
1453 { return test_bit(PFA_##name, &p->atomic_flags); }
1454 #define TASK_PFA_SET(name, func) \
1455 static inline void task_set_##func(struct task_struct *p) \
1456 { set_bit(PFA_##name, &p->atomic_flags); }
1457 #define TASK_PFA_CLEAR(name, func) \
1458 static inline void task_clear_##func(struct task_struct *p) \
1459 { clear_bit(PFA_##name, &p->atomic_flags); }
1461 TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
1462 TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
1464 TASK_PFA_TEST(SPREAD_PAGE, spread_page)
1465 TASK_PFA_SET(SPREAD_PAGE, spread_page)
1466 TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
1468 TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
1469 TASK_PFA_SET(SPREAD_SLAB, spread_slab)
1470 TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
1472 TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
1473 TASK_PFA_SET(LMK_WAITING, lmk_waiting)
1475 static inline void rcu_copy_process(struct task_struct *p)
1477 #ifdef CONFIG_PREEMPT_RCU
1478 p->rcu_read_lock_nesting = 0;
1479 p->rcu_read_unlock_special.s = 0;
1480 p->rcu_blocked_node = NULL;
1481 INIT_LIST_HEAD(&p->rcu_node_entry);
1482 #endif /* #ifdef CONFIG_PREEMPT_RCU */
1483 #ifdef CONFIG_TASKS_RCU
1484 p->rcu_tasks_holdout = false;
1485 INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
1486 p->rcu_tasks_idle_cpu = -1;
1487 #endif /* #ifdef CONFIG_TASKS_RCU */
1490 static inline void tsk_restore_flags(struct task_struct *task,
1491 unsigned long orig_flags, unsigned long flags)
1493 task->flags &= ~flags;
1494 task->flags |= orig_flags & flags;
1497 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
1498 const struct cpumask *trial);
1499 extern int task_can_attach(struct task_struct *p,
1500 const struct cpumask *cs_cpus_allowed);
1502 extern void do_set_cpus_allowed(struct task_struct *p,
1503 const struct cpumask *new_mask);
1505 extern int set_cpus_allowed_ptr(struct task_struct *p,
1506 const struct cpumask *new_mask);
1508 static inline void do_set_cpus_allowed(struct task_struct *p,
1509 const struct cpumask *new_mask)
1512 static inline int set_cpus_allowed_ptr(struct task_struct *p,
1513 const struct cpumask *new_mask)
1515 if (!cpumask_test_cpu(0, new_mask))
1521 #ifndef cpu_relax_yield
1522 #define cpu_relax_yield() cpu_relax()
1525 extern unsigned long long
1526 task_sched_runtime(struct task_struct *task);
1528 /* sched_exec is called by processes performing an exec */
1530 extern void sched_exec(void);
1532 #define sched_exec() {}
1535 #ifdef CONFIG_HOTPLUG_CPU
1536 extern void idle_task_exit(void);
1538 static inline void idle_task_exit(void) {}
1541 extern int yield_to(struct task_struct *p, bool preempt);
1542 extern void set_user_nice(struct task_struct *p, long nice);
1543 extern int task_prio(const struct task_struct *p);
1545 * task_nice - return the nice value of a given task.
1546 * @p: the task in question.
1548 * Return: The nice value [ -20 ... 0 ... 19 ].
1550 static inline int task_nice(const struct task_struct *p)
1552 return PRIO_TO_NICE((p)->static_prio);
1554 extern int can_nice(const struct task_struct *p, const int nice);
1555 extern int task_curr(const struct task_struct *p);
1556 extern int idle_cpu(int cpu);
1557 extern int sched_setscheduler(struct task_struct *, int,
1558 const struct sched_param *);
1559 extern int sched_setscheduler_nocheck(struct task_struct *, int,
1560 const struct sched_param *);
1561 extern int sched_setattr(struct task_struct *,
1562 const struct sched_attr *);
1563 extern struct task_struct *idle_task(int cpu);
1565 * is_idle_task - is the specified task an idle task?
1566 * @p: the task in question.
1568 * Return: 1 if @p is an idle task. 0 otherwise.
1570 static inline bool is_idle_task(const struct task_struct *p)
1572 return !!(p->flags & PF_IDLE);
1574 extern struct task_struct *curr_task(int cpu);
1575 extern void ia64_set_curr_task(int cpu, struct task_struct *p);
1579 union thread_union {
1580 #ifndef CONFIG_THREAD_INFO_IN_TASK
1581 struct thread_info thread_info;
1583 unsigned long stack[THREAD_SIZE/sizeof(long)];
1586 #ifndef __HAVE_ARCH_KSTACK_END
1587 static inline int kstack_end(void *addr)
1589 /* Reliable end of stack detection:
1590 * Some APM bios versions misalign the stack
1592 return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
1596 extern union thread_union init_thread_union;
1597 extern struct task_struct init_task;
1599 extern struct pid_namespace init_pid_ns;
1602 * find a task by one of its numerical ids
1604 * find_task_by_pid_ns():
1605 * finds a task by its pid in the specified namespace
1606 * find_task_by_vpid():
1607 * finds a task by its virtual pid
1609 * see also find_vpid() etc in include/linux/pid.h
1612 extern struct task_struct *find_task_by_vpid(pid_t nr);
1613 extern struct task_struct *find_task_by_pid_ns(pid_t nr,
1614 struct pid_namespace *ns);
1616 #include <asm/current.h>
1618 extern void xtime_update(unsigned long ticks);
1620 extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1621 extern int wake_up_process(struct task_struct *tsk);
1622 extern void wake_up_new_task(struct task_struct *tsk);
1624 extern void kick_process(struct task_struct *tsk);
1626 static inline void kick_process(struct task_struct *tsk) { }
1628 extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
1629 extern void sched_dead(struct task_struct *p);
1631 extern void proc_caches_init(void);
1633 extern void release_task(struct task_struct * p);
1635 #ifdef CONFIG_HAVE_COPY_THREAD_TLS
1636 extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
1637 struct task_struct *, unsigned long);
1639 extern int copy_thread(unsigned long, unsigned long, unsigned long,
1640 struct task_struct *);
1642 /* Architectures that haven't opted into copy_thread_tls get the tls argument
1643 * via pt_regs, so ignore the tls argument passed via C. */
1644 static inline int copy_thread_tls(
1645 unsigned long clone_flags, unsigned long sp, unsigned long arg,
1646 struct task_struct *p, unsigned long tls)
1648 return copy_thread(clone_flags, sp, arg, p);
1651 extern void flush_thread(void);
1653 #ifdef CONFIG_HAVE_EXIT_THREAD
1654 extern void exit_thread(struct task_struct *tsk);
1656 static inline void exit_thread(struct task_struct *tsk)
1661 extern void exit_files(struct task_struct *);
1663 extern void exit_itimers(struct signal_struct *);
1665 extern void do_group_exit(int);
1667 extern int do_execve(struct filename *,
1668 const char __user * const __user *,
1669 const char __user * const __user *);
1670 extern int do_execveat(int, struct filename *,
1671 const char __user * const __user *,
1672 const char __user * const __user *,
1674 extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
1675 extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
1676 struct task_struct *fork_idle(int);
1677 extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
1679 extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
1680 static inline void set_task_comm(struct task_struct *tsk, const char *from)
1682 __set_task_comm(tsk, from, false);
1684 extern char *get_task_comm(char *to, struct task_struct *tsk);
1687 void scheduler_ipi(void);
1688 extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
1690 static inline void scheduler_ipi(void) { }
1691 static inline unsigned long wait_task_inactive(struct task_struct *p,
1699 * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
1700 * subscriptions and synchronises with wait4(). Also used in procfs. Also
1701 * pins the final release of task.io_context. Also protects ->cpuset and
1702 * ->cgroup.subsys[]. And ->vfork_done.
1704 * Nests both inside and outside of read_lock(&tasklist_lock).
1705 * It must not be nested with write_lock_irq(&tasklist_lock),
1706 * neither inside nor outside.
1708 static inline void task_lock(struct task_struct *p)
1710 spin_lock(&p->alloc_lock);
1713 static inline void task_unlock(struct task_struct *p)
1715 spin_unlock(&p->alloc_lock);
1718 #ifdef CONFIG_THREAD_INFO_IN_TASK
1720 static inline struct thread_info *task_thread_info(struct task_struct *task)
1722 return &task->thread_info;
1726 * When accessing the stack of a non-current task that might exit, use
1727 * try_get_task_stack() instead. task_stack_page will return a pointer
1728 * that could get freed out from under you.
1730 static inline void *task_stack_page(const struct task_struct *task)
1735 #define setup_thread_stack(new,old) do { } while(0)
1737 static inline unsigned long *end_of_stack(const struct task_struct *task)
1742 #elif !defined(__HAVE_THREAD_FUNCTIONS)
1744 #define task_thread_info(task) ((struct thread_info *)(task)->stack)
1745 #define task_stack_page(task) ((void *)(task)->stack)
1747 static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
1749 *task_thread_info(p) = *task_thread_info(org);
1750 task_thread_info(p)->task = p;
1754 * Return the address of the last usable long on the stack.
1756 * When the stack grows down, this is just above the thread
1757 * info struct. Going any lower will corrupt the threadinfo.
1759 * When the stack grows up, this is the highest address.
1760 * Beyond that position, we corrupt data on the next page.
1762 static inline unsigned long *end_of_stack(struct task_struct *p)
1764 #ifdef CONFIG_STACK_GROWSUP
1765 return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
1767 return (unsigned long *)(task_thread_info(p) + 1);
1773 #ifdef CONFIG_THREAD_INFO_IN_TASK
1774 static inline void *try_get_task_stack(struct task_struct *tsk)
1776 return atomic_inc_not_zero(&tsk->stack_refcount) ?
1777 task_stack_page(tsk) : NULL;
1780 extern void put_task_stack(struct task_struct *tsk);
1782 static inline void *try_get_task_stack(struct task_struct *tsk)
1784 return task_stack_page(tsk);
1787 static inline void put_task_stack(struct task_struct *tsk) {}
1790 #define task_stack_end_corrupted(task) \
1791 (*(end_of_stack(task)) != STACK_END_MAGIC)
1793 static inline int object_is_on_stack(void *obj)
1795 void *stack = task_stack_page(current);
1797 return (obj >= stack) && (obj < (stack + THREAD_SIZE));
1800 extern void thread_stack_cache_init(void);
1802 #ifdef CONFIG_DEBUG_STACK_USAGE
1803 static inline unsigned long stack_not_used(struct task_struct *p)
1805 unsigned long *n = end_of_stack(p);
1807 do { /* Skip over canary */
1808 # ifdef CONFIG_STACK_GROWSUP
1815 # ifdef CONFIG_STACK_GROWSUP
1816 return (unsigned long)end_of_stack(p) - (unsigned long)n;
1818 return (unsigned long)n - (unsigned long)end_of_stack(p);
1822 extern void set_task_stack_end_magic(struct task_struct *tsk);
1824 /* set thread flags in other task's structures
1825 * - see asm/thread_info.h for TIF_xxxx flags available
1827 static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
1829 set_ti_thread_flag(task_thread_info(tsk), flag);
1832 static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1834 clear_ti_thread_flag(task_thread_info(tsk), flag);
1837 static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
1839 return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
1842 static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1844 return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
1847 static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
1849 return test_ti_thread_flag(task_thread_info(tsk), flag);
1852 static inline void set_tsk_need_resched(struct task_struct *tsk)
1854 set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
1857 static inline void clear_tsk_need_resched(struct task_struct *tsk)
1859 clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
1862 static inline int test_tsk_need_resched(struct task_struct *tsk)
1864 return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
1868 * cond_resched() and cond_resched_lock(): latency reduction via
1869 * explicit rescheduling in places that are safe. The return
1870 * value indicates whether a reschedule was done in fact.
1871 * cond_resched_lock() will drop the spinlock before scheduling,
1872 * cond_resched_softirq() will enable bhs before scheduling.
1874 #ifndef CONFIG_PREEMPT
1875 extern int _cond_resched(void);
1877 static inline int _cond_resched(void) { return 0; }
1880 #define cond_resched() ({ \
1881 ___might_sleep(__FILE__, __LINE__, 0); \
1885 extern int __cond_resched_lock(spinlock_t *lock);
1887 #define cond_resched_lock(lock) ({ \
1888 ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
1889 __cond_resched_lock(lock); \
1892 extern int __cond_resched_softirq(void);
1894 #define cond_resched_softirq() ({ \
1895 ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
1896 __cond_resched_softirq(); \
1899 static inline void cond_resched_rcu(void)
1901 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
1909 * Does a critical section need to be broken due to another
1910 * task waiting?: (technically does not depend on CONFIG_PREEMPT,
1911 * but a general need for low latency)
1913 static inline int spin_needbreak(spinlock_t *lock)
1915 #ifdef CONFIG_PREEMPT
1916 return spin_is_contended(lock);
1922 static __always_inline bool need_resched(void)
1924 return unlikely(tif_need_resched());
1928 * Thread group CPU time accounting.
1930 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
1931 void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
1934 * Wrappers for p->thread_info->cpu access. No-op on UP.
1938 static inline unsigned int task_cpu(const struct task_struct *p)
1940 #ifdef CONFIG_THREAD_INFO_IN_TASK
1943 return task_thread_info(p)->cpu;
1947 static inline int task_node(const struct task_struct *p)
1949 return cpu_to_node(task_cpu(p));
1952 extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
1956 static inline unsigned int task_cpu(const struct task_struct *p)
1961 static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
1965 #endif /* CONFIG_SMP */
1968 * In order to reduce various lock holder preemption latencies provide an
1969 * interface to see if a vCPU is currently running or not.
1971 * This allows us to terminate optimistic spin loops and block, analogous to
1972 * the native optimistic spin heuristic of testing if the lock owner task is
1975 #ifndef vcpu_is_preempted
1976 # define vcpu_is_preempted(cpu) false
1979 extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
1980 extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
1982 #ifdef CONFIG_CGROUP_SCHED
1983 extern struct task_group root_task_group;
1984 #endif /* CONFIG_CGROUP_SCHED */
1986 extern int task_can_switch_user(struct user_struct *up,
1987 struct task_struct *tsk);
1989 #ifndef TASK_SIZE_OF
1990 #define TASK_SIZE_OF(tsk) TASK_SIZE