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