sched.h 48 KB

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