sched.h 42 KB

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