sched.h 45 KB

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