sched.h 45 KB

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