sched.h 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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. static inline bool in_vfork(struct task_struct *tsk)
  1089. {
  1090. bool ret;
  1091. /*
  1092. * need RCU to access ->real_parent if CLONE_VM was used along with
  1093. * CLONE_PARENT.
  1094. *
  1095. * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
  1096. * imply CLONE_VM
  1097. *
  1098. * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
  1099. * ->real_parent is not necessarily the task doing vfork(), so in
  1100. * theory we can't rely on task_lock() if we want to dereference it.
  1101. *
  1102. * And in this case we can't trust the real_parent->mm == tsk->mm
  1103. * check, it can be false negative. But we do not care, if init or
  1104. * another oom-unkillable task does this it should blame itself.
  1105. */
  1106. rcu_read_lock();
  1107. ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
  1108. rcu_read_unlock();
  1109. return ret;
  1110. }
  1111. #ifdef CONFIG_NUMA_BALANCING
  1112. extern void task_numa_fault(int last_node, int node, int pages, int flags);
  1113. extern pid_t task_numa_group_id(struct task_struct *p);
  1114. extern void set_numabalancing_state(bool enabled);
  1115. extern void task_numa_free(struct task_struct *p);
  1116. extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
  1117. int src_nid, int dst_cpu);
  1118. #else
  1119. static inline void task_numa_fault(int last_node, int node, int pages,
  1120. int flags)
  1121. {
  1122. }
  1123. static inline pid_t task_numa_group_id(struct task_struct *p)
  1124. {
  1125. return 0;
  1126. }
  1127. static inline void set_numabalancing_state(bool enabled)
  1128. {
  1129. }
  1130. static inline void task_numa_free(struct task_struct *p)
  1131. {
  1132. }
  1133. static inline bool should_numa_migrate_memory(struct task_struct *p,
  1134. struct page *page, int src_nid, int dst_cpu)
  1135. {
  1136. return true;
  1137. }
  1138. #endif
  1139. static inline struct pid *task_pid(struct task_struct *task)
  1140. {
  1141. return task->pids[PIDTYPE_PID].pid;
  1142. }
  1143. static inline struct pid *task_tgid(struct task_struct *task)
  1144. {
  1145. return task->group_leader->pids[PIDTYPE_PID].pid;
  1146. }
  1147. /*
  1148. * Without tasklist or rcu lock it is not safe to dereference
  1149. * the result of task_pgrp/task_session even if task == current,
  1150. * we can race with another thread doing sys_setsid/sys_setpgid.
  1151. */
  1152. static inline struct pid *task_pgrp(struct task_struct *task)
  1153. {
  1154. return task->group_leader->pids[PIDTYPE_PGID].pid;
  1155. }
  1156. static inline struct pid *task_session(struct task_struct *task)
  1157. {
  1158. return task->group_leader->pids[PIDTYPE_SID].pid;
  1159. }
  1160. struct pid_namespace;
  1161. /*
  1162. * the helpers to get the task's different pids as they are seen
  1163. * from various namespaces
  1164. *
  1165. * task_xid_nr() : global id, i.e. the id seen from the init namespace;
  1166. * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
  1167. * current.
  1168. * task_xid_nr_ns() : id seen from the ns specified;
  1169. *
  1170. * set_task_vxid() : assigns a virtual id to a task;
  1171. *
  1172. * see also pid_nr() etc in include/linux/pid.h
  1173. */
  1174. pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
  1175. struct pid_namespace *ns);
  1176. static inline pid_t task_pid_nr(struct task_struct *tsk)
  1177. {
  1178. return tsk->pid;
  1179. }
  1180. static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
  1181. struct pid_namespace *ns)
  1182. {
  1183. return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
  1184. }
  1185. static inline pid_t task_pid_vnr(struct task_struct *tsk)
  1186. {
  1187. return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
  1188. }
  1189. static inline pid_t task_tgid_nr(struct task_struct *tsk)
  1190. {
  1191. return tsk->tgid;
  1192. }
  1193. pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
  1194. static inline pid_t task_tgid_vnr(struct task_struct *tsk)
  1195. {
  1196. return pid_vnr(task_tgid(tsk));
  1197. }
  1198. static inline int pid_alive(const struct task_struct *p);
  1199. static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
  1200. {
  1201. pid_t pid = 0;
  1202. rcu_read_lock();
  1203. if (pid_alive(tsk))
  1204. pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
  1205. rcu_read_unlock();
  1206. return pid;
  1207. }
  1208. static inline pid_t task_ppid_nr(const struct task_struct *tsk)
  1209. {
  1210. return task_ppid_nr_ns(tsk, &init_pid_ns);
  1211. }
  1212. static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
  1213. struct pid_namespace *ns)
  1214. {
  1215. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
  1216. }
  1217. static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
  1218. {
  1219. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
  1220. }
  1221. static inline pid_t task_session_nr_ns(struct task_struct *tsk,
  1222. struct pid_namespace *ns)
  1223. {
  1224. return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
  1225. }
  1226. static inline pid_t task_session_vnr(struct task_struct *tsk)
  1227. {
  1228. return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
  1229. }
  1230. /* obsolete, do not use */
  1231. static inline pid_t task_pgrp_nr(struct task_struct *tsk)
  1232. {
  1233. return task_pgrp_nr_ns(tsk, &init_pid_ns);
  1234. }
  1235. /**
  1236. * pid_alive - check that a task structure is not stale
  1237. * @p: Task structure to be checked.
  1238. *
  1239. * Test if a process is not yet dead (at most zombie state)
  1240. * If pid_alive fails, then pointers within the task structure
  1241. * can be stale and must not be dereferenced.
  1242. *
  1243. * Return: 1 if the process is alive. 0 otherwise.
  1244. */
  1245. static inline int pid_alive(const struct task_struct *p)
  1246. {
  1247. return p->pids[PIDTYPE_PID].pid != NULL;
  1248. }
  1249. /**
  1250. * is_global_init - check if a task structure is init. Since init
  1251. * is free to have sub-threads we need to check tgid.
  1252. * @tsk: Task structure to be checked.
  1253. *
  1254. * Check if a task structure is the first user space task the kernel created.
  1255. *
  1256. * Return: 1 if the task structure is init. 0 otherwise.
  1257. */
  1258. static inline int is_global_init(struct task_struct *tsk)
  1259. {
  1260. return task_tgid_nr(tsk) == 1;
  1261. }
  1262. extern struct pid *cad_pid;
  1263. extern void free_task(struct task_struct *tsk);
  1264. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  1265. extern void __put_task_struct(struct task_struct *t);
  1266. static inline void put_task_struct(struct task_struct *t)
  1267. {
  1268. if (atomic_dec_and_test(&t->usage))
  1269. __put_task_struct(t);
  1270. }
  1271. struct task_struct *task_rcu_dereference(struct task_struct **ptask);
  1272. struct task_struct *try_get_task_struct(struct task_struct **ptask);
  1273. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  1274. extern void task_cputime(struct task_struct *t,
  1275. u64 *utime, u64 *stime);
  1276. extern u64 task_gtime(struct task_struct *t);
  1277. #else
  1278. static inline void task_cputime(struct task_struct *t,
  1279. u64 *utime, u64 *stime)
  1280. {
  1281. *utime = t->utime;
  1282. *stime = t->stime;
  1283. }
  1284. static inline u64 task_gtime(struct task_struct *t)
  1285. {
  1286. return t->gtime;
  1287. }
  1288. #endif
  1289. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  1290. static inline void task_cputime_scaled(struct task_struct *t,
  1291. u64 *utimescaled,
  1292. u64 *stimescaled)
  1293. {
  1294. *utimescaled = t->utimescaled;
  1295. *stimescaled = t->stimescaled;
  1296. }
  1297. #else
  1298. static inline void task_cputime_scaled(struct task_struct *t,
  1299. u64 *utimescaled,
  1300. u64 *stimescaled)
  1301. {
  1302. task_cputime(t, utimescaled, stimescaled);
  1303. }
  1304. #endif
  1305. extern void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
  1306. extern void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
  1307. /*
  1308. * Per process flags
  1309. */
  1310. #define PF_IDLE 0x00000002 /* I am an IDLE thread */
  1311. #define PF_EXITING 0x00000004 /* getting shut down */
  1312. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  1313. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  1314. #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
  1315. #define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
  1316. #define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
  1317. #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
  1318. #define PF_DUMPCORE 0x00000200 /* dumped core */
  1319. #define PF_SIGNALED 0x00000400 /* killed by a signal */
  1320. #define PF_MEMALLOC 0x00000800 /* Allocating memory */
  1321. #define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
  1322. #define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
  1323. #define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
  1324. #define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
  1325. #define PF_FROZEN 0x00010000 /* frozen for system suspend */
  1326. #define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
  1327. #define PF_KSWAPD 0x00040000 /* I am kswapd */
  1328. #define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
  1329. #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
  1330. #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
  1331. #define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
  1332. #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
  1333. #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
  1334. #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
  1335. #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
  1336. #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
  1337. #define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
  1338. /*
  1339. * Only the _current_ task can read/write to tsk->flags, but other
  1340. * tasks can access tsk->flags in readonly mode for example
  1341. * with tsk_used_math (like during threaded core dumping).
  1342. * There is however an exception to this rule during ptrace
  1343. * or during fork: the ptracer task is allowed to write to the
  1344. * child->flags of its traced child (same goes for fork, the parent
  1345. * can write to the child->flags), because we're guaranteed the
  1346. * child is not running and in turn not changing child->flags
  1347. * at the same time the parent does it.
  1348. */
  1349. #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
  1350. #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
  1351. #define clear_used_math() clear_stopped_child_used_math(current)
  1352. #define set_used_math() set_stopped_child_used_math(current)
  1353. #define conditional_stopped_child_used_math(condition, child) \
  1354. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
  1355. #define conditional_used_math(condition) \
  1356. conditional_stopped_child_used_math(condition, current)
  1357. #define copy_to_stopped_child_used_math(child) \
  1358. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
  1359. /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
  1360. #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
  1361. #define used_math() tsk_used_math(current)
  1362. /* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
  1363. * __GFP_FS is also cleared as it implies __GFP_IO.
  1364. */
  1365. static inline gfp_t memalloc_noio_flags(gfp_t flags)
  1366. {
  1367. if (unlikely(current->flags & PF_MEMALLOC_NOIO))
  1368. flags &= ~(__GFP_IO | __GFP_FS);
  1369. return flags;
  1370. }
  1371. static inline unsigned int memalloc_noio_save(void)
  1372. {
  1373. unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
  1374. current->flags |= PF_MEMALLOC_NOIO;
  1375. return flags;
  1376. }
  1377. static inline void memalloc_noio_restore(unsigned int flags)
  1378. {
  1379. current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
  1380. }
  1381. /* Per-process atomic flags. */
  1382. #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
  1383. #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
  1384. #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
  1385. #define PFA_LMK_WAITING 3 /* Lowmemorykiller is waiting */
  1386. #define TASK_PFA_TEST(name, func) \
  1387. static inline bool task_##func(struct task_struct *p) \
  1388. { return test_bit(PFA_##name, &p->atomic_flags); }
  1389. #define TASK_PFA_SET(name, func) \
  1390. static inline void task_set_##func(struct task_struct *p) \
  1391. { set_bit(PFA_##name, &p->atomic_flags); }
  1392. #define TASK_PFA_CLEAR(name, func) \
  1393. static inline void task_clear_##func(struct task_struct *p) \
  1394. { clear_bit(PFA_##name, &p->atomic_flags); }
  1395. TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
  1396. TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
  1397. TASK_PFA_TEST(SPREAD_PAGE, spread_page)
  1398. TASK_PFA_SET(SPREAD_PAGE, spread_page)
  1399. TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
  1400. TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
  1401. TASK_PFA_SET(SPREAD_SLAB, spread_slab)
  1402. TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
  1403. TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
  1404. TASK_PFA_SET(LMK_WAITING, lmk_waiting)
  1405. /*
  1406. * task->jobctl flags
  1407. */
  1408. #define JOBCTL_STOP_SIGMASK 0xffff /* signr of the last group stop */
  1409. #define JOBCTL_STOP_DEQUEUED_BIT 16 /* stop signal dequeued */
  1410. #define JOBCTL_STOP_PENDING_BIT 17 /* task should stop for group stop */
  1411. #define JOBCTL_STOP_CONSUME_BIT 18 /* consume group stop count */
  1412. #define JOBCTL_TRAP_STOP_BIT 19 /* trap for STOP */
  1413. #define JOBCTL_TRAP_NOTIFY_BIT 20 /* trap for NOTIFY */
  1414. #define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */
  1415. #define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */
  1416. #define JOBCTL_STOP_DEQUEUED (1UL << JOBCTL_STOP_DEQUEUED_BIT)
  1417. #define JOBCTL_STOP_PENDING (1UL << JOBCTL_STOP_PENDING_BIT)
  1418. #define JOBCTL_STOP_CONSUME (1UL << JOBCTL_STOP_CONSUME_BIT)
  1419. #define JOBCTL_TRAP_STOP (1UL << JOBCTL_TRAP_STOP_BIT)
  1420. #define JOBCTL_TRAP_NOTIFY (1UL << JOBCTL_TRAP_NOTIFY_BIT)
  1421. #define JOBCTL_TRAPPING (1UL << JOBCTL_TRAPPING_BIT)
  1422. #define JOBCTL_LISTENING (1UL << JOBCTL_LISTENING_BIT)
  1423. #define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
  1424. #define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
  1425. extern bool task_set_jobctl_pending(struct task_struct *task,
  1426. unsigned long mask);
  1427. extern void task_clear_jobctl_trapping(struct task_struct *task);
  1428. extern void task_clear_jobctl_pending(struct task_struct *task,
  1429. unsigned long mask);
  1430. static inline void rcu_copy_process(struct task_struct *p)
  1431. {
  1432. #ifdef CONFIG_PREEMPT_RCU
  1433. p->rcu_read_lock_nesting = 0;
  1434. p->rcu_read_unlock_special.s = 0;
  1435. p->rcu_blocked_node = NULL;
  1436. INIT_LIST_HEAD(&p->rcu_node_entry);
  1437. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1438. #ifdef CONFIG_TASKS_RCU
  1439. p->rcu_tasks_holdout = false;
  1440. INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
  1441. p->rcu_tasks_idle_cpu = -1;
  1442. #endif /* #ifdef CONFIG_TASKS_RCU */
  1443. }
  1444. static inline void tsk_restore_flags(struct task_struct *task,
  1445. unsigned long orig_flags, unsigned long flags)
  1446. {
  1447. task->flags &= ~flags;
  1448. task->flags |= orig_flags & flags;
  1449. }
  1450. extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
  1451. const struct cpumask *trial);
  1452. extern int task_can_attach(struct task_struct *p,
  1453. const struct cpumask *cs_cpus_allowed);
  1454. #ifdef CONFIG_SMP
  1455. extern void do_set_cpus_allowed(struct task_struct *p,
  1456. const struct cpumask *new_mask);
  1457. extern int set_cpus_allowed_ptr(struct task_struct *p,
  1458. const struct cpumask *new_mask);
  1459. #else
  1460. static inline void do_set_cpus_allowed(struct task_struct *p,
  1461. const struct cpumask *new_mask)
  1462. {
  1463. }
  1464. static inline int set_cpus_allowed_ptr(struct task_struct *p,
  1465. const struct cpumask *new_mask)
  1466. {
  1467. if (!cpumask_test_cpu(0, new_mask))
  1468. return -EINVAL;
  1469. return 0;
  1470. }
  1471. #endif
  1472. #ifdef CONFIG_NO_HZ_COMMON
  1473. void calc_load_enter_idle(void);
  1474. void calc_load_exit_idle(void);
  1475. #else
  1476. static inline void calc_load_enter_idle(void) { }
  1477. static inline void calc_load_exit_idle(void) { }
  1478. #endif /* CONFIG_NO_HZ_COMMON */
  1479. #ifndef cpu_relax_yield
  1480. #define cpu_relax_yield() cpu_relax()
  1481. #endif
  1482. extern unsigned long long
  1483. task_sched_runtime(struct task_struct *task);
  1484. /* sched_exec is called by processes performing an exec */
  1485. #ifdef CONFIG_SMP
  1486. extern void sched_exec(void);
  1487. #else
  1488. #define sched_exec() {}
  1489. #endif
  1490. #ifdef CONFIG_HOTPLUG_CPU
  1491. extern void idle_task_exit(void);
  1492. #else
  1493. static inline void idle_task_exit(void) {}
  1494. #endif
  1495. #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
  1496. extern void wake_up_nohz_cpu(int cpu);
  1497. #else
  1498. static inline void wake_up_nohz_cpu(int cpu) { }
  1499. #endif
  1500. #ifdef CONFIG_NO_HZ_FULL
  1501. extern u64 scheduler_tick_max_deferment(void);
  1502. #endif
  1503. extern int yield_to(struct task_struct *p, bool preempt);
  1504. extern void set_user_nice(struct task_struct *p, long nice);
  1505. extern int task_prio(const struct task_struct *p);
  1506. /**
  1507. * task_nice - return the nice value of a given task.
  1508. * @p: the task in question.
  1509. *
  1510. * Return: The nice value [ -20 ... 0 ... 19 ].
  1511. */
  1512. static inline int task_nice(const struct task_struct *p)
  1513. {
  1514. return PRIO_TO_NICE((p)->static_prio);
  1515. }
  1516. extern int can_nice(const struct task_struct *p, const int nice);
  1517. extern int task_curr(const struct task_struct *p);
  1518. extern int idle_cpu(int cpu);
  1519. extern int sched_setscheduler(struct task_struct *, int,
  1520. const struct sched_param *);
  1521. extern int sched_setscheduler_nocheck(struct task_struct *, int,
  1522. const struct sched_param *);
  1523. extern int sched_setattr(struct task_struct *,
  1524. const struct sched_attr *);
  1525. extern struct task_struct *idle_task(int cpu);
  1526. /**
  1527. * is_idle_task - is the specified task an idle task?
  1528. * @p: the task in question.
  1529. *
  1530. * Return: 1 if @p is an idle task. 0 otherwise.
  1531. */
  1532. static inline bool is_idle_task(const struct task_struct *p)
  1533. {
  1534. return !!(p->flags & PF_IDLE);
  1535. }
  1536. extern struct task_struct *curr_task(int cpu);
  1537. extern void ia64_set_curr_task(int cpu, struct task_struct *p);
  1538. void yield(void);
  1539. union thread_union {
  1540. #ifndef CONFIG_THREAD_INFO_IN_TASK
  1541. struct thread_info thread_info;
  1542. #endif
  1543. unsigned long stack[THREAD_SIZE/sizeof(long)];
  1544. };
  1545. #ifndef __HAVE_ARCH_KSTACK_END
  1546. static inline int kstack_end(void *addr)
  1547. {
  1548. /* Reliable end of stack detection:
  1549. * Some APM bios versions misalign the stack
  1550. */
  1551. return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
  1552. }
  1553. #endif
  1554. extern union thread_union init_thread_union;
  1555. extern struct task_struct init_task;
  1556. extern struct mm_struct init_mm;
  1557. extern struct pid_namespace init_pid_ns;
  1558. /*
  1559. * find a task by one of its numerical ids
  1560. *
  1561. * find_task_by_pid_ns():
  1562. * finds a task by its pid in the specified namespace
  1563. * find_task_by_vpid():
  1564. * finds a task by its virtual pid
  1565. *
  1566. * see also find_vpid() etc in include/linux/pid.h
  1567. */
  1568. extern struct task_struct *find_task_by_vpid(pid_t nr);
  1569. extern struct task_struct *find_task_by_pid_ns(pid_t nr,
  1570. struct pid_namespace *ns);
  1571. #include <asm/current.h>
  1572. extern void xtime_update(unsigned long ticks);
  1573. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  1574. extern int wake_up_process(struct task_struct *tsk);
  1575. extern void wake_up_new_task(struct task_struct *tsk);
  1576. #ifdef CONFIG_SMP
  1577. extern void kick_process(struct task_struct *tsk);
  1578. #else
  1579. static inline void kick_process(struct task_struct *tsk) { }
  1580. #endif
  1581. extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
  1582. extern void sched_dead(struct task_struct *p);
  1583. extern void proc_caches_init(void);
  1584. extern void release_task(struct task_struct * p);
  1585. #ifdef CONFIG_HAVE_COPY_THREAD_TLS
  1586. extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
  1587. struct task_struct *, unsigned long);
  1588. #else
  1589. extern int copy_thread(unsigned long, unsigned long, unsigned long,
  1590. struct task_struct *);
  1591. /* Architectures that haven't opted into copy_thread_tls get the tls argument
  1592. * via pt_regs, so ignore the tls argument passed via C. */
  1593. static inline int copy_thread_tls(
  1594. unsigned long clone_flags, unsigned long sp, unsigned long arg,
  1595. struct task_struct *p, unsigned long tls)
  1596. {
  1597. return copy_thread(clone_flags, sp, arg, p);
  1598. }
  1599. #endif
  1600. extern void flush_thread(void);
  1601. #ifdef CONFIG_HAVE_EXIT_THREAD
  1602. extern void exit_thread(struct task_struct *tsk);
  1603. #else
  1604. static inline void exit_thread(struct task_struct *tsk)
  1605. {
  1606. }
  1607. #endif
  1608. extern void exit_files(struct task_struct *);
  1609. extern void exit_itimers(struct signal_struct *);
  1610. extern void do_group_exit(int);
  1611. extern int do_execve(struct filename *,
  1612. const char __user * const __user *,
  1613. const char __user * const __user *);
  1614. extern int do_execveat(int, struct filename *,
  1615. const char __user * const __user *,
  1616. const char __user * const __user *,
  1617. int);
  1618. extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
  1619. extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
  1620. struct task_struct *fork_idle(int);
  1621. extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  1622. extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
  1623. static inline void set_task_comm(struct task_struct *tsk, const char *from)
  1624. {
  1625. __set_task_comm(tsk, from, false);
  1626. }
  1627. extern char *get_task_comm(char *to, struct task_struct *tsk);
  1628. #ifdef CONFIG_SMP
  1629. void scheduler_ipi(void);
  1630. extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
  1631. #else
  1632. static inline void scheduler_ipi(void) { }
  1633. static inline unsigned long wait_task_inactive(struct task_struct *p,
  1634. long match_state)
  1635. {
  1636. return 1;
  1637. }
  1638. #endif
  1639. /*
  1640. * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
  1641. * subscriptions and synchronises with wait4(). Also used in procfs. Also
  1642. * pins the final release of task.io_context. Also protects ->cpuset and
  1643. * ->cgroup.subsys[]. And ->vfork_done.
  1644. *
  1645. * Nests both inside and outside of read_lock(&tasklist_lock).
  1646. * It must not be nested with write_lock_irq(&tasklist_lock),
  1647. * neither inside nor outside.
  1648. */
  1649. static inline void task_lock(struct task_struct *p)
  1650. {
  1651. spin_lock(&p->alloc_lock);
  1652. }
  1653. static inline void task_unlock(struct task_struct *p)
  1654. {
  1655. spin_unlock(&p->alloc_lock);
  1656. }
  1657. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1658. static inline struct thread_info *task_thread_info(struct task_struct *task)
  1659. {
  1660. return &task->thread_info;
  1661. }
  1662. /*
  1663. * When accessing the stack of a non-current task that might exit, use
  1664. * try_get_task_stack() instead. task_stack_page will return a pointer
  1665. * that could get freed out from under you.
  1666. */
  1667. static inline void *task_stack_page(const struct task_struct *task)
  1668. {
  1669. return task->stack;
  1670. }
  1671. #define setup_thread_stack(new,old) do { } while(0)
  1672. static inline unsigned long *end_of_stack(const struct task_struct *task)
  1673. {
  1674. return task->stack;
  1675. }
  1676. #elif !defined(__HAVE_THREAD_FUNCTIONS)
  1677. #define task_thread_info(task) ((struct thread_info *)(task)->stack)
  1678. #define task_stack_page(task) ((void *)(task)->stack)
  1679. static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
  1680. {
  1681. *task_thread_info(p) = *task_thread_info(org);
  1682. task_thread_info(p)->task = p;
  1683. }
  1684. /*
  1685. * Return the address of the last usable long on the stack.
  1686. *
  1687. * When the stack grows down, this is just above the thread
  1688. * info struct. Going any lower will corrupt the threadinfo.
  1689. *
  1690. * When the stack grows up, this is the highest address.
  1691. * Beyond that position, we corrupt data on the next page.
  1692. */
  1693. static inline unsigned long *end_of_stack(struct task_struct *p)
  1694. {
  1695. #ifdef CONFIG_STACK_GROWSUP
  1696. return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
  1697. #else
  1698. return (unsigned long *)(task_thread_info(p) + 1);
  1699. #endif
  1700. }
  1701. #endif
  1702. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1703. static inline void *try_get_task_stack(struct task_struct *tsk)
  1704. {
  1705. return atomic_inc_not_zero(&tsk->stack_refcount) ?
  1706. task_stack_page(tsk) : NULL;
  1707. }
  1708. extern void put_task_stack(struct task_struct *tsk);
  1709. #else
  1710. static inline void *try_get_task_stack(struct task_struct *tsk)
  1711. {
  1712. return task_stack_page(tsk);
  1713. }
  1714. static inline void put_task_stack(struct task_struct *tsk) {}
  1715. #endif
  1716. #define task_stack_end_corrupted(task) \
  1717. (*(end_of_stack(task)) != STACK_END_MAGIC)
  1718. static inline int object_is_on_stack(void *obj)
  1719. {
  1720. void *stack = task_stack_page(current);
  1721. return (obj >= stack) && (obj < (stack + THREAD_SIZE));
  1722. }
  1723. extern void thread_stack_cache_init(void);
  1724. #ifdef CONFIG_DEBUG_STACK_USAGE
  1725. static inline unsigned long stack_not_used(struct task_struct *p)
  1726. {
  1727. unsigned long *n = end_of_stack(p);
  1728. do { /* Skip over canary */
  1729. # ifdef CONFIG_STACK_GROWSUP
  1730. n--;
  1731. # else
  1732. n++;
  1733. # endif
  1734. } while (!*n);
  1735. # ifdef CONFIG_STACK_GROWSUP
  1736. return (unsigned long)end_of_stack(p) - (unsigned long)n;
  1737. # else
  1738. return (unsigned long)n - (unsigned long)end_of_stack(p);
  1739. # endif
  1740. }
  1741. #endif
  1742. extern void set_task_stack_end_magic(struct task_struct *tsk);
  1743. /* set thread flags in other task's structures
  1744. * - see asm/thread_info.h for TIF_xxxx flags available
  1745. */
  1746. static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1747. {
  1748. set_ti_thread_flag(task_thread_info(tsk), flag);
  1749. }
  1750. static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1751. {
  1752. clear_ti_thread_flag(task_thread_info(tsk), flag);
  1753. }
  1754. static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1755. {
  1756. return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
  1757. }
  1758. static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1759. {
  1760. return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
  1761. }
  1762. static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
  1763. {
  1764. return test_ti_thread_flag(task_thread_info(tsk), flag);
  1765. }
  1766. static inline void set_tsk_need_resched(struct task_struct *tsk)
  1767. {
  1768. set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1769. }
  1770. static inline void clear_tsk_need_resched(struct task_struct *tsk)
  1771. {
  1772. clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1773. }
  1774. static inline int test_tsk_need_resched(struct task_struct *tsk)
  1775. {
  1776. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  1777. }
  1778. static inline int restart_syscall(void)
  1779. {
  1780. set_tsk_thread_flag(current, TIF_SIGPENDING);
  1781. return -ERESTARTNOINTR;
  1782. }
  1783. static inline int signal_pending(struct task_struct *p)
  1784. {
  1785. return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
  1786. }
  1787. static inline int __fatal_signal_pending(struct task_struct *p)
  1788. {
  1789. return unlikely(sigismember(&p->pending.signal, SIGKILL));
  1790. }
  1791. static inline int fatal_signal_pending(struct task_struct *p)
  1792. {
  1793. return signal_pending(p) && __fatal_signal_pending(p);
  1794. }
  1795. static inline int signal_pending_state(long state, struct task_struct *p)
  1796. {
  1797. if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
  1798. return 0;
  1799. if (!signal_pending(p))
  1800. return 0;
  1801. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  1802. }
  1803. /*
  1804. * cond_resched() and cond_resched_lock(): latency reduction via
  1805. * explicit rescheduling in places that are safe. The return
  1806. * value indicates whether a reschedule was done in fact.
  1807. * cond_resched_lock() will drop the spinlock before scheduling,
  1808. * cond_resched_softirq() will enable bhs before scheduling.
  1809. */
  1810. #ifndef CONFIG_PREEMPT
  1811. extern int _cond_resched(void);
  1812. #else
  1813. static inline int _cond_resched(void) { return 0; }
  1814. #endif
  1815. #define cond_resched() ({ \
  1816. ___might_sleep(__FILE__, __LINE__, 0); \
  1817. _cond_resched(); \
  1818. })
  1819. extern int __cond_resched_lock(spinlock_t *lock);
  1820. #define cond_resched_lock(lock) ({ \
  1821. ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
  1822. __cond_resched_lock(lock); \
  1823. })
  1824. extern int __cond_resched_softirq(void);
  1825. #define cond_resched_softirq() ({ \
  1826. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  1827. __cond_resched_softirq(); \
  1828. })
  1829. static inline void cond_resched_rcu(void)
  1830. {
  1831. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
  1832. rcu_read_unlock();
  1833. cond_resched();
  1834. rcu_read_lock();
  1835. #endif
  1836. }
  1837. /*
  1838. * Does a critical section need to be broken due to another
  1839. * task waiting?: (technically does not depend on CONFIG_PREEMPT,
  1840. * but a general need for low latency)
  1841. */
  1842. static inline int spin_needbreak(spinlock_t *lock)
  1843. {
  1844. #ifdef CONFIG_PREEMPT
  1845. return spin_is_contended(lock);
  1846. #else
  1847. return 0;
  1848. #endif
  1849. }
  1850. static __always_inline bool need_resched(void)
  1851. {
  1852. return unlikely(tif_need_resched());
  1853. }
  1854. /*
  1855. * Thread group CPU time accounting.
  1856. */
  1857. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
  1858. void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
  1859. /*
  1860. * Reevaluate whether the task has signals pending delivery.
  1861. * Wake the task if so.
  1862. * This is required every time the blocked sigset_t changes.
  1863. * callers must hold sighand->siglock.
  1864. */
  1865. extern void recalc_sigpending_and_wake(struct task_struct *t);
  1866. extern void recalc_sigpending(void);
  1867. extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
  1868. static inline void signal_wake_up(struct task_struct *t, bool resume)
  1869. {
  1870. signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
  1871. }
  1872. static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
  1873. {
  1874. signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
  1875. }
  1876. /*
  1877. * Wrappers for p->thread_info->cpu access. No-op on UP.
  1878. */
  1879. #ifdef CONFIG_SMP
  1880. static inline unsigned int task_cpu(const struct task_struct *p)
  1881. {
  1882. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1883. return p->cpu;
  1884. #else
  1885. return task_thread_info(p)->cpu;
  1886. #endif
  1887. }
  1888. static inline int task_node(const struct task_struct *p)
  1889. {
  1890. return cpu_to_node(task_cpu(p));
  1891. }
  1892. extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
  1893. #else
  1894. static inline unsigned int task_cpu(const struct task_struct *p)
  1895. {
  1896. return 0;
  1897. }
  1898. static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  1899. {
  1900. }
  1901. #endif /* CONFIG_SMP */
  1902. /*
  1903. * In order to reduce various lock holder preemption latencies provide an
  1904. * interface to see if a vCPU is currently running or not.
  1905. *
  1906. * This allows us to terminate optimistic spin loops and block, analogous to
  1907. * the native optimistic spin heuristic of testing if the lock owner task is
  1908. * running or not.
  1909. */
  1910. #ifndef vcpu_is_preempted
  1911. # define vcpu_is_preempted(cpu) false
  1912. #endif
  1913. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  1914. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  1915. #ifdef CONFIG_CGROUP_SCHED
  1916. extern struct task_group root_task_group;
  1917. #endif /* CONFIG_CGROUP_SCHED */
  1918. extern int task_can_switch_user(struct user_struct *up,
  1919. struct task_struct *tsk);
  1920. #ifdef CONFIG_TASK_XACCT
  1921. static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
  1922. {
  1923. tsk->ioac.rchar += amt;
  1924. }
  1925. static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
  1926. {
  1927. tsk->ioac.wchar += amt;
  1928. }
  1929. static inline void inc_syscr(struct task_struct *tsk)
  1930. {
  1931. tsk->ioac.syscr++;
  1932. }
  1933. static inline void inc_syscw(struct task_struct *tsk)
  1934. {
  1935. tsk->ioac.syscw++;
  1936. }
  1937. #else
  1938. static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
  1939. {
  1940. }
  1941. static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
  1942. {
  1943. }
  1944. static inline void inc_syscr(struct task_struct *tsk)
  1945. {
  1946. }
  1947. static inline void inc_syscw(struct task_struct *tsk)
  1948. {
  1949. }
  1950. #endif
  1951. #ifndef TASK_SIZE_OF
  1952. #define TASK_SIZE_OF(tsk) TASK_SIZE
  1953. #endif
  1954. #endif