sched.h 52 KB

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