sched.h 44 KB

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