sched.h 48 KB

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