sched.h 52 KB

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