sched.h 53 KB

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