sched.h 52 KB

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