sched.h 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. #ifndef _LINUX_SCHED_H
  2. #define _LINUX_SCHED_H
  3. /*
  4. * Define 'struct task_struct' and provide the main scheduler
  5. * APIs (schedule(), wakeup variants, etc.)
  6. */
  7. #include <uapi/linux/sched.h>
  8. #include <asm/current.h>
  9. #include <linux/pid.h>
  10. #include <linux/sem.h>
  11. #include <linux/shm.h>
  12. #include <linux/kcov.h>
  13. #include <linux/mutex.h>
  14. #include <linux/plist.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/seccomp.h>
  17. #include <linux/nodemask.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/resource.h>
  20. #include <linux/latencytop.h>
  21. #include <linux/sched/prio.h>
  22. #include <linux/signal_types.h>
  23. #include <linux/mm_types_task.h>
  24. #include <linux/task_io_accounting.h>
  25. /* task_struct member predeclarations (sorted alphabetically): */
  26. struct audit_context;
  27. struct backing_dev_info;
  28. struct bio_list;
  29. struct blk_plug;
  30. struct cfs_rq;
  31. struct fs_struct;
  32. struct futex_pi_state;
  33. struct io_context;
  34. struct mempolicy;
  35. struct nameidata;
  36. struct nsproxy;
  37. struct perf_event_context;
  38. struct pid_namespace;
  39. struct pipe_inode_info;
  40. struct rcu_node;
  41. struct reclaim_state;
  42. struct robust_list_head;
  43. struct sched_attr;
  44. struct sched_param;
  45. struct seq_file;
  46. struct sighand_struct;
  47. struct signal_struct;
  48. struct task_delay_info;
  49. struct task_group;
  50. /*
  51. * Task state bitmask. NOTE! These bits are also
  52. * encoded in fs/proc/array.c: get_task_state().
  53. *
  54. * We have two separate sets of flags: task->state
  55. * is about runnability, while task->exit_state are
  56. * about the task exiting. Confusing, but this way
  57. * modifying one set can't modify the other one by
  58. * mistake.
  59. */
  60. /* Used in tsk->state: */
  61. #define TASK_RUNNING 0
  62. #define TASK_INTERRUPTIBLE 1
  63. #define TASK_UNINTERRUPTIBLE 2
  64. #define __TASK_STOPPED 4
  65. #define __TASK_TRACED 8
  66. /* Used in tsk->exit_state: */
  67. #define EXIT_DEAD 16
  68. #define EXIT_ZOMBIE 32
  69. #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
  70. /* Used in tsk->state again: */
  71. #define TASK_DEAD 64
  72. #define TASK_WAKEKILL 128
  73. #define TASK_WAKING 256
  74. #define TASK_PARKED 512
  75. #define TASK_NOLOAD 1024
  76. #define TASK_NEW 2048
  77. #define TASK_STATE_MAX 4096
  78. #define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
  79. /* Convenience macros for the sake of set_current_state: */
  80. #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
  81. #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
  82. #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
  83. #define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
  84. /* Convenience macros for the sake of wake_up(): */
  85. #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
  86. #define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
  87. /* get_task_state(): */
  88. #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
  89. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  90. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  91. #define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  92. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  93. #define task_is_stopped_or_traced(task) ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  94. #define task_contributes_to_load(task) ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  95. (task->flags & PF_FROZEN) == 0 && \
  96. (task->state & TASK_NOLOAD) == 0)
  97. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  98. #define __set_current_state(state_value) \
  99. do { \
  100. current->task_state_change = _THIS_IP_; \
  101. current->state = (state_value); \
  102. } while (0)
  103. #define set_current_state(state_value) \
  104. do { \
  105. current->task_state_change = _THIS_IP_; \
  106. smp_store_mb(current->state, (state_value)); \
  107. } while (0)
  108. #else
  109. /*
  110. * set_current_state() includes a barrier so that the write of current->state
  111. * is correctly serialised wrt the caller's subsequent test of whether to
  112. * actually sleep:
  113. *
  114. * for (;;) {
  115. * set_current_state(TASK_UNINTERRUPTIBLE);
  116. * if (!need_sleep)
  117. * break;
  118. *
  119. * schedule();
  120. * }
  121. * __set_current_state(TASK_RUNNING);
  122. *
  123. * If the caller does not need such serialisation (because, for instance, the
  124. * condition test and condition change and wakeup are under the same lock) then
  125. * use __set_current_state().
  126. *
  127. * The above is typically ordered against the wakeup, which does:
  128. *
  129. * need_sleep = false;
  130. * wake_up_state(p, TASK_UNINTERRUPTIBLE);
  131. *
  132. * Where wake_up_state() (and all other wakeup primitives) imply enough
  133. * barriers to order the store of the variable against wakeup.
  134. *
  135. * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
  136. * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
  137. * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
  138. *
  139. * This is obviously fine, since they both store the exact same value.
  140. *
  141. * Also see the comments of try_to_wake_up().
  142. */
  143. #define __set_current_state(state_value) do { current->state = (state_value); } while (0)
  144. #define set_current_state(state_value) smp_store_mb(current->state, (state_value))
  145. #endif
  146. /* Task command name length: */
  147. #define TASK_COMM_LEN 16
  148. extern cpumask_var_t cpu_isolated_map;
  149. extern void scheduler_tick(void);
  150. #define MAX_SCHEDULE_TIMEOUT LONG_MAX
  151. extern long schedule_timeout(long timeout);
  152. extern long schedule_timeout_interruptible(long timeout);
  153. extern long schedule_timeout_killable(long timeout);
  154. extern long schedule_timeout_uninterruptible(long timeout);
  155. extern long schedule_timeout_idle(long timeout);
  156. asmlinkage void schedule(void);
  157. extern void schedule_preempt_disabled(void);
  158. extern int __must_check io_schedule_prepare(void);
  159. extern void io_schedule_finish(int token);
  160. extern long io_schedule_timeout(long timeout);
  161. extern void io_schedule(void);
  162. /**
  163. * struct prev_cputime - snapshot of system and user cputime
  164. * @utime: time spent in user mode
  165. * @stime: time spent in system mode
  166. * @lock: protects the above two fields
  167. *
  168. * Stores previous user/system time values such that we can guarantee
  169. * monotonicity.
  170. */
  171. struct prev_cputime {
  172. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  173. u64 utime;
  174. u64 stime;
  175. raw_spinlock_t lock;
  176. #endif
  177. };
  178. /**
  179. * struct task_cputime - collected CPU time counts
  180. * @utime: time spent in user mode, in nanoseconds
  181. * @stime: time spent in kernel mode, in nanoseconds
  182. * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
  183. *
  184. * This structure groups together three kinds of CPU time that are tracked for
  185. * threads and thread groups. Most things considering CPU time want to group
  186. * these counts together and treat all three of them in parallel.
  187. */
  188. struct task_cputime {
  189. u64 utime;
  190. u64 stime;
  191. unsigned long long sum_exec_runtime;
  192. };
  193. /* Alternate field names when used on cache expirations: */
  194. #define virt_exp utime
  195. #define prof_exp stime
  196. #define sched_exp sum_exec_runtime
  197. struct sched_info {
  198. #ifdef CONFIG_SCHED_INFO
  199. /* Cumulative counters: */
  200. /* # of times we have run on this CPU: */
  201. unsigned long pcount;
  202. /* Time spent waiting on a runqueue: */
  203. unsigned long long run_delay;
  204. /* Timestamps: */
  205. /* When did we last run on a CPU? */
  206. unsigned long long last_arrival;
  207. /* When were we last queued to run? */
  208. unsigned long long last_queued;
  209. #endif /* CONFIG_SCHED_INFO */
  210. };
  211. /*
  212. * Integer metrics need fixed point arithmetic, e.g., sched/fair
  213. * has a few: load, load_avg, util_avg, freq, and capacity.
  214. *
  215. * We define a basic fixed point arithmetic range, and then formalize
  216. * all these metrics based on that basic range.
  217. */
  218. # define SCHED_FIXEDPOINT_SHIFT 10
  219. # define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
  220. struct load_weight {
  221. unsigned long weight;
  222. u32 inv_weight;
  223. };
  224. /*
  225. * The load_avg/util_avg accumulates an infinite geometric series
  226. * (see __update_load_avg() in kernel/sched/fair.c).
  227. *
  228. * [load_avg definition]
  229. *
  230. * load_avg = runnable% * scale_load_down(load)
  231. *
  232. * where runnable% is the time ratio that a sched_entity is runnable.
  233. * For cfs_rq, it is the aggregated load_avg of all runnable and
  234. * blocked sched_entities.
  235. *
  236. * load_avg may also take frequency scaling into account:
  237. *
  238. * load_avg = runnable% * scale_load_down(load) * freq%
  239. *
  240. * where freq% is the CPU frequency normalized to the highest frequency.
  241. *
  242. * [util_avg definition]
  243. *
  244. * util_avg = running% * SCHED_CAPACITY_SCALE
  245. *
  246. * where running% is the time ratio that a sched_entity is running on
  247. * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
  248. * and blocked sched_entities.
  249. *
  250. * util_avg may also factor frequency scaling and CPU capacity scaling:
  251. *
  252. * util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
  253. *
  254. * where freq% is the same as above, and capacity% is the CPU capacity
  255. * normalized to the greatest capacity (due to uarch differences, etc).
  256. *
  257. * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
  258. * themselves are in the range of [0, 1]. To do fixed point arithmetics,
  259. * we therefore scale them to as large a range as necessary. This is for
  260. * example reflected by util_avg's SCHED_CAPACITY_SCALE.
  261. *
  262. * [Overflow issue]
  263. *
  264. * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
  265. * with the highest load (=88761), always runnable on a single cfs_rq,
  266. * and should not overflow as the number already hits PID_MAX_LIMIT.
  267. *
  268. * For all other cases (including 32-bit kernels), struct load_weight's
  269. * weight will overflow first before we do, because:
  270. *
  271. * Max(load_avg) <= Max(load.weight)
  272. *
  273. * Then it is the load_weight's responsibility to consider overflow
  274. * issues.
  275. */
  276. struct sched_avg {
  277. u64 last_update_time;
  278. u64 load_sum;
  279. u32 util_sum;
  280. u32 period_contrib;
  281. unsigned long load_avg;
  282. unsigned long util_avg;
  283. };
  284. struct sched_statistics {
  285. #ifdef CONFIG_SCHEDSTATS
  286. u64 wait_start;
  287. u64 wait_max;
  288. u64 wait_count;
  289. u64 wait_sum;
  290. u64 iowait_count;
  291. u64 iowait_sum;
  292. u64 sleep_start;
  293. u64 sleep_max;
  294. s64 sum_sleep_runtime;
  295. u64 block_start;
  296. u64 block_max;
  297. u64 exec_max;
  298. u64 slice_max;
  299. u64 nr_migrations_cold;
  300. u64 nr_failed_migrations_affine;
  301. u64 nr_failed_migrations_running;
  302. u64 nr_failed_migrations_hot;
  303. u64 nr_forced_migrations;
  304. u64 nr_wakeups;
  305. u64 nr_wakeups_sync;
  306. u64 nr_wakeups_migrate;
  307. u64 nr_wakeups_local;
  308. u64 nr_wakeups_remote;
  309. u64 nr_wakeups_affine;
  310. u64 nr_wakeups_affine_attempts;
  311. u64 nr_wakeups_passive;
  312. u64 nr_wakeups_idle;
  313. #endif
  314. };
  315. struct sched_entity {
  316. /* For load-balancing: */
  317. struct load_weight load;
  318. struct rb_node run_node;
  319. struct list_head group_node;
  320. unsigned int on_rq;
  321. u64 exec_start;
  322. u64 sum_exec_runtime;
  323. u64 vruntime;
  324. u64 prev_sum_exec_runtime;
  325. u64 nr_migrations;
  326. struct sched_statistics statistics;
  327. #ifdef CONFIG_FAIR_GROUP_SCHED
  328. int depth;
  329. struct sched_entity *parent;
  330. /* rq on which this entity is (to be) queued: */
  331. struct cfs_rq *cfs_rq;
  332. /* rq "owned" by this entity/group: */
  333. struct cfs_rq *my_q;
  334. #endif
  335. #ifdef CONFIG_SMP
  336. /*
  337. * Per entity load average tracking.
  338. *
  339. * Put into separate cache line so it does not
  340. * collide with read-mostly values above.
  341. */
  342. struct sched_avg avg ____cacheline_aligned_in_smp;
  343. #endif
  344. };
  345. struct sched_rt_entity {
  346. struct list_head run_list;
  347. unsigned long timeout;
  348. unsigned long watchdog_stamp;
  349. unsigned int time_slice;
  350. unsigned short on_rq;
  351. unsigned short on_list;
  352. struct sched_rt_entity *back;
  353. #ifdef CONFIG_RT_GROUP_SCHED
  354. struct sched_rt_entity *parent;
  355. /* rq on which this entity is (to be) queued: */
  356. struct rt_rq *rt_rq;
  357. /* rq "owned" by this entity/group: */
  358. struct rt_rq *my_q;
  359. #endif
  360. };
  361. struct sched_dl_entity {
  362. struct rb_node rb_node;
  363. /*
  364. * Original scheduling parameters. Copied here from sched_attr
  365. * during sched_setattr(), they will remain the same until
  366. * the next sched_setattr().
  367. */
  368. u64 dl_runtime; /* Maximum runtime for each instance */
  369. u64 dl_deadline; /* Relative deadline of each instance */
  370. u64 dl_period; /* Separation of two instances (period) */
  371. u64 dl_bw; /* dl_runtime / dl_deadline */
  372. /*
  373. * Actual scheduling parameters. Initialized with the values above,
  374. * they are continously updated during task execution. Note that
  375. * the remaining runtime could be < 0 in case we are in overrun.
  376. */
  377. s64 runtime; /* Remaining runtime for this instance */
  378. u64 deadline; /* Absolute deadline for this instance */
  379. unsigned int flags; /* Specifying the scheduler behaviour */
  380. /*
  381. * Some bool flags:
  382. *
  383. * @dl_throttled tells if we exhausted the runtime. If so, the
  384. * task has to wait for a replenishment to be performed at the
  385. * next firing of dl_timer.
  386. *
  387. * @dl_boosted tells if we are boosted due to DI. If so we are
  388. * outside bandwidth enforcement mechanism (but only until we
  389. * exit the critical section);
  390. *
  391. * @dl_yielded tells if task gave up the CPU before consuming
  392. * all its available runtime during the last job.
  393. *
  394. * @dl_non_contending tells if the task is inactive while still
  395. * contributing to the active utilization. In other words, it
  396. * indicates if the inactive timer has been armed and its handler
  397. * has not been executed yet. This flag is useful to avoid race
  398. * conditions between the inactive timer handler and the wakeup
  399. * code.
  400. */
  401. int dl_throttled;
  402. int dl_boosted;
  403. int dl_yielded;
  404. int dl_non_contending;
  405. /*
  406. * Bandwidth enforcement timer. Each -deadline task has its
  407. * own bandwidth to be enforced, thus we need one timer per task.
  408. */
  409. struct hrtimer dl_timer;
  410. /*
  411. * Inactive timer, responsible for decreasing the active utilization
  412. * at the "0-lag time". When a -deadline task blocks, it contributes
  413. * to GRUB's active utilization until the "0-lag time", hence a
  414. * timer is needed to decrease the active utilization at the correct
  415. * time.
  416. */
  417. struct hrtimer inactive_timer;
  418. };
  419. union rcu_special {
  420. struct {
  421. u8 blocked;
  422. u8 need_qs;
  423. u8 exp_need_qs;
  424. /* Otherwise the compiler can store garbage here: */
  425. u8 pad;
  426. } b; /* Bits. */
  427. u32 s; /* Set of bits. */
  428. };
  429. enum perf_event_task_context {
  430. perf_invalid_context = -1,
  431. perf_hw_context = 0,
  432. perf_sw_context,
  433. perf_nr_task_contexts,
  434. };
  435. struct wake_q_node {
  436. struct wake_q_node *next;
  437. };
  438. struct task_struct {
  439. #ifdef CONFIG_THREAD_INFO_IN_TASK
  440. /*
  441. * For reasons of header soup (see current_thread_info()), this
  442. * must be the first element of task_struct.
  443. */
  444. struct thread_info thread_info;
  445. #endif
  446. /* -1 unrunnable, 0 runnable, >0 stopped: */
  447. volatile long state;
  448. void *stack;
  449. atomic_t usage;
  450. /* Per task flags (PF_*), defined further below: */
  451. unsigned int flags;
  452. unsigned int ptrace;
  453. #ifdef CONFIG_SMP
  454. struct llist_node wake_entry;
  455. int on_cpu;
  456. #ifdef CONFIG_THREAD_INFO_IN_TASK
  457. /* Current CPU: */
  458. unsigned int cpu;
  459. #endif
  460. unsigned int wakee_flips;
  461. unsigned long wakee_flip_decay_ts;
  462. struct task_struct *last_wakee;
  463. int wake_cpu;
  464. #endif
  465. int on_rq;
  466. int prio;
  467. int static_prio;
  468. int normal_prio;
  469. unsigned int rt_priority;
  470. const struct sched_class *sched_class;
  471. struct sched_entity se;
  472. struct sched_rt_entity rt;
  473. #ifdef CONFIG_CGROUP_SCHED
  474. struct task_group *sched_task_group;
  475. #endif
  476. struct sched_dl_entity dl;
  477. #ifdef CONFIG_PREEMPT_NOTIFIERS
  478. /* List of struct preempt_notifier: */
  479. struct hlist_head preempt_notifiers;
  480. #endif
  481. #ifdef CONFIG_BLK_DEV_IO_TRACE
  482. unsigned int btrace_seq;
  483. #endif
  484. unsigned int policy;
  485. int nr_cpus_allowed;
  486. cpumask_t cpus_allowed;
  487. #ifdef CONFIG_PREEMPT_RCU
  488. int rcu_read_lock_nesting;
  489. union rcu_special rcu_read_unlock_special;
  490. struct list_head rcu_node_entry;
  491. struct rcu_node *rcu_blocked_node;
  492. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  493. #ifdef CONFIG_TASKS_RCU
  494. unsigned long rcu_tasks_nvcsw;
  495. bool rcu_tasks_holdout;
  496. struct list_head rcu_tasks_holdout_list;
  497. int rcu_tasks_idle_cpu;
  498. #endif /* #ifdef CONFIG_TASKS_RCU */
  499. struct sched_info sched_info;
  500. struct list_head tasks;
  501. #ifdef CONFIG_SMP
  502. struct plist_node pushable_tasks;
  503. struct rb_node pushable_dl_tasks;
  504. #endif
  505. struct mm_struct *mm;
  506. struct mm_struct *active_mm;
  507. /* Per-thread vma caching: */
  508. struct vmacache vmacache;
  509. #ifdef SPLIT_RSS_COUNTING
  510. struct task_rss_stat rss_stat;
  511. #endif
  512. int exit_state;
  513. int exit_code;
  514. int exit_signal;
  515. /* The signal sent when the parent dies: */
  516. int pdeath_signal;
  517. /* JOBCTL_*, siglock protected: */
  518. unsigned long jobctl;
  519. /* Used for emulating ABI behavior of previous Linux versions: */
  520. unsigned int personality;
  521. /* Scheduler bits, serialized by scheduler locks: */
  522. unsigned sched_reset_on_fork:1;
  523. unsigned sched_contributes_to_load:1;
  524. unsigned sched_migrated:1;
  525. unsigned sched_remote_wakeup:1;
  526. /* Force alignment to the next boundary: */
  527. unsigned :0;
  528. /* Unserialized, strictly 'current' */
  529. /* Bit to tell LSMs we're in execve(): */
  530. unsigned in_execve:1;
  531. unsigned in_iowait:1;
  532. #ifndef TIF_RESTORE_SIGMASK
  533. unsigned restore_sigmask:1;
  534. #endif
  535. #ifdef CONFIG_MEMCG
  536. unsigned memcg_may_oom:1;
  537. #ifndef CONFIG_SLOB
  538. unsigned memcg_kmem_skip_account:1;
  539. #endif
  540. #endif
  541. #ifdef CONFIG_COMPAT_BRK
  542. unsigned brk_randomized:1;
  543. #endif
  544. #ifdef CONFIG_CGROUPS
  545. /* disallow userland-initiated cgroup migration */
  546. unsigned no_cgroup_migration:1;
  547. #endif
  548. unsigned long atomic_flags; /* Flags requiring atomic access. */
  549. struct restart_block restart_block;
  550. pid_t pid;
  551. pid_t tgid;
  552. #ifdef CONFIG_CC_STACKPROTECTOR
  553. /* Canary value for the -fstack-protector GCC feature: */
  554. unsigned long stack_canary;
  555. #endif
  556. /*
  557. * Pointers to the (original) parent process, youngest child, younger sibling,
  558. * older sibling, respectively. (p->father can be replaced with
  559. * p->real_parent->pid)
  560. */
  561. /* Real parent process: */
  562. struct task_struct __rcu *real_parent;
  563. /* Recipient of SIGCHLD, wait4() reports: */
  564. struct task_struct __rcu *parent;
  565. /*
  566. * Children/sibling form the list of natural children:
  567. */
  568. struct list_head children;
  569. struct list_head sibling;
  570. struct task_struct *group_leader;
  571. /*
  572. * 'ptraced' is the list of tasks this task is using ptrace() on.
  573. *
  574. * This includes both natural children and PTRACE_ATTACH targets.
  575. * 'ptrace_entry' is this task's link on the p->parent->ptraced list.
  576. */
  577. struct list_head ptraced;
  578. struct list_head ptrace_entry;
  579. /* PID/PID hash table linkage. */
  580. struct pid_link pids[PIDTYPE_MAX];
  581. struct list_head thread_group;
  582. struct list_head thread_node;
  583. struct completion *vfork_done;
  584. /* CLONE_CHILD_SETTID: */
  585. int __user *set_child_tid;
  586. /* CLONE_CHILD_CLEARTID: */
  587. int __user *clear_child_tid;
  588. u64 utime;
  589. u64 stime;
  590. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  591. u64 utimescaled;
  592. u64 stimescaled;
  593. #endif
  594. u64 gtime;
  595. struct prev_cputime prev_cputime;
  596. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  597. seqcount_t vtime_seqcount;
  598. unsigned long long vtime_snap;
  599. enum {
  600. /* Task is sleeping or running in a CPU with VTIME inactive: */
  601. VTIME_INACTIVE = 0,
  602. /* Task runs in userspace in a CPU with VTIME active: */
  603. VTIME_USER,
  604. /* Task runs in kernelspace in a CPU with VTIME active: */
  605. VTIME_SYS,
  606. } vtime_snap_whence;
  607. #endif
  608. #ifdef CONFIG_NO_HZ_FULL
  609. atomic_t tick_dep_mask;
  610. #endif
  611. /* Context switch counts: */
  612. unsigned long nvcsw;
  613. unsigned long nivcsw;
  614. /* Monotonic time in nsecs: */
  615. u64 start_time;
  616. /* Boot based time in nsecs: */
  617. u64 real_start_time;
  618. /* MM fault and swap info: this can arguably be seen as either mm-specific or thread-specific: */
  619. unsigned long min_flt;
  620. unsigned long maj_flt;
  621. #ifdef CONFIG_POSIX_TIMERS
  622. struct task_cputime cputime_expires;
  623. struct list_head cpu_timers[3];
  624. #endif
  625. /* Process credentials: */
  626. /* Tracer's credentials at attach: */
  627. const struct cred __rcu *ptracer_cred;
  628. /* Objective and real subjective task credentials (COW): */
  629. const struct cred __rcu *real_cred;
  630. /* Effective (overridable) subjective task credentials (COW): */
  631. const struct cred __rcu *cred;
  632. /*
  633. * executable name, excluding path.
  634. *
  635. * - normally initialized setup_new_exec()
  636. * - access it with [gs]et_task_comm()
  637. * - lock it with task_lock()
  638. */
  639. char comm[TASK_COMM_LEN];
  640. struct nameidata *nameidata;
  641. #ifdef CONFIG_SYSVIPC
  642. struct sysv_sem sysvsem;
  643. struct sysv_shm sysvshm;
  644. #endif
  645. #ifdef CONFIG_DETECT_HUNG_TASK
  646. unsigned long last_switch_count;
  647. #endif
  648. /* Filesystem information: */
  649. struct fs_struct *fs;
  650. /* Open file information: */
  651. struct files_struct *files;
  652. /* Namespaces: */
  653. struct nsproxy *nsproxy;
  654. /* Signal handlers: */
  655. struct signal_struct *signal;
  656. struct sighand_struct *sighand;
  657. sigset_t blocked;
  658. sigset_t real_blocked;
  659. /* Restored if set_restore_sigmask() was used: */
  660. sigset_t saved_sigmask;
  661. struct sigpending pending;
  662. unsigned long sas_ss_sp;
  663. size_t sas_ss_size;
  664. unsigned int sas_ss_flags;
  665. struct callback_head *task_works;
  666. struct audit_context *audit_context;
  667. #ifdef CONFIG_AUDITSYSCALL
  668. kuid_t loginuid;
  669. unsigned int sessionid;
  670. #endif
  671. struct seccomp seccomp;
  672. /* Thread group tracking: */
  673. u32 parent_exec_id;
  674. u32 self_exec_id;
  675. /* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */
  676. spinlock_t alloc_lock;
  677. /* Protection of the PI data structures: */
  678. raw_spinlock_t pi_lock;
  679. struct wake_q_node wake_q;
  680. #ifdef CONFIG_RT_MUTEXES
  681. /* PI waiters blocked on a rt_mutex held by this task: */
  682. struct rb_root pi_waiters;
  683. struct rb_node *pi_waiters_leftmost;
  684. /* Updated under owner's pi_lock and rq lock */
  685. struct task_struct *pi_top_task;
  686. /* Deadlock detection and priority inheritance handling: */
  687. struct rt_mutex_waiter *pi_blocked_on;
  688. #endif
  689. #ifdef CONFIG_DEBUG_MUTEXES
  690. /* Mutex deadlock detection: */
  691. struct mutex_waiter *blocked_on;
  692. #endif
  693. #ifdef CONFIG_TRACE_IRQFLAGS
  694. unsigned int irq_events;
  695. unsigned long hardirq_enable_ip;
  696. unsigned long hardirq_disable_ip;
  697. unsigned int hardirq_enable_event;
  698. unsigned int hardirq_disable_event;
  699. int hardirqs_enabled;
  700. int hardirq_context;
  701. unsigned long softirq_disable_ip;
  702. unsigned long softirq_enable_ip;
  703. unsigned int softirq_disable_event;
  704. unsigned int softirq_enable_event;
  705. int softirqs_enabled;
  706. int softirq_context;
  707. #endif
  708. #ifdef CONFIG_LOCKDEP
  709. # define MAX_LOCK_DEPTH 48UL
  710. u64 curr_chain_key;
  711. int lockdep_depth;
  712. unsigned int lockdep_recursion;
  713. struct held_lock held_locks[MAX_LOCK_DEPTH];
  714. gfp_t lockdep_reclaim_gfp;
  715. #endif
  716. #ifdef CONFIG_UBSAN
  717. unsigned int in_ubsan;
  718. #endif
  719. /* Journalling filesystem info: */
  720. void *journal_info;
  721. /* Stacked block device info: */
  722. struct bio_list *bio_list;
  723. #ifdef CONFIG_BLOCK
  724. /* Stack plugging: */
  725. struct blk_plug *plug;
  726. #endif
  727. /* VM state: */
  728. struct reclaim_state *reclaim_state;
  729. struct backing_dev_info *backing_dev_info;
  730. struct io_context *io_context;
  731. /* Ptrace state: */
  732. unsigned long ptrace_message;
  733. siginfo_t *last_siginfo;
  734. struct task_io_accounting ioac;
  735. #ifdef CONFIG_TASK_XACCT
  736. /* Accumulated RSS usage: */
  737. u64 acct_rss_mem1;
  738. /* Accumulated virtual memory usage: */
  739. u64 acct_vm_mem1;
  740. /* stime + utime since last update: */
  741. u64 acct_timexpd;
  742. #endif
  743. #ifdef CONFIG_CPUSETS
  744. /* Protected by ->alloc_lock: */
  745. nodemask_t mems_allowed;
  746. /* Seqence number to catch updates: */
  747. seqcount_t mems_allowed_seq;
  748. int cpuset_mem_spread_rotor;
  749. int cpuset_slab_spread_rotor;
  750. #endif
  751. #ifdef CONFIG_CGROUPS
  752. /* Control Group info protected by css_set_lock: */
  753. struct css_set __rcu *cgroups;
  754. /* cg_list protected by css_set_lock and tsk->alloc_lock: */
  755. struct list_head cg_list;
  756. #endif
  757. #ifdef CONFIG_INTEL_RDT_A
  758. int closid;
  759. #endif
  760. #ifdef CONFIG_FUTEX
  761. struct robust_list_head __user *robust_list;
  762. #ifdef CONFIG_COMPAT
  763. struct compat_robust_list_head __user *compat_robust_list;
  764. #endif
  765. struct list_head pi_state_list;
  766. struct futex_pi_state *pi_state_cache;
  767. #endif
  768. #ifdef CONFIG_PERF_EVENTS
  769. struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
  770. struct mutex perf_event_mutex;
  771. struct list_head perf_event_list;
  772. #endif
  773. #ifdef CONFIG_DEBUG_PREEMPT
  774. unsigned long preempt_disable_ip;
  775. #endif
  776. #ifdef CONFIG_NUMA
  777. /* Protected by alloc_lock: */
  778. struct mempolicy *mempolicy;
  779. short il_next;
  780. short pref_node_fork;
  781. #endif
  782. #ifdef CONFIG_NUMA_BALANCING
  783. int numa_scan_seq;
  784. unsigned int numa_scan_period;
  785. unsigned int numa_scan_period_max;
  786. int numa_preferred_nid;
  787. unsigned long numa_migrate_retry;
  788. /* Migration stamp: */
  789. u64 node_stamp;
  790. u64 last_task_numa_placement;
  791. u64 last_sum_exec_runtime;
  792. struct callback_head numa_work;
  793. struct list_head numa_entry;
  794. struct numa_group *numa_group;
  795. /*
  796. * numa_faults is an array split into four regions:
  797. * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
  798. * in this precise order.
  799. *
  800. * faults_memory: Exponential decaying average of faults on a per-node
  801. * basis. Scheduling placement decisions are made based on these
  802. * counts. The values remain static for the duration of a PTE scan.
  803. * faults_cpu: Track the nodes the process was running on when a NUMA
  804. * hinting fault was incurred.
  805. * faults_memory_buffer and faults_cpu_buffer: Record faults per node
  806. * during the current scan window. When the scan completes, the counts
  807. * in faults_memory and faults_cpu decay and these values are copied.
  808. */
  809. unsigned long *numa_faults;
  810. unsigned long total_numa_faults;
  811. /*
  812. * numa_faults_locality tracks if faults recorded during the last
  813. * scan window were remote/local or failed to migrate. The task scan
  814. * period is adapted based on the locality of the faults with different
  815. * weights depending on whether they were shared or private faults
  816. */
  817. unsigned long numa_faults_locality[3];
  818. unsigned long numa_pages_migrated;
  819. #endif /* CONFIG_NUMA_BALANCING */
  820. struct tlbflush_unmap_batch tlb_ubc;
  821. struct rcu_head rcu;
  822. /* Cache last used pipe for splice(): */
  823. struct pipe_inode_info *splice_pipe;
  824. struct page_frag task_frag;
  825. #ifdef CONFIG_TASK_DELAY_ACCT
  826. struct task_delay_info *delays;
  827. #endif
  828. #ifdef CONFIG_FAULT_INJECTION
  829. int make_it_fail;
  830. #endif
  831. /*
  832. * When (nr_dirtied >= nr_dirtied_pause), it's time to call
  833. * balance_dirty_pages() for a dirty throttling pause:
  834. */
  835. int nr_dirtied;
  836. int nr_dirtied_pause;
  837. /* Start of a write-and-pause period: */
  838. unsigned long dirty_paused_when;
  839. #ifdef CONFIG_LATENCYTOP
  840. int latency_record_count;
  841. struct latency_record latency_record[LT_SAVECOUNT];
  842. #endif
  843. /*
  844. * Time slack values; these are used to round up poll() and
  845. * select() etc timeout values. These are in nanoseconds.
  846. */
  847. u64 timer_slack_ns;
  848. u64 default_timer_slack_ns;
  849. #ifdef CONFIG_KASAN
  850. unsigned int kasan_depth;
  851. #endif
  852. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  853. /* Index of current stored address in ret_stack: */
  854. int curr_ret_stack;
  855. /* Stack of return addresses for return function tracing: */
  856. struct ftrace_ret_stack *ret_stack;
  857. /* Timestamp for last schedule: */
  858. unsigned long long ftrace_timestamp;
  859. /*
  860. * Number of functions that haven't been traced
  861. * because of depth overrun:
  862. */
  863. atomic_t trace_overrun;
  864. /* Pause tracing: */
  865. atomic_t tracing_graph_pause;
  866. #endif
  867. #ifdef CONFIG_TRACING
  868. /* State flags for use by tracers: */
  869. unsigned long trace;
  870. /* Bitmask and counter of trace recursion: */
  871. unsigned long trace_recursion;
  872. #endif /* CONFIG_TRACING */
  873. #ifdef CONFIG_KCOV
  874. /* Coverage collection mode enabled for this task (0 if disabled): */
  875. enum kcov_mode kcov_mode;
  876. /* Size of the kcov_area: */
  877. unsigned int kcov_size;
  878. /* Buffer for coverage collection: */
  879. void *kcov_area;
  880. /* KCOV descriptor wired with this task or NULL: */
  881. struct kcov *kcov;
  882. #endif
  883. #ifdef CONFIG_MEMCG
  884. struct mem_cgroup *memcg_in_oom;
  885. gfp_t memcg_oom_gfp_mask;
  886. int memcg_oom_order;
  887. /* Number of pages to reclaim on returning to userland: */
  888. unsigned int memcg_nr_pages_over_high;
  889. #endif
  890. #ifdef CONFIG_UPROBES
  891. struct uprobe_task *utask;
  892. #endif
  893. #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
  894. unsigned int sequential_io;
  895. unsigned int sequential_io_avg;
  896. #endif
  897. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  898. unsigned long task_state_change;
  899. #endif
  900. int pagefault_disabled;
  901. #ifdef CONFIG_MMU
  902. struct task_struct *oom_reaper_list;
  903. #endif
  904. #ifdef CONFIG_VMAP_STACK
  905. struct vm_struct *stack_vm_area;
  906. #endif
  907. #ifdef CONFIG_THREAD_INFO_IN_TASK
  908. /* A live task holds one reference: */
  909. atomic_t stack_refcount;
  910. #endif
  911. #ifdef CONFIG_LIVEPATCH
  912. int patch_state;
  913. #endif
  914. #ifdef CONFIG_SECURITY
  915. /* Used by LSM modules for access restriction: */
  916. void *security;
  917. #endif
  918. /* CPU-specific state of this task: */
  919. struct thread_struct thread;
  920. /*
  921. * WARNING: on x86, 'thread_struct' contains a variable-sized
  922. * structure. It *MUST* be at the end of 'task_struct'.
  923. *
  924. * Do not put anything below here!
  925. */
  926. };
  927. static inline struct pid *task_pid(struct task_struct *task)
  928. {
  929. return task->pids[PIDTYPE_PID].pid;
  930. }
  931. static inline struct pid *task_tgid(struct task_struct *task)
  932. {
  933. return task->group_leader->pids[PIDTYPE_PID].pid;
  934. }
  935. /*
  936. * Without tasklist or RCU lock it is not safe to dereference
  937. * the result of task_pgrp/task_session even if task == current,
  938. * we can race with another thread doing sys_setsid/sys_setpgid.
  939. */
  940. static inline struct pid *task_pgrp(struct task_struct *task)
  941. {
  942. return task->group_leader->pids[PIDTYPE_PGID].pid;
  943. }
  944. static inline struct pid *task_session(struct task_struct *task)
  945. {
  946. return task->group_leader->pids[PIDTYPE_SID].pid;
  947. }
  948. /*
  949. * the helpers to get the task's different pids as they are seen
  950. * from various namespaces
  951. *
  952. * task_xid_nr() : global id, i.e. the id seen from the init namespace;
  953. * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
  954. * current.
  955. * task_xid_nr_ns() : id seen from the ns specified;
  956. *
  957. * see also pid_nr() etc in include/linux/pid.h
  958. */
  959. pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
  960. static inline pid_t task_pid_nr(struct task_struct *tsk)
  961. {
  962. return tsk->pid;
  963. }
  964. static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  965. {
  966. return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
  967. }
  968. static inline pid_t task_pid_vnr(struct task_struct *tsk)
  969. {
  970. return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
  971. }
  972. static inline pid_t task_tgid_nr(struct task_struct *tsk)
  973. {
  974. return tsk->tgid;
  975. }
  976. extern pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
  977. static inline pid_t task_tgid_vnr(struct task_struct *tsk)
  978. {
  979. return pid_vnr(task_tgid(tsk));
  980. }
  981. /**
  982. * pid_alive - check that a task structure is not stale
  983. * @p: Task structure to be checked.
  984. *
  985. * Test if a process is not yet dead (at most zombie state)
  986. * If pid_alive fails, then pointers within the task structure
  987. * can be stale and must not be dereferenced.
  988. *
  989. * Return: 1 if the process is alive. 0 otherwise.
  990. */
  991. static inline int pid_alive(const struct task_struct *p)
  992. {
  993. return p->pids[PIDTYPE_PID].pid != NULL;
  994. }
  995. static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
  996. {
  997. pid_t pid = 0;
  998. rcu_read_lock();
  999. if (pid_alive(tsk))
  1000. pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
  1001. rcu_read_unlock();
  1002. return pid;
  1003. }
  1004. static inline pid_t task_ppid_nr(const struct task_struct *tsk)
  1005. {
  1006. return task_ppid_nr_ns(tsk, &init_pid_ns);
  1007. }
  1008. static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  1009. {
  1010. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
  1011. }
  1012. static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
  1013. {
  1014. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
  1015. }
  1016. static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  1017. {
  1018. return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
  1019. }
  1020. static inline pid_t task_session_vnr(struct task_struct *tsk)
  1021. {
  1022. return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
  1023. }
  1024. /* Obsolete, do not use: */
  1025. static inline pid_t task_pgrp_nr(struct task_struct *tsk)
  1026. {
  1027. return task_pgrp_nr_ns(tsk, &init_pid_ns);
  1028. }
  1029. /**
  1030. * is_global_init - check if a task structure is init. Since init
  1031. * is free to have sub-threads we need to check tgid.
  1032. * @tsk: Task structure to be checked.
  1033. *
  1034. * Check if a task structure is the first user space task the kernel created.
  1035. *
  1036. * Return: 1 if the task structure is init. 0 otherwise.
  1037. */
  1038. static inline int is_global_init(struct task_struct *tsk)
  1039. {
  1040. return task_tgid_nr(tsk) == 1;
  1041. }
  1042. extern struct pid *cad_pid;
  1043. /*
  1044. * Per process flags
  1045. */
  1046. #define PF_IDLE 0x00000002 /* I am an IDLE thread */
  1047. #define PF_EXITING 0x00000004 /* Getting shut down */
  1048. #define PF_EXITPIDONE 0x00000008 /* PI exit done on shut down */
  1049. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  1050. #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
  1051. #define PF_FORKNOEXEC 0x00000040 /* Forked but didn't exec */
  1052. #define PF_MCE_PROCESS 0x00000080 /* Process policy on mce errors */
  1053. #define PF_SUPERPRIV 0x00000100 /* Used super-user privileges */
  1054. #define PF_DUMPCORE 0x00000200 /* Dumped core */
  1055. #define PF_SIGNALED 0x00000400 /* Killed by a signal */
  1056. #define PF_MEMALLOC 0x00000800 /* Allocating memory */
  1057. #define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */
  1058. #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */
  1059. #define PF_USED_ASYNC 0x00004000 /* Used async_schedule*(), used by module init */
  1060. #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */
  1061. #define PF_FROZEN 0x00010000 /* Frozen for system suspend */
  1062. #define PF_KSWAPD 0x00020000 /* I am kswapd */
  1063. #define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */
  1064. #define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */
  1065. #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
  1066. #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
  1067. #define PF_RANDOMIZE 0x00400000 /* Randomize virtual address space */
  1068. #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
  1069. #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
  1070. #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
  1071. #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
  1072. #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
  1073. #define PF_SUSPEND_TASK 0x80000000 /* This thread called freeze_processes() and should not be frozen */
  1074. /*
  1075. * Only the _current_ task can read/write to tsk->flags, but other
  1076. * tasks can access tsk->flags in readonly mode for example
  1077. * with tsk_used_math (like during threaded core dumping).
  1078. * There is however an exception to this rule during ptrace
  1079. * or during fork: the ptracer task is allowed to write to the
  1080. * child->flags of its traced child (same goes for fork, the parent
  1081. * can write to the child->flags), because we're guaranteed the
  1082. * child is not running and in turn not changing child->flags
  1083. * at the same time the parent does it.
  1084. */
  1085. #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
  1086. #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
  1087. #define clear_used_math() clear_stopped_child_used_math(current)
  1088. #define set_used_math() set_stopped_child_used_math(current)
  1089. #define conditional_stopped_child_used_math(condition, child) \
  1090. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
  1091. #define conditional_used_math(condition) conditional_stopped_child_used_math(condition, current)
  1092. #define copy_to_stopped_child_used_math(child) \
  1093. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
  1094. /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
  1095. #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
  1096. #define used_math() tsk_used_math(current)
  1097. /* Per-process atomic flags. */
  1098. #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
  1099. #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
  1100. #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
  1101. #define TASK_PFA_TEST(name, func) \
  1102. static inline bool task_##func(struct task_struct *p) \
  1103. { return test_bit(PFA_##name, &p->atomic_flags); }
  1104. #define TASK_PFA_SET(name, func) \
  1105. static inline void task_set_##func(struct task_struct *p) \
  1106. { set_bit(PFA_##name, &p->atomic_flags); }
  1107. #define TASK_PFA_CLEAR(name, func) \
  1108. static inline void task_clear_##func(struct task_struct *p) \
  1109. { clear_bit(PFA_##name, &p->atomic_flags); }
  1110. TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
  1111. TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
  1112. TASK_PFA_TEST(SPREAD_PAGE, spread_page)
  1113. TASK_PFA_SET(SPREAD_PAGE, spread_page)
  1114. TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
  1115. TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
  1116. TASK_PFA_SET(SPREAD_SLAB, spread_slab)
  1117. TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
  1118. static inline void
  1119. current_restore_flags(unsigned long orig_flags, unsigned long flags)
  1120. {
  1121. current->flags &= ~flags;
  1122. current->flags |= orig_flags & flags;
  1123. }
  1124. extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
  1125. extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
  1126. #ifdef CONFIG_SMP
  1127. extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
  1128. extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
  1129. #else
  1130. static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  1131. {
  1132. }
  1133. static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  1134. {
  1135. if (!cpumask_test_cpu(0, new_mask))
  1136. return -EINVAL;
  1137. return 0;
  1138. }
  1139. #endif
  1140. #ifndef cpu_relax_yield
  1141. #define cpu_relax_yield() cpu_relax()
  1142. #endif
  1143. extern int yield_to(struct task_struct *p, bool preempt);
  1144. extern void set_user_nice(struct task_struct *p, long nice);
  1145. extern int task_prio(const struct task_struct *p);
  1146. /**
  1147. * task_nice - return the nice value of a given task.
  1148. * @p: the task in question.
  1149. *
  1150. * Return: The nice value [ -20 ... 0 ... 19 ].
  1151. */
  1152. static inline int task_nice(const struct task_struct *p)
  1153. {
  1154. return PRIO_TO_NICE((p)->static_prio);
  1155. }
  1156. extern int can_nice(const struct task_struct *p, const int nice);
  1157. extern int task_curr(const struct task_struct *p);
  1158. extern int idle_cpu(int cpu);
  1159. extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
  1160. extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
  1161. extern int sched_setattr(struct task_struct *, const struct sched_attr *);
  1162. extern struct task_struct *idle_task(int cpu);
  1163. /**
  1164. * is_idle_task - is the specified task an idle task?
  1165. * @p: the task in question.
  1166. *
  1167. * Return: 1 if @p is an idle task. 0 otherwise.
  1168. */
  1169. static inline bool is_idle_task(const struct task_struct *p)
  1170. {
  1171. return !!(p->flags & PF_IDLE);
  1172. }
  1173. extern struct task_struct *curr_task(int cpu);
  1174. extern void ia64_set_curr_task(int cpu, struct task_struct *p);
  1175. void yield(void);
  1176. union thread_union {
  1177. #ifndef CONFIG_THREAD_INFO_IN_TASK
  1178. struct thread_info thread_info;
  1179. #endif
  1180. unsigned long stack[THREAD_SIZE/sizeof(long)];
  1181. };
  1182. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1183. static inline struct thread_info *task_thread_info(struct task_struct *task)
  1184. {
  1185. return &task->thread_info;
  1186. }
  1187. #elif !defined(__HAVE_THREAD_FUNCTIONS)
  1188. # define task_thread_info(task) ((struct thread_info *)(task)->stack)
  1189. #endif
  1190. /*
  1191. * find a task by one of its numerical ids
  1192. *
  1193. * find_task_by_pid_ns():
  1194. * finds a task by its pid in the specified namespace
  1195. * find_task_by_vpid():
  1196. * finds a task by its virtual pid
  1197. *
  1198. * see also find_vpid() etc in include/linux/pid.h
  1199. */
  1200. extern struct task_struct *find_task_by_vpid(pid_t nr);
  1201. extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
  1202. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  1203. extern int wake_up_process(struct task_struct *tsk);
  1204. extern void wake_up_new_task(struct task_struct *tsk);
  1205. #ifdef CONFIG_SMP
  1206. extern void kick_process(struct task_struct *tsk);
  1207. #else
  1208. static inline void kick_process(struct task_struct *tsk) { }
  1209. #endif
  1210. extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
  1211. static inline void set_task_comm(struct task_struct *tsk, const char *from)
  1212. {
  1213. __set_task_comm(tsk, from, false);
  1214. }
  1215. extern char *get_task_comm(char *to, struct task_struct *tsk);
  1216. #ifdef CONFIG_SMP
  1217. void scheduler_ipi(void);
  1218. extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
  1219. #else
  1220. static inline void scheduler_ipi(void) { }
  1221. static inline unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  1222. {
  1223. return 1;
  1224. }
  1225. #endif
  1226. /*
  1227. * Set thread flags in other task's structures.
  1228. * See asm/thread_info.h for TIF_xxxx flags available:
  1229. */
  1230. static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1231. {
  1232. set_ti_thread_flag(task_thread_info(tsk), flag);
  1233. }
  1234. static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1235. {
  1236. clear_ti_thread_flag(task_thread_info(tsk), flag);
  1237. }
  1238. static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1239. {
  1240. return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
  1241. }
  1242. static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1243. {
  1244. return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
  1245. }
  1246. static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
  1247. {
  1248. return test_ti_thread_flag(task_thread_info(tsk), flag);
  1249. }
  1250. static inline void set_tsk_need_resched(struct task_struct *tsk)
  1251. {
  1252. set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1253. }
  1254. static inline void clear_tsk_need_resched(struct task_struct *tsk)
  1255. {
  1256. clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1257. }
  1258. static inline int test_tsk_need_resched(struct task_struct *tsk)
  1259. {
  1260. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  1261. }
  1262. /*
  1263. * cond_resched() and cond_resched_lock(): latency reduction via
  1264. * explicit rescheduling in places that are safe. The return
  1265. * value indicates whether a reschedule was done in fact.
  1266. * cond_resched_lock() will drop the spinlock before scheduling,
  1267. * cond_resched_softirq() will enable bhs before scheduling.
  1268. */
  1269. #ifndef CONFIG_PREEMPT
  1270. extern int _cond_resched(void);
  1271. #else
  1272. static inline int _cond_resched(void) { return 0; }
  1273. #endif
  1274. #define cond_resched() ({ \
  1275. ___might_sleep(__FILE__, __LINE__, 0); \
  1276. _cond_resched(); \
  1277. })
  1278. extern int __cond_resched_lock(spinlock_t *lock);
  1279. #define cond_resched_lock(lock) ({ \
  1280. ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
  1281. __cond_resched_lock(lock); \
  1282. })
  1283. extern int __cond_resched_softirq(void);
  1284. #define cond_resched_softirq() ({ \
  1285. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  1286. __cond_resched_softirq(); \
  1287. })
  1288. static inline void cond_resched_rcu(void)
  1289. {
  1290. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
  1291. rcu_read_unlock();
  1292. cond_resched();
  1293. rcu_read_lock();
  1294. #endif
  1295. }
  1296. /*
  1297. * Does a critical section need to be broken due to another
  1298. * task waiting?: (technically does not depend on CONFIG_PREEMPT,
  1299. * but a general need for low latency)
  1300. */
  1301. static inline int spin_needbreak(spinlock_t *lock)
  1302. {
  1303. #ifdef CONFIG_PREEMPT
  1304. return spin_is_contended(lock);
  1305. #else
  1306. return 0;
  1307. #endif
  1308. }
  1309. static __always_inline bool need_resched(void)
  1310. {
  1311. return unlikely(tif_need_resched());
  1312. }
  1313. /*
  1314. * Wrappers for p->thread_info->cpu access. No-op on UP.
  1315. */
  1316. #ifdef CONFIG_SMP
  1317. static inline unsigned int task_cpu(const struct task_struct *p)
  1318. {
  1319. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1320. return p->cpu;
  1321. #else
  1322. return task_thread_info(p)->cpu;
  1323. #endif
  1324. }
  1325. extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
  1326. #else
  1327. static inline unsigned int task_cpu(const struct task_struct *p)
  1328. {
  1329. return 0;
  1330. }
  1331. static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  1332. {
  1333. }
  1334. #endif /* CONFIG_SMP */
  1335. /*
  1336. * In order to reduce various lock holder preemption latencies provide an
  1337. * interface to see if a vCPU is currently running or not.
  1338. *
  1339. * This allows us to terminate optimistic spin loops and block, analogous to
  1340. * the native optimistic spin heuristic of testing if the lock owner task is
  1341. * running or not.
  1342. */
  1343. #ifndef vcpu_is_preempted
  1344. # define vcpu_is_preempted(cpu) false
  1345. #endif
  1346. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  1347. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  1348. #ifndef TASK_SIZE_OF
  1349. #define TASK_SIZE_OF(tsk) TASK_SIZE
  1350. #endif
  1351. #endif