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