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