sched.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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_PARKED | 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() executes a full memory barrier before accessing the
  151. * task state.
  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 in_user_fault:1;
  638. #ifdef CONFIG_MEMCG_KMEM
  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. #ifdef CONFIG_BLK_CGROUP
  650. /* to be used once the psi infrastructure lands upstream. */
  651. unsigned use_memdelay:1;
  652. #endif
  653. unsigned long atomic_flags; /* Flags requiring atomic access. */
  654. struct restart_block restart_block;
  655. pid_t pid;
  656. pid_t tgid;
  657. #ifdef CONFIG_STACKPROTECTOR
  658. /* Canary value for the -fstack-protector GCC feature: */
  659. unsigned long stack_canary;
  660. #endif
  661. /*
  662. * Pointers to the (original) parent process, youngest child, younger sibling,
  663. * older sibling, respectively. (p->father can be replaced with
  664. * p->real_parent->pid)
  665. */
  666. /* Real parent process: */
  667. struct task_struct __rcu *real_parent;
  668. /* Recipient of SIGCHLD, wait4() reports: */
  669. struct task_struct __rcu *parent;
  670. /*
  671. * Children/sibling form the list of natural children:
  672. */
  673. struct list_head children;
  674. struct list_head sibling;
  675. struct task_struct *group_leader;
  676. /*
  677. * 'ptraced' is the list of tasks this task is using ptrace() on.
  678. *
  679. * This includes both natural children and PTRACE_ATTACH targets.
  680. * 'ptrace_entry' is this task's link on the p->parent->ptraced list.
  681. */
  682. struct list_head ptraced;
  683. struct list_head ptrace_entry;
  684. /* PID/PID hash table linkage. */
  685. struct pid_link pids[PIDTYPE_MAX];
  686. struct list_head thread_group;
  687. struct list_head thread_node;
  688. struct completion *vfork_done;
  689. /* CLONE_CHILD_SETTID: */
  690. int __user *set_child_tid;
  691. /* CLONE_CHILD_CLEARTID: */
  692. int __user *clear_child_tid;
  693. u64 utime;
  694. u64 stime;
  695. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  696. u64 utimescaled;
  697. u64 stimescaled;
  698. #endif
  699. u64 gtime;
  700. struct prev_cputime prev_cputime;
  701. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  702. struct vtime vtime;
  703. #endif
  704. #ifdef CONFIG_NO_HZ_FULL
  705. atomic_t tick_dep_mask;
  706. #endif
  707. /* Context switch counts: */
  708. unsigned long nvcsw;
  709. unsigned long nivcsw;
  710. /* Monotonic time in nsecs: */
  711. u64 start_time;
  712. /* Boot based time in nsecs: */
  713. u64 real_start_time;
  714. /* MM fault and swap info: this can arguably be seen as either mm-specific or thread-specific: */
  715. unsigned long min_flt;
  716. unsigned long maj_flt;
  717. #ifdef CONFIG_POSIX_TIMERS
  718. struct task_cputime cputime_expires;
  719. struct list_head cpu_timers[3];
  720. #endif
  721. /* Process credentials: */
  722. /* Tracer's credentials at attach: */
  723. const struct cred __rcu *ptracer_cred;
  724. /* Objective and real subjective task credentials (COW): */
  725. const struct cred __rcu *real_cred;
  726. /* Effective (overridable) subjective task credentials (COW): */
  727. const struct cred __rcu *cred;
  728. /*
  729. * executable name, excluding path.
  730. *
  731. * - normally initialized setup_new_exec()
  732. * - access it with [gs]et_task_comm()
  733. * - lock it with task_lock()
  734. */
  735. char comm[TASK_COMM_LEN];
  736. struct nameidata *nameidata;
  737. #ifdef CONFIG_SYSVIPC
  738. struct sysv_sem sysvsem;
  739. struct sysv_shm sysvshm;
  740. #endif
  741. #ifdef CONFIG_DETECT_HUNG_TASK
  742. unsigned long last_switch_count;
  743. #endif
  744. /* Filesystem information: */
  745. struct fs_struct *fs;
  746. /* Open file information: */
  747. struct files_struct *files;
  748. /* Namespaces: */
  749. struct nsproxy *nsproxy;
  750. /* Signal handlers: */
  751. struct signal_struct *signal;
  752. struct sighand_struct *sighand;
  753. sigset_t blocked;
  754. sigset_t real_blocked;
  755. /* Restored if set_restore_sigmask() was used: */
  756. sigset_t saved_sigmask;
  757. struct sigpending pending;
  758. unsigned long sas_ss_sp;
  759. size_t sas_ss_size;
  760. unsigned int sas_ss_flags;
  761. struct callback_head *task_works;
  762. struct audit_context *audit_context;
  763. #ifdef CONFIG_AUDITSYSCALL
  764. kuid_t loginuid;
  765. unsigned int sessionid;
  766. #endif
  767. struct seccomp seccomp;
  768. /* Thread group tracking: */
  769. u32 parent_exec_id;
  770. u32 self_exec_id;
  771. /* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */
  772. spinlock_t alloc_lock;
  773. /* Protection of the PI data structures: */
  774. raw_spinlock_t pi_lock;
  775. struct wake_q_node wake_q;
  776. #ifdef CONFIG_RT_MUTEXES
  777. /* PI waiters blocked on a rt_mutex held by this task: */
  778. struct rb_root_cached pi_waiters;
  779. /* Updated under owner's pi_lock and rq lock */
  780. struct task_struct *pi_top_task;
  781. /* Deadlock detection and priority inheritance handling: */
  782. struct rt_mutex_waiter *pi_blocked_on;
  783. #endif
  784. #ifdef CONFIG_DEBUG_MUTEXES
  785. /* Mutex deadlock detection: */
  786. struct mutex_waiter *blocked_on;
  787. #endif
  788. #ifdef CONFIG_TRACE_IRQFLAGS
  789. unsigned int irq_events;
  790. unsigned long hardirq_enable_ip;
  791. unsigned long hardirq_disable_ip;
  792. unsigned int hardirq_enable_event;
  793. unsigned int hardirq_disable_event;
  794. int hardirqs_enabled;
  795. int hardirq_context;
  796. unsigned long softirq_disable_ip;
  797. unsigned long softirq_enable_ip;
  798. unsigned int softirq_disable_event;
  799. unsigned int softirq_enable_event;
  800. int softirqs_enabled;
  801. int softirq_context;
  802. #endif
  803. #ifdef CONFIG_LOCKDEP
  804. # define MAX_LOCK_DEPTH 48UL
  805. u64 curr_chain_key;
  806. int lockdep_depth;
  807. unsigned int lockdep_recursion;
  808. struct held_lock held_locks[MAX_LOCK_DEPTH];
  809. #endif
  810. #ifdef CONFIG_UBSAN
  811. unsigned int in_ubsan;
  812. #endif
  813. /* Journalling filesystem info: */
  814. void *journal_info;
  815. /* Stacked block device info: */
  816. struct bio_list *bio_list;
  817. #ifdef CONFIG_BLOCK
  818. /* Stack plugging: */
  819. struct blk_plug *plug;
  820. #endif
  821. /* VM state: */
  822. struct reclaim_state *reclaim_state;
  823. struct backing_dev_info *backing_dev_info;
  824. struct io_context *io_context;
  825. /* Ptrace state: */
  826. unsigned long ptrace_message;
  827. siginfo_t *last_siginfo;
  828. struct task_io_accounting ioac;
  829. #ifdef CONFIG_TASK_XACCT
  830. /* Accumulated RSS usage: */
  831. u64 acct_rss_mem1;
  832. /* Accumulated virtual memory usage: */
  833. u64 acct_vm_mem1;
  834. /* stime + utime since last update: */
  835. u64 acct_timexpd;
  836. #endif
  837. #ifdef CONFIG_CPUSETS
  838. /* Protected by ->alloc_lock: */
  839. nodemask_t mems_allowed;
  840. /* Seqence number to catch updates: */
  841. seqcount_t mems_allowed_seq;
  842. int cpuset_mem_spread_rotor;
  843. int cpuset_slab_spread_rotor;
  844. #endif
  845. #ifdef CONFIG_CGROUPS
  846. /* Control Group info protected by css_set_lock: */
  847. struct css_set __rcu *cgroups;
  848. /* cg_list protected by css_set_lock and tsk->alloc_lock: */
  849. struct list_head cg_list;
  850. #endif
  851. #ifdef CONFIG_INTEL_RDT
  852. u32 closid;
  853. u32 rmid;
  854. #endif
  855. #ifdef CONFIG_FUTEX
  856. struct robust_list_head __user *robust_list;
  857. #ifdef CONFIG_COMPAT
  858. struct compat_robust_list_head __user *compat_robust_list;
  859. #endif
  860. struct list_head pi_state_list;
  861. struct futex_pi_state *pi_state_cache;
  862. #endif
  863. #ifdef CONFIG_PERF_EVENTS
  864. struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
  865. struct mutex perf_event_mutex;
  866. struct list_head perf_event_list;
  867. #endif
  868. #ifdef CONFIG_DEBUG_PREEMPT
  869. unsigned long preempt_disable_ip;
  870. #endif
  871. #ifdef CONFIG_NUMA
  872. /* Protected by alloc_lock: */
  873. struct mempolicy *mempolicy;
  874. short il_prev;
  875. short pref_node_fork;
  876. #endif
  877. #ifdef CONFIG_NUMA_BALANCING
  878. int numa_scan_seq;
  879. unsigned int numa_scan_period;
  880. unsigned int numa_scan_period_max;
  881. int numa_preferred_nid;
  882. unsigned long numa_migrate_retry;
  883. /* Migration stamp: */
  884. u64 node_stamp;
  885. u64 last_task_numa_placement;
  886. u64 last_sum_exec_runtime;
  887. struct callback_head numa_work;
  888. struct numa_group *numa_group;
  889. /*
  890. * numa_faults is an array split into four regions:
  891. * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
  892. * in this precise order.
  893. *
  894. * faults_memory: Exponential decaying average of faults on a per-node
  895. * basis. Scheduling placement decisions are made based on these
  896. * counts. The values remain static for the duration of a PTE scan.
  897. * faults_cpu: Track the nodes the process was running on when a NUMA
  898. * hinting fault was incurred.
  899. * faults_memory_buffer and faults_cpu_buffer: Record faults per node
  900. * during the current scan window. When the scan completes, the counts
  901. * in faults_memory and faults_cpu decay and these values are copied.
  902. */
  903. unsigned long *numa_faults;
  904. unsigned long total_numa_faults;
  905. /*
  906. * numa_faults_locality tracks if faults recorded during the last
  907. * scan window were remote/local or failed to migrate. The task scan
  908. * period is adapted based on the locality of the faults with different
  909. * weights depending on whether they were shared or private faults
  910. */
  911. unsigned long numa_faults_locality[3];
  912. unsigned long numa_pages_migrated;
  913. #endif /* CONFIG_NUMA_BALANCING */
  914. #ifdef CONFIG_RSEQ
  915. struct rseq __user *rseq;
  916. u32 rseq_len;
  917. u32 rseq_sig;
  918. /*
  919. * RmW on rseq_event_mask must be performed atomically
  920. * with respect to preemption.
  921. */
  922. unsigned long rseq_event_mask;
  923. #endif
  924. struct tlbflush_unmap_batch tlb_ubc;
  925. struct rcu_head rcu;
  926. /* Cache last used pipe for splice(): */
  927. struct pipe_inode_info *splice_pipe;
  928. struct page_frag task_frag;
  929. #ifdef CONFIG_TASK_DELAY_ACCT
  930. struct task_delay_info *delays;
  931. #endif
  932. #ifdef CONFIG_FAULT_INJECTION
  933. int make_it_fail;
  934. unsigned int fail_nth;
  935. #endif
  936. /*
  937. * When (nr_dirtied >= nr_dirtied_pause), it's time to call
  938. * balance_dirty_pages() for a dirty throttling pause:
  939. */
  940. int nr_dirtied;
  941. int nr_dirtied_pause;
  942. /* Start of a write-and-pause period: */
  943. unsigned long dirty_paused_when;
  944. #ifdef CONFIG_LATENCYTOP
  945. int latency_record_count;
  946. struct latency_record latency_record[LT_SAVECOUNT];
  947. #endif
  948. /*
  949. * Time slack values; these are used to round up poll() and
  950. * select() etc timeout values. These are in nanoseconds.
  951. */
  952. u64 timer_slack_ns;
  953. u64 default_timer_slack_ns;
  954. #ifdef CONFIG_KASAN
  955. unsigned int kasan_depth;
  956. #endif
  957. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  958. /* Index of current stored address in ret_stack: */
  959. int curr_ret_stack;
  960. /* Stack of return addresses for return function tracing: */
  961. struct ftrace_ret_stack *ret_stack;
  962. /* Timestamp for last schedule: */
  963. unsigned long long ftrace_timestamp;
  964. /*
  965. * Number of functions that haven't been traced
  966. * because of depth overrun:
  967. */
  968. atomic_t trace_overrun;
  969. /* Pause tracing: */
  970. atomic_t tracing_graph_pause;
  971. #endif
  972. #ifdef CONFIG_TRACING
  973. /* State flags for use by tracers: */
  974. unsigned long trace;
  975. /* Bitmask and counter of trace recursion: */
  976. unsigned long trace_recursion;
  977. #endif /* CONFIG_TRACING */
  978. #ifdef CONFIG_KCOV
  979. /* Coverage collection mode enabled for this task (0 if disabled): */
  980. unsigned int kcov_mode;
  981. /* Size of the kcov_area: */
  982. unsigned int kcov_size;
  983. /* Buffer for coverage collection: */
  984. void *kcov_area;
  985. /* KCOV descriptor wired with this task or NULL: */
  986. struct kcov *kcov;
  987. #endif
  988. #ifdef CONFIG_MEMCG
  989. struct mem_cgroup *memcg_in_oom;
  990. gfp_t memcg_oom_gfp_mask;
  991. int memcg_oom_order;
  992. /* Number of pages to reclaim on returning to userland: */
  993. unsigned int memcg_nr_pages_over_high;
  994. /* Used by memcontrol for targeted memcg charge: */
  995. struct mem_cgroup *active_memcg;
  996. #endif
  997. #ifdef CONFIG_BLK_CGROUP
  998. struct request_queue *throttle_queue;
  999. #endif
  1000. #ifdef CONFIG_UPROBES
  1001. struct uprobe_task *utask;
  1002. #endif
  1003. #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
  1004. unsigned int sequential_io;
  1005. unsigned int sequential_io_avg;
  1006. #endif
  1007. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  1008. unsigned long task_state_change;
  1009. #endif
  1010. int pagefault_disabled;
  1011. #ifdef CONFIG_MMU
  1012. struct task_struct *oom_reaper_list;
  1013. #endif
  1014. #ifdef CONFIG_VMAP_STACK
  1015. struct vm_struct *stack_vm_area;
  1016. #endif
  1017. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1018. /* A live task holds one reference: */
  1019. atomic_t stack_refcount;
  1020. #endif
  1021. #ifdef CONFIG_LIVEPATCH
  1022. int patch_state;
  1023. #endif
  1024. #ifdef CONFIG_SECURITY
  1025. /* Used by LSM modules for access restriction: */
  1026. void *security;
  1027. #endif
  1028. /*
  1029. * New fields for task_struct should be added above here, so that
  1030. * they are included in the randomized portion of task_struct.
  1031. */
  1032. randomized_struct_fields_end
  1033. /* CPU-specific state of this task: */
  1034. struct thread_struct thread;
  1035. /*
  1036. * WARNING: on x86, 'thread_struct' contains a variable-sized
  1037. * structure. It *MUST* be at the end of 'task_struct'.
  1038. *
  1039. * Do not put anything below here!
  1040. */
  1041. };
  1042. static inline struct pid *task_pid(struct task_struct *task)
  1043. {
  1044. return task->pids[PIDTYPE_PID].pid;
  1045. }
  1046. static inline struct pid *task_tgid(struct task_struct *task)
  1047. {
  1048. return task->group_leader->pids[PIDTYPE_PID].pid;
  1049. }
  1050. /*
  1051. * Without tasklist or RCU lock it is not safe to dereference
  1052. * the result of task_pgrp/task_session even if task == current,
  1053. * we can race with another thread doing sys_setsid/sys_setpgid.
  1054. */
  1055. static inline struct pid *task_pgrp(struct task_struct *task)
  1056. {
  1057. return task->group_leader->pids[PIDTYPE_PGID].pid;
  1058. }
  1059. static inline struct pid *task_session(struct task_struct *task)
  1060. {
  1061. return task->group_leader->pids[PIDTYPE_SID].pid;
  1062. }
  1063. /*
  1064. * the helpers to get the task's different pids as they are seen
  1065. * from various namespaces
  1066. *
  1067. * task_xid_nr() : global id, i.e. the id seen from the init namespace;
  1068. * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
  1069. * current.
  1070. * task_xid_nr_ns() : id seen from the ns specified;
  1071. *
  1072. * see also pid_nr() etc in include/linux/pid.h
  1073. */
  1074. pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
  1075. static inline pid_t task_pid_nr(struct task_struct *tsk)
  1076. {
  1077. return tsk->pid;
  1078. }
  1079. static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  1080. {
  1081. return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
  1082. }
  1083. static inline pid_t task_pid_vnr(struct task_struct *tsk)
  1084. {
  1085. return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
  1086. }
  1087. static inline pid_t task_tgid_nr(struct task_struct *tsk)
  1088. {
  1089. return tsk->tgid;
  1090. }
  1091. /**
  1092. * pid_alive - check that a task structure is not stale
  1093. * @p: Task structure to be checked.
  1094. *
  1095. * Test if a process is not yet dead (at most zombie state)
  1096. * If pid_alive fails, then pointers within the task structure
  1097. * can be stale and must not be dereferenced.
  1098. *
  1099. * Return: 1 if the process is alive. 0 otherwise.
  1100. */
  1101. static inline int pid_alive(const struct task_struct *p)
  1102. {
  1103. return p->pids[PIDTYPE_PID].pid != NULL;
  1104. }
  1105. static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  1106. {
  1107. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
  1108. }
  1109. static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
  1110. {
  1111. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
  1112. }
  1113. static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  1114. {
  1115. return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
  1116. }
  1117. static inline pid_t task_session_vnr(struct task_struct *tsk)
  1118. {
  1119. return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
  1120. }
  1121. static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
  1122. {
  1123. return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, ns);
  1124. }
  1125. static inline pid_t task_tgid_vnr(struct task_struct *tsk)
  1126. {
  1127. return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, NULL);
  1128. }
  1129. static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
  1130. {
  1131. pid_t pid = 0;
  1132. rcu_read_lock();
  1133. if (pid_alive(tsk))
  1134. pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
  1135. rcu_read_unlock();
  1136. return pid;
  1137. }
  1138. static inline pid_t task_ppid_nr(const struct task_struct *tsk)
  1139. {
  1140. return task_ppid_nr_ns(tsk, &init_pid_ns);
  1141. }
  1142. /* Obsolete, do not use: */
  1143. static inline pid_t task_pgrp_nr(struct task_struct *tsk)
  1144. {
  1145. return task_pgrp_nr_ns(tsk, &init_pid_ns);
  1146. }
  1147. #define TASK_REPORT_IDLE (TASK_REPORT + 1)
  1148. #define TASK_REPORT_MAX (TASK_REPORT_IDLE << 1)
  1149. static inline unsigned int task_state_index(struct task_struct *tsk)
  1150. {
  1151. unsigned int tsk_state = READ_ONCE(tsk->state);
  1152. unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
  1153. BUILD_BUG_ON_NOT_POWER_OF_2(TASK_REPORT_MAX);
  1154. if (tsk_state == TASK_IDLE)
  1155. state = TASK_REPORT_IDLE;
  1156. return fls(state);
  1157. }
  1158. static inline char task_index_to_char(unsigned int state)
  1159. {
  1160. static const char state_char[] = "RSDTtXZPI";
  1161. BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != sizeof(state_char) - 1);
  1162. return state_char[state];
  1163. }
  1164. static inline char task_state_to_char(struct task_struct *tsk)
  1165. {
  1166. return task_index_to_char(task_state_index(tsk));
  1167. }
  1168. /**
  1169. * is_global_init - check if a task structure is init. Since init
  1170. * is free to have sub-threads we need to check tgid.
  1171. * @tsk: Task structure to be checked.
  1172. *
  1173. * Check if a task structure is the first user space task the kernel created.
  1174. *
  1175. * Return: 1 if the task structure is init. 0 otherwise.
  1176. */
  1177. static inline int is_global_init(struct task_struct *tsk)
  1178. {
  1179. return task_tgid_nr(tsk) == 1;
  1180. }
  1181. extern struct pid *cad_pid;
  1182. /*
  1183. * Per process flags
  1184. */
  1185. #define PF_IDLE 0x00000002 /* I am an IDLE thread */
  1186. #define PF_EXITING 0x00000004 /* Getting shut down */
  1187. #define PF_EXITPIDONE 0x00000008 /* PI exit done on shut down */
  1188. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  1189. #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
  1190. #define PF_FORKNOEXEC 0x00000040 /* Forked but didn't exec */
  1191. #define PF_MCE_PROCESS 0x00000080 /* Process policy on mce errors */
  1192. #define PF_SUPERPRIV 0x00000100 /* Used super-user privileges */
  1193. #define PF_DUMPCORE 0x00000200 /* Dumped core */
  1194. #define PF_SIGNALED 0x00000400 /* Killed by a signal */
  1195. #define PF_MEMALLOC 0x00000800 /* Allocating memory */
  1196. #define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */
  1197. #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */
  1198. #define PF_USED_ASYNC 0x00004000 /* Used async_schedule*(), used by module init */
  1199. #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */
  1200. #define PF_FROZEN 0x00010000 /* Frozen for system suspend */
  1201. #define PF_KSWAPD 0x00020000 /* I am kswapd */
  1202. #define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */
  1203. #define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */
  1204. #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
  1205. #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
  1206. #define PF_RANDOMIZE 0x00400000 /* Randomize virtual address space */
  1207. #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
  1208. #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
  1209. #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
  1210. #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
  1211. #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
  1212. #define PF_SUSPEND_TASK 0x80000000 /* This thread called freeze_processes() and should not be frozen */
  1213. /*
  1214. * Only the _current_ task can read/write to tsk->flags, but other
  1215. * tasks can access tsk->flags in readonly mode for example
  1216. * with tsk_used_math (like during threaded core dumping).
  1217. * There is however an exception to this rule during ptrace
  1218. * or during fork: the ptracer task is allowed to write to the
  1219. * child->flags of its traced child (same goes for fork, the parent
  1220. * can write to the child->flags), because we're guaranteed the
  1221. * child is not running and in turn not changing child->flags
  1222. * at the same time the parent does it.
  1223. */
  1224. #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
  1225. #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
  1226. #define clear_used_math() clear_stopped_child_used_math(current)
  1227. #define set_used_math() set_stopped_child_used_math(current)
  1228. #define conditional_stopped_child_used_math(condition, child) \
  1229. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
  1230. #define conditional_used_math(condition) conditional_stopped_child_used_math(condition, current)
  1231. #define copy_to_stopped_child_used_math(child) \
  1232. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
  1233. /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
  1234. #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
  1235. #define used_math() tsk_used_math(current)
  1236. static inline bool is_percpu_thread(void)
  1237. {
  1238. #ifdef CONFIG_SMP
  1239. return (current->flags & PF_NO_SETAFFINITY) &&
  1240. (current->nr_cpus_allowed == 1);
  1241. #else
  1242. return true;
  1243. #endif
  1244. }
  1245. /* Per-process atomic flags. */
  1246. #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
  1247. #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
  1248. #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
  1249. #define PFA_SPEC_SSB_DISABLE 3 /* Speculative Store Bypass disabled */
  1250. #define PFA_SPEC_SSB_FORCE_DISABLE 4 /* Speculative Store Bypass force disabled*/
  1251. #define TASK_PFA_TEST(name, func) \
  1252. static inline bool task_##func(struct task_struct *p) \
  1253. { return test_bit(PFA_##name, &p->atomic_flags); }
  1254. #define TASK_PFA_SET(name, func) \
  1255. static inline void task_set_##func(struct task_struct *p) \
  1256. { set_bit(PFA_##name, &p->atomic_flags); }
  1257. #define TASK_PFA_CLEAR(name, func) \
  1258. static inline void task_clear_##func(struct task_struct *p) \
  1259. { clear_bit(PFA_##name, &p->atomic_flags); }
  1260. TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
  1261. TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
  1262. TASK_PFA_TEST(SPREAD_PAGE, spread_page)
  1263. TASK_PFA_SET(SPREAD_PAGE, spread_page)
  1264. TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
  1265. TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
  1266. TASK_PFA_SET(SPREAD_SLAB, spread_slab)
  1267. TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
  1268. TASK_PFA_TEST(SPEC_SSB_DISABLE, spec_ssb_disable)
  1269. TASK_PFA_SET(SPEC_SSB_DISABLE, spec_ssb_disable)
  1270. TASK_PFA_CLEAR(SPEC_SSB_DISABLE, spec_ssb_disable)
  1271. TASK_PFA_TEST(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
  1272. TASK_PFA_SET(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
  1273. static inline void
  1274. current_restore_flags(unsigned long orig_flags, unsigned long flags)
  1275. {
  1276. current->flags &= ~flags;
  1277. current->flags |= orig_flags & flags;
  1278. }
  1279. extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
  1280. extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
  1281. #ifdef CONFIG_SMP
  1282. extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
  1283. extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
  1284. #else
  1285. static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  1286. {
  1287. }
  1288. static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  1289. {
  1290. if (!cpumask_test_cpu(0, new_mask))
  1291. return -EINVAL;
  1292. return 0;
  1293. }
  1294. #endif
  1295. #ifndef cpu_relax_yield
  1296. #define cpu_relax_yield() cpu_relax()
  1297. #endif
  1298. extern int yield_to(struct task_struct *p, bool preempt);
  1299. extern void set_user_nice(struct task_struct *p, long nice);
  1300. extern int task_prio(const struct task_struct *p);
  1301. /**
  1302. * task_nice - return the nice value of a given task.
  1303. * @p: the task in question.
  1304. *
  1305. * Return: The nice value [ -20 ... 0 ... 19 ].
  1306. */
  1307. static inline int task_nice(const struct task_struct *p)
  1308. {
  1309. return PRIO_TO_NICE((p)->static_prio);
  1310. }
  1311. extern int can_nice(const struct task_struct *p, const int nice);
  1312. extern int task_curr(const struct task_struct *p);
  1313. extern int idle_cpu(int cpu);
  1314. extern int available_idle_cpu(int cpu);
  1315. extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
  1316. extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
  1317. extern int sched_setattr(struct task_struct *, const struct sched_attr *);
  1318. extern int sched_setattr_nocheck(struct task_struct *, const struct sched_attr *);
  1319. extern struct task_struct *idle_task(int cpu);
  1320. /**
  1321. * is_idle_task - is the specified task an idle task?
  1322. * @p: the task in question.
  1323. *
  1324. * Return: 1 if @p is an idle task. 0 otherwise.
  1325. */
  1326. static inline bool is_idle_task(const struct task_struct *p)
  1327. {
  1328. return !!(p->flags & PF_IDLE);
  1329. }
  1330. extern struct task_struct *curr_task(int cpu);
  1331. extern void ia64_set_curr_task(int cpu, struct task_struct *p);
  1332. void yield(void);
  1333. union thread_union {
  1334. #ifndef CONFIG_ARCH_TASK_STRUCT_ON_STACK
  1335. struct task_struct task;
  1336. #endif
  1337. #ifndef CONFIG_THREAD_INFO_IN_TASK
  1338. struct thread_info thread_info;
  1339. #endif
  1340. unsigned long stack[THREAD_SIZE/sizeof(long)];
  1341. };
  1342. #ifndef CONFIG_THREAD_INFO_IN_TASK
  1343. extern struct thread_info init_thread_info;
  1344. #endif
  1345. extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
  1346. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1347. static inline struct thread_info *task_thread_info(struct task_struct *task)
  1348. {
  1349. return &task->thread_info;
  1350. }
  1351. #elif !defined(__HAVE_THREAD_FUNCTIONS)
  1352. # define task_thread_info(task) ((struct thread_info *)(task)->stack)
  1353. #endif
  1354. /*
  1355. * find a task by one of its numerical ids
  1356. *
  1357. * find_task_by_pid_ns():
  1358. * finds a task by its pid in the specified namespace
  1359. * find_task_by_vpid():
  1360. * finds a task by its virtual pid
  1361. *
  1362. * see also find_vpid() etc in include/linux/pid.h
  1363. */
  1364. extern struct task_struct *find_task_by_vpid(pid_t nr);
  1365. extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
  1366. /*
  1367. * find a task by its virtual pid and get the task struct
  1368. */
  1369. extern struct task_struct *find_get_task_by_vpid(pid_t nr);
  1370. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  1371. extern int wake_up_process(struct task_struct *tsk);
  1372. extern void wake_up_new_task(struct task_struct *tsk);
  1373. #ifdef CONFIG_SMP
  1374. extern void kick_process(struct task_struct *tsk);
  1375. #else
  1376. static inline void kick_process(struct task_struct *tsk) { }
  1377. #endif
  1378. extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
  1379. static inline void set_task_comm(struct task_struct *tsk, const char *from)
  1380. {
  1381. __set_task_comm(tsk, from, false);
  1382. }
  1383. extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
  1384. #define get_task_comm(buf, tsk) ({ \
  1385. BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \
  1386. __get_task_comm(buf, sizeof(buf), tsk); \
  1387. })
  1388. #ifdef CONFIG_SMP
  1389. void scheduler_ipi(void);
  1390. extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
  1391. #else
  1392. static inline void scheduler_ipi(void) { }
  1393. static inline unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  1394. {
  1395. return 1;
  1396. }
  1397. #endif
  1398. /*
  1399. * Set thread flags in other task's structures.
  1400. * See asm/thread_info.h for TIF_xxxx flags available:
  1401. */
  1402. static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1403. {
  1404. set_ti_thread_flag(task_thread_info(tsk), flag);
  1405. }
  1406. static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1407. {
  1408. clear_ti_thread_flag(task_thread_info(tsk), flag);
  1409. }
  1410. static inline void update_tsk_thread_flag(struct task_struct *tsk, int flag,
  1411. bool value)
  1412. {
  1413. update_ti_thread_flag(task_thread_info(tsk), flag, value);
  1414. }
  1415. static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1416. {
  1417. return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
  1418. }
  1419. static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1420. {
  1421. return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
  1422. }
  1423. static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
  1424. {
  1425. return test_ti_thread_flag(task_thread_info(tsk), flag);
  1426. }
  1427. static inline void set_tsk_need_resched(struct task_struct *tsk)
  1428. {
  1429. set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1430. }
  1431. static inline void clear_tsk_need_resched(struct task_struct *tsk)
  1432. {
  1433. clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1434. }
  1435. static inline int test_tsk_need_resched(struct task_struct *tsk)
  1436. {
  1437. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  1438. }
  1439. /*
  1440. * cond_resched() and cond_resched_lock(): latency reduction via
  1441. * explicit rescheduling in places that are safe. The return
  1442. * value indicates whether a reschedule was done in fact.
  1443. * cond_resched_lock() will drop the spinlock before scheduling,
  1444. */
  1445. #ifndef CONFIG_PREEMPT
  1446. extern int _cond_resched(void);
  1447. #else
  1448. static inline int _cond_resched(void) { return 0; }
  1449. #endif
  1450. #define cond_resched() ({ \
  1451. ___might_sleep(__FILE__, __LINE__, 0); \
  1452. _cond_resched(); \
  1453. })
  1454. extern int __cond_resched_lock(spinlock_t *lock);
  1455. #define cond_resched_lock(lock) ({ \
  1456. ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
  1457. __cond_resched_lock(lock); \
  1458. })
  1459. static inline void cond_resched_rcu(void)
  1460. {
  1461. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
  1462. rcu_read_unlock();
  1463. cond_resched();
  1464. rcu_read_lock();
  1465. #endif
  1466. }
  1467. /*
  1468. * Does a critical section need to be broken due to another
  1469. * task waiting?: (technically does not depend on CONFIG_PREEMPT,
  1470. * but a general need for low latency)
  1471. */
  1472. static inline int spin_needbreak(spinlock_t *lock)
  1473. {
  1474. #ifdef CONFIG_PREEMPT
  1475. return spin_is_contended(lock);
  1476. #else
  1477. return 0;
  1478. #endif
  1479. }
  1480. static __always_inline bool need_resched(void)
  1481. {
  1482. return unlikely(tif_need_resched());
  1483. }
  1484. /*
  1485. * Wrappers for p->thread_info->cpu access. No-op on UP.
  1486. */
  1487. #ifdef CONFIG_SMP
  1488. static inline unsigned int task_cpu(const struct task_struct *p)
  1489. {
  1490. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1491. return p->cpu;
  1492. #else
  1493. return task_thread_info(p)->cpu;
  1494. #endif
  1495. }
  1496. extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
  1497. #else
  1498. static inline unsigned int task_cpu(const struct task_struct *p)
  1499. {
  1500. return 0;
  1501. }
  1502. static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  1503. {
  1504. }
  1505. #endif /* CONFIG_SMP */
  1506. /*
  1507. * In order to reduce various lock holder preemption latencies provide an
  1508. * interface to see if a vCPU is currently running or not.
  1509. *
  1510. * This allows us to terminate optimistic spin loops and block, analogous to
  1511. * the native optimistic spin heuristic of testing if the lock owner task is
  1512. * running or not.
  1513. */
  1514. #ifndef vcpu_is_preempted
  1515. # define vcpu_is_preempted(cpu) false
  1516. #endif
  1517. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  1518. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  1519. #ifndef TASK_SIZE_OF
  1520. #define TASK_SIZE_OF(tsk) TASK_SIZE
  1521. #endif
  1522. #ifdef CONFIG_RSEQ
  1523. /*
  1524. * Map the event mask on the user-space ABI enum rseq_cs_flags
  1525. * for direct mask checks.
  1526. */
  1527. enum rseq_event_mask_bits {
  1528. RSEQ_EVENT_PREEMPT_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT,
  1529. RSEQ_EVENT_SIGNAL_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT,
  1530. RSEQ_EVENT_MIGRATE_BIT = RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT,
  1531. };
  1532. enum rseq_event_mask {
  1533. RSEQ_EVENT_PREEMPT = (1U << RSEQ_EVENT_PREEMPT_BIT),
  1534. RSEQ_EVENT_SIGNAL = (1U << RSEQ_EVENT_SIGNAL_BIT),
  1535. RSEQ_EVENT_MIGRATE = (1U << RSEQ_EVENT_MIGRATE_BIT),
  1536. };
  1537. static inline void rseq_set_notify_resume(struct task_struct *t)
  1538. {
  1539. if (t->rseq)
  1540. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1541. }
  1542. void __rseq_handle_notify_resume(struct ksignal *sig, struct pt_regs *regs);
  1543. static inline void rseq_handle_notify_resume(struct ksignal *ksig,
  1544. struct pt_regs *regs)
  1545. {
  1546. if (current->rseq)
  1547. __rseq_handle_notify_resume(ksig, regs);
  1548. }
  1549. static inline void rseq_signal_deliver(struct ksignal *ksig,
  1550. struct pt_regs *regs)
  1551. {
  1552. preempt_disable();
  1553. __set_bit(RSEQ_EVENT_SIGNAL_BIT, &current->rseq_event_mask);
  1554. preempt_enable();
  1555. rseq_handle_notify_resume(ksig, regs);
  1556. }
  1557. /* rseq_preempt() requires preemption to be disabled. */
  1558. static inline void rseq_preempt(struct task_struct *t)
  1559. {
  1560. __set_bit(RSEQ_EVENT_PREEMPT_BIT, &t->rseq_event_mask);
  1561. rseq_set_notify_resume(t);
  1562. }
  1563. /* rseq_migrate() requires preemption to be disabled. */
  1564. static inline void rseq_migrate(struct task_struct *t)
  1565. {
  1566. __set_bit(RSEQ_EVENT_MIGRATE_BIT, &t->rseq_event_mask);
  1567. rseq_set_notify_resume(t);
  1568. }
  1569. /*
  1570. * If parent process has a registered restartable sequences area, the
  1571. * child inherits. Only applies when forking a process, not a thread.
  1572. */
  1573. static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
  1574. {
  1575. if (clone_flags & CLONE_THREAD) {
  1576. t->rseq = NULL;
  1577. t->rseq_len = 0;
  1578. t->rseq_sig = 0;
  1579. t->rseq_event_mask = 0;
  1580. } else {
  1581. t->rseq = current->rseq;
  1582. t->rseq_len = current->rseq_len;
  1583. t->rseq_sig = current->rseq_sig;
  1584. t->rseq_event_mask = current->rseq_event_mask;
  1585. }
  1586. }
  1587. static inline void rseq_execve(struct task_struct *t)
  1588. {
  1589. t->rseq = NULL;
  1590. t->rseq_len = 0;
  1591. t->rseq_sig = 0;
  1592. t->rseq_event_mask = 0;
  1593. }
  1594. #else
  1595. static inline void rseq_set_notify_resume(struct task_struct *t)
  1596. {
  1597. }
  1598. static inline void rseq_handle_notify_resume(struct ksignal *ksig,
  1599. struct pt_regs *regs)
  1600. {
  1601. }
  1602. static inline void rseq_signal_deliver(struct ksignal *ksig,
  1603. struct pt_regs *regs)
  1604. {
  1605. }
  1606. static inline void rseq_preempt(struct task_struct *t)
  1607. {
  1608. }
  1609. static inline void rseq_migrate(struct task_struct *t)
  1610. {
  1611. }
  1612. static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
  1613. {
  1614. }
  1615. static inline void rseq_execve(struct task_struct *t)
  1616. {
  1617. }
  1618. #endif
  1619. #ifdef CONFIG_DEBUG_RSEQ
  1620. void rseq_syscall(struct pt_regs *regs);
  1621. #else
  1622. static inline void rseq_syscall(struct pt_regs *regs)
  1623. {
  1624. }
  1625. #endif
  1626. #endif