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