sched.h 62 KB

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