UPSTREAM: psi: pressure stall information for CPU, memory, and IO
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / include / linux / sched.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_H
3 #define _LINUX_SCHED_H
4
5 /*
6 * Define 'struct task_struct' and provide the main scheduler
7 * APIs (schedule(), wakeup variants, etc.)
8 */
9
10 #include <uapi/linux/sched.h>
11
12 #include <asm/current.h>
13
14 #include <linux/pid.h>
15 #include <linux/sem.h>
16 #include <linux/shm.h>
17 #include <linux/kcov.h>
18 #include <linux/mutex.h>
19 #include <linux/plist.h>
20 #include <linux/hrtimer.h>
21 #include <linux/seccomp.h>
22 #include <linux/nodemask.h>
23 #include <linux/rcupdate.h>
24 #include <linux/resource.h>
25 #include <linux/latencytop.h>
26 #include <linux/sched/prio.h>
27 #include <linux/signal_types.h>
28 #include <linux/psi_types.h>
29 #include <linux/mm_types_task.h>
30 #include <linux/task_io_accounting.h>
31
32 /* task_struct member predeclarations (sorted alphabetically): */
33 struct audit_context;
34 struct backing_dev_info;
35 struct bio_list;
36 struct blk_plug;
37 struct cfs_rq;
38 struct fs_struct;
39 struct futex_pi_state;
40 struct io_context;
41 struct mempolicy;
42 struct nameidata;
43 struct nsproxy;
44 struct perf_event_context;
45 struct pid_namespace;
46 struct pipe_inode_info;
47 struct rcu_node;
48 struct reclaim_state;
49 struct robust_list_head;
50 struct sched_attr;
51 struct sched_param;
52 struct seq_file;
53 struct sighand_struct;
54 struct signal_struct;
55 struct task_delay_info;
56 struct task_group;
57
58 /*
59 * Task state bitmask. NOTE! These bits are also
60 * encoded in fs/proc/array.c: get_task_state().
61 *
62 * We have two separate sets of flags: task->state
63 * is about runnability, while task->exit_state are
64 * about the task exiting. Confusing, but this way
65 * modifying one set can't modify the other one by
66 * mistake.
67 */
68
69 /* Used in tsk->state: */
70 #define TASK_RUNNING 0x0000
71 #define TASK_INTERRUPTIBLE 0x0001
72 #define TASK_UNINTERRUPTIBLE 0x0002
73 #define __TASK_STOPPED 0x0004
74 #define __TASK_TRACED 0x0008
75 /* Used in tsk->exit_state: */
76 #define EXIT_DEAD 0x0010
77 #define EXIT_ZOMBIE 0x0020
78 #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
79 /* Used in tsk->state again: */
80 #define TASK_PARKED 0x0040
81 #define TASK_DEAD 0x0080
82 #define TASK_WAKEKILL 0x0100
83 #define TASK_WAKING 0x0200
84 #define TASK_NOLOAD 0x0400
85 #define TASK_NEW 0x0800
86 #define TASK_STATE_MAX 0x1000
87
88 /* Convenience macros for the sake of set_current_state: */
89 #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
90 #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
91 #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
92
93 #define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
94
95 /* Convenience macros for the sake of wake_up(): */
96 #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
97 #define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
98
99 /* get_task_state(): */
100 #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
101 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
102 __TASK_TRACED | EXIT_DEAD | EXIT_ZOMBIE | \
103 TASK_PARKED)
104
105 #define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
106
107 #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
108
109 #define task_is_stopped_or_traced(task) ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
110
111 #define task_contributes_to_load(task) ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
112 (task->flags & PF_FROZEN) == 0 && \
113 (task->state & TASK_NOLOAD) == 0)
114
115 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
116
117 /*
118 * Special states are those that do not use the normal wait-loop pattern. See
119 * the comment with set_special_state().
120 */
121 #define is_special_task_state(state) \
122 ((state) & (__TASK_STOPPED | __TASK_TRACED | TASK_DEAD))
123
124 #define __set_current_state(state_value) \
125 do { \
126 WARN_ON_ONCE(is_special_task_state(state_value));\
127 current->task_state_change = _THIS_IP_; \
128 current->state = (state_value); \
129 } while (0)
130
131 #define set_current_state(state_value) \
132 do { \
133 WARN_ON_ONCE(is_special_task_state(state_value));\
134 current->task_state_change = _THIS_IP_; \
135 smp_store_mb(current->state, (state_value)); \
136 } while (0)
137
138 #define set_special_state(state_value) \
139 do { \
140 unsigned long flags; /* may shadow */ \
141 WARN_ON_ONCE(!is_special_task_state(state_value)); \
142 raw_spin_lock_irqsave(&current->pi_lock, flags); \
143 current->task_state_change = _THIS_IP_; \
144 current->state = (state_value); \
145 raw_spin_unlock_irqrestore(&current->pi_lock, flags); \
146 } while (0)
147 #else
148 /*
149 * set_current_state() includes a barrier so that the write of current->state
150 * is correctly serialised wrt the caller's subsequent test of whether to
151 * actually sleep:
152 *
153 * for (;;) {
154 * set_current_state(TASK_UNINTERRUPTIBLE);
155 * if (!need_sleep)
156 * break;
157 *
158 * schedule();
159 * }
160 * __set_current_state(TASK_RUNNING);
161 *
162 * If the caller does not need such serialisation (because, for instance, the
163 * condition test and condition change and wakeup are under the same lock) then
164 * use __set_current_state().
165 *
166 * The above is typically ordered against the wakeup, which does:
167 *
168 * need_sleep = false;
169 * wake_up_state(p, TASK_UNINTERRUPTIBLE);
170 *
171 * Where wake_up_state() (and all other wakeup primitives) imply enough
172 * barriers to order the store of the variable against wakeup.
173 *
174 * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
175 * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
176 * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
177 *
178 * However, with slightly different timing the wakeup TASK_RUNNING store can
179 * also collide with the TASK_UNINTERRUPTIBLE store. Loosing that store is not
180 * a problem either because that will result in one extra go around the loop
181 * and our @cond test will save the day.
182 *
183 * Also see the comments of try_to_wake_up().
184 */
185 #define __set_current_state(state_value) \
186 current->state = (state_value)
187
188 #define set_current_state(state_value) \
189 smp_store_mb(current->state, (state_value))
190
191 /*
192 * set_special_state() should be used for those states when the blocking task
193 * can not use the regular condition based wait-loop. In that case we must
194 * serialize against wakeups such that any possible in-flight TASK_RUNNING stores
195 * will not collide with our state change.
196 */
197 #define set_special_state(state_value) \
198 do { \
199 unsigned long flags; /* may shadow */ \
200 raw_spin_lock_irqsave(&current->pi_lock, flags); \
201 current->state = (state_value); \
202 raw_spin_unlock_irqrestore(&current->pi_lock, flags); \
203 } while (0)
204
205 #endif
206
207 /* Task command name length: */
208 #define TASK_COMM_LEN 16
209
210 enum task_event {
211 PUT_PREV_TASK = 0,
212 PICK_NEXT_TASK = 1,
213 TASK_WAKE = 2,
214 TASK_MIGRATE = 3,
215 TASK_UPDATE = 4,
216 IRQ_UPDATE = 5,
217 };
218
219 extern cpumask_var_t cpu_isolated_map;
220
221 extern void scheduler_tick(void);
222
223 #define MAX_SCHEDULE_TIMEOUT LONG_MAX
224
225 extern long schedule_timeout(long timeout);
226 extern long schedule_timeout_interruptible(long timeout);
227 extern long schedule_timeout_killable(long timeout);
228 extern long schedule_timeout_uninterruptible(long timeout);
229 extern long schedule_timeout_idle(long timeout);
230 asmlinkage void schedule(void);
231 extern void schedule_preempt_disabled(void);
232
233 extern int __must_check io_schedule_prepare(void);
234 extern void io_schedule_finish(int token);
235 extern long io_schedule_timeout(long timeout);
236 extern void io_schedule(void);
237
238 /**
239 * struct prev_cputime - snapshot of system and user cputime
240 * @utime: time spent in user mode
241 * @stime: time spent in system mode
242 * @lock: protects the above two fields
243 *
244 * Stores previous user/system time values such that we can guarantee
245 * monotonicity.
246 */
247 struct prev_cputime {
248 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
249 u64 utime;
250 u64 stime;
251 raw_spinlock_t lock;
252 #endif
253 };
254
255 /**
256 * struct task_cputime - collected CPU time counts
257 * @utime: time spent in user mode, in nanoseconds
258 * @stime: time spent in kernel mode, in nanoseconds
259 * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
260 *
261 * This structure groups together three kinds of CPU time that are tracked for
262 * threads and thread groups. Most things considering CPU time want to group
263 * these counts together and treat all three of them in parallel.
264 */
265 struct task_cputime {
266 u64 utime;
267 u64 stime;
268 unsigned long long sum_exec_runtime;
269 };
270
271 /* Alternate field names when used on cache expirations: */
272 #define virt_exp utime
273 #define prof_exp stime
274 #define sched_exp sum_exec_runtime
275
276 enum vtime_state {
277 /* Task is sleeping or running in a CPU with VTIME inactive: */
278 VTIME_INACTIVE = 0,
279 /* Task runs in userspace in a CPU with VTIME active: */
280 VTIME_USER,
281 /* Task runs in kernelspace in a CPU with VTIME active: */
282 VTIME_SYS,
283 };
284
285 struct vtime {
286 seqcount_t seqcount;
287 unsigned long long starttime;
288 enum vtime_state state;
289 u64 utime;
290 u64 stime;
291 u64 gtime;
292 };
293
294 struct sched_info {
295 #ifdef CONFIG_SCHED_INFO
296 /* Cumulative counters: */
297
298 /* # of times we have run on this CPU: */
299 unsigned long pcount;
300
301 /* Time spent waiting on a runqueue: */
302 unsigned long long run_delay;
303
304 /* Timestamps: */
305
306 /* When did we last run on a CPU? */
307 unsigned long long last_arrival;
308
309 /* When were we last queued to run? */
310 unsigned long long last_queued;
311
312 #endif /* CONFIG_SCHED_INFO */
313 };
314
315 /*
316 * Integer metrics need fixed point arithmetic, e.g., sched/fair
317 * has a few: load, load_avg, util_avg, freq, and capacity.
318 *
319 * We define a basic fixed point arithmetic range, and then formalize
320 * all these metrics based on that basic range.
321 */
322 # define SCHED_FIXEDPOINT_SHIFT 10
323 # define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
324
325 struct load_weight {
326 unsigned long weight;
327 u32 inv_weight;
328 };
329
330 /**
331 * struct util_est - Estimation utilization of FAIR tasks
332 * @enqueued: instantaneous estimated utilization of a task/cpu
333 * @ewma: the Exponential Weighted Moving Average (EWMA)
334 * utilization of a task
335 *
336 * Support data structure to track an Exponential Weighted Moving Average
337 * (EWMA) of a FAIR task's utilization. New samples are added to the moving
338 * average each time a task completes an activation. Sample's weight is chosen
339 * so that the EWMA will be relatively insensitive to transient changes to the
340 * task's workload.
341 *
342 * The enqueued attribute has a slightly different meaning for tasks and cpus:
343 * - task: the task's util_avg at last task dequeue time
344 * - cfs_rq: the sum of util_est.enqueued for each RUNNABLE task on that CPU
345 * Thus, the util_est.enqueued of a task represents the contribution on the
346 * estimated utilization of the CPU where that task is currently enqueued.
347 *
348 * Only for tasks we track a moving average of the past instantaneous
349 * estimated utilization. This allows to absorb sporadic drops in utilization
350 * of an otherwise almost periodic task.
351 */
352 struct util_est {
353 unsigned int enqueued;
354 unsigned int ewma;
355 #define UTIL_EST_WEIGHT_SHIFT 2
356 };
357
358 /*
359 * The load_avg/util_avg accumulates an infinite geometric series
360 * (see __update_load_avg() in kernel/sched/fair.c).
361 *
362 * [load_avg definition]
363 *
364 * load_avg = runnable% * scale_load_down(load)
365 *
366 * where runnable% is the time ratio that a sched_entity is runnable.
367 * For cfs_rq, it is the aggregated load_avg of all runnable and
368 * blocked sched_entities.
369 *
370 * load_avg may also take frequency scaling into account:
371 *
372 * load_avg = runnable% * scale_load_down(load) * freq%
373 *
374 * where freq% is the CPU frequency normalized to the highest frequency.
375 *
376 * [util_avg definition]
377 *
378 * util_avg = running% * SCHED_CAPACITY_SCALE
379 *
380 * where running% is the time ratio that a sched_entity is running on
381 * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
382 * and blocked sched_entities.
383 *
384 * util_avg may also factor frequency scaling and CPU capacity scaling:
385 *
386 * util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
387 *
388 * where freq% is the same as above, and capacity% is the CPU capacity
389 * normalized to the greatest capacity (due to uarch differences, etc).
390 *
391 * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
392 * themselves are in the range of [0, 1]. To do fixed point arithmetics,
393 * we therefore scale them to as large a range as necessary. This is for
394 * example reflected by util_avg's SCHED_CAPACITY_SCALE.
395 *
396 * [Overflow issue]
397 *
398 * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
399 * with the highest load (=88761), always runnable on a single cfs_rq,
400 * and should not overflow as the number already hits PID_MAX_LIMIT.
401 *
402 * For all other cases (including 32-bit kernels), struct load_weight's
403 * weight will overflow first before we do, because:
404 *
405 * Max(load_avg) <= Max(load.weight)
406 *
407 * Then it is the load_weight's responsibility to consider overflow
408 * issues.
409 */
410 struct sched_avg {
411 u64 last_update_time;
412 u64 load_sum;
413 u32 util_sum;
414 u32 period_contrib;
415 unsigned long load_avg;
416 unsigned long util_avg;
417 struct util_est util_est;
418 };
419
420 struct ontime_avg {
421 u64 ontime_migration_time;
422 u64 load_sum;
423 u32 period_contrib;
424 unsigned long load_avg;
425 };
426
427 struct ontime_entity {
428 struct ontime_avg avg;
429 int migrating;
430 int cpu;
431 };
432
433 struct sched_statistics {
434 #ifdef CONFIG_SCHEDSTATS
435 u64 wait_start;
436 u64 wait_max;
437 u64 wait_count;
438 u64 wait_sum;
439 u64 iowait_count;
440 u64 iowait_sum;
441
442 u64 sleep_start;
443 u64 sleep_max;
444 s64 sum_sleep_runtime;
445
446 u64 block_start;
447 u64 block_max;
448 u64 exec_max;
449 u64 slice_max;
450
451 u64 nr_migrations_cold;
452 u64 nr_failed_migrations_affine;
453 u64 nr_failed_migrations_running;
454 u64 nr_failed_migrations_hot;
455 u64 nr_forced_migrations;
456
457 u64 nr_wakeups;
458 u64 nr_wakeups_sync;
459 u64 nr_wakeups_migrate;
460 u64 nr_wakeups_local;
461 u64 nr_wakeups_remote;
462 u64 nr_wakeups_affine;
463 u64 nr_wakeups_affine_attempts;
464 u64 nr_wakeups_passive;
465 u64 nr_wakeups_idle;
466 #endif
467 };
468
469 struct sched_entity {
470 /* For load-balancing: */
471 struct load_weight load;
472 struct rb_node run_node;
473 struct list_head group_node;
474 unsigned int on_rq;
475
476 u64 exec_start;
477 u64 sum_exec_runtime;
478 u64 vruntime;
479 u64 prev_sum_exec_runtime;
480
481 u64 nr_migrations;
482
483 struct sched_statistics statistics;
484
485 #ifdef CONFIG_FAIR_GROUP_SCHED
486 int depth;
487 struct sched_entity *parent;
488 /* rq on which this entity is (to be) queued: */
489 struct cfs_rq *cfs_rq;
490 /* rq "owned" by this entity/group: */
491 struct cfs_rq *my_q;
492 #endif
493
494 #ifdef CONFIG_SMP
495 /*
496 * Per entity load average tracking.
497 *
498 * Put into separate cache line so it does not
499 * collide with read-mostly values above.
500 */
501 struct sched_avg avg ____cacheline_aligned_in_smp;
502 #endif
503 struct ontime_entity ontime;
504 };
505
506 #ifdef CONFIG_SCHED_WALT
507 #define RAVG_HIST_SIZE_MAX 5
508
509 /* ravg represents frequency scaled cpu-demand of tasks */
510 struct ravg {
511 /*
512 * 'mark_start' marks the beginning of an event (task waking up, task
513 * starting to execute, task being preempted) within a window
514 *
515 * 'sum' represents how runnable a task has been within current
516 * window. It incorporates both running time and wait time and is
517 * frequency scaled.
518 *
519 * 'sum_history' keeps track of history of 'sum' seen over previous
520 * RAVG_HIST_SIZE windows. Windows where task was entirely sleeping are
521 * ignored.
522 *
523 * 'demand' represents maximum sum seen over previous
524 * sysctl_sched_ravg_hist_size windows. 'demand' could drive frequency
525 * demand for tasks.
526 *
527 * 'curr_window' represents task's contribution to cpu busy time
528 * statistics (rq->curr_runnable_sum) in current window
529 *
530 * 'prev_window' represents task's contribution to cpu busy time
531 * statistics (rq->prev_runnable_sum) in previous window
532 */
533 u64 mark_start;
534 u32 sum, demand;
535 u32 sum_history[RAVG_HIST_SIZE_MAX];
536 u32 curr_window, prev_window;
537 u16 active_windows;
538 };
539 #endif
540
541 struct sched_rt_entity {
542 struct list_head run_list;
543 unsigned long timeout;
544 unsigned long watchdog_stamp;
545 unsigned int time_slice;
546 unsigned short on_rq;
547 unsigned short on_list;
548
549 struct sched_rt_entity *back;
550 #ifdef CONFIG_RT_GROUP_SCHED
551 struct sched_rt_entity *parent;
552 /* rq on which this entity is (to be) queued: */
553 struct rt_rq *rt_rq;
554 /* rq "owned" by this entity/group: */
555 struct rt_rq *my_q;
556 #endif
557
558 #ifdef CONFIG_SMP
559 #ifdef CONFIG_SCHED_USE_FLUID_RT
560 int sync_flag;
561 #endif
562 /*
563 * Per entity load average tracking.
564 *
565 * Put into separate cache line so it does not
566 * collide with read-mostly values above.
567 */
568 struct sched_avg avg;// ____cacheline_aligned_in_smp;
569 #endif
570 } __randomize_layout;
571
572 struct sched_dl_entity {
573 struct rb_node rb_node;
574
575 /*
576 * Original scheduling parameters. Copied here from sched_attr
577 * during sched_setattr(), they will remain the same until
578 * the next sched_setattr().
579 */
580 u64 dl_runtime; /* Maximum runtime for each instance */
581 u64 dl_deadline; /* Relative deadline of each instance */
582 u64 dl_period; /* Separation of two instances (period) */
583 u64 dl_bw; /* dl_runtime / dl_period */
584 u64 dl_density; /* dl_runtime / dl_deadline */
585
586 /*
587 * Actual scheduling parameters. Initialized with the values above,
588 * they are continously updated during task execution. Note that
589 * the remaining runtime could be < 0 in case we are in overrun.
590 */
591 s64 runtime; /* Remaining runtime for this instance */
592 u64 deadline; /* Absolute deadline for this instance */
593 unsigned int flags; /* Specifying the scheduler behaviour */
594
595 /*
596 * Some bool flags:
597 *
598 * @dl_throttled tells if we exhausted the runtime. If so, the
599 * task has to wait for a replenishment to be performed at the
600 * next firing of dl_timer.
601 *
602 * @dl_boosted tells if we are boosted due to DI. If so we are
603 * outside bandwidth enforcement mechanism (but only until we
604 * exit the critical section);
605 *
606 * @dl_yielded tells if task gave up the CPU before consuming
607 * all its available runtime during the last job.
608 *
609 * @dl_non_contending tells if the task is inactive while still
610 * contributing to the active utilization. In other words, it
611 * indicates if the inactive timer has been armed and its handler
612 * has not been executed yet. This flag is useful to avoid race
613 * conditions between the inactive timer handler and the wakeup
614 * code.
615 */
616 int dl_throttled;
617 int dl_boosted;
618 int dl_yielded;
619 int dl_non_contending;
620
621 /*
622 * Bandwidth enforcement timer. Each -deadline task has its
623 * own bandwidth to be enforced, thus we need one timer per task.
624 */
625 struct hrtimer dl_timer;
626
627 /*
628 * Inactive timer, responsible for decreasing the active utilization
629 * at the "0-lag time". When a -deadline task blocks, it contributes
630 * to GRUB's active utilization until the "0-lag time", hence a
631 * timer is needed to decrease the active utilization at the correct
632 * time.
633 */
634 struct hrtimer inactive_timer;
635 };
636
637 union rcu_special {
638 struct {
639 u8 blocked;
640 u8 need_qs;
641 u8 exp_need_qs;
642
643 /* Otherwise the compiler can store garbage here: */
644 u8 pad;
645 } b; /* Bits. */
646 u32 s; /* Set of bits. */
647 };
648
649 enum perf_event_task_context {
650 perf_invalid_context = -1,
651 perf_hw_context = 0,
652 perf_sw_context,
653 perf_nr_task_contexts,
654 };
655
656 struct wake_q_node {
657 struct wake_q_node *next;
658 };
659
660 struct task_struct {
661 #ifdef CONFIG_THREAD_INFO_IN_TASK
662 /*
663 * For reasons of header soup (see current_thread_info()), this
664 * must be the first element of task_struct.
665 */
666 struct thread_info thread_info;
667 #endif
668 /* -1 unrunnable, 0 runnable, >0 stopped: */
669 volatile long state;
670
671 /*
672 * This begins the randomizable portion of task_struct. Only
673 * scheduling-critical items should be added above here.
674 */
675 randomized_struct_fields_start
676
677 void *stack;
678 atomic_t usage;
679 /* Per task flags (PF_*), defined further below: */
680 unsigned int flags;
681 unsigned int ptrace;
682
683 #ifdef CONFIG_SMP
684 struct llist_node wake_entry;
685 int on_cpu;
686 #ifdef CONFIG_THREAD_INFO_IN_TASK
687 /* Current CPU: */
688 unsigned int cpu;
689 #endif
690 unsigned int wakee_flips;
691 unsigned long wakee_flip_decay_ts;
692 struct task_struct *last_wakee;
693
694 int wake_cpu;
695 #endif
696 int on_rq;
697
698 int prio;
699 int static_prio;
700 int normal_prio;
701 unsigned int rt_priority;
702
703 const struct sched_class *sched_class;
704 struct sched_entity se;
705 struct sched_rt_entity rt;
706 #ifdef CONFIG_SCHED_WALT
707 struct ravg ravg;
708 /*
709 * 'init_load_pct' represents the initial task load assigned to children
710 * of this task
711 */
712 u32 init_load_pct;
713 u64 last_sleep_ts;
714 #endif
715 #ifdef CONFIG_SCHED_USE_FLUID_RT
716 int victim_flag;
717 #endif
718
719 #ifdef CONFIG_SCHED_EMS
720 struct task_band *band;
721 struct list_head band_members;
722 #endif
723
724 #ifdef CONFIG_CGROUP_SCHED
725 struct task_group *sched_task_group;
726 #endif
727 struct sched_dl_entity dl;
728
729 #ifdef CONFIG_PREEMPT_NOTIFIERS
730 /* List of struct preempt_notifier: */
731 struct hlist_head preempt_notifiers;
732 #endif
733
734 #ifdef CONFIG_BLK_DEV_IO_TRACE
735 unsigned int btrace_seq;
736 #endif
737
738 unsigned int policy;
739 int nr_cpus_allowed;
740 cpumask_t cpus_allowed;
741
742 #ifdef CONFIG_PREEMPT_RCU
743 int rcu_read_lock_nesting;
744 union rcu_special rcu_read_unlock_special;
745 struct list_head rcu_node_entry;
746 struct rcu_node *rcu_blocked_node;
747 #endif /* #ifdef CONFIG_PREEMPT_RCU */
748
749 #ifdef CONFIG_TASKS_RCU
750 unsigned long rcu_tasks_nvcsw;
751 u8 rcu_tasks_holdout;
752 u8 rcu_tasks_idx;
753 int rcu_tasks_idle_cpu;
754 struct list_head rcu_tasks_holdout_list;
755 #endif /* #ifdef CONFIG_TASKS_RCU */
756
757 struct sched_info sched_info;
758
759 struct list_head tasks;
760 #ifdef CONFIG_SMP
761 struct plist_node pushable_tasks;
762 struct rb_node pushable_dl_tasks;
763 #endif
764
765 struct mm_struct *mm;
766 struct mm_struct *active_mm;
767
768 /* Per-thread vma caching: */
769 struct vmacache vmacache;
770
771 #ifdef SPLIT_RSS_COUNTING
772 struct task_rss_stat rss_stat;
773 #endif
774 int exit_state;
775 int exit_code;
776 int exit_signal;
777 /* The signal sent when the parent dies: */
778 int pdeath_signal;
779 /* JOBCTL_*, siglock protected: */
780 unsigned long jobctl;
781
782 /* Used for emulating ABI behavior of previous Linux versions: */
783 unsigned int personality;
784
785 /* Scheduler bits, serialized by scheduler locks: */
786 unsigned sched_reset_on_fork:1;
787 unsigned sched_contributes_to_load:1;
788 unsigned sched_migrated:1;
789 unsigned sched_remote_wakeup:1;
790 #ifdef CONFIG_PSI
791 unsigned sched_psi_wake_requeue:1;
792 #endif
793
794 /* Force alignment to the next boundary: */
795 unsigned :0;
796
797 /* Unserialized, strictly 'current' */
798
799 /* Bit to tell LSMs we're in execve(): */
800 unsigned in_execve:1;
801 unsigned in_iowait:1;
802 #ifndef TIF_RESTORE_SIGMASK
803 unsigned restore_sigmask:1;
804 #endif
805 #ifdef CONFIG_MEMCG
806 unsigned memcg_may_oom:1;
807 #ifndef CONFIG_SLOB
808 unsigned memcg_kmem_skip_account:1;
809 #endif
810 #endif
811 #ifdef CONFIG_COMPAT_BRK
812 unsigned brk_randomized:1;
813 #endif
814 #ifdef CONFIG_CGROUPS
815 /* disallow userland-initiated cgroup migration */
816 unsigned no_cgroup_migration:1;
817 #endif
818
819 unsigned long atomic_flags; /* Flags requiring atomic access. */
820
821 struct restart_block restart_block;
822
823 pid_t pid;
824 pid_t tgid;
825
826 #ifdef CONFIG_CC_STACKPROTECTOR
827 /* Canary value for the -fstack-protector GCC feature: */
828 unsigned long stack_canary;
829 #endif
830 /*
831 * Pointers to the (original) parent process, youngest child, younger sibling,
832 * older sibling, respectively. (p->father can be replaced with
833 * p->real_parent->pid)
834 */
835
836 /* Real parent process: */
837 struct task_struct __rcu *real_parent;
838
839 /* Recipient of SIGCHLD, wait4() reports: */
840 struct task_struct __rcu *parent;
841
842 /*
843 * Children/sibling form the list of natural children:
844 */
845 struct list_head children;
846 struct list_head sibling;
847 struct task_struct *group_leader;
848
849 /*
850 * 'ptraced' is the list of tasks this task is using ptrace() on.
851 *
852 * This includes both natural children and PTRACE_ATTACH targets.
853 * 'ptrace_entry' is this task's link on the p->parent->ptraced list.
854 */
855 struct list_head ptraced;
856 struct list_head ptrace_entry;
857
858 /* PID/PID hash table linkage. */
859 struct pid_link pids[PIDTYPE_MAX];
860 struct list_head thread_group;
861 struct list_head thread_node;
862
863 struct completion *vfork_done;
864
865 /* CLONE_CHILD_SETTID: */
866 int __user *set_child_tid;
867
868 /* CLONE_CHILD_CLEARTID: */
869 int __user *clear_child_tid;
870
871 u64 utime;
872 u64 stime;
873 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
874 u64 utimescaled;
875 u64 stimescaled;
876 #endif
877 u64 gtime;
878 #ifdef CONFIG_CPU_FREQ_TIMES
879 u64 *time_in_state;
880 unsigned int max_state;
881 #endif
882 struct prev_cputime prev_cputime;
883 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
884 struct vtime vtime;
885 #endif
886
887 #ifdef CONFIG_NO_HZ_FULL
888 atomic_t tick_dep_mask;
889 #endif
890 /* Context switch counts: */
891 unsigned long nvcsw;
892 unsigned long nivcsw;
893
894 /* Monotonic time in nsecs: */
895 u64 start_time;
896
897 /* Boot based time in nsecs: */
898 u64 real_start_time;
899
900 /* MM fault and swap info: this can arguably be seen as either mm-specific or thread-specific: */
901 unsigned long min_flt;
902 unsigned long maj_flt;
903
904 #ifdef CONFIG_POSIX_TIMERS
905 struct task_cputime cputime_expires;
906 struct list_head cpu_timers[3];
907 #endif
908
909 /* Process credentials: */
910
911 /* Tracer's credentials at attach: */
912 const struct cred __rcu *ptracer_cred;
913
914 /* Objective and real subjective task credentials (COW): */
915 const struct cred __rcu *real_cred;
916
917 /* Effective (overridable) subjective task credentials (COW): */
918 const struct cred __rcu *cred;
919
920 /*
921 * executable name, excluding path.
922 *
923 * - normally initialized setup_new_exec()
924 * - access it with [gs]et_task_comm()
925 * - lock it with task_lock()
926 */
927 char comm[TASK_COMM_LEN];
928
929 struct nameidata *nameidata;
930
931 #ifdef CONFIG_SYSVIPC
932 struct sysv_sem sysvsem;
933 struct sysv_shm sysvshm;
934 #endif
935 #ifdef CONFIG_DETECT_HUNG_TASK
936 unsigned long last_switch_count;
937 #endif
938 /* Filesystem information: */
939 struct fs_struct *fs;
940
941 /* Open file information: */
942 struct files_struct *files;
943
944 /* Namespaces: */
945 struct nsproxy *nsproxy;
946
947 /* Signal handlers: */
948 struct signal_struct *signal;
949 struct sighand_struct *sighand;
950 sigset_t blocked;
951 sigset_t real_blocked;
952 /* Restored if set_restore_sigmask() was used: */
953 sigset_t saved_sigmask;
954 struct sigpending pending;
955 unsigned long sas_ss_sp;
956 size_t sas_ss_size;
957 unsigned int sas_ss_flags;
958
959 struct callback_head *task_works;
960
961 struct audit_context *audit_context;
962 #ifdef CONFIG_AUDITSYSCALL
963 kuid_t loginuid;
964 unsigned int sessionid;
965 #endif
966 struct seccomp seccomp;
967
968 /* Thread group tracking: */
969 u32 parent_exec_id;
970 u32 self_exec_id;
971
972 /* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */
973 spinlock_t alloc_lock;
974
975 /* Protection of the PI data structures: */
976 raw_spinlock_t pi_lock;
977
978 struct wake_q_node wake_q;
979
980 #ifdef CONFIG_RT_MUTEXES
981 /* PI waiters blocked on a rt_mutex held by this task: */
982 struct rb_root_cached pi_waiters;
983 /* Updated under owner's pi_lock and rq lock */
984 struct task_struct *pi_top_task;
985 /* Deadlock detection and priority inheritance handling: */
986 struct rt_mutex_waiter *pi_blocked_on;
987 #endif
988
989 #ifdef CONFIG_DEBUG_MUTEXES
990 /* Mutex deadlock detection: */
991 struct mutex_waiter *blocked_on;
992 #endif
993
994 #ifdef CONFIG_TRACE_IRQFLAGS
995 unsigned int irq_events;
996 unsigned long hardirq_enable_ip;
997 unsigned long hardirq_disable_ip;
998 unsigned int hardirq_enable_event;
999 unsigned int hardirq_disable_event;
1000 int hardirqs_enabled;
1001 int hardirq_context;
1002 unsigned long softirq_disable_ip;
1003 unsigned long softirq_enable_ip;
1004 unsigned int softirq_disable_event;
1005 unsigned int softirq_enable_event;
1006 int softirqs_enabled;
1007 int softirq_context;
1008 #endif
1009
1010 #ifdef CONFIG_LOCKDEP
1011 # define MAX_LOCK_DEPTH 48UL
1012 u64 curr_chain_key;
1013 int lockdep_depth;
1014 unsigned int lockdep_recursion;
1015 struct held_lock held_locks[MAX_LOCK_DEPTH];
1016 #endif
1017
1018 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
1019 #define MAX_XHLOCKS_NR 64UL
1020 struct hist_lock *xhlocks; /* Crossrelease history locks */
1021 unsigned int xhlock_idx;
1022 /* For restoring at history boundaries */
1023 unsigned int xhlock_idx_hist[XHLOCK_CTX_NR];
1024 unsigned int hist_id;
1025 /* For overwrite check at each context exit */
1026 unsigned int hist_id_save[XHLOCK_CTX_NR];
1027 #endif
1028
1029 #ifdef CONFIG_UBSAN
1030 unsigned int in_ubsan;
1031 #endif
1032
1033 /* Journalling filesystem info: */
1034 void *journal_info;
1035
1036 /* Stacked block device info: */
1037 struct bio_list *bio_list;
1038
1039 #ifdef CONFIG_BLOCK
1040 /* Stack plugging: */
1041 struct blk_plug *plug;
1042 #endif
1043
1044 /* VM state: */
1045 struct reclaim_state *reclaim_state;
1046
1047 struct backing_dev_info *backing_dev_info;
1048
1049 struct io_context *io_context;
1050
1051 /* Ptrace state: */
1052 unsigned long ptrace_message;
1053 siginfo_t *last_siginfo;
1054
1055 struct task_io_accounting ioac;
1056 #ifdef CONFIG_PSI
1057 /* Pressure stall state */
1058 unsigned int psi_flags;
1059 #endif
1060 #ifdef CONFIG_TASK_XACCT
1061 /* Accumulated RSS usage: */
1062 u64 acct_rss_mem1;
1063 /* Accumulated virtual memory usage: */
1064 u64 acct_vm_mem1;
1065 /* stime + utime since last update: */
1066 u64 acct_timexpd;
1067 #endif
1068 #ifdef CONFIG_CPUSETS
1069 /* Protected by ->alloc_lock: */
1070 nodemask_t mems_allowed;
1071 /* Seqence number to catch updates: */
1072 seqcount_t mems_allowed_seq;
1073 int cpuset_mem_spread_rotor;
1074 int cpuset_slab_spread_rotor;
1075 #endif
1076 #ifdef CONFIG_CGROUPS
1077 /* Control Group info protected by css_set_lock: */
1078 struct css_set __rcu *cgroups;
1079 /* cg_list protected by css_set_lock and tsk->alloc_lock: */
1080 struct list_head cg_list;
1081 #endif
1082 #ifdef CONFIG_INTEL_RDT
1083 u32 closid;
1084 u32 rmid;
1085 #endif
1086 #ifdef CONFIG_FUTEX
1087 struct robust_list_head __user *robust_list;
1088 #ifdef CONFIG_COMPAT
1089 struct compat_robust_list_head __user *compat_robust_list;
1090 #endif
1091 struct list_head pi_state_list;
1092 struct futex_pi_state *pi_state_cache;
1093 #endif
1094 #ifdef CONFIG_PERF_EVENTS
1095 struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1096 struct mutex perf_event_mutex;
1097 struct list_head perf_event_list;
1098 #endif
1099 #ifdef CONFIG_DEBUG_PREEMPT
1100 unsigned long preempt_disable_ip;
1101 #endif
1102 #ifdef CONFIG_NUMA
1103 /* Protected by alloc_lock: */
1104 struct mempolicy *mempolicy;
1105 short il_prev;
1106 short pref_node_fork;
1107 #endif
1108 #ifdef CONFIG_NUMA_BALANCING
1109 int numa_scan_seq;
1110 unsigned int numa_scan_period;
1111 unsigned int numa_scan_period_max;
1112 int numa_preferred_nid;
1113 unsigned long numa_migrate_retry;
1114 /* Migration stamp: */
1115 u64 node_stamp;
1116 u64 last_task_numa_placement;
1117 u64 last_sum_exec_runtime;
1118 struct callback_head numa_work;
1119
1120 struct list_head numa_entry;
1121 struct numa_group *numa_group;
1122
1123 /*
1124 * numa_faults is an array split into four regions:
1125 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1126 * in this precise order.
1127 *
1128 * faults_memory: Exponential decaying average of faults on a per-node
1129 * basis. Scheduling placement decisions are made based on these
1130 * counts. The values remain static for the duration of a PTE scan.
1131 * faults_cpu: Track the nodes the process was running on when a NUMA
1132 * hinting fault was incurred.
1133 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1134 * during the current scan window. When the scan completes, the counts
1135 * in faults_memory and faults_cpu decay and these values are copied.
1136 */
1137 unsigned long *numa_faults;
1138 unsigned long total_numa_faults;
1139
1140 /*
1141 * numa_faults_locality tracks if faults recorded during the last
1142 * scan window were remote/local or failed to migrate. The task scan
1143 * period is adapted based on the locality of the faults with different
1144 * weights depending on whether they were shared or private faults
1145 */
1146 unsigned long numa_faults_locality[3];
1147
1148 unsigned long numa_pages_migrated;
1149 #endif /* CONFIG_NUMA_BALANCING */
1150
1151 struct tlbflush_unmap_batch tlb_ubc;
1152
1153 struct rcu_head rcu;
1154
1155 /* Cache last used pipe for splice(): */
1156 struct pipe_inode_info *splice_pipe;
1157
1158 struct page_frag task_frag;
1159
1160 #ifdef CONFIG_TASK_DELAY_ACCT
1161 struct task_delay_info *delays;
1162 #endif
1163
1164 #ifdef CONFIG_FAULT_INJECTION
1165 int make_it_fail;
1166 unsigned int fail_nth;
1167 #endif
1168 /*
1169 * When (nr_dirtied >= nr_dirtied_pause), it's time to call
1170 * balance_dirty_pages() for a dirty throttling pause:
1171 */
1172 int nr_dirtied;
1173 int nr_dirtied_pause;
1174 /* Start of a write-and-pause period: */
1175 unsigned long dirty_paused_when;
1176
1177 #ifdef CONFIG_LATENCYTOP
1178 int latency_record_count;
1179 struct latency_record latency_record[LT_SAVECOUNT];
1180 #endif
1181 /*
1182 * Time slack values; these are used to round up poll() and
1183 * select() etc timeout values. These are in nanoseconds.
1184 */
1185 u64 timer_slack_ns;
1186 u64 default_timer_slack_ns;
1187
1188 #ifdef CONFIG_KASAN
1189 unsigned int kasan_depth;
1190 #endif
1191
1192 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1193 /* Index of current stored address in ret_stack: */
1194 int curr_ret_stack;
1195
1196 /* Stack of return addresses for return function tracing: */
1197 struct ftrace_ret_stack *ret_stack;
1198
1199 /* Timestamp for last schedule: */
1200 unsigned long long ftrace_timestamp;
1201
1202 /*
1203 * Number of functions that haven't been traced
1204 * because of depth overrun:
1205 */
1206 atomic_t trace_overrun;
1207
1208 /* Pause tracing: */
1209 atomic_t tracing_graph_pause;
1210 #endif
1211
1212 #ifdef CONFIG_TRACING
1213 /* State flags for use by tracers: */
1214 unsigned long trace;
1215
1216 /* Bitmask and counter of trace recursion: */
1217 unsigned long trace_recursion;
1218 #endif /* CONFIG_TRACING */
1219
1220 #ifdef CONFIG_KCOV
1221 /* Coverage collection mode enabled for this task (0 if disabled): */
1222 enum kcov_mode kcov_mode;
1223
1224 /* Size of the kcov_area: */
1225 unsigned int kcov_size;
1226
1227 /* Buffer for coverage collection: */
1228 void *kcov_area;
1229
1230 /* KCOV descriptor wired with this task or NULL: */
1231 struct kcov *kcov;
1232 #endif
1233
1234 #ifdef CONFIG_MEMCG
1235 struct mem_cgroup *memcg_in_oom;
1236 gfp_t memcg_oom_gfp_mask;
1237 int memcg_oom_order;
1238
1239 /* Number of pages to reclaim on returning to userland: */
1240 unsigned int memcg_nr_pages_over_high;
1241 #endif
1242
1243 #ifdef CONFIG_UPROBES
1244 struct uprobe_task *utask;
1245 #endif
1246 #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1247 unsigned int sequential_io;
1248 unsigned int sequential_io_avg;
1249 #endif
1250 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1251 unsigned long task_state_change;
1252 #endif
1253 int pagefault_disabled;
1254 #ifdef CONFIG_MMU
1255 struct task_struct *oom_reaper_list;
1256 #endif
1257 #ifdef CONFIG_VMAP_STACK
1258 struct vm_struct *stack_vm_area;
1259 #endif
1260 #ifdef CONFIG_THREAD_INFO_IN_TASK
1261 /* A live task holds one reference: */
1262 atomic_t stack_refcount;
1263 #endif
1264 #ifdef CONFIG_LIVEPATCH
1265 int patch_state;
1266 #endif
1267 #ifdef CONFIG_SECURITY
1268 /* Used by LSM modules for access restriction: */
1269 void *security;
1270 #endif
1271
1272 /*
1273 * New fields for task_struct should be added above here, so that
1274 * they are included in the randomized portion of task_struct.
1275 */
1276 randomized_struct_fields_end
1277
1278 /* CPU-specific state of this task: */
1279 struct thread_struct thread;
1280
1281 /*
1282 * WARNING: on x86, 'thread_struct' contains a variable-sized
1283 * structure. It *MUST* be at the end of 'task_struct'.
1284 *
1285 * Do not put anything below here!
1286 */
1287 };
1288
1289 static inline struct pid *task_pid(struct task_struct *task)
1290 {
1291 return task->pids[PIDTYPE_PID].pid;
1292 }
1293
1294 static inline struct pid *task_tgid(struct task_struct *task)
1295 {
1296 return task->group_leader->pids[PIDTYPE_PID].pid;
1297 }
1298
1299 /*
1300 * Without tasklist or RCU lock it is not safe to dereference
1301 * the result of task_pgrp/task_session even if task == current,
1302 * we can race with another thread doing sys_setsid/sys_setpgid.
1303 */
1304 static inline struct pid *task_pgrp(struct task_struct *task)
1305 {
1306 return task->group_leader->pids[PIDTYPE_PGID].pid;
1307 }
1308
1309 static inline struct pid *task_session(struct task_struct *task)
1310 {
1311 return task->group_leader->pids[PIDTYPE_SID].pid;
1312 }
1313
1314 /*
1315 * the helpers to get the task's different pids as they are seen
1316 * from various namespaces
1317 *
1318 * task_xid_nr() : global id, i.e. the id seen from the init namespace;
1319 * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
1320 * current.
1321 * task_xid_nr_ns() : id seen from the ns specified;
1322 *
1323 * see also pid_nr() etc in include/linux/pid.h
1324 */
1325 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
1326
1327 static inline pid_t task_pid_nr(struct task_struct *tsk)
1328 {
1329 return tsk->pid;
1330 }
1331
1332 static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1333 {
1334 return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1335 }
1336
1337 static inline pid_t task_pid_vnr(struct task_struct *tsk)
1338 {
1339 return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
1340 }
1341
1342
1343 static inline pid_t task_tgid_nr(struct task_struct *tsk)
1344 {
1345 return tsk->tgid;
1346 }
1347
1348 /**
1349 * pid_alive - check that a task structure is not stale
1350 * @p: Task structure to be checked.
1351 *
1352 * Test if a process is not yet dead (at most zombie state)
1353 * If pid_alive fails, then pointers within the task structure
1354 * can be stale and must not be dereferenced.
1355 *
1356 * Return: 1 if the process is alive. 0 otherwise.
1357 */
1358 static inline int pid_alive(const struct task_struct *p)
1359 {
1360 return p->pids[PIDTYPE_PID].pid != NULL;
1361 }
1362
1363 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1364 {
1365 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
1366 }
1367
1368 static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
1369 {
1370 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
1371 }
1372
1373
1374 static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1375 {
1376 return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
1377 }
1378
1379 static inline pid_t task_session_vnr(struct task_struct *tsk)
1380 {
1381 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
1382 }
1383
1384 static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1385 {
1386 return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, ns);
1387 }
1388
1389 static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1390 {
1391 return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, NULL);
1392 }
1393
1394 static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1395 {
1396 pid_t pid = 0;
1397
1398 rcu_read_lock();
1399 if (pid_alive(tsk))
1400 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1401 rcu_read_unlock();
1402
1403 return pid;
1404 }
1405
1406 static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1407 {
1408 return task_ppid_nr_ns(tsk, &init_pid_ns);
1409 }
1410
1411 /* Obsolete, do not use: */
1412 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
1413 {
1414 return task_pgrp_nr_ns(tsk, &init_pid_ns);
1415 }
1416
1417 #define TASK_REPORT_IDLE (TASK_REPORT + 1)
1418 #define TASK_REPORT_MAX (TASK_REPORT_IDLE << 1)
1419
1420 static inline unsigned int __get_task_state(struct task_struct *tsk)
1421 {
1422 unsigned int tsk_state = READ_ONCE(tsk->state);
1423 unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
1424
1425 BUILD_BUG_ON_NOT_POWER_OF_2(TASK_REPORT_MAX);
1426
1427 if (tsk_state == TASK_IDLE)
1428 state = TASK_REPORT_IDLE;
1429
1430 return fls(state);
1431 }
1432
1433 static inline char __task_state_to_char(unsigned int state)
1434 {
1435 static const char state_char[] = "RSDTtXZPI";
1436
1437 BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != sizeof(state_char) - 1);
1438
1439 return state_char[state];
1440 }
1441
1442 static inline char task_state_to_char(struct task_struct *tsk)
1443 {
1444 return __task_state_to_char(__get_task_state(tsk));
1445 }
1446
1447 /**
1448 * is_global_init - check if a task structure is init. Since init
1449 * is free to have sub-threads we need to check tgid.
1450 * @tsk: Task structure to be checked.
1451 *
1452 * Check if a task structure is the first user space task the kernel created.
1453 *
1454 * Return: 1 if the task structure is init. 0 otherwise.
1455 */
1456 static inline int is_global_init(struct task_struct *tsk)
1457 {
1458 return task_tgid_nr(tsk) == 1;
1459 }
1460
1461 extern struct pid *cad_pid;
1462
1463 /*
1464 * Per process flags
1465 */
1466 #define PF_IDLE 0x00000002 /* I am an IDLE thread */
1467 #define PF_EXITING 0x00000004 /* Getting shut down */
1468 #define PF_EXITPIDONE 0x00000008 /* PI exit done on shut down */
1469 #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
1470 #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
1471 #define PF_FORKNOEXEC 0x00000040 /* Forked but didn't exec */
1472 #define PF_MCE_PROCESS 0x00000080 /* Process policy on mce errors */
1473 #define PF_SUPERPRIV 0x00000100 /* Used super-user privileges */
1474 #define PF_DUMPCORE 0x00000200 /* Dumped core */
1475 #define PF_SIGNALED 0x00000400 /* Killed by a signal */
1476 #define PF_MEMALLOC 0x00000800 /* Allocating memory */
1477 #define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */
1478 #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */
1479 #define PF_USED_ASYNC 0x00004000 /* Used async_schedule*(), used by module init */
1480 #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */
1481 #define PF_FROZEN 0x00010000 /* Frozen for system suspend */
1482 #define PF_KSWAPD 0x00020000 /* I am kswapd */
1483 #define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */
1484 #define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */
1485 #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
1486 #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
1487 #define PF_RANDOMIZE 0x00400000 /* Randomize virtual address space */
1488 #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
1489 #define PF_MEMSTALL 0x01000000 /* Stalled due to lack of memory */
1490 #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
1491 #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
1492 #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
1493 #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
1494 #define PF_SUSPEND_TASK 0x80000000 /* This thread called freeze_processes() and should not be frozen */
1495
1496 /*
1497 * Only the _current_ task can read/write to tsk->flags, but other
1498 * tasks can access tsk->flags in readonly mode for example
1499 * with tsk_used_math (like during threaded core dumping).
1500 * There is however an exception to this rule during ptrace
1501 * or during fork: the ptracer task is allowed to write to the
1502 * child->flags of its traced child (same goes for fork, the parent
1503 * can write to the child->flags), because we're guaranteed the
1504 * child is not running and in turn not changing child->flags
1505 * at the same time the parent does it.
1506 */
1507 #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
1508 #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
1509 #define clear_used_math() clear_stopped_child_used_math(current)
1510 #define set_used_math() set_stopped_child_used_math(current)
1511
1512 #define conditional_stopped_child_used_math(condition, child) \
1513 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
1514
1515 #define conditional_used_math(condition) conditional_stopped_child_used_math(condition, current)
1516
1517 #define copy_to_stopped_child_used_math(child) \
1518 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
1519
1520 /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
1521 #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
1522 #define used_math() tsk_used_math(current)
1523
1524 static inline bool is_percpu_thread(void)
1525 {
1526 #ifdef CONFIG_SMP
1527 return (current->flags & PF_NO_SETAFFINITY) &&
1528 (current->nr_cpus_allowed == 1);
1529 #else
1530 return true;
1531 #endif
1532 }
1533
1534 /* Per-process atomic flags. */
1535 #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
1536 #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
1537 #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
1538 #define PFA_SPEC_SSB_DISABLE 3 /* Speculative Store Bypass disabled */
1539 #define PFA_SPEC_SSB_FORCE_DISABLE 4 /* Speculative Store Bypass force disabled*/
1540 #define PFA_SPEC_IB_DISABLE 5 /* Indirect branch speculation restricted */
1541 #define PFA_SPEC_IB_FORCE_DISABLE 6 /* Indirect branch speculation permanently restricted */
1542 #define PFA_LMK_WAITING 7 /* Lowmemorykiller is waiting */
1543
1544 #define TASK_PFA_TEST(name, func) \
1545 static inline bool task_##func(struct task_struct *p) \
1546 { return test_bit(PFA_##name, &p->atomic_flags); }
1547
1548 #define TASK_PFA_SET(name, func) \
1549 static inline void task_set_##func(struct task_struct *p) \
1550 { set_bit(PFA_##name, &p->atomic_flags); }
1551
1552 #define TASK_PFA_CLEAR(name, func) \
1553 static inline void task_clear_##func(struct task_struct *p) \
1554 { clear_bit(PFA_##name, &p->atomic_flags); }
1555
1556 TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
1557 TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
1558
1559 TASK_PFA_TEST(SPREAD_PAGE, spread_page)
1560 TASK_PFA_SET(SPREAD_PAGE, spread_page)
1561 TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
1562
1563 TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
1564 TASK_PFA_SET(SPREAD_SLAB, spread_slab)
1565 TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
1566
1567 TASK_PFA_TEST(SPEC_SSB_DISABLE, spec_ssb_disable)
1568 TASK_PFA_SET(SPEC_SSB_DISABLE, spec_ssb_disable)
1569 TASK_PFA_CLEAR(SPEC_SSB_DISABLE, spec_ssb_disable)
1570
1571 TASK_PFA_TEST(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1572 TASK_PFA_SET(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1573
1574 TASK_PFA_TEST(SPEC_IB_DISABLE, spec_ib_disable)
1575 TASK_PFA_SET(SPEC_IB_DISABLE, spec_ib_disable)
1576 TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable)
1577
1578 TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
1579 TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
1580
1581 TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
1582 TASK_PFA_SET(LMK_WAITING, lmk_waiting)
1583
1584 static inline void
1585 current_restore_flags(unsigned long orig_flags, unsigned long flags)
1586 {
1587 current->flags &= ~flags;
1588 current->flags |= orig_flags & flags;
1589 }
1590
1591 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
1592 extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
1593 #ifdef CONFIG_SMP
1594 extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
1595 extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
1596 #else
1597 static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1598 {
1599 }
1600 static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1601 {
1602 if (!cpumask_test_cpu(0, new_mask))
1603 return -EINVAL;
1604 return 0;
1605 }
1606 #endif
1607
1608 #ifndef cpu_relax_yield
1609 #define cpu_relax_yield() cpu_relax()
1610 #endif
1611
1612 extern int yield_to(struct task_struct *p, bool preempt);
1613 extern void set_user_nice(struct task_struct *p, long nice);
1614 extern int task_prio(const struct task_struct *p);
1615
1616 /**
1617 * task_nice - return the nice value of a given task.
1618 * @p: the task in question.
1619 *
1620 * Return: The nice value [ -20 ... 0 ... 19 ].
1621 */
1622 static inline int task_nice(const struct task_struct *p)
1623 {
1624 return PRIO_TO_NICE((p)->static_prio);
1625 }
1626
1627 extern int can_nice(const struct task_struct *p, const int nice);
1628 extern int task_curr(const struct task_struct *p);
1629 extern int idle_cpu(int cpu);
1630 extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
1631 extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
1632 extern int sched_setattr(struct task_struct *, const struct sched_attr *);
1633 extern struct task_struct *idle_task(int cpu);
1634
1635 /**
1636 * is_idle_task - is the specified task an idle task?
1637 * @p: the task in question.
1638 *
1639 * Return: 1 if @p is an idle task. 0 otherwise.
1640 */
1641 static inline bool is_idle_task(const struct task_struct *p)
1642 {
1643 return !!(p->flags & PF_IDLE);
1644 }
1645
1646 extern struct task_struct *curr_task(int cpu);
1647 extern void ia64_set_curr_task(int cpu, struct task_struct *p);
1648
1649 void yield(void);
1650
1651 union thread_union {
1652 #ifndef CONFIG_THREAD_INFO_IN_TASK
1653 struct thread_info thread_info;
1654 #endif
1655 unsigned long stack[THREAD_SIZE/sizeof(long)];
1656 };
1657
1658 #ifdef CONFIG_THREAD_INFO_IN_TASK
1659 static inline struct thread_info *task_thread_info(struct task_struct *task)
1660 {
1661 return &task->thread_info;
1662 }
1663 #elif !defined(__HAVE_THREAD_FUNCTIONS)
1664 # define task_thread_info(task) ((struct thread_info *)(task)->stack)
1665 #endif
1666
1667 /*
1668 * find a task by one of its numerical ids
1669 *
1670 * find_task_by_pid_ns():
1671 * finds a task by its pid in the specified namespace
1672 * find_task_by_vpid():
1673 * finds a task by its virtual pid
1674 *
1675 * see also find_vpid() etc in include/linux/pid.h
1676 */
1677
1678 extern struct task_struct *find_task_by_vpid(pid_t nr);
1679 extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
1680
1681 extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1682 extern int wake_up_process(struct task_struct *tsk);
1683 extern void wake_up_new_task(struct task_struct *tsk);
1684
1685 #ifdef CONFIG_SMP
1686 extern void kick_process(struct task_struct *tsk);
1687 #else
1688 static inline void kick_process(struct task_struct *tsk) { }
1689 #endif
1690
1691 extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
1692
1693 static inline void set_task_comm(struct task_struct *tsk, const char *from)
1694 {
1695 __set_task_comm(tsk, from, false);
1696 }
1697
1698 extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
1699 #define get_task_comm(buf, tsk) ({ \
1700 BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \
1701 __get_task_comm(buf, sizeof(buf), tsk); \
1702 })
1703
1704 #ifdef CONFIG_SMP
1705 void scheduler_ipi(void);
1706 extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
1707 #else
1708 static inline void scheduler_ipi(void) { }
1709 static inline unsigned long wait_task_inactive(struct task_struct *p, long match_state)
1710 {
1711 return 1;
1712 }
1713 #endif
1714
1715 /*
1716 * Set thread flags in other task's structures.
1717 * See asm/thread_info.h for TIF_xxxx flags available:
1718 */
1719 static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
1720 {
1721 set_ti_thread_flag(task_thread_info(tsk), flag);
1722 }
1723
1724 static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1725 {
1726 clear_ti_thread_flag(task_thread_info(tsk), flag);
1727 }
1728
1729 static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
1730 {
1731 return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
1732 }
1733
1734 static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1735 {
1736 return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
1737 }
1738
1739 static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
1740 {
1741 return test_ti_thread_flag(task_thread_info(tsk), flag);
1742 }
1743
1744 static inline void set_tsk_need_resched(struct task_struct *tsk)
1745 {
1746 set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
1747 }
1748
1749 static inline void clear_tsk_need_resched(struct task_struct *tsk)
1750 {
1751 clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
1752 }
1753
1754 static inline int test_tsk_need_resched(struct task_struct *tsk)
1755 {
1756 return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
1757 }
1758
1759 /*
1760 * cond_resched() and cond_resched_lock(): latency reduction via
1761 * explicit rescheduling in places that are safe. The return
1762 * value indicates whether a reschedule was done in fact.
1763 * cond_resched_lock() will drop the spinlock before scheduling,
1764 * cond_resched_softirq() will enable bhs before scheduling.
1765 */
1766 #ifndef CONFIG_PREEMPT
1767 extern int _cond_resched(void);
1768 #else
1769 static inline int _cond_resched(void) { return 0; }
1770 #endif
1771
1772 #define cond_resched() ({ \
1773 ___might_sleep(__FILE__, __LINE__, 0); \
1774 _cond_resched(); \
1775 })
1776
1777 extern int __cond_resched_lock(spinlock_t *lock);
1778
1779 #define cond_resched_lock(lock) ({ \
1780 ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
1781 __cond_resched_lock(lock); \
1782 })
1783
1784 extern int __cond_resched_softirq(void);
1785
1786 #define cond_resched_softirq() ({ \
1787 ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
1788 __cond_resched_softirq(); \
1789 })
1790
1791 static inline void cond_resched_rcu(void)
1792 {
1793 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
1794 rcu_read_unlock();
1795 cond_resched();
1796 rcu_read_lock();
1797 #endif
1798 }
1799
1800 /*
1801 * Does a critical section need to be broken due to another
1802 * task waiting?: (technically does not depend on CONFIG_PREEMPT,
1803 * but a general need for low latency)
1804 */
1805 static inline int spin_needbreak(spinlock_t *lock)
1806 {
1807 #ifdef CONFIG_PREEMPT
1808 return spin_is_contended(lock);
1809 #else
1810 return 0;
1811 #endif
1812 }
1813
1814 static __always_inline bool need_resched(void)
1815 {
1816 return unlikely(tif_need_resched());
1817 }
1818
1819 /*
1820 * Wrappers for p->thread_info->cpu access. No-op on UP.
1821 */
1822 #ifdef CONFIG_SMP
1823
1824 static inline unsigned int task_cpu(const struct task_struct *p)
1825 {
1826 #ifdef CONFIG_THREAD_INFO_IN_TASK
1827 return p->cpu;
1828 #else
1829 return task_thread_info(p)->cpu;
1830 #endif
1831 }
1832
1833 extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
1834
1835 #else
1836
1837 static inline unsigned int task_cpu(const struct task_struct *p)
1838 {
1839 return 0;
1840 }
1841
1842 static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
1843 {
1844 }
1845
1846 #endif /* CONFIG_SMP */
1847
1848 /*
1849 * In order to reduce various lock holder preemption latencies provide an
1850 * interface to see if a vCPU is currently running or not.
1851 *
1852 * This allows us to terminate optimistic spin loops and block, analogous to
1853 * the native optimistic spin heuristic of testing if the lock owner task is
1854 * running or not.
1855 */
1856 #ifndef vcpu_is_preempted
1857 # define vcpu_is_preempted(cpu) false
1858 #endif
1859
1860 extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
1861 extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
1862
1863 #ifndef TASK_SIZE_OF
1864 #define TASK_SIZE_OF(tsk) TASK_SIZE
1865 #endif
1866
1867 #endif