• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/irqflags.h>
22 #include <linux/seccomp.h>
23 #include <linux/nodemask.h>
24 #include <linux/rcupdate.h>
25 #include <linux/refcount.h>
26 #include <linux/resource.h>
27 #include <linux/latencytop.h>
28 #include <linux/sched/prio.h>
29 #include <linux/sched/types.h>
30 #include <linux/signal_types.h>
31 #include <linux/mm_types_task.h>
32 #include <linux/task_io_accounting.h>
33 #include <linux/posix-timers.h>
34 #include <linux/rseq.h>
35 #include <linux/seqlock.h>
36 #include <linux/kcsan.h>
37 #include <linux/sched/rtg.h>
38 
39 /* task_struct member predeclarations (sorted alphabetically): */
40 struct audit_context;
41 struct backing_dev_info;
42 struct bio_list;
43 struct blk_plug;
44 struct bpf_run_ctx;
45 struct capture_control;
46 struct cfs_rq;
47 struct fs_struct;
48 struct futex_pi_state;
49 struct io_context;
50 struct mempolicy;
51 struct nameidata;
52 struct nsproxy;
53 struct perf_event_context;
54 struct pid_namespace;
55 struct pipe_inode_info;
56 struct rcu_node;
57 #ifdef CONFIG_RECLAIM_ACCT
58 struct reclaim_acct;
59 #endif
60 struct reclaim_state;
61 struct robust_list_head;
62 struct root_domain;
63 struct rq;
64 struct sched_attr;
65 struct sched_param;
66 struct seq_file;
67 struct sighand_struct;
68 struct signal_struct;
69 struct task_delay_info;
70 struct task_group;
71 struct io_uring_task;
72 
73 /*
74  * Task state bitmask. NOTE! These bits are also
75  * encoded in fs/proc/array.c: get_task_state().
76  *
77  * We have two separate sets of flags: task->state
78  * is about runnability, while task->exit_state are
79  * about the task exiting. Confusing, but this way
80  * modifying one set can't modify the other one by
81  * mistake.
82  */
83 
84 /* Used in tsk->state: */
85 #define TASK_RUNNING 0x0000
86 #define TASK_INTERRUPTIBLE 0x0001
87 #define TASK_UNINTERRUPTIBLE 0x0002
88 #define __TASK_STOPPED 0x0004
89 #define __TASK_TRACED 0x0008
90 /* Used in tsk->exit_state: */
91 #define EXIT_DEAD 0x0010
92 #define EXIT_ZOMBIE 0x0020
93 #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
94 /* Used in tsk->state again: */
95 #define TASK_PARKED 0x0040
96 #define TASK_DEAD 0x0080
97 #define TASK_WAKEKILL 0x0100
98 #define TASK_WAKING 0x0200
99 #define TASK_NOLOAD 0x0400
100 #define TASK_NEW 0x0800
101 #define TASK_STATE_MAX 0x1000
102 
103 /* Convenience macros for the sake of set_current_state: */
104 #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
105 #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
106 #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
107 
108 #define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
109 
110 /* Convenience macros for the sake of wake_up(): */
111 #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
112 
113 /* get_task_state(): */
114 #define TASK_REPORT                                                                                                    \
115     (TASK_RUNNING | TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE | __TASK_STOPPED | __TASK_TRACED | EXIT_DEAD |           \
116      EXIT_ZOMBIE | TASK_PARKED)
117 
118 #define task_is_traced(task) (((task)->state & __TASK_TRACED) != 0)
119 
120 #define task_is_stopped(task) (((task)->state & __TASK_STOPPED) != 0)
121 
122 #define task_is_stopped_or_traced(task) (((task)->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
123 
124 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
125 
126 /*
127  * Special states are those that do not use the normal wait-loop pattern. See
128  * the comment with set_special_state().
129  */
130 #define is_special_task_state(state) ((state) & (__TASK_STOPPED | __TASK_TRACED | TASK_PARKED | TASK_DEAD))
131 
132 #define __set_current_state(state_value)                                                                               \
133     do {                                                                                                               \
134         WARN_ON_ONCE(is_special_task_state(state_value));                                                              \
135         current->task_state_change = _THIS_IP_;                                                                        \
136         current->state = (state_value);                                                                                \
137     } while (0)
138 
139 #define set_current_state(state_value)                                                                                 \
140     do {                                                                                                               \
141         WARN_ON_ONCE(is_special_task_state(state_value));                                                              \
142         current->task_state_change = _THIS_IP_;                                                                        \
143         smp_store_mb(current->state, (state_value));                                                                   \
144     } while (0)
145 
146 #define set_special_state(state_value)                                                                                 \
147     do {                                                                                                               \
148         unsigned long flags; /* may shadow */                                                                          \
149         WARN_ON_ONCE(!is_special_task_state(state_value));                                                             \
150         raw_spin_lock_irqsave(&current->pi_lock, flags);                                                               \
151         current->task_state_change = _THIS_IP_;                                                                        \
152         current->state = (state_value);                                                                                \
153         raw_spin_unlock_irqrestore(&current->pi_lock, flags);                                                          \
154     } while (0)
155 #else
156 /*
157  * set_current_state() includes a barrier so that the write of current->state
158  * is correctly serialised wrt the caller's subsequent test of whether to
159  * actually sleep:
160  *
161  *   for (;;) {
162  *    set_current_state(TASK_UNINTERRUPTIBLE);
163  *    if (CONDITION)
164  *       break;
165  *
166  *    schedule();
167  *   }
168  *   __set_current_state(TASK_RUNNING);
169  *
170  * If the caller does not need such serialisation (because, for instance, the
171  * CONDITION test and condition change and wakeup are under the same lock) then
172  * use __set_current_state().
173  *
174  * The above is typically ordered against the wakeup, which does:
175  *
176  *   CONDITION = 1;
177  *   wake_up_state(p, TASK_UNINTERRUPTIBLE);
178  *
179  * where wake_up_state()/try_to_wake_up() executes a full memory barrier before
180  * accessing p->state.
181  *
182  * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
183  * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
184  * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
185  *
186  * However, with slightly different timing the wakeup TASK_RUNNING store can
187  * also collide with the TASK_UNINTERRUPTIBLE store. Losing that store is not
188  * a problem either because that will result in one extra go around the loop
189  * and our @cond test will save the day.
190  *
191  * Also see the comments of try_to_wake_up().
192  */
193 #define __set_current_state(state_value) current->state = (state_value)
194 
195 #define set_current_state(state_value) smp_store_mb(current->state, (state_value))
196 
197 /*
198  * set_special_state() should be used for those states when the blocking task
199  * can not use the regular condition based wait-loop. In that case we must
200  * serialize against wakeups such that any possible in-flight TASK_RUNNING stores
201  * will not collide with our state change.
202  */
203 #define set_special_state(state_value)                                                                                 \
204     do {                                                                                                               \
205         unsigned long flags; /* may shadow */                                                                          \
206         raw_spin_lock_irqsave(&current->pi_lock, flags);                                                               \
207         current->state = (state_value);                                                                                \
208         raw_spin_unlock_irqrestore(&current->pi_lock, flags);                                                          \
209     } while (0)
210 
211 #endif
212 
213 /* Task command name length: */
214 #define TASK_COMM_LEN 16
215 
216 enum task_event {
217     PUT_PREV_TASK = 0,
218     PICK_NEXT_TASK = 1,
219     TASK_WAKE = 2,
220     TASK_MIGRATE = 3,
221     TASK_UPDATE = 4,
222     IRQ_UPDATE = 5,
223 };
224 
225 /* Note: this need to be in sync with migrate_type_names array */
226 enum migrate_types {
227     GROUP_TO_RQ,
228     RQ_TO_GROUP,
229 };
230 
231 #ifdef CONFIG_CPU_ISOLATION_OPT
232 extern int sched_isolate_count(const cpumask_t *mask, bool include_offline);
233 extern int sched_isolate_cpu(int cpu);
234 extern int sched_unisolate_cpu(int cpu);
235 extern int sched_unisolate_cpu_unlocked(int cpu);
236 #else
sched_isolate_count(const cpumask_t * mask,bool include_offline)237 static inline int sched_isolate_count(const cpumask_t *mask, bool include_offline)
238 {
239     cpumask_t count_mask;
240 
241     if (include_offline) {
242         cpumask_andnot(&count_mask, mask, cpu_online_mask);
243     } else {
244         return 0;
245     }
246 
247     return cpumask_weight(&count_mask);
248 }
249 
sched_isolate_cpu(int cpu)250 static inline int sched_isolate_cpu(int cpu)
251 {
252     return 0;
253 }
254 
sched_unisolate_cpu(int cpu)255 static inline int sched_unisolate_cpu(int cpu)
256 {
257     return 0;
258 }
259 
sched_unisolate_cpu_unlocked(int cpu)260 static inline int sched_unisolate_cpu_unlocked(int cpu)
261 {
262     return 0;
263 }
264 #endif
265 
266 extern void scheduler_tick(void);
267 
268 #define MAX_SCHEDULE_TIMEOUT LONG_MAX
269 
270 extern long schedule_timeout(long timeout);
271 extern long schedule_timeout_interruptible(long timeout);
272 extern long schedule_timeout_killable(long timeout);
273 extern long schedule_timeout_uninterruptible(long timeout);
274 extern long schedule_timeout_idle(long timeout);
275 asmlinkage void schedule(void);
276 extern void schedule_preempt_disabled(void);
277 asmlinkage void preempt_schedule_irq(void);
278 
279 extern int __must_check io_schedule_prepare(void);
280 extern void io_schedule_finish(int token);
281 extern long io_schedule_timeout(long timeout);
282 extern void io_schedule(void);
283 
284 /**
285  * struct prev_cputime - snapshot of system and user cputime
286  * @utime: time spent in user mode
287  * @stime: time spent in system mode
288  * @lock: protects the above two fields
289  *
290  * Stores previous user/system time values such that we can guarantee
291  * monotonicity.
292  */
293 struct prev_cputime {
294 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
295     u64 utime;
296     u64 stime;
297     raw_spinlock_t lock;
298 #endif
299 };
300 
301 enum vtime_state {
302     /* Task is sleeping or running in a CPU with VTIME inactive: */
303     VTIME_INACTIVE = 0,
304     /* Task is idle */
305     VTIME_IDLE,
306     /* Task runs in kernelspace in a CPU with VTIME active: */
307     VTIME_SYS,
308     /* Task runs in userspace in a CPU with VTIME active: */
309     VTIME_USER,
310     /* Task runs as guests in a CPU with VTIME active: */
311     VTIME_GUEST,
312 };
313 
314 struct vtime {
315     seqcount_t seqcount;
316     unsigned long long starttime;
317     enum vtime_state state;
318     unsigned int cpu;
319     u64 utime;
320     u64 stime;
321     u64 gtime;
322 };
323 
324 /*
325  * Utilization clamp constraints.
326  * @UCLAMP_MIN:    Minimum utilization
327  * @UCLAMP_MAX:    Maximum utilization
328  * @UCLAMP_CNT:    Utilization clamp constraints count
329  */
330 enum uclamp_id { UCLAMP_MIN = 0, UCLAMP_MAX, UCLAMP_CNT };
331 
332 #ifdef CONFIG_SMP
333 extern struct root_domain def_root_domain;
334 extern struct mutex sched_domains_mutex;
335 #endif
336 
337 struct sched_info {
338 #ifdef CONFIG_SCHED_INFO
339     /* Cumulative counters: */
340 
341     /* # of times we have run on this CPU: */
342     unsigned long pcount;
343 
344     /* Time spent waiting on a runqueue: */
345     unsigned long long run_delay;
346 
347     /* Timestamps: */
348 
349     /* When did we last run on a CPU? */
350     unsigned long long last_arrival;
351 
352     /* When were we last queued to run? */
353     unsigned long long last_queued;
354 
355 #endif /* CONFIG_SCHED_INFO */
356 };
357 
358 /*
359  * Integer metrics need fixed point arithmetic, e.g., sched/fair
360  * has a few: load, load_avg, util_avg, freq, and capacity.
361  *
362  * We define a basic fixed point arithmetic range, and then formalize
363  * all these metrics based on that basic range.
364  */
365 #define SCHED_FIXEDPOINT_SHIFT 10
366 #define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
367 
368 /* Increase resolution of cpu_capacity calculations */
369 #define SCHED_CAPACITY_SHIFT SCHED_FIXEDPOINT_SHIFT
370 #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
371 
372 struct load_weight {
373     unsigned long weight;
374     u32 inv_weight;
375 };
376 
377 /**
378  * struct util_est - Estimation utilization of FAIR tasks
379  * @enqueued: instantaneous estimated utilization of a task/cpu
380  * @ewma:     the Exponential Weighted Moving Average (EWMA)
381  *            utilization of a task
382  *
383  * Support data structure to track an Exponential Weighted Moving Average
384  * (EWMA) of a FAIR task's utilization. New samples are added to the moving
385  * average each time a task completes an activation. Sample's weight is chosen
386  * so that the EWMA will be relatively insensitive to transient changes to the
387  * task's workload.
388  *
389  * The enqueued attribute has a slightly different meaning for tasks and cpus:
390  * - task:   the task's util_avg at last task dequeue time
391  * - cfs_rq: the sum of util_est.enqueued for each RUNNABLE task on that CPU
392  * Thus, the util_est.enqueued of a task represents the contribution on the
393  * estimated utilization of the CPU where that task is currently enqueued.
394  *
395  * Only for tasks we track a moving average of the past instantaneous
396  * estimated utilization. This allows to absorb sporadic drops in utilization
397  * of an otherwise almost periodic task.
398  *
399  * The UTIL_AVG_UNCHANGED flag is used to synchronize util_est with util_avg
400  * updates. When a task is dequeued, its util_est should not be updated if its
401  * util_avg has not been updated in the meantime.
402  * This information is mapped into the MSB bit of util_est.enqueued at dequeue
403  * time. Since max value of util_est.enqueued for a task is 1024 (PELT util_avg
404  * for a task) it is safe to use MSB.
405  */
406 struct util_est {
407     unsigned int enqueued;
408     unsigned int ewma;
409 #define UTIL_EST_WEIGHT_SHIFT 2
410 #define UTIL_AVG_UNCHANGED 0x80000000
411 } __attribute__((__aligned__(sizeof(u64))));
412 
413 /*
414  * The load/runnable/util_avg accumulates an infinite geometric series
415  * (see __update_load_avg_cfs_rq() in kernel/sched/pelt.c).
416  *
417  * [load_avg definition]
418  *
419  *   load_avg = runnable% * scale_load_down(load)
420  *
421  * [runnable_avg definition]
422  *
423  *   runnable_avg = runnable% * SCHED_CAPACITY_SCALE
424  *
425  * [util_avg definition]
426  *
427  *   util_avg = running% * SCHED_CAPACITY_SCALE
428  *
429  * where runnable% is the time ratio that a sched_entity is runnable and
430  * running% the time ratio that a sched_entity is running.
431  *
432  * For cfs_rq, they are the aggregated values of all runnable and blocked
433  * sched_entities.
434  *
435  * The load/runnable/util_avg doesn't directly factor frequency scaling and CPU
436  * capacity scaling. The scaling is done through the rq_clock_pelt that is used
437  * for computing those signals (see update_rq_clock_pelt())
438  *
439  * N.B., the above ratios (runnable% and running%) themselves are in the
440  * range of [0, 1]. To do fixed point arithmetics, we therefore scale them
441  * to as large a range as necessary. This is for example reflected by
442  * util_avg's SCHED_CAPACITY_SCALE.
443  *
444  * [Overflow issue]
445  *
446  * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
447  * with the highest load (=88761), always runnable on a single cfs_rq,
448  * and should not overflow as the number already hits PID_MAX_LIMIT.
449  *
450  * For all other cases (including 32-bit kernels), struct load_weight's
451  * weight will overflow first before we do, because:
452  *
453  *    Max(load_avg) <= Max(load.weight)
454  *
455  * Then it is the load_weight's responsibility to consider overflow
456  * issues.
457  */
458 struct sched_avg {
459     u64 last_update_time;
460     u64 load_sum;
461     u64 runnable_sum;
462     u32 util_sum;
463     u32 period_contrib;
464     unsigned long load_avg;
465     unsigned long runnable_avg;
466     unsigned long util_avg;
467     struct util_est util_est;
468 } ____cacheline_aligned;
469 
470 struct sched_statistics {
471 #ifdef CONFIG_SCHEDSTATS
472     u64 wait_start;
473     u64 wait_max;
474     u64 wait_count;
475     u64 wait_sum;
476     u64 iowait_count;
477     u64 iowait_sum;
478 
479     u64 sleep_start;
480     u64 sleep_max;
481     s64 sum_sleep_runtime;
482 
483     u64 block_start;
484     u64 block_max;
485     u64 exec_max;
486     u64 slice_max;
487 
488     u64 nr_migrations_cold;
489     u64 nr_failed_migrations_affine;
490     u64 nr_failed_migrations_running;
491     u64 nr_failed_migrations_hot;
492     u64 nr_forced_migrations;
493 
494     u64 nr_wakeups;
495     u64 nr_wakeups_sync;
496     u64 nr_wakeups_migrate;
497     u64 nr_wakeups_local;
498     u64 nr_wakeups_remote;
499     u64 nr_wakeups_affine;
500     u64 nr_wakeups_affine_attempts;
501     u64 nr_wakeups_passive;
502     u64 nr_wakeups_idle;
503 #endif
504 };
505 
506 struct sched_entity {
507     /* For load-balancing: */
508     struct load_weight load;
509     struct rb_node run_node;
510     struct list_head group_node;
511     unsigned int on_rq;
512 
513     u64 exec_start;
514     u64 sum_exec_runtime;
515     u64 vruntime;
516     u64 prev_sum_exec_runtime;
517 
518     u64 nr_migrations;
519 
520     struct sched_statistics statistics;
521 
522 #ifdef CONFIG_FAIR_GROUP_SCHED
523     int depth;
524     struct sched_entity *parent;
525     /* rq on which this entity is (to be) queued: */
526     struct cfs_rq *cfs_rq;
527     /* rq "owned" by this entity/group: */
528     struct cfs_rq *my_q;
529     /* cached value of my_q->h_nr_running */
530     unsigned long runnable_weight;
531 #endif
532 
533 #ifdef CONFIG_SCHED_LATENCY_NICE
534 	int				latency_weight;
535 #endif
536 #ifdef CONFIG_SMP
537     /*
538      * Per entity load average tracking.
539      *
540      * Put into separate cache line so it does not
541      * collide with read-mostly values above.
542      */
543     struct sched_avg avg;
544 #endif
545 };
546 
547 #ifdef CONFIG_SCHED_WALT
548 extern void sched_exit(struct task_struct *p);
549 extern int sched_set_init_task_load(struct task_struct *p, int init_load_pct);
550 extern u32 sched_get_init_task_load(struct task_struct *p);
551 extern void free_task_load_ptrs(struct task_struct *p);
552 #define RAVG_HIST_SIZE_MAX 5
553 struct ravg {
554     /*
555      * 'mark_start' marks the beginning of an event (task waking up, task
556      * starting to execute, task being preempted) within a window
557      *
558      * 'sum' represents how runnable a task has been within current
559      * window. It incorporates both running time and wait time and is
560      * frequency scaled.
561      *
562      * 'sum_history' keeps track of history of 'sum' seen over previous
563      * RAVG_HIST_SIZE windows. Windows where task was entirely sleeping are
564      * ignored.
565      *
566      * 'demand' represents maximum sum seen over previous
567      * sysctl_sched_ravg_hist_size windows. 'demand' could drive frequency
568      * demand for tasks.
569      *
570      * 'curr_window_cpu' represents task's contribution to cpu busy time on
571      * various CPUs in the current window
572      *
573      * 'prev_window_cpu' represents task's contribution to cpu busy time on
574      * various CPUs in the previous window
575      *
576      * 'curr_window' represents the sum of all entries in curr_window_cpu
577      *
578      * 'prev_window' represents the sum of all entries in prev_window_cpu
579      *
580      */
581     u64 mark_start;
582     u32 sum, demand;
583     u32 sum_history[RAVG_HIST_SIZE_MAX];
584     u32 *curr_window_cpu, *prev_window_cpu;
585     u32 curr_window, prev_window;
586     u16 active_windows;
587     u16 demand_scaled;
588 };
589 #else
sched_exit(struct task_struct * p)590 static inline void sched_exit(struct task_struct *p)
591 {
592 }
free_task_load_ptrs(struct task_struct * p)593 static inline void free_task_load_ptrs(struct task_struct *p)
594 {
595 }
596 #endif /* CONFIG_SCHED_WALT */
597 
598 struct sched_rt_entity {
599     struct list_head run_list;
600     unsigned long timeout;
601     unsigned long watchdog_stamp;
602     unsigned int time_slice;
603     unsigned short on_rq;
604     unsigned short on_list;
605 
606     struct sched_rt_entity *back;
607 #ifdef CONFIG_RT_GROUP_SCHED
608     struct sched_rt_entity *parent;
609     /* rq on which this entity is (to be) queued: */
610     struct rt_rq *rt_rq;
611     /* rq "owned" by this entity/group: */
612     struct rt_rq *my_q;
613 #endif
614 } __randomize_layout;
615 
616 struct sched_dl_entity {
617     struct rb_node rb_node;
618 
619     /*
620      * Original scheduling parameters. Copied here from sched_attr
621      * during sched_setattr(), they will remain the same until
622      * the next sched_setattr().
623      */
624     u64 dl_runtime;  /* Maximum runtime for each instance    */
625     u64 dl_deadline; /* Relative deadline of each instance    */
626     u64 dl_period;   /* Separation of two instances (period) */
627     u64 dl_bw;       /* dl_runtime / dl_period        */
628     u64 dl_density;  /* dl_runtime / dl_deadline        */
629 
630     /*
631      * Actual scheduling parameters. Initialized with the values above,
632      * they are continuously updated during task execution. Note that
633      * the remaining runtime could be < 0 in case we are in overrun.
634      */
635     s64 runtime;        /* Remaining runtime for this instance    */
636     u64 deadline;       /* Absolute deadline for this instance    */
637     unsigned int flags; /* Specifying the scheduler behaviour    */
638 
639     /*
640      * Some bool flags:
641      *
642      * @dl_throttled tells if we exhausted the runtime. If so, the
643      * task has to wait for a replenishment to be performed at the
644      * next firing of dl_timer.
645      *
646      * @dl_boosted tells if we are boosted due to DI. If so we are
647      * outside bandwidth enforcement mechanism (but only until we
648      * exit the critical section);
649      *
650      * @dl_yielded tells if task gave up the CPU before consuming
651      * all its available runtime during the last job.
652      *
653      * @dl_non_contending tells if the task is inactive while still
654      * contributing to the active utilization. In other words, it
655      * indicates if the inactive timer has been armed and its handler
656      * has not been executed yet. This flag is useful to avoid race
657      * conditions between the inactive timer handler and the wakeup
658      * code.
659      *
660      * @dl_overrun tells if the task asked to be informed about runtime
661      * overruns.
662      */
663     unsigned int dl_throttled : 1;
664     unsigned int dl_yielded : 1;
665     unsigned int dl_non_contending : 1;
666     unsigned int dl_overrun : 1;
667 
668     /*
669      * Bandwidth enforcement timer. Each -deadline task has its
670      * own bandwidth to be enforced, thus we need one timer per task.
671      */
672     struct hrtimer dl_timer;
673 
674     /*
675      * Inactive timer, responsible for decreasing the active utilization
676      * at the "0-lag time". When a -deadline task blocks, it contributes
677      * to GRUB's active utilization until the "0-lag time", hence a
678      * timer is needed to decrease the active utilization at the correct
679      * time.
680      */
681     struct hrtimer inactive_timer;
682 
683 #ifdef CONFIG_RT_MUTEXES
684     /*
685      * Priority Inheritance. When a DEADLINE scheduling entity is boosted
686      * pi_se points to the donor, otherwise points to the dl_se it belongs
687      * to (the original one/itself).
688      */
689     struct sched_dl_entity *pi_se;
690 #endif
691 };
692 
693 #ifdef CONFIG_UCLAMP_TASK
694 /* Number of utilization clamp buckets (shorter alias) */
695 #define UCLAMP_BUCKETS CONFIG_UCLAMP_BUCKETS_COUNT
696 
697 /*
698  * Utilization clamp for a scheduling entity
699  * @value:        clamp value "assigned" to a se
700  * @bucket_id:        bucket index corresponding to the "assigned" value
701  * @active:        the se is currently refcounted in a rq's bucket
702  * @user_defined:    the requested clamp value comes from user-space
703  *
704  * The bucket_id is the index of the clamp bucket matching the clamp value
705  * which is pre-computed and stored to avoid expensive integer divisions from
706  * the fast path.
707  *
708  * The active bit is set whenever a task has got an "effective" value assigned,
709  * which can be different from the clamp value "requested" from user-space.
710  * This allows to know a task is refcounted in the rq's bucket corresponding
711  * to the "effective" bucket_id.
712  *
713  * The user_defined bit is set whenever a task has got a task-specific clamp
714  * value requested from userspace, i.e. the system defaults apply to this task
715  * just as a restriction. This allows to relax default clamps when a less
716  * restrictive task-specific value has been requested, thus allowing to
717  * implement a "nice" semantic. For example, a task running with a 20%
718  * default boost can still drop its own boosting to 0%.
719  */
720 struct uclamp_se {
721     unsigned int value : bits_per(SCHED_CAPACITY_SCALE);
722     unsigned int bucket_id : bits_per(UCLAMP_BUCKETS);
723     unsigned int active : 1;
724     unsigned int user_defined : 1;
725 };
726 #endif /* CONFIG_UCLAMP_TASK */
727 
728 union rcu_special {
729     struct {
730         u8 blocked;
731         u8 need_qs;
732         u8 exp_hint; /* Hint for performance. */
733         u8 need_mb;  /* Readers need smp_mb(). */
734     } b;             /* Bits. */
735     u32 s;           /* Set of bits. */
736 };
737 
738 enum perf_event_task_context {
739     perf_invalid_context = -1,
740     perf_hw_context = 0,
741     perf_sw_context,
742     perf_nr_task_contexts,
743 };
744 
745 struct wake_q_node {
746     struct wake_q_node *next;
747 };
748 
749 struct task_struct {
750 #ifdef CONFIG_THREAD_INFO_IN_TASK
751     /*
752      * For reasons of header soup (see current_thread_info()), this
753      * must be the first element of task_struct.
754      */
755     struct thread_info thread_info;
756 #endif
757     /* -1 unrunnable, 0 runnable, >0 stopped: */
758     volatile long state;
759 
760     /*
761      * This begins the randomizable portion of task_struct. Only
762      * scheduling-critical items should be added above here.
763      */
764     randomized_struct_fields_start
765 
766         void *stack;
767     refcount_t usage;
768     /* Per task flags (PF_*), defined further below: */
769     unsigned int flags;
770     unsigned int ptrace;
771 
772 #ifdef CONFIG_SMP
773     int on_cpu;
774     struct __call_single_node wake_entry;
775 #ifdef CONFIG_THREAD_INFO_IN_TASK
776     /* Current CPU: */
777     unsigned int cpu;
778 #endif
779     unsigned int wakee_flips;
780     unsigned long wakee_flip_decay_ts;
781     struct task_struct *last_wakee;
782 
783     /*
784      * recent_used_cpu is initially set as the last CPU used by a task
785      * that wakes affine another task. Waker/wakee relationships can
786      * push tasks around a CPU where each wakeup moves to the next one.
787      * Tracking a recently used CPU allows a quick search for a recently
788      * used CPU that may be idle.
789      */
790     int recent_used_cpu;
791     int wake_cpu;
792 #endif
793     int on_rq;
794 
795     int prio;
796     int static_prio;
797     int normal_prio;
798     unsigned int rt_priority;
799 #ifdef CONFIG_SCHED_LATENCY_NICE
800 	int				latency_prio;
801 #endif
802 
803     const struct sched_class *sched_class;
804     struct sched_entity se;
805     struct sched_rt_entity rt;
806 #ifdef CONFIG_SCHED_WALT
807     struct ravg ravg;
808     /*
809      * 'init_load_pct' represents the initial task load assigned to children
810      * of this task
811      */
812     u32 init_load_pct;
813     u64 last_sleep_ts;
814 #endif
815 
816 #ifdef CONFIG_SCHED_RTG
817     int rtg_depth;
818     struct related_thread_group *grp;
819     struct list_head grp_list;
820 #endif
821 
822 #ifdef CONFIG_CGROUP_SCHED
823     struct task_group *sched_task_group;
824 #endif
825     struct sched_dl_entity dl;
826 
827 #ifdef CONFIG_UCLAMP_TASK
828     /*
829      * Clamp values requested for a scheduling entity.
830      * Must be updated with task_rq_lock() held.
831      */
832     struct uclamp_se uclamp_req[UCLAMP_CNT];
833     /*
834      * Effective clamp values used for a scheduling entity.
835      * Must be updated with task_rq_lock() held.
836      */
837     struct uclamp_se uclamp[UCLAMP_CNT];
838 #endif
839 
840 #ifdef CONFIG_PREEMPT_NOTIFIERS
841     /* List of struct preempt_notifier: */
842     struct hlist_head preempt_notifiers;
843 #endif
844 
845 #ifdef CONFIG_BLK_DEV_IO_TRACE
846     unsigned int btrace_seq;
847 #endif
848 
849     unsigned int policy;
850     int nr_cpus_allowed;
851     const cpumask_t *cpus_ptr;
852     cpumask_t cpus_mask;
853 
854 #ifdef CONFIG_PREEMPT_RCU
855     int rcu_read_lock_nesting;
856     union rcu_special rcu_read_unlock_special;
857     struct list_head rcu_node_entry;
858     struct rcu_node *rcu_blocked_node;
859 #endif /* #ifdef CONFIG_PREEMPT_RCU */
860 
861 #ifdef CONFIG_TASKS_RCU
862     unsigned long rcu_tasks_nvcsw;
863     u8 rcu_tasks_holdout;
864     u8 rcu_tasks_idx;
865     int rcu_tasks_idle_cpu;
866     struct list_head rcu_tasks_holdout_list;
867 #endif /* #ifdef CONFIG_TASKS_RCU */
868 
869 #ifdef CONFIG_TASKS_TRACE_RCU
870     int trc_reader_nesting;
871     int trc_ipi_to_cpu;
872     union rcu_special trc_reader_special;
873     bool trc_reader_checked;
874     struct list_head trc_holdout_list;
875 #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
876 
877     struct sched_info sched_info;
878 
879     struct list_head tasks;
880 #ifdef CONFIG_SMP
881     struct plist_node pushable_tasks;
882     struct rb_node pushable_dl_tasks;
883 #endif
884 
885     struct mm_struct *mm;
886     struct mm_struct *active_mm;
887 
888     /* Per-thread vma caching: */
889     struct vmacache vmacache;
890 
891 #ifdef SPLIT_RSS_COUNTING
892     struct task_rss_stat rss_stat;
893 #endif
894     int exit_state;
895     int exit_code;
896     int exit_signal;
897     /* The signal sent when the parent dies: */
898     int pdeath_signal;
899     /* JOBCTL_*, siglock protected: */
900     unsigned long jobctl;
901 
902     /* Used for emulating ABI behavior of previous Linux versions: */
903     unsigned int personality;
904 
905     /* Scheduler bits, serialized by scheduler locks: */
906     unsigned sched_reset_on_fork : 1;
907     unsigned sched_contributes_to_load : 1;
908     unsigned sched_migrated : 1;
909 #ifdef CONFIG_PSI
910     unsigned sched_psi_wake_requeue : 1;
911 #endif
912 
913     /* Force alignment to the next boundary: */
914     unsigned : 0;
915 
916     /* Unserialized, strictly 'current' */
917 
918     /*
919      * This field must not be in the scheduler word above due to wakelist
920      * queueing no longer being serialized by p->on_cpu. However:
921      *
922      * p->XXX = X;            ttwu()
923      * schedule()              if (p->on_rq && ..) // false
924      *   smp_mb__after_spinlock();      if (smp_load_acquire(&p->on_cpu) && //true
925      *   deactivate_task()              ttwu_queue_wakelist())
926      *     p->on_rq = 0;            p->sched_remote_wakeup = Y;
927      *
928      * guarantees all stores of 'current' are visible before
929      * ->sched_remote_wakeup gets used, so it can be in this word.
930      */
931     unsigned sched_remote_wakeup : 1;
932 
933     /* Bit to tell LSMs we're in execve(): */
934     unsigned in_execve : 1;
935     unsigned in_iowait : 1;
936 #ifndef TIF_RESTORE_SIGMASK
937     unsigned restore_sigmask : 1;
938 #endif
939 #ifdef CONFIG_MEMCG
940     unsigned in_user_fault : 1;
941 #endif
942 #ifdef CONFIG_COMPAT_BRK
943     unsigned brk_randomized : 1;
944 #endif
945 #ifdef CONFIG_CGROUPS
946     /* disallow userland-initiated cgroup migration */
947     unsigned no_cgroup_migration : 1;
948     /* task is frozen/stopped (used by the cgroup freezer) */
949     unsigned frozen : 1;
950 #endif
951 #ifdef CONFIG_BLK_CGROUP
952     unsigned use_memdelay : 1;
953 #endif
954 #ifdef CONFIG_PSI
955     /* Stalled due to lack of memory */
956     unsigned in_memstall : 1;
957 #endif
958 
959     unsigned long atomic_flags; /* Flags requiring atomic access. */
960 
961     struct restart_block restart_block;
962 
963     pid_t pid;
964     pid_t tgid;
965 
966 #ifdef CONFIG_STACKPROTECTOR
967     /* Canary value for the -fstack-protector GCC feature: */
968     unsigned long stack_canary;
969 #endif
970     /*
971      * Pointers to the (original) parent process, youngest child, younger sibling,
972      * older sibling, respectively.  (p->father can be replaced with
973      * p->real_parent->pid)
974      */
975 
976     /* Real parent process: */
977     struct task_struct __rcu *real_parent;
978 
979     /* Recipient of SIGCHLD, wait4() reports: */
980     struct task_struct __rcu *parent;
981 
982     /*
983      * Children/sibling form the list of natural children:
984      */
985     struct list_head children;
986     struct list_head sibling;
987     struct task_struct *group_leader;
988 
989     /*
990      * 'ptraced' is the list of tasks this task is using ptrace() on.
991      *
992      * This includes both natural children and PTRACE_ATTACH targets.
993      * 'ptrace_entry' is this task's link on the p->parent->ptraced list.
994      */
995     struct list_head ptraced;
996     struct list_head ptrace_entry;
997 
998     /* PID/PID hash table linkage. */
999     struct pid *thread_pid;
1000     struct hlist_node pid_links[PIDTYPE_MAX];
1001     struct list_head thread_group;
1002     struct list_head thread_node;
1003 
1004     struct completion *vfork_done;
1005 
1006     /* CLONE_CHILD_SETTID: */
1007     int __user *set_child_tid;
1008 
1009     /* CLONE_CHILD_CLEARTID: */
1010     int __user *clear_child_tid;
1011 
1012     /* PF_IO_WORKER */
1013     void *pf_io_worker;
1014 
1015     u64 utime;
1016     u64 stime;
1017 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
1018     u64 utimescaled;
1019     u64 stimescaled;
1020 #endif
1021     u64 gtime;
1022     struct prev_cputime prev_cputime;
1023 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1024     struct vtime vtime;
1025 #endif
1026 
1027 #ifdef CONFIG_NO_HZ_FULL
1028     atomic_t tick_dep_mask;
1029 #endif
1030     /* Context switch counts: */
1031     unsigned long nvcsw;
1032     unsigned long nivcsw;
1033 
1034     /* Monotonic time in nsecs: */
1035     u64 start_time;
1036 
1037     /* Boot based time in nsecs: */
1038     u64 start_boottime;
1039 
1040     /* MM fault and swap info: this can arguably be seen as either mm-specific or thread-specific: */
1041     unsigned long min_flt;
1042     unsigned long maj_flt;
1043 
1044     /* Empty if CONFIG_POSIX_CPUTIMERS=n */
1045     struct posix_cputimers posix_cputimers;
1046 
1047 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
1048     struct posix_cputimers_work posix_cputimers_work;
1049 #endif
1050 
1051     /* Process credentials: */
1052 
1053     /* Tracer's credentials at attach: */
1054     const struct cred __rcu *ptracer_cred;
1055 
1056     /* Objective and real subjective task credentials (COW): */
1057     const struct cred __rcu *real_cred;
1058 
1059     /* Effective (overridable) subjective task credentials (COW): */
1060     const struct cred __rcu *cred;
1061 
1062 #ifdef CONFIG_KEYS
1063     /* Cached requested key. */
1064     struct key *cached_requested_key;
1065 #endif
1066 
1067     /*
1068      * executable name, excluding path.
1069      *
1070      * - normally initialized setup_new_exec()
1071      * - access it with [gs]et_task_comm()
1072      * - lock it with task_lock()
1073      */
1074     char comm[TASK_COMM_LEN];
1075 
1076     struct nameidata *nameidata;
1077 
1078 #ifdef CONFIG_SYSVIPC
1079     struct sysv_sem sysvsem;
1080     struct sysv_shm sysvshm;
1081 #endif
1082 #ifdef CONFIG_DETECT_HUNG_TASK
1083     unsigned long last_switch_count;
1084     unsigned long last_switch_time;
1085 #endif
1086     /* Filesystem information: */
1087     struct fs_struct *fs;
1088 
1089     /* Open file information: */
1090     struct files_struct *files;
1091 
1092 #ifdef CONFIG_IO_URING
1093     struct io_uring_task *io_uring;
1094 #endif
1095 
1096     /* Namespaces: */
1097     struct nsproxy *nsproxy;
1098 
1099     /* Signal handlers: */
1100     struct signal_struct *signal;
1101     struct sighand_struct __rcu *sighand;
1102     sigset_t blocked;
1103     sigset_t real_blocked;
1104     /* Restored if set_restore_sigmask() was used: */
1105     sigset_t saved_sigmask;
1106     struct sigpending pending;
1107     unsigned long sas_ss_sp;
1108     size_t sas_ss_size;
1109     unsigned int sas_ss_flags;
1110 
1111     struct callback_head *task_works;
1112 
1113 #ifdef CONFIG_AUDIT
1114 #ifdef CONFIG_AUDITSYSCALL
1115     struct audit_context *audit_context;
1116 #endif
1117     kuid_t loginuid;
1118     unsigned int sessionid;
1119 #endif
1120     struct seccomp seccomp;
1121 
1122     /* Thread group tracking: */
1123     u64 parent_exec_id;
1124     u64 self_exec_id;
1125 
1126     /* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */
1127     spinlock_t alloc_lock;
1128 
1129     /* Protection of the PI data structures: */
1130     raw_spinlock_t pi_lock;
1131 
1132     struct wake_q_node wake_q;
1133 
1134 #ifdef CONFIG_RT_MUTEXES
1135     /* PI waiters blocked on a rt_mutex held by this task: */
1136     struct rb_root_cached pi_waiters;
1137     /* Updated under owner's pi_lock and rq lock */
1138     struct task_struct *pi_top_task;
1139     /* Deadlock detection and priority inheritance handling: */
1140     struct rt_mutex_waiter *pi_blocked_on;
1141 #endif
1142 
1143 #ifdef CONFIG_DEBUG_MUTEXES
1144     /* Mutex deadlock detection: */
1145     struct mutex_waiter *blocked_on;
1146 #endif
1147 
1148 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1149     int non_block_count;
1150 #endif
1151 
1152 #ifdef CONFIG_TRACE_IRQFLAGS
1153     struct irqtrace_events irqtrace;
1154     unsigned int hardirq_threaded;
1155     u64 hardirq_chain_key;
1156     int softirqs_enabled;
1157     int softirq_context;
1158     int irq_config;
1159 #endif
1160 
1161 #ifdef CONFIG_LOCKDEP
1162 #define MAX_LOCK_DEPTH 48UL
1163     u64 curr_chain_key;
1164     int lockdep_depth;
1165     unsigned int lockdep_recursion;
1166     struct held_lock held_locks[MAX_LOCK_DEPTH];
1167 #endif
1168 
1169 #if defined(CONFIG_UBSAN) && !defined(CONFIG_UBSAN_TRAP)
1170     unsigned int in_ubsan;
1171 #endif
1172 
1173     /* Journalling filesystem info: */
1174     void *journal_info;
1175 
1176     /* Stacked block device info: */
1177     struct bio_list *bio_list;
1178 
1179 #ifdef CONFIG_BLOCK
1180     /* Stack plugging: */
1181     struct blk_plug *plug;
1182 #endif
1183 
1184     /* VM state: */
1185     struct reclaim_state *reclaim_state;
1186 
1187     struct backing_dev_info *backing_dev_info;
1188 
1189     struct io_context *io_context;
1190 
1191 #ifdef CONFIG_COMPACTION
1192     struct capture_control *capture_control;
1193 #endif
1194     /* Ptrace state: */
1195     unsigned long ptrace_message;
1196     kernel_siginfo_t *last_siginfo;
1197 
1198     struct task_io_accounting ioac;
1199 #ifdef CONFIG_PSI
1200     /* Pressure stall state */
1201     unsigned int psi_flags;
1202 #endif
1203 #ifdef CONFIG_TASK_XACCT
1204     /* Accumulated RSS usage: */
1205     u64 acct_rss_mem1;
1206     /* Accumulated virtual memory usage: */
1207     u64 acct_vm_mem1;
1208     /* stime + utime since last update: */
1209     u64 acct_timexpd;
1210 #endif
1211 #ifdef CONFIG_CPUSETS
1212     /* Protected by ->alloc_lock: */
1213     nodemask_t mems_allowed;
1214     /* Seqence number to catch updates: */
1215     seqcount_spinlock_t mems_allowed_seq;
1216     int cpuset_mem_spread_rotor;
1217     int cpuset_slab_spread_rotor;
1218 #endif
1219 #ifdef CONFIG_CGROUPS
1220     /* Control Group info protected by css_set_lock: */
1221     struct css_set __rcu *cgroups;
1222     /* cg_list protected by css_set_lock and tsk->alloc_lock: */
1223     struct list_head cg_list;
1224 #endif
1225 #ifdef CONFIG_X86_CPU_RESCTRL
1226     u32 closid;
1227     u32 rmid;
1228 #endif
1229 #ifdef CONFIG_FUTEX
1230     struct robust_list_head __user *robust_list;
1231 #ifdef CONFIG_COMPAT
1232     struct compat_robust_list_head __user *compat_robust_list;
1233 #endif
1234     struct list_head pi_state_list;
1235     struct futex_pi_state *pi_state_cache;
1236     struct mutex futex_exit_mutex;
1237     unsigned int futex_state;
1238 #endif
1239 #ifdef CONFIG_PERF_EVENTS
1240     struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1241     struct mutex perf_event_mutex;
1242     struct list_head perf_event_list;
1243 #endif
1244 #ifdef CONFIG_DEBUG_PREEMPT
1245     unsigned long preempt_disable_ip;
1246 #endif
1247 #ifdef CONFIG_NUMA
1248     /* Protected by alloc_lock: */
1249     struct mempolicy *mempolicy;
1250     short il_prev;
1251     short pref_node_fork;
1252 #endif
1253 #ifdef CONFIG_NUMA_BALANCING
1254     int numa_scan_seq;
1255     unsigned int numa_scan_period;
1256     unsigned int numa_scan_period_max;
1257     int numa_preferred_nid;
1258     unsigned long numa_migrate_retry;
1259     /* Migration stamp: */
1260     u64 node_stamp;
1261     u64 last_task_numa_placement;
1262     u64 last_sum_exec_runtime;
1263     struct callback_head numa_work;
1264 
1265     /*
1266      * This pointer is only modified for current in syscall and
1267      * pagefault context (and for tasks being destroyed), so it can be read
1268      * from any of the following contexts:
1269      *  - RCU read-side critical section
1270      *  - current->numa_group from everywhere
1271      *  - task's runqueue locked, task not running
1272      */
1273     struct numa_group __rcu *numa_group;
1274 
1275     /*
1276      * numa_faults is an array split into four regions:
1277      * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1278      * in this precise order.
1279      *
1280      * faults_memory: Exponential decaying average of faults on a per-node
1281      * basis. Scheduling placement decisions are made based on these
1282      * counts. The values remain static for the duration of a PTE scan.
1283      * faults_cpu: Track the nodes the process was running on when a NUMA
1284      * hinting fault was incurred.
1285      * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1286      * during the current scan window. When the scan completes, the counts
1287      * in faults_memory and faults_cpu decay and these values are copied.
1288      */
1289     unsigned long *numa_faults;
1290     unsigned long total_numa_faults;
1291 
1292     /*
1293      * numa_faults_locality tracks if faults recorded during the last
1294      * scan window were remote/local or failed to migrate. The task scan
1295      * period is adapted based on the locality of the faults with different
1296      * weights depending on whether they were shared or private faults
1297      */
1298     unsigned long numa_faults_locality[3];
1299 
1300     unsigned long numa_pages_migrated;
1301 #endif /* CONFIG_NUMA_BALANCING */
1302 
1303 #ifdef CONFIG_RSEQ
1304     struct rseq __user *rseq;
1305     u32 rseq_sig;
1306     /*
1307      * RmW on rseq_event_mask must be performed atomically
1308      * with respect to preemption.
1309      */
1310     unsigned long rseq_event_mask;
1311 #endif
1312 
1313     struct tlbflush_unmap_batch tlb_ubc;
1314 
1315     union {
1316         refcount_t rcu_users;
1317         struct rcu_head rcu;
1318     };
1319 
1320     /* Cache last used pipe for splice(): */
1321     struct pipe_inode_info *splice_pipe;
1322 
1323     struct page_frag task_frag;
1324 
1325 #ifdef CONFIG_TASK_DELAY_ACCT
1326     struct task_delay_info *delays;
1327 #endif
1328 
1329 #ifdef CONFIG_RECLAIM_ACCT
1330     struct reclaim_acct *reclaim_acct;
1331 #endif
1332 
1333 #ifdef CONFIG_FAULT_INJECTION
1334     int make_it_fail;
1335     unsigned int fail_nth;
1336 #endif
1337     /*
1338      * When (nr_dirtied >= nr_dirtied_pause), it's time to call
1339      * balance_dirty_pages() for a dirty throttling pause:
1340      */
1341     int nr_dirtied;
1342     int nr_dirtied_pause;
1343     /* Start of a write-and-pause period: */
1344     unsigned long dirty_paused_when;
1345 
1346 #ifdef CONFIG_LATENCYTOP
1347     int latency_record_count;
1348     struct latency_record latency_record[LT_SAVECOUNT];
1349 #endif
1350     /*
1351      * Time slack values; these are used to round up poll() and
1352      * select() etc timeout values. These are in nanoseconds.
1353      */
1354     u64 timer_slack_ns;
1355     u64 default_timer_slack_ns;
1356 
1357 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
1358     unsigned int kasan_depth;
1359 #endif
1360 
1361 #ifdef CONFIG_KCSAN
1362     struct kcsan_ctx kcsan_ctx;
1363 #ifdef CONFIG_TRACE_IRQFLAGS
1364     struct irqtrace_events kcsan_save_irqtrace;
1365 #endif
1366 #endif
1367 
1368 #if IS_ENABLED(CONFIG_KUNIT)
1369     struct kunit *kunit_test;
1370 #endif
1371 
1372 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1373     /* Index of current stored address in ret_stack: */
1374     int curr_ret_stack;
1375     int curr_ret_depth;
1376 
1377     /* Stack of return addresses for return function tracing: */
1378     struct ftrace_ret_stack *ret_stack;
1379 
1380     /* Timestamp for last schedule: */
1381     unsigned long long ftrace_timestamp;
1382 
1383     /*
1384      * Number of functions that haven't been traced
1385      * because of depth overrun:
1386      */
1387     atomic_t trace_overrun;
1388 
1389     /* Pause tracing: */
1390     atomic_t tracing_graph_pause;
1391 #endif
1392 
1393 #ifdef CONFIG_TRACING
1394     /* State flags for use by tracers: */
1395     unsigned long trace;
1396 
1397     /* Bitmask and counter of trace recursion: */
1398     unsigned long trace_recursion;
1399 #endif /* CONFIG_TRACING */
1400 
1401 #ifdef CONFIG_KCOV
1402     /* See kernel/kcov.c for more details. */
1403 
1404     /* Coverage collection mode enabled for this task (0 if disabled): */
1405     unsigned int kcov_mode;
1406 
1407     /* Size of the kcov_area: */
1408     unsigned int kcov_size;
1409 
1410     /* Buffer for coverage collection: */
1411     void *kcov_area;
1412 
1413     /* KCOV descriptor wired with this task or NULL: */
1414     struct kcov *kcov;
1415 
1416     /* KCOV common handle for remote coverage collection: */
1417     u64 kcov_handle;
1418 
1419     /* KCOV sequence number: */
1420     int kcov_sequence;
1421 
1422     /* Collect coverage from softirq context: */
1423     unsigned int kcov_softirq;
1424 #endif
1425 
1426 #ifdef CONFIG_MEMCG
1427     struct mem_cgroup *memcg_in_oom;
1428     gfp_t memcg_oom_gfp_mask;
1429     int memcg_oom_order;
1430 
1431     /* Number of pages to reclaim on returning to userland: */
1432     unsigned int memcg_nr_pages_over_high;
1433 
1434     /* Used by memcontrol for targeted memcg charge: */
1435     struct mem_cgroup *active_memcg;
1436 #endif
1437 
1438 #ifdef CONFIG_BLK_CGROUP
1439     struct request_queue *throttle_queue;
1440 #endif
1441 
1442 #ifdef CONFIG_UPROBES
1443     struct uprobe_task *utask;
1444 #endif
1445 #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1446     unsigned int sequential_io;
1447     unsigned int sequential_io_avg;
1448 #endif
1449 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1450     unsigned long task_state_change;
1451 #endif
1452     int pagefault_disabled;
1453 #ifdef CONFIG_MMU
1454     struct task_struct *oom_reaper_list;
1455     struct timer_list	oom_reaper_timer;
1456 #endif
1457 #ifdef CONFIG_VMAP_STACK
1458     struct vm_struct *stack_vm_area;
1459 #endif
1460 #ifdef CONFIG_THREAD_INFO_IN_TASK
1461     /* A live task holds one reference: */
1462     refcount_t stack_refcount;
1463 #endif
1464 #ifdef CONFIG_LIVEPATCH
1465     int patch_state;
1466 #endif
1467 #ifdef CONFIG_SECURITY
1468     /* Used by LSM modules for access restriction: */
1469     void *security;
1470 #endif
1471 #ifdef CONFIG_BPF_SYSCALL
1472     /* Used for BPF run context */
1473     struct bpf_run_ctx *bpf_ctx;
1474 #endif
1475 
1476 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
1477     unsigned long lowest_stack;
1478     unsigned long prev_lowest_stack;
1479 #endif
1480 
1481 #ifdef CONFIG_X86_MCE
1482     void __user *mce_vaddr;
1483     __u64 mce_kflags;
1484     u64 mce_addr;
1485     __u64 mce_ripv : 1, mce_whole_page : 1, __mce_reserved : 62;
1486     struct callback_head mce_kill_me;
1487     int mce_count;
1488 #endif
1489 
1490 #ifdef CONFIG_ACCESS_TOKENID
1491     u64 token;
1492     u64 ftoken;
1493 #endif
1494     /*
1495      * New fields for task_struct should be added above here, so that
1496      * they are included in the randomized portion of task_struct.
1497      */
1498     randomized_struct_fields_end
1499 
1500         /* CPU-specific state of this task: */
1501         struct thread_struct thread;
1502 
1503     /*
1504      * WARNING: on x86, 'thread_struct' contains a variable-sized
1505      * structure.  It *MUST* be at the end of 'task_struct'.
1506      *
1507      * Do not put anything below here!
1508      */
1509 };
1510 
task_pid(struct task_struct * task)1511 static inline struct pid *task_pid(struct task_struct *task)
1512 {
1513     return task->thread_pid;
1514 }
1515 
1516 /*
1517  * the helpers to get the task's different pids as they are seen
1518  * from various namespaces
1519  *
1520  * task_xid_nr()     : global id, i.e. the id seen from the init namespace;
1521  * task_xid_vnr()    : virtual id, i.e. the id seen from the pid namespace of
1522  *                     current.
1523  * task_xid_nr_ns()  : id seen from the ns specified;
1524  *
1525  * see also pid_nr() etc in include/linux/pid.h
1526  */
1527 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
1528 
task_pid_nr(struct task_struct * tsk)1529 static inline pid_t task_pid_nr(struct task_struct *tsk)
1530 {
1531     return tsk->pid;
1532 }
1533 
task_pid_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)1534 static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1535 {
1536     return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1537 }
1538 
task_pid_vnr(struct task_struct * tsk)1539 static inline pid_t task_pid_vnr(struct task_struct *tsk)
1540 {
1541     return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
1542 }
1543 
task_tgid_nr(struct task_struct * tsk)1544 static inline pid_t task_tgid_nr(struct task_struct *tsk)
1545 {
1546     return tsk->tgid;
1547 }
1548 
1549 /**
1550  * pid_alive - check that a task structure is not stale
1551  * @p: Task structure to be checked.
1552  *
1553  * Test if a process is not yet dead (at most zombie state)
1554  * If pid_alive fails, then pointers within the task structure
1555  * can be stale and must not be dereferenced.
1556  *
1557  * Return: 1 if the process is alive. 0 otherwise.
1558  */
pid_alive(const struct task_struct * p)1559 static inline int pid_alive(const struct task_struct *p)
1560 {
1561     return p->thread_pid != NULL;
1562 }
1563 
task_pgrp_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)1564 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1565 {
1566     return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
1567 }
1568 
task_pgrp_vnr(struct task_struct * tsk)1569 static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
1570 {
1571     return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
1572 }
1573 
task_session_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)1574 static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1575 {
1576     return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
1577 }
1578 
task_session_vnr(struct task_struct * tsk)1579 static inline pid_t task_session_vnr(struct task_struct *tsk)
1580 {
1581     return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
1582 }
1583 
task_tgid_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)1584 static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1585 {
1586     return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
1587 }
1588 
task_tgid_vnr(struct task_struct * tsk)1589 static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1590 {
1591     return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL);
1592 }
1593 
task_ppid_nr_ns(const struct task_struct * tsk,struct pid_namespace * ns)1594 static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1595 {
1596     pid_t pid = 0;
1597 
1598     rcu_read_lock();
1599     if (pid_alive(tsk)) {
1600         pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1601     }
1602     rcu_read_unlock();
1603 
1604     return pid;
1605 }
1606 
task_ppid_nr(const struct task_struct * tsk)1607 static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1608 {
1609     return task_ppid_nr_ns(tsk, &init_pid_ns);
1610 }
1611 
1612 /* Obsolete, do not use */
task_pgrp_nr(struct task_struct * tsk)1613 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
1614 {
1615     return task_pgrp_nr_ns(tsk, &init_pid_ns);
1616 }
1617 
1618 #define TASK_REPORT_IDLE (TASK_REPORT + 1)
1619 #define TASK_REPORT_MAX (TASK_REPORT_IDLE << 1)
1620 
task_state_index(struct task_struct * tsk)1621 static inline unsigned int task_state_index(struct task_struct *tsk)
1622 {
1623     unsigned int tsk_state = READ_ONCE(tsk->state);
1624     unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
1625 
1626     BUILD_BUG_ON_NOT_POWER_OF_2(TASK_REPORT_MAX);
1627 
1628     if (tsk_state == TASK_IDLE) {
1629         state = TASK_REPORT_IDLE;
1630     }
1631 
1632     return fls(state);
1633 }
1634 
task_index_to_char(unsigned int state)1635 static inline char task_index_to_char(unsigned int state)
1636 {
1637     static const char state_char[] = "RSDTtXZPI";
1638 
1639     BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != sizeof(state_char) - 1);
1640 
1641     return state_char[state];
1642 }
1643 
task_state_to_char(struct task_struct * tsk)1644 static inline char task_state_to_char(struct task_struct *tsk)
1645 {
1646     return task_index_to_char(task_state_index(tsk));
1647 }
1648 
1649 /**
1650  * is_global_init - check if a task structure is init. Since init
1651  * is free to have sub-threads we need to check tgid.
1652  * @tsk: Task structure to be checked.
1653  *
1654  * Check if a task structure is the first user space task the kernel created.
1655  *
1656  * Return: 1 if the task structure is init. 0 otherwise.
1657  */
is_global_init(struct task_struct * tsk)1658 static inline int is_global_init(struct task_struct *tsk)
1659 {
1660     return task_tgid_nr(tsk) == 1;
1661 }
1662 
1663 extern struct pid *cad_pid;
1664 
1665 /*
1666  * Per process flags
1667  */
1668 #define PF_VCPU 0x00000001           /* I'm a virtual CPU */
1669 #define PF_IDLE 0x00000002           /* I am an IDLE thread */
1670 #define PF_EXITING 0x00000004        /* Getting shut down */
1671 #define PF_IO_WORKER 0x00000010      /* Task is an IO worker */
1672 #define PF_WQ_WORKER 0x00000020      /* I'm a workqueue worker */
1673 #define PF_FORKNOEXEC 0x00000040     /* Forked but didn't exec */
1674 #define PF_MCE_PROCESS 0x00000080    /* Process policy on mce errors */
1675 #define PF_SUPERPRIV 0x00000100      /* Used super-user privileges */
1676 #define PF_DUMPCORE 0x00000200       /* Dumped core */
1677 #define PF_SIGNALED 0x00000400       /* Killed by a signal */
1678 #define PF_MEMALLOC 0x00000800       /* Allocating memory */
1679 #define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */
1680 #define PF_USED_MATH 0x00002000      /* If unset the fpu must be initialized before use */
1681 #define PF_NOFREEZE 0x00008000       /* This thread should not be frozen */
1682 #define PF_FROZEN 0x00010000         /* Frozen for system suspend */
1683 #define PF_KSWAPD 0x00020000         /* I am kswapd */
1684 #define PF_MEMALLOC_NOFS 0x00040000  /* All allocation requests will inherit GFP_NOFS */
1685 #define PF_MEMALLOC_NOIO 0x00080000  /* All allocation requests will inherit GFP_NOIO */
1686 #define PF_LOCAL_THROTTLE                                                                                              \
1687     0x00100000                       /* Throttle writes only against the bdi I write to,                               \
1688                                       * I am cleaning dirty pages from some other bdi. */
1689 #define PF_KTHREAD 0x00200000        /* I am a kernel thread */
1690 #define PF_RANDOMIZE 0x00400000      /* Randomize virtual address space */
1691 #define PF_SWAPWRITE 0x00800000      /* Allowed to write to swap */
1692 #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_mask */
1693 #define PF_MCE_EARLY 0x08000000      /* Early kill for mce process policy */
1694 #define PF_MEMALLOC_NOCMA 0x10000000 /* All allocation request will have _GFP_MOVABLE cleared */
1695 #define PF_FREEZER_SKIP 0x40000000   /* Freezer should not count it as freezable */
1696 #define PF_SUSPEND_TASK 0x80000000   /* This thread called freeze_processes() and should not be frozen */
1697 
1698 /*
1699  * Only the _current_ task can read/write to tsk->flags, but other
1700  * tasks can access tsk->flags in readonly mode for example
1701  * with tsk_used_math (like during threaded core dumping).
1702  * There is however an exception to this rule during ptrace
1703  * or during fork: the ptracer task is allowed to write to the
1704  * child->flags of its traced child (same goes for fork, the parent
1705  * can write to the child->flags), because we're guaranteed the
1706  * child is not running and in turn not changing child->flags
1707  * at the same time the parent does it.
1708  */
1709 #define clear_stopped_child_used_math(child)                                                                           \
1710     do {                                                                                                               \
1711         (child)->flags &= ~PF_USED_MATH;                                                                               \
1712     } while (0)
1713 #define set_stopped_child_used_math(child)                                                                             \
1714     do {                                                                                                               \
1715         (child)->flags |= PF_USED_MATH;                                                                                \
1716     } while (0)
1717 #define clear_used_math() clear_stopped_child_used_math(current)
1718 #define set_used_math() set_stopped_child_used_math(current)
1719 
1720 #define conditional_stopped_child_used_math(condition, child)                                                          \
1721     do {                                                                                                               \
1722         (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0;                             \
1723     } while (0)
1724 
1725 #define conditional_used_math(condition) conditional_stopped_child_used_math(condition, current)
1726 
1727 #define copy_to_stopped_child_used_math(child)                                                                         \
1728     do {                                                                                                               \
1729         (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH;                              \
1730     } while (0)
1731 
1732 /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
1733 #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
1734 #define used_math() tsk_used_math(current)
1735 
is_percpu_thread(void)1736 static __always_inline bool is_percpu_thread(void)
1737 {
1738 #ifdef CONFIG_SMP
1739     return (current->flags & PF_NO_SETAFFINITY) && (current->nr_cpus_allowed == 1);
1740 #else
1741     return true;
1742 #endif
1743 }
1744 
1745 /* Per-process atomic flags. */
1746 #define PFA_NO_NEW_PRIVS 0           /* May not gain new privileges. */
1747 #define PFA_SPREAD_PAGE 1            /* Spread page cache over cpuset */
1748 #define PFA_SPREAD_SLAB 2            /* Spread some slab caches over cpuset */
1749 #define PFA_SPEC_SSB_DISABLE 3       /* Speculative Store Bypass disabled */
1750 #define PFA_SPEC_SSB_FORCE_DISABLE 4 /* Speculative Store Bypass force disabled */
1751 #define PFA_SPEC_IB_DISABLE 5        /* Indirect branch speculation restricted */
1752 #define PFA_SPEC_IB_FORCE_DISABLE 6  /* Indirect branch speculation permanently restricted */
1753 #define PFA_SPEC_SSB_NOEXEC 7        /* Speculative Store Bypass clear on execve() */
1754 
1755 #define TASK_PFA_TEST(name, func)                                                                                      \
1756     static inline bool task_##func(struct task_struct *p)                                                              \
1757     {                                                                                                                  \
1758         return test_bit(PFA_##name, &p->atomic_flags);                                                                 \
1759     }
1760 
1761 #define TASK_PFA_SET(name, func)                                                                                       \
1762     static inline void task_set_##func(struct task_struct *p)                                                          \
1763     {                                                                                                                  \
1764         set_bit(PFA_##name, &p->atomic_flags);                                                                         \
1765     }
1766 
1767 #define TASK_PFA_CLEAR(name, func)                                                                                     \
1768     static inline void task_clear_##func(struct task_struct *p)                                                        \
1769     {                                                                                                                  \
1770         clear_bit(PFA_##name, &p->atomic_flags);                                                                       \
1771     }
1772 
TASK_PFA_TEST(NO_NEW_PRIVS,no_new_privs)1773 TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
1774 TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
1775 
1776 TASK_PFA_TEST(SPREAD_PAGE, spread_page)
1777 TASK_PFA_SET(SPREAD_PAGE, spread_page)
1778 TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
1779 
1780 TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
1781 TASK_PFA_SET(SPREAD_SLAB, spread_slab)
1782 TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
1783 
1784 TASK_PFA_TEST(SPEC_SSB_DISABLE, spec_ssb_disable)
1785 TASK_PFA_SET(SPEC_SSB_DISABLE, spec_ssb_disable)
1786 TASK_PFA_CLEAR(SPEC_SSB_DISABLE, spec_ssb_disable)
1787 
1788 TASK_PFA_TEST(SPEC_SSB_NOEXEC, spec_ssb_noexec)
1789 TASK_PFA_SET(SPEC_SSB_NOEXEC, spec_ssb_noexec)
1790 TASK_PFA_CLEAR(SPEC_SSB_NOEXEC, spec_ssb_noexec)
1791 
1792 TASK_PFA_TEST(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1793 TASK_PFA_SET(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1794 
1795 TASK_PFA_TEST(SPEC_IB_DISABLE, spec_ib_disable)
1796 TASK_PFA_SET(SPEC_IB_DISABLE, spec_ib_disable)
1797 TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable)
1798 
1799 TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
1800 TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
1801 
1802 static inline void current_restore_flags(unsigned long orig_flags, unsigned long flags)
1803 {
1804     current->flags &= ~flags;
1805     current->flags |= orig_flags & flags;
1806 }
1807 
1808 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
1809 extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
1810 #ifdef CONFIG_SMP
1811 extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
1812 extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
1813 #else
do_set_cpus_allowed(struct task_struct * p,const struct cpumask * new_mask)1814 static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1815 {
1816 }
set_cpus_allowed_ptr(struct task_struct * p,const struct cpumask * new_mask)1817 static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1818 {
1819     if (!cpumask_test_cpu(0, new_mask)) {
1820         return -EINVAL;
1821     }
1822     return 0;
1823 }
1824 #endif
1825 
1826 extern int yield_to(struct task_struct *p, bool preempt);
1827 extern void set_user_nice(struct task_struct *p, long nice);
1828 extern int task_prio(const struct task_struct *p);
1829 
1830 /**
1831  * task_nice - return the nice value of a given task.
1832  * @p: the task in question.
1833  *
1834  * Return: The nice value [ -20 ... 0 ... 19 ].
1835  */
task_nice(const struct task_struct * p)1836 static inline int task_nice(const struct task_struct *p)
1837 {
1838     return PRIO_TO_NICE((p)->static_prio);
1839 }
1840 
1841 extern int can_nice(const struct task_struct *p, const int nice);
1842 extern int task_curr(const struct task_struct *p);
1843 extern int idle_cpu(int cpu);
1844 extern int available_idle_cpu(int cpu);
1845 extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
1846 extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
1847 extern void sched_set_fifo(struct task_struct *p);
1848 extern void sched_set_fifo_low(struct task_struct *p);
1849 extern void sched_set_normal(struct task_struct *p, int nice);
1850 extern int sched_setattr(struct task_struct *, const struct sched_attr *);
1851 extern int sched_setattr_nocheck(struct task_struct *, const struct sched_attr *);
1852 extern struct task_struct *idle_task(int cpu);
1853 
1854 /**
1855  * is_idle_task - is the specified task an idle task?
1856  * @p: the task in question.
1857  *
1858  * Return: 1 if @p is an idle task. 0 otherwise.
1859  */
is_idle_task(const struct task_struct * p)1860 static __always_inline bool is_idle_task(const struct task_struct *p)
1861 {
1862     return !!(p->flags & PF_IDLE);
1863 }
1864 
1865 extern struct task_struct *curr_task(int cpu);
1866 extern void ia64_set_curr_task(int cpu, struct task_struct *p);
1867 
1868 void yield(void);
1869 
1870 union thread_union {
1871 #ifndef CONFIG_ARCH_TASK_STRUCT_ON_STACK
1872     struct task_struct task;
1873 #endif
1874 #ifndef CONFIG_THREAD_INFO_IN_TASK
1875     struct thread_info thread_info;
1876 #endif
1877     unsigned long stack[THREAD_SIZE / sizeof(long)];
1878 };
1879 
1880 #ifndef CONFIG_THREAD_INFO_IN_TASK
1881 extern struct thread_info init_thread_info;
1882 #endif
1883 
1884 extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
1885 
1886 #ifdef CONFIG_THREAD_INFO_IN_TASK
task_thread_info(struct task_struct * task)1887 static inline struct thread_info *task_thread_info(struct task_struct *task)
1888 {
1889     return &task->thread_info;
1890 }
1891 #elif !defined(__HAVE_THREAD_FUNCTIONS)
1892 #define task_thread_info(task) ((struct thread_info *)(task)->stack)
1893 #endif
1894 
1895 /*
1896  * find a task by one of its numerical ids
1897  *
1898  * find_task_by_pid_ns():
1899  *      finds a task by its pid in the specified namespace
1900  * find_task_by_vpid():
1901  *      finds a task by its virtual pid
1902  *
1903  * see also find_vpid() etc in include/linux/pid.h
1904  */
1905 
1906 extern struct task_struct *find_task_by_vpid(pid_t nr);
1907 extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
1908 
1909 /*
1910  * find a task by its virtual pid and get the task struct
1911  */
1912 extern struct task_struct *find_get_task_by_vpid(pid_t nr);
1913 
1914 extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1915 extern int wake_up_process(struct task_struct *tsk);
1916 extern void wake_up_new_task(struct task_struct *tsk);
1917 
1918 #ifdef CONFIG_SMP
1919 extern void kick_process(struct task_struct *tsk);
1920 #else
kick_process(struct task_struct * tsk)1921 static inline void kick_process(struct task_struct *tsk)
1922 {
1923 }
1924 #endif
1925 
1926 extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
1927 
set_task_comm(struct task_struct * tsk,const char * from)1928 static inline void set_task_comm(struct task_struct *tsk, const char *from)
1929 {
1930     __set_task_comm(tsk, from, false);
1931 }
1932 
1933 extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
1934 #define get_task_comm(buf, tsk)                                                                                        \
1935     ( {                                                                                                                \
1936         BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN);                                                                    \
1937         __get_task_comm(buf, sizeof(buf), tsk);                                                                        \
1938     })
1939 
1940 #ifdef CONFIG_SMP
scheduler_ipi(void)1941 static __always_inline void scheduler_ipi(void)
1942 {
1943     /*
1944      * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1945      * TIF_NEED_RESCHED remotely (for the first time) will also send
1946      * this IPI.
1947      */
1948     preempt_fold_need_resched();
1949 }
1950 extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
1951 #else
scheduler_ipi(void)1952 static inline void scheduler_ipi(void)
1953 {
1954 }
wait_task_inactive(struct task_struct * p,long match_state)1955 static inline unsigned long wait_task_inactive(struct task_struct *p, long match_state)
1956 {
1957     return 1;
1958 }
1959 #endif
1960 
1961 /*
1962  * Set thread flags in other task's structures.
1963  * See asm/thread_info.h for TIF_xxxx flags available:
1964  */
set_tsk_thread_flag(struct task_struct * tsk,int flag)1965 static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
1966 {
1967     set_ti_thread_flag(task_thread_info(tsk), flag);
1968 }
1969 
clear_tsk_thread_flag(struct task_struct * tsk,int flag)1970 static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1971 {
1972     clear_ti_thread_flag(task_thread_info(tsk), flag);
1973 }
1974 
update_tsk_thread_flag(struct task_struct * tsk,int flag,bool value)1975 static inline void update_tsk_thread_flag(struct task_struct *tsk, int flag, bool value)
1976 {
1977     update_ti_thread_flag(task_thread_info(tsk), flag, value);
1978 }
1979 
test_and_set_tsk_thread_flag(struct task_struct * tsk,int flag)1980 static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
1981 {
1982     return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
1983 }
1984 
test_and_clear_tsk_thread_flag(struct task_struct * tsk,int flag)1985 static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1986 {
1987     return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
1988 }
1989 
test_tsk_thread_flag(struct task_struct * tsk,int flag)1990 static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
1991 {
1992     return test_ti_thread_flag(task_thread_info(tsk), flag);
1993 }
1994 
set_tsk_need_resched(struct task_struct * tsk)1995 static inline void set_tsk_need_resched(struct task_struct *tsk)
1996 {
1997     set_tsk_thread_flag(tsk, TIF_NEED_RESCHED);
1998 }
1999 
clear_tsk_need_resched(struct task_struct * tsk)2000 static inline void clear_tsk_need_resched(struct task_struct *tsk)
2001 {
2002     clear_tsk_thread_flag(tsk, TIF_NEED_RESCHED);
2003 }
2004 
test_tsk_need_resched(struct task_struct * tsk)2005 static inline int test_tsk_need_resched(struct task_struct *tsk)
2006 {
2007     return unlikely(test_tsk_thread_flag(tsk, TIF_NEED_RESCHED));
2008 }
2009 
2010 /*
2011  * cond_resched() and cond_resched_lock(): latency reduction via
2012  * explicit rescheduling in places that are safe. The return
2013  * value indicates whether a reschedule was done in fact.
2014  * cond_resched_lock() will drop the spinlock before scheduling,
2015  */
2016 #ifndef CONFIG_PREEMPTION
2017 extern int _cond_resched(void);
2018 #else
_cond_resched(void)2019 static inline int _cond_resched(void)
2020 {
2021     return 0;
2022 }
2023 #endif
2024 
2025 #define cond_resched()                                                                                                 \
2026     ( {                                                                                                                \
2027         ___might_sleep(__FILE__, __LINE__, 0);                                                                         \
2028         _cond_resched();                                                                                               \
2029     })
2030 
2031 extern int __cond_resched_lock(spinlock_t *lock);
2032 
2033 #define cond_resched_lock(lock)                                                                                        \
2034     ( {                                                                                                                \
2035         ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);                                                       \
2036         __cond_resched_lock(lock);                                                                                     \
2037     })
2038 
cond_resched_rcu(void)2039 static inline void cond_resched_rcu(void)
2040 {
2041 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
2042     rcu_read_unlock();
2043     cond_resched();
2044     rcu_read_lock();
2045 #endif
2046 }
2047 
2048 /*
2049  * Does a critical section need to be broken due to another
2050  * task waiting?: (technically does not depend on CONFIG_PREEMPTION,
2051  * but a general need for low latency)
2052  */
spin_needbreak(spinlock_t * lock)2053 static inline int spin_needbreak(spinlock_t *lock)
2054 {
2055 #ifdef CONFIG_PREEMPTION
2056     return spin_is_contended(lock);
2057 #else
2058     return 0;
2059 #endif
2060 }
2061 
need_resched(void)2062 static __always_inline bool need_resched(void)
2063 {
2064     return unlikely(tif_need_resched());
2065 }
2066 
2067 /*
2068  * Wrappers for p->thread_info->cpu access. No-op on UP.
2069  */
2070 #ifdef CONFIG_SMP
2071 
task_cpu(const struct task_struct * p)2072 static inline unsigned int task_cpu(const struct task_struct *p)
2073 {
2074 #ifdef CONFIG_THREAD_INFO_IN_TASK
2075     return READ_ONCE(p->cpu);
2076 #else
2077     return READ_ONCE(task_thread_info(p)->cpu);
2078 #endif
2079 }
2080 
2081 extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
2082 
2083 #else
2084 
task_cpu(const struct task_struct * p)2085 static inline unsigned int task_cpu(const struct task_struct *p)
2086 {
2087     return 0;
2088 }
2089 
set_task_cpu(struct task_struct * p,unsigned int cpu)2090 static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
2091 {
2092 }
2093 
2094 #endif /* CONFIG_SMP */
2095 
2096 /*
2097  * In order to reduce various lock holder preemption latencies provide an
2098  * interface to see if a vCPU is currently running or not.
2099  *
2100  * This allows us to terminate optimistic spin loops and block, analogous to
2101  * the native optimistic spin heuristic of testing if the lock owner task is
2102  * running or not.
2103  */
2104 #ifndef vcpu_is_preempted
vcpu_is_preempted(int cpu)2105 static inline bool vcpu_is_preempted(int cpu)
2106 {
2107     return false;
2108 }
2109 #endif
2110 
2111 extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
2112 extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
2113 
2114 #ifndef TASK_SIZE_OF
2115 #define TASK_SIZE_OF(tsk) TASK_SIZE
2116 #endif
2117 
2118 #ifdef CONFIG_RSEQ
2119 
2120 /*
2121  * Map the event mask on the user-space ABI enum rseq_cs_flags
2122  * for direct mask checks.
2123  */
2124 enum rseq_event_mask_bits {
2125     RSEQ_EVENT_PREEMPT_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT,
2126     RSEQ_EVENT_SIGNAL_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT,
2127     RSEQ_EVENT_MIGRATE_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT,
2128 };
2129 
2130 enum rseq_event_mask {
2131     RSEQ_EVENT_PREEMPT = (1U << RSEQ_EVENT_PREEMPT_BIT),
2132     RSEQ_EVENT_SIGNAL = (1U << RSEQ_EVENT_SIGNAL_BIT),
2133     RSEQ_EVENT_MIGRATE = (1U << RSEQ_EVENT_MIGRATE_BIT),
2134 };
2135 
rseq_set_notify_resume(struct task_struct * t)2136 static inline void rseq_set_notify_resume(struct task_struct *t)
2137 {
2138     if (t->rseq) {
2139         set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
2140     }
2141 }
2142 
2143 void __rseq_handle_notify_resume(struct ksignal *sig, struct pt_regs *regs);
2144 
rseq_handle_notify_resume(struct ksignal * ksig,struct pt_regs * regs)2145 static inline void rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
2146 {
2147     if (current->rseq) {
2148         __rseq_handle_notify_resume(ksig, regs);
2149     }
2150 }
2151 
rseq_signal_deliver(struct ksignal * ksig,struct pt_regs * regs)2152 static inline void rseq_signal_deliver(struct ksignal *ksig, struct pt_regs *regs)
2153 {
2154     preempt_disable();
2155     __set_bit(RSEQ_EVENT_SIGNAL_BIT, &current->rseq_event_mask);
2156     preempt_enable();
2157     rseq_handle_notify_resume(ksig, regs);
2158 }
2159 
2160 /* rseq_preempt() requires preemption to be disabled. */
rseq_preempt(struct task_struct * t)2161 static inline void rseq_preempt(struct task_struct *t)
2162 {
2163     __set_bit(RSEQ_EVENT_PREEMPT_BIT, &t->rseq_event_mask);
2164     rseq_set_notify_resume(t);
2165 }
2166 
2167 /* rseq_migrate() requires preemption to be disabled. */
rseq_migrate(struct task_struct * t)2168 static inline void rseq_migrate(struct task_struct *t)
2169 {
2170     __set_bit(RSEQ_EVENT_MIGRATE_BIT, &t->rseq_event_mask);
2171     rseq_set_notify_resume(t);
2172 }
2173 
2174 /*
2175  * If parent process has a registered restartable sequences area, the
2176  * child inherits. Unregister rseq for a clone with CLONE_VM set.
2177  */
rseq_fork(struct task_struct * t,unsigned long clone_flags)2178 static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
2179 {
2180     if (clone_flags & CLONE_VM) {
2181         t->rseq = NULL;
2182         t->rseq_sig = 0;
2183         t->rseq_event_mask = 0;
2184     } else {
2185         t->rseq = current->rseq;
2186         t->rseq_sig = current->rseq_sig;
2187         t->rseq_event_mask = current->rseq_event_mask;
2188     }
2189 }
2190 
rseq_execve(struct task_struct * t)2191 static inline void rseq_execve(struct task_struct *t)
2192 {
2193     t->rseq = NULL;
2194     t->rseq_sig = 0;
2195     t->rseq_event_mask = 0;
2196 }
2197 
2198 #else
2199 
rseq_set_notify_resume(struct task_struct * t)2200 static inline void rseq_set_notify_resume(struct task_struct *t)
2201 {
2202 }
rseq_handle_notify_resume(struct ksignal * ksig,struct pt_regs * regs)2203 static inline void rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
2204 {
2205 }
rseq_signal_deliver(struct ksignal * ksig,struct pt_regs * regs)2206 static inline void rseq_signal_deliver(struct ksignal *ksig, struct pt_regs *regs)
2207 {
2208 }
rseq_preempt(struct task_struct * t)2209 static inline void rseq_preempt(struct task_struct *t)
2210 {
2211 }
rseq_migrate(struct task_struct * t)2212 static inline void rseq_migrate(struct task_struct *t)
2213 {
2214 }
rseq_fork(struct task_struct * t,unsigned long clone_flags)2215 static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
2216 {
2217 }
rseq_execve(struct task_struct * t)2218 static inline void rseq_execve(struct task_struct *t)
2219 {
2220 }
2221 
2222 #endif
2223 
2224 #ifdef CONFIG_DEBUG_RSEQ
2225 
2226 void rseq_syscall(struct pt_regs *regs);
2227 
2228 #else
2229 
rseq_syscall(struct pt_regs * regs)2230 static inline void rseq_syscall(struct pt_regs *regs)
2231 {
2232 }
2233 
2234 #endif
2235 
2236 const struct sched_avg *sched_trace_cfs_rq_avg(struct cfs_rq *cfs_rq);
2237 char *sched_trace_cfs_rq_path(struct cfs_rq *cfs_rq, char *str, int len);
2238 int sched_trace_cfs_rq_cpu(struct cfs_rq *cfs_rq);
2239 
2240 const struct sched_avg *sched_trace_rq_avg_rt(struct rq *rq);
2241 const struct sched_avg *sched_trace_rq_avg_dl(struct rq *rq);
2242 const struct sched_avg *sched_trace_rq_avg_irq(struct rq *rq);
2243 
2244 int sched_trace_rq_cpu(struct rq *rq);
2245 int sched_trace_rq_cpu_capacity(struct rq *rq);
2246 int sched_trace_rq_nr_running(struct rq *rq);
2247 
2248 const struct cpumask *sched_trace_rd_span(struct root_domain *rd);
2249 
2250 #endif
2251