blob: f996d1f343bb7cd1ec2a8dd15df0e2b006f0de20 [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 *
545 * @dl_boosted tells if we are boosted due to DI. If so we are
546 * outside bandwidth enforcement mechanism (but only until we
547 * exit the critical section);
548 *
549 * @dl_yielded tells if task gave up the CPU before consuming
550 * all its available runtime during the last job.
551 *
552 * @dl_non_contending tells if the task is inactive while still
553 * contributing to the active utilization. In other words, it
554 * indicates if the inactive timer has been armed and its handler
555 * has not been executed yet. This flag is useful to avoid race
556 * conditions between the inactive timer handler and the wakeup
557 * code.
558 *
559 * @dl_overrun tells if the task asked to be informed about runtime
560 * overruns.
561 */
562 unsigned int dl_throttled : 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563 unsigned int dl_yielded : 1;
564 unsigned int dl_non_contending : 1;
565 unsigned int dl_overrun : 1;
566
567 /*
568 * Bandwidth enforcement timer. Each -deadline task has its
569 * own bandwidth to be enforced, thus we need one timer per task.
570 */
571 struct hrtimer dl_timer;
572
573 /*
574 * Inactive timer, responsible for decreasing the active utilization
575 * at the "0-lag time". When a -deadline task blocks, it contributes
576 * to GRUB's active utilization until the "0-lag time", hence a
577 * timer is needed to decrease the active utilization at the correct
578 * time.
579 */
580 struct hrtimer inactive_timer;
Olivier Deprez157378f2022-04-04 15:47:50 +0200581
582#ifdef CONFIG_RT_MUTEXES
583 /*
584 * Priority Inheritance. When a DEADLINE scheduling entity is boosted
585 * pi_se points to the donor, otherwise points to the dl_se it belongs
586 * to (the original one/itself).
587 */
588 struct sched_dl_entity *pi_se;
589#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590};
591
David Brazdil0f672f62019-12-10 10:32:29 +0000592#ifdef CONFIG_UCLAMP_TASK
593/* Number of utilization clamp buckets (shorter alias) */
594#define UCLAMP_BUCKETS CONFIG_UCLAMP_BUCKETS_COUNT
595
596/*
597 * Utilization clamp for a scheduling entity
598 * @value: clamp value "assigned" to a se
599 * @bucket_id: bucket index corresponding to the "assigned" value
600 * @active: the se is currently refcounted in a rq's bucket
601 * @user_defined: the requested clamp value comes from user-space
602 *
603 * The bucket_id is the index of the clamp bucket matching the clamp value
604 * which is pre-computed and stored to avoid expensive integer divisions from
605 * the fast path.
606 *
607 * The active bit is set whenever a task has got an "effective" value assigned,
608 * which can be different from the clamp value "requested" from user-space.
609 * This allows to know a task is refcounted in the rq's bucket corresponding
610 * to the "effective" bucket_id.
611 *
612 * The user_defined bit is set whenever a task has got a task-specific clamp
613 * value requested from userspace, i.e. the system defaults apply to this task
614 * just as a restriction. This allows to relax default clamps when a less
615 * restrictive task-specific value has been requested, thus allowing to
616 * implement a "nice" semantic. For example, a task running with a 20%
617 * default boost can still drop its own boosting to 0%.
618 */
619struct uclamp_se {
620 unsigned int value : bits_per(SCHED_CAPACITY_SCALE);
621 unsigned int bucket_id : bits_per(UCLAMP_BUCKETS);
622 unsigned int active : 1;
623 unsigned int user_defined : 1;
624};
625#endif /* CONFIG_UCLAMP_TASK */
626
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000627union rcu_special {
628 struct {
629 u8 blocked;
630 u8 need_qs;
David Brazdil0f672f62019-12-10 10:32:29 +0000631 u8 exp_hint; /* Hint for performance. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200632 u8 need_mb; /* Readers need smp_mb(). */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633 } b; /* Bits. */
634 u32 s; /* Set of bits. */
635};
636
637enum perf_event_task_context {
638 perf_invalid_context = -1,
639 perf_hw_context = 0,
640 perf_sw_context,
641 perf_nr_task_contexts,
642};
643
644struct wake_q_node {
645 struct wake_q_node *next;
646};
647
648struct task_struct {
649#ifdef CONFIG_THREAD_INFO_IN_TASK
650 /*
651 * For reasons of header soup (see current_thread_info()), this
652 * must be the first element of task_struct.
653 */
654 struct thread_info thread_info;
655#endif
656 /* -1 unrunnable, 0 runnable, >0 stopped: */
657 volatile long state;
658
659 /*
660 * This begins the randomizable portion of task_struct. Only
661 * scheduling-critical items should be added above here.
662 */
663 randomized_struct_fields_start
664
665 void *stack;
David Brazdil0f672f62019-12-10 10:32:29 +0000666 refcount_t usage;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000667 /* Per task flags (PF_*), defined further below: */
668 unsigned int flags;
669 unsigned int ptrace;
670
671#ifdef CONFIG_SMP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000672 int on_cpu;
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 struct __call_single_node wake_entry;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000674#ifdef CONFIG_THREAD_INFO_IN_TASK
675 /* Current CPU: */
676 unsigned int cpu;
677#endif
678 unsigned int wakee_flips;
679 unsigned long wakee_flip_decay_ts;
680 struct task_struct *last_wakee;
681
682 /*
683 * recent_used_cpu is initially set as the last CPU used by a task
684 * that wakes affine another task. Waker/wakee relationships can
685 * push tasks around a CPU where each wakeup moves to the next one.
686 * Tracking a recently used CPU allows a quick search for a recently
687 * used CPU that may be idle.
688 */
689 int recent_used_cpu;
690 int wake_cpu;
691#endif
692 int on_rq;
693
694 int prio;
695 int static_prio;
696 int normal_prio;
697 unsigned int rt_priority;
698
699 const struct sched_class *sched_class;
700 struct sched_entity se;
701 struct sched_rt_entity rt;
702#ifdef CONFIG_CGROUP_SCHED
703 struct task_group *sched_task_group;
704#endif
705 struct sched_dl_entity dl;
706
David Brazdil0f672f62019-12-10 10:32:29 +0000707#ifdef CONFIG_UCLAMP_TASK
Olivier Deprez157378f2022-04-04 15:47:50 +0200708 /*
709 * Clamp values requested for a scheduling entity.
710 * Must be updated with task_rq_lock() held.
711 */
David Brazdil0f672f62019-12-10 10:32:29 +0000712 struct uclamp_se uclamp_req[UCLAMP_CNT];
Olivier Deprez157378f2022-04-04 15:47:50 +0200713 /*
714 * Effective clamp values used for a scheduling entity.
715 * Must be updated with task_rq_lock() held.
716 */
David Brazdil0f672f62019-12-10 10:32:29 +0000717 struct uclamp_se uclamp[UCLAMP_CNT];
718#endif
719
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000720#ifdef CONFIG_PREEMPT_NOTIFIERS
721 /* List of struct preempt_notifier: */
722 struct hlist_head preempt_notifiers;
723#endif
724
725#ifdef CONFIG_BLK_DEV_IO_TRACE
726 unsigned int btrace_seq;
727#endif
728
729 unsigned int policy;
730 int nr_cpus_allowed;
David Brazdil0f672f62019-12-10 10:32:29 +0000731 const cpumask_t *cpus_ptr;
732 cpumask_t cpus_mask;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000733
734#ifdef CONFIG_PREEMPT_RCU
735 int rcu_read_lock_nesting;
736 union rcu_special rcu_read_unlock_special;
737 struct list_head rcu_node_entry;
738 struct rcu_node *rcu_blocked_node;
739#endif /* #ifdef CONFIG_PREEMPT_RCU */
740
741#ifdef CONFIG_TASKS_RCU
742 unsigned long rcu_tasks_nvcsw;
743 u8 rcu_tasks_holdout;
744 u8 rcu_tasks_idx;
745 int rcu_tasks_idle_cpu;
746 struct list_head rcu_tasks_holdout_list;
747#endif /* #ifdef CONFIG_TASKS_RCU */
748
Olivier Deprez157378f2022-04-04 15:47:50 +0200749#ifdef CONFIG_TASKS_TRACE_RCU
750 int trc_reader_nesting;
751 int trc_ipi_to_cpu;
752 union rcu_special trc_reader_special;
753 bool trc_reader_checked;
754 struct list_head trc_holdout_list;
755#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
756
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757 struct sched_info sched_info;
758
759 struct list_head tasks;
760#ifdef CONFIG_SMP
761 struct plist_node pushable_tasks;
762 struct rb_node pushable_dl_tasks;
763#endif
764
765 struct mm_struct *mm;
766 struct mm_struct *active_mm;
767
768 /* Per-thread vma caching: */
769 struct vmacache vmacache;
770
771#ifdef SPLIT_RSS_COUNTING
772 struct task_rss_stat rss_stat;
773#endif
774 int exit_state;
775 int exit_code;
776 int exit_signal;
777 /* The signal sent when the parent dies: */
778 int pdeath_signal;
779 /* JOBCTL_*, siglock protected: */
780 unsigned long jobctl;
781
782 /* Used for emulating ABI behavior of previous Linux versions: */
783 unsigned int personality;
784
785 /* Scheduler bits, serialized by scheduler locks: */
786 unsigned sched_reset_on_fork:1;
787 unsigned sched_contributes_to_load:1;
788 unsigned sched_migrated:1;
David Brazdil0f672f62019-12-10 10:32:29 +0000789#ifdef CONFIG_PSI
790 unsigned sched_psi_wake_requeue:1;
791#endif
792
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000793 /* Force alignment to the next boundary: */
794 unsigned :0;
795
796 /* Unserialized, strictly 'current' */
797
Olivier Deprez157378f2022-04-04 15:47:50 +0200798 /*
799 * This field must not be in the scheduler word above due to wakelist
800 * queueing no longer being serialized by p->on_cpu. However:
801 *
802 * p->XXX = X; ttwu()
803 * schedule() if (p->on_rq && ..) // false
804 * smp_mb__after_spinlock(); if (smp_load_acquire(&p->on_cpu) && //true
805 * deactivate_task() ttwu_queue_wakelist())
806 * p->on_rq = 0; p->sched_remote_wakeup = Y;
807 *
808 * guarantees all stores of 'current' are visible before
809 * ->sched_remote_wakeup gets used, so it can be in this word.
810 */
811 unsigned sched_remote_wakeup:1;
812
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 /* Bit to tell LSMs we're in execve(): */
814 unsigned in_execve:1;
815 unsigned in_iowait:1;
816#ifndef TIF_RESTORE_SIGMASK
817 unsigned restore_sigmask:1;
818#endif
819#ifdef CONFIG_MEMCG
820 unsigned in_user_fault:1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000821#endif
822#ifdef CONFIG_COMPAT_BRK
823 unsigned brk_randomized:1;
824#endif
825#ifdef CONFIG_CGROUPS
826 /* disallow userland-initiated cgroup migration */
827 unsigned no_cgroup_migration:1;
David Brazdil0f672f62019-12-10 10:32:29 +0000828 /* task is frozen/stopped (used by the cgroup freezer) */
829 unsigned frozen:1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000830#endif
831#ifdef CONFIG_BLK_CGROUP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000832 unsigned use_memdelay:1;
833#endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200834#ifdef CONFIG_PSI
835 /* Stalled due to lack of memory */
836 unsigned in_memstall:1;
837#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000838
839 unsigned long atomic_flags; /* Flags requiring atomic access. */
840
841 struct restart_block restart_block;
842
843 pid_t pid;
844 pid_t tgid;
845
846#ifdef CONFIG_STACKPROTECTOR
847 /* Canary value for the -fstack-protector GCC feature: */
848 unsigned long stack_canary;
849#endif
850 /*
851 * Pointers to the (original) parent process, youngest child, younger sibling,
852 * older sibling, respectively. (p->father can be replaced with
853 * p->real_parent->pid)
854 */
855
856 /* Real parent process: */
857 struct task_struct __rcu *real_parent;
858
859 /* Recipient of SIGCHLD, wait4() reports: */
860 struct task_struct __rcu *parent;
861
862 /*
863 * Children/sibling form the list of natural children:
864 */
865 struct list_head children;
866 struct list_head sibling;
867 struct task_struct *group_leader;
868
869 /*
870 * 'ptraced' is the list of tasks this task is using ptrace() on.
871 *
872 * This includes both natural children and PTRACE_ATTACH targets.
873 * 'ptrace_entry' is this task's link on the p->parent->ptraced list.
874 */
875 struct list_head ptraced;
876 struct list_head ptrace_entry;
877
878 /* PID/PID hash table linkage. */
879 struct pid *thread_pid;
880 struct hlist_node pid_links[PIDTYPE_MAX];
881 struct list_head thread_group;
882 struct list_head thread_node;
883
884 struct completion *vfork_done;
885
886 /* CLONE_CHILD_SETTID: */
887 int __user *set_child_tid;
888
889 /* CLONE_CHILD_CLEARTID: */
890 int __user *clear_child_tid;
891
892 u64 utime;
893 u64 stime;
894#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
895 u64 utimescaled;
896 u64 stimescaled;
897#endif
898 u64 gtime;
899 struct prev_cputime prev_cputime;
900#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
901 struct vtime vtime;
902#endif
903
904#ifdef CONFIG_NO_HZ_FULL
905 atomic_t tick_dep_mask;
906#endif
907 /* Context switch counts: */
908 unsigned long nvcsw;
909 unsigned long nivcsw;
910
911 /* Monotonic time in nsecs: */
912 u64 start_time;
913
914 /* Boot based time in nsecs: */
Olivier Deprez157378f2022-04-04 15:47:50 +0200915 u64 start_boottime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916
917 /* MM fault and swap info: this can arguably be seen as either mm-specific or thread-specific: */
918 unsigned long min_flt;
919 unsigned long maj_flt;
920
David Brazdil0f672f62019-12-10 10:32:29 +0000921 /* Empty if CONFIG_POSIX_CPUTIMERS=n */
922 struct posix_cputimers posix_cputimers;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923
Olivier Deprez157378f2022-04-04 15:47:50 +0200924#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
925 struct posix_cputimers_work posix_cputimers_work;
926#endif
927
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928 /* Process credentials: */
929
930 /* Tracer's credentials at attach: */
931 const struct cred __rcu *ptracer_cred;
932
933 /* Objective and real subjective task credentials (COW): */
934 const struct cred __rcu *real_cred;
935
936 /* Effective (overridable) subjective task credentials (COW): */
937 const struct cred __rcu *cred;
938
David Brazdil0f672f62019-12-10 10:32:29 +0000939#ifdef CONFIG_KEYS
940 /* Cached requested key. */
941 struct key *cached_requested_key;
942#endif
943
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944 /*
945 * executable name, excluding path.
946 *
947 * - normally initialized setup_new_exec()
948 * - access it with [gs]et_task_comm()
949 * - lock it with task_lock()
950 */
951 char comm[TASK_COMM_LEN];
952
953 struct nameidata *nameidata;
954
955#ifdef CONFIG_SYSVIPC
956 struct sysv_sem sysvsem;
957 struct sysv_shm sysvshm;
958#endif
959#ifdef CONFIG_DETECT_HUNG_TASK
960 unsigned long last_switch_count;
961 unsigned long last_switch_time;
962#endif
963 /* Filesystem information: */
964 struct fs_struct *fs;
965
966 /* Open file information: */
967 struct files_struct *files;
968
Olivier Deprez157378f2022-04-04 15:47:50 +0200969#ifdef CONFIG_IO_URING
970 struct io_uring_task *io_uring;
971#endif
972
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000973 /* Namespaces: */
974 struct nsproxy *nsproxy;
975
976 /* Signal handlers: */
977 struct signal_struct *signal;
Olivier Deprez157378f2022-04-04 15:47:50 +0200978 struct sighand_struct __rcu *sighand;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000979 sigset_t blocked;
980 sigset_t real_blocked;
981 /* Restored if set_restore_sigmask() was used: */
982 sigset_t saved_sigmask;
983 struct sigpending pending;
984 unsigned long sas_ss_sp;
985 size_t sas_ss_size;
986 unsigned int sas_ss_flags;
987
988 struct callback_head *task_works;
989
David Brazdil0f672f62019-12-10 10:32:29 +0000990#ifdef CONFIG_AUDIT
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991#ifdef CONFIG_AUDITSYSCALL
David Brazdil0f672f62019-12-10 10:32:29 +0000992 struct audit_context *audit_context;
993#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000994 kuid_t loginuid;
995 unsigned int sessionid;
996#endif
997 struct seccomp seccomp;
998
999 /* Thread group tracking: */
Olivier Deprez0e641232021-09-23 10:07:05 +02001000 u64 parent_exec_id;
1001 u64 self_exec_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001002
1003 /* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */
1004 spinlock_t alloc_lock;
1005
1006 /* Protection of the PI data structures: */
1007 raw_spinlock_t pi_lock;
1008
1009 struct wake_q_node wake_q;
1010
1011#ifdef CONFIG_RT_MUTEXES
1012 /* PI waiters blocked on a rt_mutex held by this task: */
1013 struct rb_root_cached pi_waiters;
1014 /* Updated under owner's pi_lock and rq lock */
1015 struct task_struct *pi_top_task;
1016 /* Deadlock detection and priority inheritance handling: */
1017 struct rt_mutex_waiter *pi_blocked_on;
1018#endif
1019
1020#ifdef CONFIG_DEBUG_MUTEXES
1021 /* Mutex deadlock detection: */
1022 struct mutex_waiter *blocked_on;
1023#endif
1024
David Brazdil0f672f62019-12-10 10:32:29 +00001025#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1026 int non_block_count;
1027#endif
1028
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029#ifdef CONFIG_TRACE_IRQFLAGS
Olivier Deprez157378f2022-04-04 15:47:50 +02001030 struct irqtrace_events irqtrace;
1031 unsigned int hardirq_threaded;
1032 u64 hardirq_chain_key;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001033 int softirqs_enabled;
1034 int softirq_context;
Olivier Deprez157378f2022-04-04 15:47:50 +02001035 int irq_config;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036#endif
1037
1038#ifdef CONFIG_LOCKDEP
1039# define MAX_LOCK_DEPTH 48UL
1040 u64 curr_chain_key;
1041 int lockdep_depth;
1042 unsigned int lockdep_recursion;
1043 struct held_lock held_locks[MAX_LOCK_DEPTH];
1044#endif
1045
Olivier Deprez157378f2022-04-04 15:47:50 +02001046#if defined(CONFIG_UBSAN) && !defined(CONFIG_UBSAN_TRAP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001047 unsigned int in_ubsan;
1048#endif
1049
1050 /* Journalling filesystem info: */
1051 void *journal_info;
1052
1053 /* Stacked block device info: */
1054 struct bio_list *bio_list;
1055
1056#ifdef CONFIG_BLOCK
1057 /* Stack plugging: */
1058 struct blk_plug *plug;
1059#endif
1060
1061 /* VM state: */
1062 struct reclaim_state *reclaim_state;
1063
1064 struct backing_dev_info *backing_dev_info;
1065
1066 struct io_context *io_context;
1067
David Brazdil0f672f62019-12-10 10:32:29 +00001068#ifdef CONFIG_COMPACTION
1069 struct capture_control *capture_control;
1070#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071 /* Ptrace state: */
1072 unsigned long ptrace_message;
David Brazdil0f672f62019-12-10 10:32:29 +00001073 kernel_siginfo_t *last_siginfo;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001074
1075 struct task_io_accounting ioac;
David Brazdil0f672f62019-12-10 10:32:29 +00001076#ifdef CONFIG_PSI
1077 /* Pressure stall state */
1078 unsigned int psi_flags;
1079#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001080#ifdef CONFIG_TASK_XACCT
1081 /* Accumulated RSS usage: */
1082 u64 acct_rss_mem1;
1083 /* Accumulated virtual memory usage: */
1084 u64 acct_vm_mem1;
1085 /* stime + utime since last update: */
1086 u64 acct_timexpd;
1087#endif
1088#ifdef CONFIG_CPUSETS
1089 /* Protected by ->alloc_lock: */
1090 nodemask_t mems_allowed;
1091 /* Seqence number to catch updates: */
Olivier Deprez157378f2022-04-04 15:47:50 +02001092 seqcount_spinlock_t mems_allowed_seq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093 int cpuset_mem_spread_rotor;
1094 int cpuset_slab_spread_rotor;
1095#endif
1096#ifdef CONFIG_CGROUPS
1097 /* Control Group info protected by css_set_lock: */
1098 struct css_set __rcu *cgroups;
1099 /* cg_list protected by css_set_lock and tsk->alloc_lock: */
1100 struct list_head cg_list;
1101#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001102#ifdef CONFIG_X86_CPU_RESCTRL
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001103 u32 closid;
1104 u32 rmid;
1105#endif
1106#ifdef CONFIG_FUTEX
1107 struct robust_list_head __user *robust_list;
1108#ifdef CONFIG_COMPAT
1109 struct compat_robust_list_head __user *compat_robust_list;
1110#endif
1111 struct list_head pi_state_list;
1112 struct futex_pi_state *pi_state_cache;
David Brazdil0f672f62019-12-10 10:32:29 +00001113 struct mutex futex_exit_mutex;
1114 unsigned int futex_state;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001115#endif
1116#ifdef CONFIG_PERF_EVENTS
1117 struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1118 struct mutex perf_event_mutex;
1119 struct list_head perf_event_list;
1120#endif
1121#ifdef CONFIG_DEBUG_PREEMPT
1122 unsigned long preempt_disable_ip;
1123#endif
1124#ifdef CONFIG_NUMA
1125 /* Protected by alloc_lock: */
1126 struct mempolicy *mempolicy;
1127 short il_prev;
1128 short pref_node_fork;
1129#endif
1130#ifdef CONFIG_NUMA_BALANCING
1131 int numa_scan_seq;
1132 unsigned int numa_scan_period;
1133 unsigned int numa_scan_period_max;
1134 int numa_preferred_nid;
1135 unsigned long numa_migrate_retry;
1136 /* Migration stamp: */
1137 u64 node_stamp;
1138 u64 last_task_numa_placement;
1139 u64 last_sum_exec_runtime;
1140 struct callback_head numa_work;
1141
David Brazdil0f672f62019-12-10 10:32:29 +00001142 /*
1143 * This pointer is only modified for current in syscall and
1144 * pagefault context (and for tasks being destroyed), so it can be read
1145 * from any of the following contexts:
1146 * - RCU read-side critical section
1147 * - current->numa_group from everywhere
1148 * - task's runqueue locked, task not running
1149 */
1150 struct numa_group __rcu *numa_group;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001151
1152 /*
1153 * numa_faults is an array split into four regions:
1154 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
1155 * in this precise order.
1156 *
1157 * faults_memory: Exponential decaying average of faults on a per-node
1158 * basis. Scheduling placement decisions are made based on these
1159 * counts. The values remain static for the duration of a PTE scan.
1160 * faults_cpu: Track the nodes the process was running on when a NUMA
1161 * hinting fault was incurred.
1162 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
1163 * during the current scan window. When the scan completes, the counts
1164 * in faults_memory and faults_cpu decay and these values are copied.
1165 */
1166 unsigned long *numa_faults;
1167 unsigned long total_numa_faults;
1168
1169 /*
1170 * numa_faults_locality tracks if faults recorded during the last
1171 * scan window were remote/local or failed to migrate. The task scan
1172 * period is adapted based on the locality of the faults with different
1173 * weights depending on whether they were shared or private faults
1174 */
1175 unsigned long numa_faults_locality[3];
1176
1177 unsigned long numa_pages_migrated;
1178#endif /* CONFIG_NUMA_BALANCING */
1179
1180#ifdef CONFIG_RSEQ
1181 struct rseq __user *rseq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001182 u32 rseq_sig;
1183 /*
1184 * RmW on rseq_event_mask must be performed atomically
1185 * with respect to preemption.
1186 */
1187 unsigned long rseq_event_mask;
1188#endif
1189
1190 struct tlbflush_unmap_batch tlb_ubc;
1191
David Brazdil0f672f62019-12-10 10:32:29 +00001192 union {
1193 refcount_t rcu_users;
1194 struct rcu_head rcu;
1195 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001196
1197 /* Cache last used pipe for splice(): */
1198 struct pipe_inode_info *splice_pipe;
1199
1200 struct page_frag task_frag;
1201
1202#ifdef CONFIG_TASK_DELAY_ACCT
1203 struct task_delay_info *delays;
1204#endif
1205
1206#ifdef CONFIG_FAULT_INJECTION
1207 int make_it_fail;
1208 unsigned int fail_nth;
1209#endif
1210 /*
1211 * When (nr_dirtied >= nr_dirtied_pause), it's time to call
1212 * balance_dirty_pages() for a dirty throttling pause:
1213 */
1214 int nr_dirtied;
1215 int nr_dirtied_pause;
1216 /* Start of a write-and-pause period: */
1217 unsigned long dirty_paused_when;
1218
1219#ifdef CONFIG_LATENCYTOP
1220 int latency_record_count;
1221 struct latency_record latency_record[LT_SAVECOUNT];
1222#endif
1223 /*
1224 * Time slack values; these are used to round up poll() and
1225 * select() etc timeout values. These are in nanoseconds.
1226 */
1227 u64 timer_slack_ns;
1228 u64 default_timer_slack_ns;
1229
1230#ifdef CONFIG_KASAN
1231 unsigned int kasan_depth;
1232#endif
1233
Olivier Deprez157378f2022-04-04 15:47:50 +02001234#ifdef CONFIG_KCSAN
1235 struct kcsan_ctx kcsan_ctx;
1236#ifdef CONFIG_TRACE_IRQFLAGS
1237 struct irqtrace_events kcsan_save_irqtrace;
1238#endif
1239#endif
1240
1241#if IS_ENABLED(CONFIG_KUNIT)
1242 struct kunit *kunit_test;
1243#endif
1244
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1246 /* Index of current stored address in ret_stack: */
1247 int curr_ret_stack;
1248 int curr_ret_depth;
1249
1250 /* Stack of return addresses for return function tracing: */
1251 struct ftrace_ret_stack *ret_stack;
1252
1253 /* Timestamp for last schedule: */
1254 unsigned long long ftrace_timestamp;
1255
1256 /*
1257 * Number of functions that haven't been traced
1258 * because of depth overrun:
1259 */
1260 atomic_t trace_overrun;
1261
1262 /* Pause tracing: */
1263 atomic_t tracing_graph_pause;
1264#endif
1265
1266#ifdef CONFIG_TRACING
1267 /* State flags for use by tracers: */
1268 unsigned long trace;
1269
1270 /* Bitmask and counter of trace recursion: */
1271 unsigned long trace_recursion;
1272#endif /* CONFIG_TRACING */
1273
1274#ifdef CONFIG_KCOV
Olivier Deprez157378f2022-04-04 15:47:50 +02001275 /* See kernel/kcov.c for more details. */
1276
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001277 /* Coverage collection mode enabled for this task (0 if disabled): */
1278 unsigned int kcov_mode;
1279
1280 /* Size of the kcov_area: */
1281 unsigned int kcov_size;
1282
1283 /* Buffer for coverage collection: */
1284 void *kcov_area;
1285
1286 /* KCOV descriptor wired with this task or NULL: */
1287 struct kcov *kcov;
Olivier Deprez157378f2022-04-04 15:47:50 +02001288
1289 /* KCOV common handle for remote coverage collection: */
1290 u64 kcov_handle;
1291
1292 /* KCOV sequence number: */
1293 int kcov_sequence;
1294
1295 /* Collect coverage from softirq context: */
1296 unsigned int kcov_softirq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001297#endif
1298
1299#ifdef CONFIG_MEMCG
1300 struct mem_cgroup *memcg_in_oom;
1301 gfp_t memcg_oom_gfp_mask;
1302 int memcg_oom_order;
1303
1304 /* Number of pages to reclaim on returning to userland: */
1305 unsigned int memcg_nr_pages_over_high;
1306
1307 /* Used by memcontrol for targeted memcg charge: */
1308 struct mem_cgroup *active_memcg;
1309#endif
1310
1311#ifdef CONFIG_BLK_CGROUP
1312 struct request_queue *throttle_queue;
1313#endif
1314
1315#ifdef CONFIG_UPROBES
1316 struct uprobe_task *utask;
1317#endif
1318#if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1319 unsigned int sequential_io;
1320 unsigned int sequential_io_avg;
1321#endif
1322#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1323 unsigned long task_state_change;
1324#endif
1325 int pagefault_disabled;
1326#ifdef CONFIG_MMU
1327 struct task_struct *oom_reaper_list;
1328#endif
1329#ifdef CONFIG_VMAP_STACK
1330 struct vm_struct *stack_vm_area;
1331#endif
1332#ifdef CONFIG_THREAD_INFO_IN_TASK
1333 /* A live task holds one reference: */
David Brazdil0f672f62019-12-10 10:32:29 +00001334 refcount_t stack_refcount;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001335#endif
1336#ifdef CONFIG_LIVEPATCH
1337 int patch_state;
1338#endif
1339#ifdef CONFIG_SECURITY
1340 /* Used by LSM modules for access restriction: */
1341 void *security;
1342#endif
1343
David Brazdil0f672f62019-12-10 10:32:29 +00001344#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
1345 unsigned long lowest_stack;
1346 unsigned long prev_lowest_stack;
1347#endif
1348
Olivier Deprez157378f2022-04-04 15:47:50 +02001349#ifdef CONFIG_X86_MCE
1350 void __user *mce_vaddr;
1351 __u64 mce_kflags;
1352 u64 mce_addr;
1353 __u64 mce_ripv : 1,
1354 mce_whole_page : 1,
1355 __mce_reserved : 62;
1356 struct callback_head mce_kill_me;
1357 int mce_count;
1358#endif
1359
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001360 /*
1361 * New fields for task_struct should be added above here, so that
1362 * they are included in the randomized portion of task_struct.
1363 */
1364 randomized_struct_fields_end
1365
1366 /* CPU-specific state of this task: */
1367 struct thread_struct thread;
1368
1369 /*
1370 * WARNING: on x86, 'thread_struct' contains a variable-sized
1371 * structure. It *MUST* be at the end of 'task_struct'.
1372 *
1373 * Do not put anything below here!
1374 */
1375};
1376
1377static inline struct pid *task_pid(struct task_struct *task)
1378{
1379 return task->thread_pid;
1380}
1381
1382/*
1383 * the helpers to get the task's different pids as they are seen
1384 * from various namespaces
1385 *
1386 * task_xid_nr() : global id, i.e. the id seen from the init namespace;
1387 * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
1388 * current.
1389 * task_xid_nr_ns() : id seen from the ns specified;
1390 *
1391 * see also pid_nr() etc in include/linux/pid.h
1392 */
1393pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
1394
1395static inline pid_t task_pid_nr(struct task_struct *tsk)
1396{
1397 return tsk->pid;
1398}
1399
1400static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1401{
1402 return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1403}
1404
1405static inline pid_t task_pid_vnr(struct task_struct *tsk)
1406{
1407 return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
1408}
1409
1410
1411static inline pid_t task_tgid_nr(struct task_struct *tsk)
1412{
1413 return tsk->tgid;
1414}
1415
1416/**
1417 * pid_alive - check that a task structure is not stale
1418 * @p: Task structure to be checked.
1419 *
1420 * Test if a process is not yet dead (at most zombie state)
1421 * If pid_alive fails, then pointers within the task structure
1422 * can be stale and must not be dereferenced.
1423 *
1424 * Return: 1 if the process is alive. 0 otherwise.
1425 */
1426static inline int pid_alive(const struct task_struct *p)
1427{
1428 return p->thread_pid != NULL;
1429}
1430
1431static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1432{
1433 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
1434}
1435
1436static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
1437{
1438 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
1439}
1440
1441
1442static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1443{
1444 return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
1445}
1446
1447static inline pid_t task_session_vnr(struct task_struct *tsk)
1448{
1449 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
1450}
1451
1452static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1453{
1454 return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
1455}
1456
1457static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1458{
1459 return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL);
1460}
1461
1462static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1463{
1464 pid_t pid = 0;
1465
1466 rcu_read_lock();
1467 if (pid_alive(tsk))
1468 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1469 rcu_read_unlock();
1470
1471 return pid;
1472}
1473
1474static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1475{
1476 return task_ppid_nr_ns(tsk, &init_pid_ns);
1477}
1478
1479/* Obsolete, do not use: */
1480static inline pid_t task_pgrp_nr(struct task_struct *tsk)
1481{
1482 return task_pgrp_nr_ns(tsk, &init_pid_ns);
1483}
1484
1485#define TASK_REPORT_IDLE (TASK_REPORT + 1)
1486#define TASK_REPORT_MAX (TASK_REPORT_IDLE << 1)
1487
1488static inline unsigned int task_state_index(struct task_struct *tsk)
1489{
1490 unsigned int tsk_state = READ_ONCE(tsk->state);
1491 unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
1492
1493 BUILD_BUG_ON_NOT_POWER_OF_2(TASK_REPORT_MAX);
1494
1495 if (tsk_state == TASK_IDLE)
1496 state = TASK_REPORT_IDLE;
1497
1498 return fls(state);
1499}
1500
1501static inline char task_index_to_char(unsigned int state)
1502{
1503 static const char state_char[] = "RSDTtXZPI";
1504
1505 BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != sizeof(state_char) - 1);
1506
1507 return state_char[state];
1508}
1509
1510static inline char task_state_to_char(struct task_struct *tsk)
1511{
1512 return task_index_to_char(task_state_index(tsk));
1513}
1514
1515/**
1516 * is_global_init - check if a task structure is init. Since init
1517 * is free to have sub-threads we need to check tgid.
1518 * @tsk: Task structure to be checked.
1519 *
1520 * Check if a task structure is the first user space task the kernel created.
1521 *
1522 * Return: 1 if the task structure is init. 0 otherwise.
1523 */
1524static inline int is_global_init(struct task_struct *tsk)
1525{
1526 return task_tgid_nr(tsk) == 1;
1527}
1528
1529extern struct pid *cad_pid;
1530
1531/*
1532 * Per process flags
1533 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001534#define PF_VCPU 0x00000001 /* I'm a virtual CPU */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001535#define PF_IDLE 0x00000002 /* I am an IDLE thread */
1536#define PF_EXITING 0x00000004 /* Getting shut down */
Olivier Deprez157378f2022-04-04 15:47:50 +02001537#define PF_IO_WORKER 0x00000010 /* Task is an IO worker */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001538#define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
1539#define PF_FORKNOEXEC 0x00000040 /* Forked but didn't exec */
1540#define PF_MCE_PROCESS 0x00000080 /* Process policy on mce errors */
1541#define PF_SUPERPRIV 0x00000100 /* Used super-user privileges */
1542#define PF_DUMPCORE 0x00000200 /* Dumped core */
1543#define PF_SIGNALED 0x00000400 /* Killed by a signal */
1544#define PF_MEMALLOC 0x00000800 /* Allocating memory */
1545#define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */
1546#define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001547#define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */
1548#define PF_FROZEN 0x00010000 /* Frozen for system suspend */
1549#define PF_KSWAPD 0x00020000 /* I am kswapd */
1550#define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */
1551#define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */
Olivier Deprez157378f2022-04-04 15:47:50 +02001552#define PF_LOCAL_THROTTLE 0x00100000 /* Throttle writes only against the bdi I write to,
1553 * I am cleaning dirty pages from some other bdi. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
1555#define PF_RANDOMIZE 0x00400000 /* Randomize virtual address space */
1556#define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
David Brazdil0f672f62019-12-10 10:32:29 +00001557#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_mask */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001558#define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
David Brazdil0f672f62019-12-10 10:32:29 +00001559#define PF_MEMALLOC_NOCMA 0x10000000 /* All allocation request will have _GFP_MOVABLE cleared */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001560#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
1561#define PF_SUSPEND_TASK 0x80000000 /* This thread called freeze_processes() and should not be frozen */
1562
1563/*
1564 * Only the _current_ task can read/write to tsk->flags, but other
1565 * tasks can access tsk->flags in readonly mode for example
1566 * with tsk_used_math (like during threaded core dumping).
1567 * There is however an exception to this rule during ptrace
1568 * or during fork: the ptracer task is allowed to write to the
1569 * child->flags of its traced child (same goes for fork, the parent
1570 * can write to the child->flags), because we're guaranteed the
1571 * child is not running and in turn not changing child->flags
1572 * at the same time the parent does it.
1573 */
1574#define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
1575#define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
1576#define clear_used_math() clear_stopped_child_used_math(current)
1577#define set_used_math() set_stopped_child_used_math(current)
1578
1579#define conditional_stopped_child_used_math(condition, child) \
1580 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
1581
1582#define conditional_used_math(condition) conditional_stopped_child_used_math(condition, current)
1583
1584#define copy_to_stopped_child_used_math(child) \
1585 do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
1586
1587/* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
1588#define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
1589#define used_math() tsk_used_math(current)
1590
Olivier Deprez157378f2022-04-04 15:47:50 +02001591static __always_inline bool is_percpu_thread(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592{
1593#ifdef CONFIG_SMP
1594 return (current->flags & PF_NO_SETAFFINITY) &&
1595 (current->nr_cpus_allowed == 1);
1596#else
1597 return true;
1598#endif
1599}
1600
1601/* Per-process atomic flags. */
1602#define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
1603#define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
1604#define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
1605#define PFA_SPEC_SSB_DISABLE 3 /* Speculative Store Bypass disabled */
1606#define PFA_SPEC_SSB_FORCE_DISABLE 4 /* Speculative Store Bypass force disabled*/
1607#define PFA_SPEC_IB_DISABLE 5 /* Indirect branch speculation restricted */
1608#define PFA_SPEC_IB_FORCE_DISABLE 6 /* Indirect branch speculation permanently restricted */
David Brazdil0f672f62019-12-10 10:32:29 +00001609#define PFA_SPEC_SSB_NOEXEC 7 /* Speculative Store Bypass clear on execve() */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001610
1611#define TASK_PFA_TEST(name, func) \
1612 static inline bool task_##func(struct task_struct *p) \
1613 { return test_bit(PFA_##name, &p->atomic_flags); }
1614
1615#define TASK_PFA_SET(name, func) \
1616 static inline void task_set_##func(struct task_struct *p) \
1617 { set_bit(PFA_##name, &p->atomic_flags); }
1618
1619#define TASK_PFA_CLEAR(name, func) \
1620 static inline void task_clear_##func(struct task_struct *p) \
1621 { clear_bit(PFA_##name, &p->atomic_flags); }
1622
1623TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
1624TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
1625
1626TASK_PFA_TEST(SPREAD_PAGE, spread_page)
1627TASK_PFA_SET(SPREAD_PAGE, spread_page)
1628TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
1629
1630TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
1631TASK_PFA_SET(SPREAD_SLAB, spread_slab)
1632TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
1633
1634TASK_PFA_TEST(SPEC_SSB_DISABLE, spec_ssb_disable)
1635TASK_PFA_SET(SPEC_SSB_DISABLE, spec_ssb_disable)
1636TASK_PFA_CLEAR(SPEC_SSB_DISABLE, spec_ssb_disable)
1637
David Brazdil0f672f62019-12-10 10:32:29 +00001638TASK_PFA_TEST(SPEC_SSB_NOEXEC, spec_ssb_noexec)
1639TASK_PFA_SET(SPEC_SSB_NOEXEC, spec_ssb_noexec)
1640TASK_PFA_CLEAR(SPEC_SSB_NOEXEC, spec_ssb_noexec)
1641
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001642TASK_PFA_TEST(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1643TASK_PFA_SET(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1644
1645TASK_PFA_TEST(SPEC_IB_DISABLE, spec_ib_disable)
1646TASK_PFA_SET(SPEC_IB_DISABLE, spec_ib_disable)
1647TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable)
1648
1649TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
1650TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
1651
1652static inline void
1653current_restore_flags(unsigned long orig_flags, unsigned long flags)
1654{
1655 current->flags &= ~flags;
1656 current->flags |= orig_flags & flags;
1657}
1658
1659extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
1660extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
1661#ifdef CONFIG_SMP
1662extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
1663extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
1664#else
1665static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1666{
1667}
1668static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1669{
1670 if (!cpumask_test_cpu(0, new_mask))
1671 return -EINVAL;
1672 return 0;
1673}
1674#endif
1675
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001676extern int yield_to(struct task_struct *p, bool preempt);
1677extern void set_user_nice(struct task_struct *p, long nice);
1678extern int task_prio(const struct task_struct *p);
1679
1680/**
1681 * task_nice - return the nice value of a given task.
1682 * @p: the task in question.
1683 *
1684 * Return: The nice value [ -20 ... 0 ... 19 ].
1685 */
1686static inline int task_nice(const struct task_struct *p)
1687{
1688 return PRIO_TO_NICE((p)->static_prio);
1689}
1690
1691extern int can_nice(const struct task_struct *p, const int nice);
1692extern int task_curr(const struct task_struct *p);
1693extern int idle_cpu(int cpu);
1694extern int available_idle_cpu(int cpu);
1695extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
1696extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
Olivier Deprez157378f2022-04-04 15:47:50 +02001697extern void sched_set_fifo(struct task_struct *p);
1698extern void sched_set_fifo_low(struct task_struct *p);
1699extern void sched_set_normal(struct task_struct *p, int nice);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001700extern int sched_setattr(struct task_struct *, const struct sched_attr *);
1701extern int sched_setattr_nocheck(struct task_struct *, const struct sched_attr *);
1702extern struct task_struct *idle_task(int cpu);
1703
1704/**
1705 * is_idle_task - is the specified task an idle task?
1706 * @p: the task in question.
1707 *
1708 * Return: 1 if @p is an idle task. 0 otherwise.
1709 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001710static __always_inline bool is_idle_task(const struct task_struct *p)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001711{
1712 return !!(p->flags & PF_IDLE);
1713}
1714
1715extern struct task_struct *curr_task(int cpu);
1716extern void ia64_set_curr_task(int cpu, struct task_struct *p);
1717
1718void yield(void);
1719
1720union thread_union {
1721#ifndef CONFIG_ARCH_TASK_STRUCT_ON_STACK
1722 struct task_struct task;
1723#endif
1724#ifndef CONFIG_THREAD_INFO_IN_TASK
1725 struct thread_info thread_info;
1726#endif
1727 unsigned long stack[THREAD_SIZE/sizeof(long)];
1728};
1729
1730#ifndef CONFIG_THREAD_INFO_IN_TASK
1731extern struct thread_info init_thread_info;
1732#endif
1733
1734extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
1735
1736#ifdef CONFIG_THREAD_INFO_IN_TASK
1737static inline struct thread_info *task_thread_info(struct task_struct *task)
1738{
1739 return &task->thread_info;
1740}
1741#elif !defined(__HAVE_THREAD_FUNCTIONS)
1742# define task_thread_info(task) ((struct thread_info *)(task)->stack)
1743#endif
1744
1745/*
1746 * find a task by one of its numerical ids
1747 *
1748 * find_task_by_pid_ns():
1749 * finds a task by its pid in the specified namespace
1750 * find_task_by_vpid():
1751 * finds a task by its virtual pid
1752 *
1753 * see also find_vpid() etc in include/linux/pid.h
1754 */
1755
1756extern struct task_struct *find_task_by_vpid(pid_t nr);
1757extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
1758
1759/*
1760 * find a task by its virtual pid and get the task struct
1761 */
1762extern struct task_struct *find_get_task_by_vpid(pid_t nr);
1763
1764extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1765extern int wake_up_process(struct task_struct *tsk);
1766extern void wake_up_new_task(struct task_struct *tsk);
1767
1768#ifdef CONFIG_SMP
1769extern void kick_process(struct task_struct *tsk);
1770#else
1771static inline void kick_process(struct task_struct *tsk) { }
1772#endif
1773
1774extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
1775
1776static inline void set_task_comm(struct task_struct *tsk, const char *from)
1777{
1778 __set_task_comm(tsk, from, false);
1779}
1780
1781extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
1782#define get_task_comm(buf, tsk) ({ \
1783 BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \
1784 __get_task_comm(buf, sizeof(buf), tsk); \
1785})
1786
1787#ifdef CONFIG_SMP
Olivier Deprez157378f2022-04-04 15:47:50 +02001788static __always_inline void scheduler_ipi(void)
1789{
1790 /*
1791 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1792 * TIF_NEED_RESCHED remotely (for the first time) will also send
1793 * this IPI.
1794 */
1795 preempt_fold_need_resched();
1796}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001797extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
1798#else
1799static inline void scheduler_ipi(void) { }
1800static inline unsigned long wait_task_inactive(struct task_struct *p, long match_state)
1801{
1802 return 1;
1803}
1804#endif
1805
1806/*
1807 * Set thread flags in other task's structures.
1808 * See asm/thread_info.h for TIF_xxxx flags available:
1809 */
1810static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
1811{
1812 set_ti_thread_flag(task_thread_info(tsk), flag);
1813}
1814
1815static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1816{
1817 clear_ti_thread_flag(task_thread_info(tsk), flag);
1818}
1819
1820static inline void update_tsk_thread_flag(struct task_struct *tsk, int flag,
1821 bool value)
1822{
1823 update_ti_thread_flag(task_thread_info(tsk), flag, value);
1824}
1825
1826static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
1827{
1828 return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
1829}
1830
1831static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
1832{
1833 return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
1834}
1835
1836static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
1837{
1838 return test_ti_thread_flag(task_thread_info(tsk), flag);
1839}
1840
1841static inline void set_tsk_need_resched(struct task_struct *tsk)
1842{
1843 set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
1844}
1845
1846static inline void clear_tsk_need_resched(struct task_struct *tsk)
1847{
1848 clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
1849}
1850
1851static inline int test_tsk_need_resched(struct task_struct *tsk)
1852{
1853 return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
1854}
1855
1856/*
1857 * cond_resched() and cond_resched_lock(): latency reduction via
1858 * explicit rescheduling in places that are safe. The return
1859 * value indicates whether a reschedule was done in fact.
1860 * cond_resched_lock() will drop the spinlock before scheduling,
1861 */
David Brazdil0f672f62019-12-10 10:32:29 +00001862#ifndef CONFIG_PREEMPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001863extern int _cond_resched(void);
1864#else
1865static inline int _cond_resched(void) { return 0; }
1866#endif
1867
1868#define cond_resched() ({ \
1869 ___might_sleep(__FILE__, __LINE__, 0); \
1870 _cond_resched(); \
1871})
1872
1873extern int __cond_resched_lock(spinlock_t *lock);
1874
1875#define cond_resched_lock(lock) ({ \
1876 ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
1877 __cond_resched_lock(lock); \
1878})
1879
1880static inline void cond_resched_rcu(void)
1881{
1882#if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
1883 rcu_read_unlock();
1884 cond_resched();
1885 rcu_read_lock();
1886#endif
1887}
1888
1889/*
1890 * Does a critical section need to be broken due to another
David Brazdil0f672f62019-12-10 10:32:29 +00001891 * task waiting?: (technically does not depend on CONFIG_PREEMPTION,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001892 * but a general need for low latency)
1893 */
1894static inline int spin_needbreak(spinlock_t *lock)
1895{
David Brazdil0f672f62019-12-10 10:32:29 +00001896#ifdef CONFIG_PREEMPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001897 return spin_is_contended(lock);
1898#else
1899 return 0;
1900#endif
1901}
1902
1903static __always_inline bool need_resched(void)
1904{
1905 return unlikely(tif_need_resched());
1906}
1907
1908/*
1909 * Wrappers for p->thread_info->cpu access. No-op on UP.
1910 */
1911#ifdef CONFIG_SMP
1912
1913static inline unsigned int task_cpu(const struct task_struct *p)
1914{
1915#ifdef CONFIG_THREAD_INFO_IN_TASK
David Brazdil0f672f62019-12-10 10:32:29 +00001916 return READ_ONCE(p->cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001917#else
David Brazdil0f672f62019-12-10 10:32:29 +00001918 return READ_ONCE(task_thread_info(p)->cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001919#endif
1920}
1921
1922extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
1923
1924#else
1925
1926static inline unsigned int task_cpu(const struct task_struct *p)
1927{
1928 return 0;
1929}
1930
1931static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
1932{
1933}
1934
1935#endif /* CONFIG_SMP */
1936
1937/*
1938 * In order to reduce various lock holder preemption latencies provide an
1939 * interface to see if a vCPU is currently running or not.
1940 *
1941 * This allows us to terminate optimistic spin loops and block, analogous to
1942 * the native optimistic spin heuristic of testing if the lock owner task is
1943 * running or not.
1944 */
1945#ifndef vcpu_is_preempted
David Brazdil0f672f62019-12-10 10:32:29 +00001946static inline bool vcpu_is_preempted(int cpu)
1947{
1948 return false;
1949}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001950#endif
1951
1952extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
1953extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
1954
1955#ifndef TASK_SIZE_OF
1956#define TASK_SIZE_OF(tsk) TASK_SIZE
1957#endif
1958
1959#ifdef CONFIG_RSEQ
1960
1961/*
1962 * Map the event mask on the user-space ABI enum rseq_cs_flags
1963 * for direct mask checks.
1964 */
1965enum rseq_event_mask_bits {
1966 RSEQ_EVENT_PREEMPT_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT,
1967 RSEQ_EVENT_SIGNAL_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT,
1968 RSEQ_EVENT_MIGRATE_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT,
1969};
1970
1971enum rseq_event_mask {
1972 RSEQ_EVENT_PREEMPT = (1U << RSEQ_EVENT_PREEMPT_BIT),
1973 RSEQ_EVENT_SIGNAL = (1U << RSEQ_EVENT_SIGNAL_BIT),
1974 RSEQ_EVENT_MIGRATE = (1U << RSEQ_EVENT_MIGRATE_BIT),
1975};
1976
1977static inline void rseq_set_notify_resume(struct task_struct *t)
1978{
1979 if (t->rseq)
1980 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1981}
1982
1983void __rseq_handle_notify_resume(struct ksignal *sig, struct pt_regs *regs);
1984
1985static inline void rseq_handle_notify_resume(struct ksignal *ksig,
1986 struct pt_regs *regs)
1987{
1988 if (current->rseq)
1989 __rseq_handle_notify_resume(ksig, regs);
1990}
1991
1992static inline void rseq_signal_deliver(struct ksignal *ksig,
1993 struct pt_regs *regs)
1994{
1995 preempt_disable();
1996 __set_bit(RSEQ_EVENT_SIGNAL_BIT, &current->rseq_event_mask);
1997 preempt_enable();
1998 rseq_handle_notify_resume(ksig, regs);
1999}
2000
2001/* rseq_preempt() requires preemption to be disabled. */
2002static inline void rseq_preempt(struct task_struct *t)
2003{
2004 __set_bit(RSEQ_EVENT_PREEMPT_BIT, &t->rseq_event_mask);
2005 rseq_set_notify_resume(t);
2006}
2007
2008/* rseq_migrate() requires preemption to be disabled. */
2009static inline void rseq_migrate(struct task_struct *t)
2010{
2011 __set_bit(RSEQ_EVENT_MIGRATE_BIT, &t->rseq_event_mask);
2012 rseq_set_notify_resume(t);
2013}
2014
2015/*
2016 * If parent process has a registered restartable sequences area, the
Olivier Deprez0e641232021-09-23 10:07:05 +02002017 * child inherits. Unregister rseq for a clone with CLONE_VM set.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002018 */
2019static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
2020{
Olivier Deprez0e641232021-09-23 10:07:05 +02002021 if (clone_flags & CLONE_VM) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002022 t->rseq = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002023 t->rseq_sig = 0;
2024 t->rseq_event_mask = 0;
2025 } else {
2026 t->rseq = current->rseq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002027 t->rseq_sig = current->rseq_sig;
2028 t->rseq_event_mask = current->rseq_event_mask;
2029 }
2030}
2031
2032static inline void rseq_execve(struct task_struct *t)
2033{
2034 t->rseq = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002035 t->rseq_sig = 0;
2036 t->rseq_event_mask = 0;
2037}
2038
2039#else
2040
2041static inline void rseq_set_notify_resume(struct task_struct *t)
2042{
2043}
2044static inline void rseq_handle_notify_resume(struct ksignal *ksig,
2045 struct pt_regs *regs)
2046{
2047}
2048static inline void rseq_signal_deliver(struct ksignal *ksig,
2049 struct pt_regs *regs)
2050{
2051}
2052static inline void rseq_preempt(struct task_struct *t)
2053{
2054}
2055static inline void rseq_migrate(struct task_struct *t)
2056{
2057}
2058static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
2059{
2060}
2061static inline void rseq_execve(struct task_struct *t)
2062{
2063}
2064
2065#endif
2066
2067#ifdef CONFIG_DEBUG_RSEQ
2068
2069void rseq_syscall(struct pt_regs *regs);
2070
2071#else
2072
2073static inline void rseq_syscall(struct pt_regs *regs)
2074{
2075}
2076
2077#endif
2078
David Brazdil0f672f62019-12-10 10:32:29 +00002079const struct sched_avg *sched_trace_cfs_rq_avg(struct cfs_rq *cfs_rq);
2080char *sched_trace_cfs_rq_path(struct cfs_rq *cfs_rq, char *str, int len);
2081int sched_trace_cfs_rq_cpu(struct cfs_rq *cfs_rq);
2082
2083const struct sched_avg *sched_trace_rq_avg_rt(struct rq *rq);
2084const struct sched_avg *sched_trace_rq_avg_dl(struct rq *rq);
2085const struct sched_avg *sched_trace_rq_avg_irq(struct rq *rq);
2086
2087int sched_trace_rq_cpu(struct rq *rq);
Olivier Deprez157378f2022-04-04 15:47:50 +02002088int sched_trace_rq_cpu_capacity(struct rq *rq);
2089int sched_trace_rq_nr_running(struct rq *rq);
David Brazdil0f672f62019-12-10 10:32:29 +00002090
2091const struct cpumask *sched_trace_rd_span(struct root_domain *rd);
2092
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002093#endif