sched.h 45 KB

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