sched.h 52 KB

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