sched.h 51 KB

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