sched.h 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105
  1. #ifndef _LINUX_SCHED_H
  2. #define _LINUX_SCHED_H
  3. #include <uapi/linux/sched.h>
  4. #include <linux/sched/prio.h>
  5. struct sched_param {
  6. int sched_priority;
  7. };
  8. #include <asm/param.h> /* for HZ */
  9. #include <linux/capability.h>
  10. #include <linux/threads.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/timex.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/plist.h>
  16. #include <linux/rbtree.h>
  17. #include <linux/thread_info.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/errno.h>
  20. #include <linux/nodemask.h>
  21. #include <linux/mm_types.h>
  22. #include <linux/preempt_mask.h>
  23. #include <asm/page.h>
  24. #include <asm/ptrace.h>
  25. #include <linux/cputime.h>
  26. #include <linux/smp.h>
  27. #include <linux/sem.h>
  28. #include <linux/shm.h>
  29. #include <linux/signal.h>
  30. #include <linux/compiler.h>
  31. #include <linux/completion.h>
  32. #include <linux/pid.h>
  33. #include <linux/percpu.h>
  34. #include <linux/topology.h>
  35. #include <linux/proportions.h>
  36. #include <linux/seccomp.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/rculist.h>
  39. #include <linux/rtmutex.h>
  40. #include <linux/time.h>
  41. #include <linux/param.h>
  42. #include <linux/resource.h>
  43. #include <linux/timer.h>
  44. #include <linux/hrtimer.h>
  45. #include <linux/task_io_accounting.h>
  46. #include <linux/latencytop.h>
  47. #include <linux/cred.h>
  48. #include <linux/llist.h>
  49. #include <linux/uidgid.h>
  50. #include <linux/gfp.h>
  51. #include <linux/magic.h>
  52. #include <asm/processor.h>
  53. #define SCHED_ATTR_SIZE_VER0 48 /* sizeof first published struct */
  54. /*
  55. * Extended scheduling parameters data structure.
  56. *
  57. * This is needed because the original struct sched_param can not be
  58. * altered without introducing ABI issues with legacy applications
  59. * (e.g., in sched_getparam()).
  60. *
  61. * However, the possibility of specifying more than just a priority for
  62. * the tasks may be useful for a wide variety of application fields, e.g.,
  63. * multimedia, streaming, automation and control, and many others.
  64. *
  65. * This variant (sched_attr) is meant at describing a so-called
  66. * sporadic time-constrained task. In such model a task is specified by:
  67. * - the activation period or minimum instance inter-arrival time;
  68. * - the maximum (or average, depending on the actual scheduling
  69. * discipline) computation time of all instances, a.k.a. runtime;
  70. * - the deadline (relative to the actual activation time) of each
  71. * instance.
  72. * Very briefly, a periodic (sporadic) task asks for the execution of
  73. * some specific computation --which is typically called an instance--
  74. * (at most) every period. Moreover, each instance typically lasts no more
  75. * than the runtime and must be completed by time instant t equal to
  76. * the instance activation time + the deadline.
  77. *
  78. * This is reflected by the actual fields of the sched_attr structure:
  79. *
  80. * @size size of the structure, for fwd/bwd compat.
  81. *
  82. * @sched_policy task's scheduling policy
  83. * @sched_flags for customizing the scheduler behaviour
  84. * @sched_nice task's nice value (SCHED_NORMAL/BATCH)
  85. * @sched_priority task's static priority (SCHED_FIFO/RR)
  86. * @sched_deadline representative of the task's deadline
  87. * @sched_runtime representative of the task's runtime
  88. * @sched_period representative of the task's period
  89. *
  90. * Given this task model, there are a multiplicity of scheduling algorithms
  91. * and policies, that can be used to ensure all the tasks will make their
  92. * timing constraints.
  93. *
  94. * As of now, the SCHED_DEADLINE policy (sched_dl scheduling class) is the
  95. * only user of this new interface. More information about the algorithm
  96. * available in the scheduling class file or in Documentation/.
  97. */
  98. struct sched_attr {
  99. u32 size;
  100. u32 sched_policy;
  101. u64 sched_flags;
  102. /* SCHED_NORMAL, SCHED_BATCH */
  103. s32 sched_nice;
  104. /* SCHED_FIFO, SCHED_RR */
  105. u32 sched_priority;
  106. /* SCHED_DEADLINE */
  107. u64 sched_runtime;
  108. u64 sched_deadline;
  109. u64 sched_period;
  110. };
  111. struct futex_pi_state;
  112. struct robust_list_head;
  113. struct bio_list;
  114. struct fs_struct;
  115. struct perf_event_context;
  116. struct blk_plug;
  117. struct filename;
  118. #define VMACACHE_BITS 2
  119. #define VMACACHE_SIZE (1U << VMACACHE_BITS)
  120. #define VMACACHE_MASK (VMACACHE_SIZE - 1)
  121. /*
  122. * These are the constant used to fake the fixed-point load-average
  123. * counting. Some notes:
  124. * - 11 bit fractions expand to 22 bits by the multiplies: this gives
  125. * a load-average precision of 10 bits integer + 11 bits fractional
  126. * - if you want to count load-averages more often, you need more
  127. * precision, or rounding will get you. With 2-second counting freq,
  128. * the EXP_n values would be 1981, 2034 and 2043 if still using only
  129. * 11 bit fractions.
  130. */
  131. extern unsigned long avenrun[]; /* Load averages */
  132. extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
  133. #define FSHIFT 11 /* nr of bits of precision */
  134. #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
  135. #define LOAD_FREQ (5*HZ+1) /* 5 sec intervals */
  136. #define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
  137. #define EXP_5 2014 /* 1/exp(5sec/5min) */
  138. #define EXP_15 2037 /* 1/exp(5sec/15min) */
  139. #define CALC_LOAD(load,exp,n) \
  140. load *= exp; \
  141. load += n*(FIXED_1-exp); \
  142. load >>= FSHIFT;
  143. extern unsigned long total_forks;
  144. extern int nr_threads;
  145. DECLARE_PER_CPU(unsigned long, process_counts);
  146. extern int nr_processes(void);
  147. extern unsigned long nr_running(void);
  148. extern bool single_task_running(void);
  149. extern unsigned long nr_iowait(void);
  150. extern unsigned long nr_iowait_cpu(int cpu);
  151. extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
  152. extern void calc_global_load(unsigned long ticks);
  153. extern void update_cpu_load_nohz(void);
  154. extern unsigned long get_parent_ip(unsigned long addr);
  155. extern void dump_cpu_task(int cpu);
  156. struct seq_file;
  157. struct cfs_rq;
  158. struct task_group;
  159. #ifdef CONFIG_SCHED_DEBUG
  160. extern void proc_sched_show_task(struct task_struct *p, struct seq_file *m);
  161. extern void proc_sched_set_task(struct task_struct *p);
  162. extern void
  163. print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
  164. #endif
  165. /*
  166. * Task state bitmask. NOTE! These bits are also
  167. * encoded in fs/proc/array.c: get_task_state().
  168. *
  169. * We have two separate sets of flags: task->state
  170. * is about runnability, while task->exit_state are
  171. * about the task exiting. Confusing, but this way
  172. * modifying one set can't modify the other one by
  173. * mistake.
  174. */
  175. #define TASK_RUNNING 0
  176. #define TASK_INTERRUPTIBLE 1
  177. #define TASK_UNINTERRUPTIBLE 2
  178. #define __TASK_STOPPED 4
  179. #define __TASK_TRACED 8
  180. /* in tsk->exit_state */
  181. #define EXIT_DEAD 16
  182. #define EXIT_ZOMBIE 32
  183. #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
  184. /* in tsk->state again */
  185. #define TASK_DEAD 64
  186. #define TASK_WAKEKILL 128
  187. #define TASK_WAKING 256
  188. #define TASK_PARKED 512
  189. #define TASK_STATE_MAX 1024
  190. #define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWP"
  191. extern char ___assert_task_state[1 - 2*!!(
  192. sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
  193. /* Convenience macros for the sake of set_task_state */
  194. #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
  195. #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
  196. #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
  197. /* Convenience macros for the sake of wake_up */
  198. #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
  199. #define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
  200. /* get_task_state() */
  201. #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
  202. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  203. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  204. #define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  205. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  206. #define task_is_stopped_or_traced(task) \
  207. ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  208. #define task_contributes_to_load(task) \
  209. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  210. (task->flags & PF_FROZEN) == 0)
  211. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  212. #define __set_task_state(tsk, state_value) \
  213. do { \
  214. (tsk)->task_state_change = _THIS_IP_; \
  215. (tsk)->state = (state_value); \
  216. } while (0)
  217. #define set_task_state(tsk, state_value) \
  218. do { \
  219. (tsk)->task_state_change = _THIS_IP_; \
  220. set_mb((tsk)->state, (state_value)); \
  221. } while (0)
  222. /*
  223. * set_current_state() includes a barrier so that the write of current->state
  224. * is correctly serialised wrt the caller's subsequent test of whether to
  225. * actually sleep:
  226. *
  227. * set_current_state(TASK_UNINTERRUPTIBLE);
  228. * if (do_i_need_to_sleep())
  229. * schedule();
  230. *
  231. * If the caller does not need such serialisation then use __set_current_state()
  232. */
  233. #define __set_current_state(state_value) \
  234. do { \
  235. current->task_state_change = _THIS_IP_; \
  236. current->state = (state_value); \
  237. } while (0)
  238. #define set_current_state(state_value) \
  239. do { \
  240. current->task_state_change = _THIS_IP_; \
  241. set_mb(current->state, (state_value)); \
  242. } while (0)
  243. #else
  244. #define __set_task_state(tsk, state_value) \
  245. do { (tsk)->state = (state_value); } while (0)
  246. #define set_task_state(tsk, state_value) \
  247. set_mb((tsk)->state, (state_value))
  248. /*
  249. * set_current_state() includes a barrier so that the write of current->state
  250. * is correctly serialised wrt the caller's subsequent test of whether to
  251. * actually sleep:
  252. *
  253. * set_current_state(TASK_UNINTERRUPTIBLE);
  254. * if (do_i_need_to_sleep())
  255. * schedule();
  256. *
  257. * If the caller does not need such serialisation then use __set_current_state()
  258. */
  259. #define __set_current_state(state_value) \
  260. do { current->state = (state_value); } while (0)
  261. #define set_current_state(state_value) \
  262. set_mb(current->state, (state_value))
  263. #endif
  264. /* Task command name length */
  265. #define TASK_COMM_LEN 16
  266. #include <linux/spinlock.h>
  267. /*
  268. * This serializes "schedule()" and also protects
  269. * the run-queue from deletions/modifications (but
  270. * _adding_ to the beginning of the run-queue has
  271. * a separate lock).
  272. */
  273. extern rwlock_t tasklist_lock;
  274. extern spinlock_t mmlist_lock;
  275. struct task_struct;
  276. #ifdef CONFIG_PROVE_RCU
  277. extern int lockdep_tasklist_lock_is_held(void);
  278. #endif /* #ifdef CONFIG_PROVE_RCU */
  279. extern void sched_init(void);
  280. extern void sched_init_smp(void);
  281. extern asmlinkage void schedule_tail(struct task_struct *prev);
  282. extern void init_idle(struct task_struct *idle, int cpu);
  283. extern void init_idle_bootup_task(struct task_struct *idle);
  284. extern cpumask_var_t cpu_isolated_map;
  285. extern int runqueue_is_locked(int cpu);
  286. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  287. extern void nohz_balance_enter_idle(int cpu);
  288. extern void set_cpu_sd_state_idle(void);
  289. extern int get_nohz_timer_target(int pinned);
  290. #else
  291. static inline void nohz_balance_enter_idle(int cpu) { }
  292. static inline void set_cpu_sd_state_idle(void) { }
  293. static inline int get_nohz_timer_target(int pinned)
  294. {
  295. return smp_processor_id();
  296. }
  297. #endif
  298. /*
  299. * Only dump TASK_* tasks. (0 for all tasks)
  300. */
  301. extern void show_state_filter(unsigned long state_filter);
  302. static inline void show_state(void)
  303. {
  304. show_state_filter(0);
  305. }
  306. extern void show_regs(struct pt_regs *);
  307. /*
  308. * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
  309. * task), SP is the stack pointer of the first frame that should be shown in the back
  310. * trace (or NULL if the entire call-chain of the task should be shown).
  311. */
  312. extern void show_stack(struct task_struct *task, unsigned long *sp);
  313. extern void cpu_init (void);
  314. extern void trap_init(void);
  315. extern void update_process_times(int user);
  316. extern void scheduler_tick(void);
  317. extern void sched_show_task(struct task_struct *p);
  318. #ifdef CONFIG_LOCKUP_DETECTOR
  319. extern void touch_softlockup_watchdog(void);
  320. extern void touch_softlockup_watchdog_sync(void);
  321. extern void touch_all_softlockup_watchdogs(void);
  322. extern int proc_dowatchdog_thresh(struct ctl_table *table, int write,
  323. void __user *buffer,
  324. size_t *lenp, loff_t *ppos);
  325. extern unsigned int softlockup_panic;
  326. void lockup_detector_init(void);
  327. #else
  328. static inline void touch_softlockup_watchdog(void)
  329. {
  330. }
  331. static inline void touch_softlockup_watchdog_sync(void)
  332. {
  333. }
  334. static inline void touch_all_softlockup_watchdogs(void)
  335. {
  336. }
  337. static inline void lockup_detector_init(void)
  338. {
  339. }
  340. #endif
  341. #ifdef CONFIG_DETECT_HUNG_TASK
  342. void reset_hung_task_detector(void);
  343. #else
  344. static inline void reset_hung_task_detector(void)
  345. {
  346. }
  347. #endif
  348. /* Attach to any functions which should be ignored in wchan output. */
  349. #define __sched __attribute__((__section__(".sched.text")))
  350. /* Linker adds these: start and end of __sched functions */
  351. extern char __sched_text_start[], __sched_text_end[];
  352. /* Is this address in the __sched functions? */
  353. extern int in_sched_functions(unsigned long addr);
  354. #define MAX_SCHEDULE_TIMEOUT LONG_MAX
  355. extern signed long schedule_timeout(signed long timeout);
  356. extern signed long schedule_timeout_interruptible(signed long timeout);
  357. extern signed long schedule_timeout_killable(signed long timeout);
  358. extern signed long schedule_timeout_uninterruptible(signed long timeout);
  359. asmlinkage void schedule(void);
  360. extern void schedule_preempt_disabled(void);
  361. extern long io_schedule_timeout(long timeout);
  362. static inline void io_schedule(void)
  363. {
  364. io_schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  365. }
  366. struct nsproxy;
  367. struct user_namespace;
  368. #ifdef CONFIG_MMU
  369. extern void arch_pick_mmap_layout(struct mm_struct *mm);
  370. extern unsigned long
  371. arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
  372. unsigned long, unsigned long);
  373. extern unsigned long
  374. arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
  375. unsigned long len, unsigned long pgoff,
  376. unsigned long flags);
  377. #else
  378. static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
  379. #endif
  380. #define SUID_DUMP_DISABLE 0 /* No setuid dumping */
  381. #define SUID_DUMP_USER 1 /* Dump as user of process */
  382. #define SUID_DUMP_ROOT 2 /* Dump as root */
  383. /* mm flags */
  384. /* for SUID_DUMP_* above */
  385. #define MMF_DUMPABLE_BITS 2
  386. #define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
  387. extern void set_dumpable(struct mm_struct *mm, int value);
  388. /*
  389. * This returns the actual value of the suid_dumpable flag. For things
  390. * that are using this for checking for privilege transitions, it must
  391. * test against SUID_DUMP_USER rather than treating it as a boolean
  392. * value.
  393. */
  394. static inline int __get_dumpable(unsigned long mm_flags)
  395. {
  396. return mm_flags & MMF_DUMPABLE_MASK;
  397. }
  398. static inline int get_dumpable(struct mm_struct *mm)
  399. {
  400. return __get_dumpable(mm->flags);
  401. }
  402. /* coredump filter bits */
  403. #define MMF_DUMP_ANON_PRIVATE 2
  404. #define MMF_DUMP_ANON_SHARED 3
  405. #define MMF_DUMP_MAPPED_PRIVATE 4
  406. #define MMF_DUMP_MAPPED_SHARED 5
  407. #define MMF_DUMP_ELF_HEADERS 6
  408. #define MMF_DUMP_HUGETLB_PRIVATE 7
  409. #define MMF_DUMP_HUGETLB_SHARED 8
  410. #define MMF_DUMP_FILTER_SHIFT MMF_DUMPABLE_BITS
  411. #define MMF_DUMP_FILTER_BITS 7
  412. #define MMF_DUMP_FILTER_MASK \
  413. (((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
  414. #define MMF_DUMP_FILTER_DEFAULT \
  415. ((1 << MMF_DUMP_ANON_PRIVATE) | (1 << MMF_DUMP_ANON_SHARED) |\
  416. (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
  417. #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
  418. # define MMF_DUMP_MASK_DEFAULT_ELF (1 << MMF_DUMP_ELF_HEADERS)
  419. #else
  420. # define MMF_DUMP_MASK_DEFAULT_ELF 0
  421. #endif
  422. /* leave room for more dump flags */
  423. #define MMF_VM_MERGEABLE 16 /* KSM may merge identical pages */
  424. #define MMF_VM_HUGEPAGE 17 /* set when VM_HUGEPAGE is set on vma */
  425. #define MMF_EXE_FILE_CHANGED 18 /* see prctl_set_mm_exe_file() */
  426. #define MMF_HAS_UPROBES 19 /* has uprobes */
  427. #define MMF_RECALC_UPROBES 20 /* MMF_HAS_UPROBES can be wrong */
  428. #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK)
  429. struct sighand_struct {
  430. atomic_t count;
  431. struct k_sigaction action[_NSIG];
  432. spinlock_t siglock;
  433. wait_queue_head_t signalfd_wqh;
  434. };
  435. struct pacct_struct {
  436. int ac_flag;
  437. long ac_exitcode;
  438. unsigned long ac_mem;
  439. cputime_t ac_utime, ac_stime;
  440. unsigned long ac_minflt, ac_majflt;
  441. };
  442. struct cpu_itimer {
  443. cputime_t expires;
  444. cputime_t incr;
  445. u32 error;
  446. u32 incr_error;
  447. };
  448. /**
  449. * struct cputime - snaphsot of system and user cputime
  450. * @utime: time spent in user mode
  451. * @stime: time spent in system mode
  452. *
  453. * Gathers a generic snapshot of user and system time.
  454. */
  455. struct cputime {
  456. cputime_t utime;
  457. cputime_t stime;
  458. };
  459. /**
  460. * struct task_cputime - collected CPU time counts
  461. * @utime: time spent in user mode, in &cputime_t units
  462. * @stime: time spent in kernel mode, in &cputime_t units
  463. * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
  464. *
  465. * This is an extension of struct cputime that includes the total runtime
  466. * spent by the task from the scheduler point of view.
  467. *
  468. * As a result, this structure groups together three kinds of CPU time
  469. * that are tracked for threads and thread groups. Most things considering
  470. * CPU time want to group these counts together and treat all three
  471. * of them in parallel.
  472. */
  473. struct task_cputime {
  474. cputime_t utime;
  475. cputime_t stime;
  476. unsigned long long sum_exec_runtime;
  477. };
  478. /* Alternate field names when used to cache expirations. */
  479. #define prof_exp stime
  480. #define virt_exp utime
  481. #define sched_exp sum_exec_runtime
  482. #define INIT_CPUTIME \
  483. (struct task_cputime) { \
  484. .utime = 0, \
  485. .stime = 0, \
  486. .sum_exec_runtime = 0, \
  487. }
  488. #ifdef CONFIG_PREEMPT_COUNT
  489. #define PREEMPT_DISABLED (1 + PREEMPT_ENABLED)
  490. #else
  491. #define PREEMPT_DISABLED PREEMPT_ENABLED
  492. #endif
  493. /*
  494. * Disable preemption until the scheduler is running.
  495. * Reset by start_kernel()->sched_init()->init_idle().
  496. *
  497. * We include PREEMPT_ACTIVE to avoid cond_resched() from working
  498. * before the scheduler is active -- see should_resched().
  499. */
  500. #define INIT_PREEMPT_COUNT (PREEMPT_DISABLED + PREEMPT_ACTIVE)
  501. /**
  502. * struct thread_group_cputimer - thread group interval timer counts
  503. * @cputime: thread group interval timers.
  504. * @running: non-zero when there are timers running and
  505. * @cputime receives updates.
  506. * @lock: lock for fields in this struct.
  507. *
  508. * This structure contains the version of task_cputime, above, that is
  509. * used for thread group CPU timer calculations.
  510. */
  511. struct thread_group_cputimer {
  512. struct task_cputime cputime;
  513. int running;
  514. raw_spinlock_t lock;
  515. };
  516. #include <linux/rwsem.h>
  517. struct autogroup;
  518. /*
  519. * NOTE! "signal_struct" does not have its own
  520. * locking, because a shared signal_struct always
  521. * implies a shared sighand_struct, so locking
  522. * sighand_struct is always a proper superset of
  523. * the locking of signal_struct.
  524. */
  525. struct signal_struct {
  526. atomic_t sigcnt;
  527. atomic_t live;
  528. int nr_threads;
  529. struct list_head thread_head;
  530. wait_queue_head_t wait_chldexit; /* for wait4() */
  531. /* current thread group signal load-balancing target: */
  532. struct task_struct *curr_target;
  533. /* shared signal handling: */
  534. struct sigpending shared_pending;
  535. /* thread group exit support */
  536. int group_exit_code;
  537. /* overloaded:
  538. * - notify group_exit_task when ->count is equal to notify_count
  539. * - everyone except group_exit_task is stopped during signal delivery
  540. * of fatal signals, group_exit_task processes the signal.
  541. */
  542. int notify_count;
  543. struct task_struct *group_exit_task;
  544. /* thread group stop support, overloads group_exit_code too */
  545. int group_stop_count;
  546. unsigned int flags; /* see SIGNAL_* flags below */
  547. /*
  548. * PR_SET_CHILD_SUBREAPER marks a process, like a service
  549. * manager, to re-parent orphan (double-forking) child processes
  550. * to this process instead of 'init'. The service manager is
  551. * able to receive SIGCHLD signals and is able to investigate
  552. * the process until it calls wait(). All children of this
  553. * process will inherit a flag if they should look for a
  554. * child_subreaper process at exit.
  555. */
  556. unsigned int is_child_subreaper:1;
  557. unsigned int has_child_subreaper:1;
  558. /* POSIX.1b Interval Timers */
  559. int posix_timer_id;
  560. struct list_head posix_timers;
  561. /* ITIMER_REAL timer for the process */
  562. struct hrtimer real_timer;
  563. struct pid *leader_pid;
  564. ktime_t it_real_incr;
  565. /*
  566. * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
  567. * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
  568. * values are defined to 0 and 1 respectively
  569. */
  570. struct cpu_itimer it[2];
  571. /*
  572. * Thread group totals for process CPU timers.
  573. * See thread_group_cputimer(), et al, for details.
  574. */
  575. struct thread_group_cputimer cputimer;
  576. /* Earliest-expiration cache. */
  577. struct task_cputime cputime_expires;
  578. struct list_head cpu_timers[3];
  579. struct pid *tty_old_pgrp;
  580. /* boolean value for session group leader */
  581. int leader;
  582. struct tty_struct *tty; /* NULL if no tty */
  583. #ifdef CONFIG_SCHED_AUTOGROUP
  584. struct autogroup *autogroup;
  585. #endif
  586. /*
  587. * Cumulative resource counters for dead threads in the group,
  588. * and for reaped dead child processes forked by this group.
  589. * Live threads maintain their own counters and add to these
  590. * in __exit_signal, except for the group leader.
  591. */
  592. seqlock_t stats_lock;
  593. cputime_t utime, stime, cutime, cstime;
  594. cputime_t gtime;
  595. cputime_t cgtime;
  596. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  597. struct cputime prev_cputime;
  598. #endif
  599. unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
  600. unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
  601. unsigned long inblock, oublock, cinblock, coublock;
  602. unsigned long maxrss, cmaxrss;
  603. struct task_io_accounting ioac;
  604. /*
  605. * Cumulative ns of schedule CPU time fo dead threads in the
  606. * group, not including a zombie group leader, (This only differs
  607. * from jiffies_to_ns(utime + stime) if sched_clock uses something
  608. * other than jiffies.)
  609. */
  610. unsigned long long sum_sched_runtime;
  611. /*
  612. * We don't bother to synchronize most readers of this at all,
  613. * because there is no reader checking a limit that actually needs
  614. * to get both rlim_cur and rlim_max atomically, and either one
  615. * alone is a single word that can safely be read normally.
  616. * getrlimit/setrlimit use task_lock(current->group_leader) to
  617. * protect this instead of the siglock, because they really
  618. * have no need to disable irqs.
  619. */
  620. struct rlimit rlim[RLIM_NLIMITS];
  621. #ifdef CONFIG_BSD_PROCESS_ACCT
  622. struct pacct_struct pacct; /* per-process accounting information */
  623. #endif
  624. #ifdef CONFIG_TASKSTATS
  625. struct taskstats *stats;
  626. #endif
  627. #ifdef CONFIG_AUDIT
  628. unsigned audit_tty;
  629. unsigned audit_tty_log_passwd;
  630. struct tty_audit_buf *tty_audit_buf;
  631. #endif
  632. #ifdef CONFIG_CGROUPS
  633. /*
  634. * group_rwsem prevents new tasks from entering the threadgroup and
  635. * member tasks from exiting,a more specifically, setting of
  636. * PF_EXITING. fork and exit paths are protected with this rwsem
  637. * using threadgroup_change_begin/end(). Users which require
  638. * threadgroup to remain stable should use threadgroup_[un]lock()
  639. * which also takes care of exec path. Currently, cgroup is the
  640. * only user.
  641. */
  642. struct rw_semaphore group_rwsem;
  643. #endif
  644. oom_flags_t oom_flags;
  645. short oom_score_adj; /* OOM kill score adjustment */
  646. short oom_score_adj_min; /* OOM kill score adjustment min value.
  647. * Only settable by CAP_SYS_RESOURCE. */
  648. struct mutex cred_guard_mutex; /* guard against foreign influences on
  649. * credential calculations
  650. * (notably. ptrace) */
  651. };
  652. /*
  653. * Bits in flags field of signal_struct.
  654. */
  655. #define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */
  656. #define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */
  657. #define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */
  658. #define SIGNAL_GROUP_COREDUMP 0x00000008 /* coredump in progress */
  659. /*
  660. * Pending notifications to parent.
  661. */
  662. #define SIGNAL_CLD_STOPPED 0x00000010
  663. #define SIGNAL_CLD_CONTINUED 0x00000020
  664. #define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
  665. #define SIGNAL_UNKILLABLE 0x00000040 /* for init: ignore fatal signals */
  666. /* If true, all threads except ->group_exit_task have pending SIGKILL */
  667. static inline int signal_group_exit(const struct signal_struct *sig)
  668. {
  669. return (sig->flags & SIGNAL_GROUP_EXIT) ||
  670. (sig->group_exit_task != NULL);
  671. }
  672. /*
  673. * Some day this will be a full-fledged user tracking system..
  674. */
  675. struct user_struct {
  676. atomic_t __count; /* reference count */
  677. atomic_t processes; /* How many processes does this user have? */
  678. atomic_t sigpending; /* How many pending signals does this user have? */
  679. #ifdef CONFIG_INOTIFY_USER
  680. atomic_t inotify_watches; /* How many inotify watches does this user have? */
  681. atomic_t inotify_devs; /* How many inotify devs does this user have opened? */
  682. #endif
  683. #ifdef CONFIG_FANOTIFY
  684. atomic_t fanotify_listeners;
  685. #endif
  686. #ifdef CONFIG_EPOLL
  687. atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
  688. #endif
  689. #ifdef CONFIG_POSIX_MQUEUE
  690. /* protected by mq_lock */
  691. unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
  692. #endif
  693. unsigned long locked_shm; /* How many pages of mlocked shm ? */
  694. #ifdef CONFIG_KEYS
  695. struct key *uid_keyring; /* UID specific keyring */
  696. struct key *session_keyring; /* UID's default session keyring */
  697. #endif
  698. /* Hash table maintenance information */
  699. struct hlist_node uidhash_node;
  700. kuid_t uid;
  701. #ifdef CONFIG_PERF_EVENTS
  702. atomic_long_t locked_vm;
  703. #endif
  704. };
  705. extern int uids_sysfs_init(void);
  706. extern struct user_struct *find_user(kuid_t);
  707. extern struct user_struct root_user;
  708. #define INIT_USER (&root_user)
  709. struct backing_dev_info;
  710. struct reclaim_state;
  711. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  712. struct sched_info {
  713. /* cumulative counters */
  714. unsigned long pcount; /* # of times run on this cpu */
  715. unsigned long long run_delay; /* time spent waiting on a runqueue */
  716. /* timestamps */
  717. unsigned long long last_arrival,/* when we last ran on a cpu */
  718. last_queued; /* when we were last queued to run */
  719. };
  720. #endif /* defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) */
  721. #ifdef CONFIG_TASK_DELAY_ACCT
  722. struct task_delay_info {
  723. spinlock_t lock;
  724. unsigned int flags; /* Private per-task flags */
  725. /* For each stat XXX, add following, aligned appropriately
  726. *
  727. * struct timespec XXX_start, XXX_end;
  728. * u64 XXX_delay;
  729. * u32 XXX_count;
  730. *
  731. * Atomicity of updates to XXX_delay, XXX_count protected by
  732. * single lock above (split into XXX_lock if contention is an issue).
  733. */
  734. /*
  735. * XXX_count is incremented on every XXX operation, the delay
  736. * associated with the operation is added to XXX_delay.
  737. * XXX_delay contains the accumulated delay time in nanoseconds.
  738. */
  739. u64 blkio_start; /* Shared by blkio, swapin */
  740. u64 blkio_delay; /* wait for sync block io completion */
  741. u64 swapin_delay; /* wait for swapin block io completion */
  742. u32 blkio_count; /* total count of the number of sync block */
  743. /* io operations performed */
  744. u32 swapin_count; /* total count of the number of swapin block */
  745. /* io operations performed */
  746. u64 freepages_start;
  747. u64 freepages_delay; /* wait for memory reclaim */
  748. u32 freepages_count; /* total count of memory reclaim */
  749. };
  750. #endif /* CONFIG_TASK_DELAY_ACCT */
  751. static inline int sched_info_on(void)
  752. {
  753. #ifdef CONFIG_SCHEDSTATS
  754. return 1;
  755. #elif defined(CONFIG_TASK_DELAY_ACCT)
  756. extern int delayacct_on;
  757. return delayacct_on;
  758. #else
  759. return 0;
  760. #endif
  761. }
  762. enum cpu_idle_type {
  763. CPU_IDLE,
  764. CPU_NOT_IDLE,
  765. CPU_NEWLY_IDLE,
  766. CPU_MAX_IDLE_TYPES
  767. };
  768. /*
  769. * Increase resolution of cpu_capacity calculations
  770. */
  771. #define SCHED_CAPACITY_SHIFT 10
  772. #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
  773. /*
  774. * sched-domains (multiprocessor balancing) declarations:
  775. */
  776. #ifdef CONFIG_SMP
  777. #define SD_LOAD_BALANCE 0x0001 /* Do load balancing on this domain. */
  778. #define SD_BALANCE_NEWIDLE 0x0002 /* Balance when about to become idle */
  779. #define SD_BALANCE_EXEC 0x0004 /* Balance on exec */
  780. #define SD_BALANCE_FORK 0x0008 /* Balance on fork, clone */
  781. #define SD_BALANCE_WAKE 0x0010 /* Balance on wakeup */
  782. #define SD_WAKE_AFFINE 0x0020 /* Wake task to waking CPU */
  783. #define SD_SHARE_CPUCAPACITY 0x0080 /* Domain members share cpu power */
  784. #define SD_SHARE_POWERDOMAIN 0x0100 /* Domain members share power domain */
  785. #define SD_SHARE_PKG_RESOURCES 0x0200 /* Domain members share cpu pkg resources */
  786. #define SD_SERIALIZE 0x0400 /* Only a single load balancing instance */
  787. #define SD_ASYM_PACKING 0x0800 /* Place busy groups earlier in the domain */
  788. #define SD_PREFER_SIBLING 0x1000 /* Prefer to place tasks in a sibling domain */
  789. #define SD_OVERLAP 0x2000 /* sched_domains of this level overlap */
  790. #define SD_NUMA 0x4000 /* cross-node balancing */
  791. #ifdef CONFIG_SCHED_SMT
  792. static inline int cpu_smt_flags(void)
  793. {
  794. return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
  795. }
  796. #endif
  797. #ifdef CONFIG_SCHED_MC
  798. static inline int cpu_core_flags(void)
  799. {
  800. return SD_SHARE_PKG_RESOURCES;
  801. }
  802. #endif
  803. #ifdef CONFIG_NUMA
  804. static inline int cpu_numa_flags(void)
  805. {
  806. return SD_NUMA;
  807. }
  808. #endif
  809. struct sched_domain_attr {
  810. int relax_domain_level;
  811. };
  812. #define SD_ATTR_INIT (struct sched_domain_attr) { \
  813. .relax_domain_level = -1, \
  814. }
  815. extern int sched_domain_level_max;
  816. struct sched_group;
  817. struct sched_domain {
  818. /* These fields must be setup */
  819. struct sched_domain *parent; /* top domain must be null terminated */
  820. struct sched_domain *child; /* bottom domain must be null terminated */
  821. struct sched_group *groups; /* the balancing groups of the domain */
  822. unsigned long min_interval; /* Minimum balance interval ms */
  823. unsigned long max_interval; /* Maximum balance interval ms */
  824. unsigned int busy_factor; /* less balancing by factor if busy */
  825. unsigned int imbalance_pct; /* No balance until over watermark */
  826. unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
  827. unsigned int busy_idx;
  828. unsigned int idle_idx;
  829. unsigned int newidle_idx;
  830. unsigned int wake_idx;
  831. unsigned int forkexec_idx;
  832. unsigned int smt_gain;
  833. int nohz_idle; /* NOHZ IDLE status */
  834. int flags; /* See SD_* */
  835. int level;
  836. /* Runtime fields. */
  837. unsigned long last_balance; /* init to jiffies. units in jiffies */
  838. unsigned int balance_interval; /* initialise to 1. units in ms. */
  839. unsigned int nr_balance_failed; /* initialise to 0 */
  840. /* idle_balance() stats */
  841. u64 max_newidle_lb_cost;
  842. unsigned long next_decay_max_lb_cost;
  843. #ifdef CONFIG_SCHEDSTATS
  844. /* load_balance() stats */
  845. unsigned int lb_count[CPU_MAX_IDLE_TYPES];
  846. unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
  847. unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
  848. unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
  849. unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
  850. unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
  851. unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
  852. unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
  853. /* Active load balancing */
  854. unsigned int alb_count;
  855. unsigned int alb_failed;
  856. unsigned int alb_pushed;
  857. /* SD_BALANCE_EXEC stats */
  858. unsigned int sbe_count;
  859. unsigned int sbe_balanced;
  860. unsigned int sbe_pushed;
  861. /* SD_BALANCE_FORK stats */
  862. unsigned int sbf_count;
  863. unsigned int sbf_balanced;
  864. unsigned int sbf_pushed;
  865. /* try_to_wake_up() stats */
  866. unsigned int ttwu_wake_remote;
  867. unsigned int ttwu_move_affine;
  868. unsigned int ttwu_move_balance;
  869. #endif
  870. #ifdef CONFIG_SCHED_DEBUG
  871. char *name;
  872. #endif
  873. union {
  874. void *private; /* used during construction */
  875. struct rcu_head rcu; /* used during destruction */
  876. };
  877. unsigned int span_weight;
  878. /*
  879. * Span of all CPUs in this domain.
  880. *
  881. * NOTE: this field is variable length. (Allocated dynamically
  882. * by attaching extra space to the end of the structure,
  883. * depending on how many CPUs the kernel has booted up with)
  884. */
  885. unsigned long span[0];
  886. };
  887. static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
  888. {
  889. return to_cpumask(sd->span);
  890. }
  891. extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  892. struct sched_domain_attr *dattr_new);
  893. /* Allocate an array of sched domains, for partition_sched_domains(). */
  894. cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
  895. void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
  896. bool cpus_share_cache(int this_cpu, int that_cpu);
  897. typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
  898. typedef int (*sched_domain_flags_f)(void);
  899. #define SDTL_OVERLAP 0x01
  900. struct sd_data {
  901. struct sched_domain **__percpu sd;
  902. struct sched_group **__percpu sg;
  903. struct sched_group_capacity **__percpu sgc;
  904. };
  905. struct sched_domain_topology_level {
  906. sched_domain_mask_f mask;
  907. sched_domain_flags_f sd_flags;
  908. int flags;
  909. int numa_level;
  910. struct sd_data data;
  911. #ifdef CONFIG_SCHED_DEBUG
  912. char *name;
  913. #endif
  914. };
  915. extern struct sched_domain_topology_level *sched_domain_topology;
  916. extern void set_sched_topology(struct sched_domain_topology_level *tl);
  917. extern void wake_up_if_idle(int cpu);
  918. #ifdef CONFIG_SCHED_DEBUG
  919. # define SD_INIT_NAME(type) .name = #type
  920. #else
  921. # define SD_INIT_NAME(type)
  922. #endif
  923. #else /* CONFIG_SMP */
  924. struct sched_domain_attr;
  925. static inline void
  926. partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  927. struct sched_domain_attr *dattr_new)
  928. {
  929. }
  930. static inline bool cpus_share_cache(int this_cpu, int that_cpu)
  931. {
  932. return true;
  933. }
  934. #endif /* !CONFIG_SMP */
  935. struct io_context; /* See blkdev.h */
  936. #ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
  937. extern void prefetch_stack(struct task_struct *t);
  938. #else
  939. static inline void prefetch_stack(struct task_struct *t) { }
  940. #endif
  941. struct audit_context; /* See audit.c */
  942. struct mempolicy;
  943. struct pipe_inode_info;
  944. struct uts_namespace;
  945. struct load_weight {
  946. unsigned long weight;
  947. u32 inv_weight;
  948. };
  949. struct sched_avg {
  950. u64 last_runnable_update;
  951. s64 decay_count;
  952. /*
  953. * utilization_avg_contrib describes the amount of time that a
  954. * sched_entity is running on a CPU. It is based on running_avg_sum
  955. * and is scaled in the range [0..SCHED_LOAD_SCALE].
  956. * load_avg_contrib described the amount of time that a sched_entity
  957. * is runnable on a rq. It is based on both runnable_avg_sum and the
  958. * weight of the task.
  959. */
  960. unsigned long load_avg_contrib, utilization_avg_contrib;
  961. /*
  962. * These sums represent an infinite geometric series and so are bound
  963. * above by 1024/(1-y). Thus we only need a u32 to store them for all
  964. * choices of y < 1-2^(-32)*1024.
  965. * running_avg_sum reflects the time that the sched_entity is
  966. * effectively running on the CPU.
  967. * runnable_avg_sum represents the amount of time a sched_entity is on
  968. * a runqueue which includes the running time that is monitored by
  969. * running_avg_sum.
  970. */
  971. u32 runnable_avg_sum, avg_period, running_avg_sum;
  972. };
  973. #ifdef CONFIG_SCHEDSTATS
  974. struct sched_statistics {
  975. u64 wait_start;
  976. u64 wait_max;
  977. u64 wait_count;
  978. u64 wait_sum;
  979. u64 iowait_count;
  980. u64 iowait_sum;
  981. u64 sleep_start;
  982. u64 sleep_max;
  983. s64 sum_sleep_runtime;
  984. u64 block_start;
  985. u64 block_max;
  986. u64 exec_max;
  987. u64 slice_max;
  988. u64 nr_migrations_cold;
  989. u64 nr_failed_migrations_affine;
  990. u64 nr_failed_migrations_running;
  991. u64 nr_failed_migrations_hot;
  992. u64 nr_forced_migrations;
  993. u64 nr_wakeups;
  994. u64 nr_wakeups_sync;
  995. u64 nr_wakeups_migrate;
  996. u64 nr_wakeups_local;
  997. u64 nr_wakeups_remote;
  998. u64 nr_wakeups_affine;
  999. u64 nr_wakeups_affine_attempts;
  1000. u64 nr_wakeups_passive;
  1001. u64 nr_wakeups_idle;
  1002. };
  1003. #endif
  1004. struct sched_entity {
  1005. struct load_weight load; /* for load-balancing */
  1006. struct rb_node run_node;
  1007. struct list_head group_node;
  1008. unsigned int on_rq;
  1009. u64 exec_start;
  1010. u64 sum_exec_runtime;
  1011. u64 vruntime;
  1012. u64 prev_sum_exec_runtime;
  1013. u64 nr_migrations;
  1014. #ifdef CONFIG_SCHEDSTATS
  1015. struct sched_statistics statistics;
  1016. #endif
  1017. #ifdef CONFIG_FAIR_GROUP_SCHED
  1018. int depth;
  1019. struct sched_entity *parent;
  1020. /* rq on which this entity is (to be) queued: */
  1021. struct cfs_rq *cfs_rq;
  1022. /* rq "owned" by this entity/group: */
  1023. struct cfs_rq *my_q;
  1024. #endif
  1025. #ifdef CONFIG_SMP
  1026. /* Per-entity load-tracking */
  1027. struct sched_avg avg;
  1028. #endif
  1029. };
  1030. struct sched_rt_entity {
  1031. struct list_head run_list;
  1032. unsigned long timeout;
  1033. unsigned long watchdog_stamp;
  1034. unsigned int time_slice;
  1035. struct sched_rt_entity *back;
  1036. #ifdef CONFIG_RT_GROUP_SCHED
  1037. struct sched_rt_entity *parent;
  1038. /* rq on which this entity is (to be) queued: */
  1039. struct rt_rq *rt_rq;
  1040. /* rq "owned" by this entity/group: */
  1041. struct rt_rq *my_q;
  1042. #endif
  1043. };
  1044. struct sched_dl_entity {
  1045. struct rb_node rb_node;
  1046. /*
  1047. * Original scheduling parameters. Copied here from sched_attr
  1048. * during sched_setattr(), they will remain the same until
  1049. * the next sched_setattr().
  1050. */
  1051. u64 dl_runtime; /* maximum runtime for each instance */
  1052. u64 dl_deadline; /* relative deadline of each instance */
  1053. u64 dl_period; /* separation of two instances (period) */
  1054. u64 dl_bw; /* dl_runtime / dl_deadline */
  1055. /*
  1056. * Actual scheduling parameters. Initialized with the values above,
  1057. * they are continously updated during task execution. Note that
  1058. * the remaining runtime could be < 0 in case we are in overrun.
  1059. */
  1060. s64 runtime; /* remaining runtime for this instance */
  1061. u64 deadline; /* absolute deadline for this instance */
  1062. unsigned int flags; /* specifying the scheduler behaviour */
  1063. /*
  1064. * Some bool flags:
  1065. *
  1066. * @dl_throttled tells if we exhausted the runtime. If so, the
  1067. * task has to wait for a replenishment to be performed at the
  1068. * next firing of dl_timer.
  1069. *
  1070. * @dl_new tells if a new instance arrived. If so we must
  1071. * start executing it with full runtime and reset its absolute
  1072. * deadline;
  1073. *
  1074. * @dl_boosted tells if we are boosted due to DI. If so we are
  1075. * outside bandwidth enforcement mechanism (but only until we
  1076. * exit the critical section);
  1077. *
  1078. * @dl_yielded tells if task gave up the cpu before consuming
  1079. * all its available runtime during the last job.
  1080. */
  1081. int dl_throttled, dl_new, dl_boosted, dl_yielded;
  1082. /*
  1083. * Bandwidth enforcement timer. Each -deadline task has its
  1084. * own bandwidth to be enforced, thus we need one timer per task.
  1085. */
  1086. struct hrtimer dl_timer;
  1087. };
  1088. union rcu_special {
  1089. struct {
  1090. bool blocked;
  1091. bool need_qs;
  1092. } b;
  1093. short s;
  1094. };
  1095. struct rcu_node;
  1096. enum perf_event_task_context {
  1097. perf_invalid_context = -1,
  1098. perf_hw_context = 0,
  1099. perf_sw_context,
  1100. perf_nr_task_contexts,
  1101. };
  1102. struct task_struct {
  1103. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  1104. void *stack;
  1105. atomic_t usage;
  1106. unsigned int flags; /* per process flags, defined below */
  1107. unsigned int ptrace;
  1108. #ifdef CONFIG_SMP
  1109. struct llist_node wake_entry;
  1110. int on_cpu;
  1111. struct task_struct *last_wakee;
  1112. unsigned long wakee_flips;
  1113. unsigned long wakee_flip_decay_ts;
  1114. int wake_cpu;
  1115. #endif
  1116. int on_rq;
  1117. int prio, static_prio, normal_prio;
  1118. unsigned int rt_priority;
  1119. const struct sched_class *sched_class;
  1120. struct sched_entity se;
  1121. struct sched_rt_entity rt;
  1122. #ifdef CONFIG_CGROUP_SCHED
  1123. struct task_group *sched_task_group;
  1124. #endif
  1125. struct sched_dl_entity dl;
  1126. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1127. /* list of struct preempt_notifier: */
  1128. struct hlist_head preempt_notifiers;
  1129. #endif
  1130. #ifdef CONFIG_BLK_DEV_IO_TRACE
  1131. unsigned int btrace_seq;
  1132. #endif
  1133. unsigned int policy;
  1134. int nr_cpus_allowed;
  1135. cpumask_t cpus_allowed;
  1136. #ifdef CONFIG_PREEMPT_RCU
  1137. int rcu_read_lock_nesting;
  1138. union rcu_special rcu_read_unlock_special;
  1139. struct list_head rcu_node_entry;
  1140. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1141. #ifdef CONFIG_PREEMPT_RCU
  1142. struct rcu_node *rcu_blocked_node;
  1143. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1144. #ifdef CONFIG_TASKS_RCU
  1145. unsigned long rcu_tasks_nvcsw;
  1146. bool rcu_tasks_holdout;
  1147. struct list_head rcu_tasks_holdout_list;
  1148. int rcu_tasks_idle_cpu;
  1149. #endif /* #ifdef CONFIG_TASKS_RCU */
  1150. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  1151. struct sched_info sched_info;
  1152. #endif
  1153. struct list_head tasks;
  1154. #ifdef CONFIG_SMP
  1155. struct plist_node pushable_tasks;
  1156. struct rb_node pushable_dl_tasks;
  1157. #endif
  1158. struct mm_struct *mm, *active_mm;
  1159. #ifdef CONFIG_COMPAT_BRK
  1160. unsigned brk_randomized:1;
  1161. #endif
  1162. /* per-thread vma caching */
  1163. u32 vmacache_seqnum;
  1164. struct vm_area_struct *vmacache[VMACACHE_SIZE];
  1165. #if defined(SPLIT_RSS_COUNTING)
  1166. struct task_rss_stat rss_stat;
  1167. #endif
  1168. /* task state */
  1169. int exit_state;
  1170. int exit_code, exit_signal;
  1171. int pdeath_signal; /* The signal sent when the parent dies */
  1172. unsigned int jobctl; /* JOBCTL_*, siglock protected */
  1173. /* Used for emulating ABI behavior of previous Linux versions */
  1174. unsigned int personality;
  1175. unsigned in_execve:1; /* Tell the LSMs that the process is doing an
  1176. * execve */
  1177. unsigned in_iowait:1;
  1178. /* Revert to default priority/policy when forking */
  1179. unsigned sched_reset_on_fork:1;
  1180. unsigned sched_contributes_to_load:1;
  1181. #ifdef CONFIG_MEMCG_KMEM
  1182. unsigned memcg_kmem_skip_account:1;
  1183. #endif
  1184. unsigned long atomic_flags; /* Flags needing atomic access. */
  1185. struct restart_block restart_block;
  1186. pid_t pid;
  1187. pid_t tgid;
  1188. #ifdef CONFIG_CC_STACKPROTECTOR
  1189. /* Canary value for the -fstack-protector gcc feature */
  1190. unsigned long stack_canary;
  1191. #endif
  1192. /*
  1193. * pointers to (original) parent process, youngest child, younger sibling,
  1194. * older sibling, respectively. (p->father can be replaced with
  1195. * p->real_parent->pid)
  1196. */
  1197. struct task_struct __rcu *real_parent; /* real parent process */
  1198. struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
  1199. /*
  1200. * children/sibling forms the list of my natural children
  1201. */
  1202. struct list_head children; /* list of my children */
  1203. struct list_head sibling; /* linkage in my parent's children list */
  1204. struct task_struct *group_leader; /* threadgroup leader */
  1205. /*
  1206. * ptraced is the list of tasks this task is using ptrace on.
  1207. * This includes both natural children and PTRACE_ATTACH targets.
  1208. * p->ptrace_entry is p's link on the p->parent->ptraced list.
  1209. */
  1210. struct list_head ptraced;
  1211. struct list_head ptrace_entry;
  1212. /* PID/PID hash table linkage. */
  1213. struct pid_link pids[PIDTYPE_MAX];
  1214. struct list_head thread_group;
  1215. struct list_head thread_node;
  1216. struct completion *vfork_done; /* for vfork() */
  1217. int __user *set_child_tid; /* CLONE_CHILD_SETTID */
  1218. int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
  1219. cputime_t utime, stime, utimescaled, stimescaled;
  1220. cputime_t gtime;
  1221. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  1222. struct cputime prev_cputime;
  1223. #endif
  1224. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  1225. seqlock_t vtime_seqlock;
  1226. unsigned long long vtime_snap;
  1227. enum {
  1228. VTIME_SLEEPING = 0,
  1229. VTIME_USER,
  1230. VTIME_SYS,
  1231. } vtime_snap_whence;
  1232. #endif
  1233. unsigned long nvcsw, nivcsw; /* context switch counts */
  1234. u64 start_time; /* monotonic time in nsec */
  1235. u64 real_start_time; /* boot based time in nsec */
  1236. /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
  1237. unsigned long min_flt, maj_flt;
  1238. struct task_cputime cputime_expires;
  1239. struct list_head cpu_timers[3];
  1240. /* process credentials */
  1241. const struct cred __rcu *real_cred; /* objective and real subjective task
  1242. * credentials (COW) */
  1243. const struct cred __rcu *cred; /* effective (overridable) subjective task
  1244. * credentials (COW) */
  1245. char comm[TASK_COMM_LEN]; /* executable name excluding path
  1246. - access with [gs]et_task_comm (which lock
  1247. it with task_lock())
  1248. - initialized normally by setup_new_exec */
  1249. /* file system info */
  1250. int link_count, total_link_count;
  1251. #ifdef CONFIG_SYSVIPC
  1252. /* ipc stuff */
  1253. struct sysv_sem sysvsem;
  1254. struct sysv_shm sysvshm;
  1255. #endif
  1256. #ifdef CONFIG_DETECT_HUNG_TASK
  1257. /* hung task detection */
  1258. unsigned long last_switch_count;
  1259. #endif
  1260. /* CPU-specific state of this task */
  1261. struct thread_struct thread;
  1262. /* filesystem information */
  1263. struct fs_struct *fs;
  1264. /* open file information */
  1265. struct files_struct *files;
  1266. /* namespaces */
  1267. struct nsproxy *nsproxy;
  1268. /* signal handlers */
  1269. struct signal_struct *signal;
  1270. struct sighand_struct *sighand;
  1271. sigset_t blocked, real_blocked;
  1272. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  1273. struct sigpending pending;
  1274. unsigned long sas_ss_sp;
  1275. size_t sas_ss_size;
  1276. int (*notifier)(void *priv);
  1277. void *notifier_data;
  1278. sigset_t *notifier_mask;
  1279. struct callback_head *task_works;
  1280. struct audit_context *audit_context;
  1281. #ifdef CONFIG_AUDITSYSCALL
  1282. kuid_t loginuid;
  1283. unsigned int sessionid;
  1284. #endif
  1285. struct seccomp seccomp;
  1286. /* Thread group tracking */
  1287. u32 parent_exec_id;
  1288. u32 self_exec_id;
  1289. /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
  1290. * mempolicy */
  1291. spinlock_t alloc_lock;
  1292. /* Protection of the PI data structures: */
  1293. raw_spinlock_t pi_lock;
  1294. #ifdef CONFIG_RT_MUTEXES
  1295. /* PI waiters blocked on a rt_mutex held by this task */
  1296. struct rb_root pi_waiters;
  1297. struct rb_node *pi_waiters_leftmost;
  1298. /* Deadlock detection and priority inheritance handling */
  1299. struct rt_mutex_waiter *pi_blocked_on;
  1300. #endif
  1301. #ifdef CONFIG_DEBUG_MUTEXES
  1302. /* mutex deadlock detection */
  1303. struct mutex_waiter *blocked_on;
  1304. #endif
  1305. #ifdef CONFIG_TRACE_IRQFLAGS
  1306. unsigned int irq_events;
  1307. unsigned long hardirq_enable_ip;
  1308. unsigned long hardirq_disable_ip;
  1309. unsigned int hardirq_enable_event;
  1310. unsigned int hardirq_disable_event;
  1311. int hardirqs_enabled;
  1312. int hardirq_context;
  1313. unsigned long softirq_disable_ip;
  1314. unsigned long softirq_enable_ip;
  1315. unsigned int softirq_disable_event;
  1316. unsigned int softirq_enable_event;
  1317. int softirqs_enabled;
  1318. int softirq_context;
  1319. #endif
  1320. #ifdef CONFIG_LOCKDEP
  1321. # define MAX_LOCK_DEPTH 48UL
  1322. u64 curr_chain_key;
  1323. int lockdep_depth;
  1324. unsigned int lockdep_recursion;
  1325. struct held_lock held_locks[MAX_LOCK_DEPTH];
  1326. gfp_t lockdep_reclaim_gfp;
  1327. #endif
  1328. /* journalling filesystem info */
  1329. void *journal_info;
  1330. /* stacked block device info */
  1331. struct bio_list *bio_list;
  1332. #ifdef CONFIG_BLOCK
  1333. /* stack plugging */
  1334. struct blk_plug *plug;
  1335. #endif
  1336. /* VM state */
  1337. struct reclaim_state *reclaim_state;
  1338. struct backing_dev_info *backing_dev_info;
  1339. struct io_context *io_context;
  1340. unsigned long ptrace_message;
  1341. siginfo_t *last_siginfo; /* For ptrace use. */
  1342. struct task_io_accounting ioac;
  1343. #if defined(CONFIG_TASK_XACCT)
  1344. u64 acct_rss_mem1; /* accumulated rss usage */
  1345. u64 acct_vm_mem1; /* accumulated virtual memory usage */
  1346. cputime_t acct_timexpd; /* stime + utime since last update */
  1347. #endif
  1348. #ifdef CONFIG_CPUSETS
  1349. nodemask_t mems_allowed; /* Protected by alloc_lock */
  1350. seqcount_t mems_allowed_seq; /* Seqence no to catch updates */
  1351. int cpuset_mem_spread_rotor;
  1352. int cpuset_slab_spread_rotor;
  1353. #endif
  1354. #ifdef CONFIG_CGROUPS
  1355. /* Control Group info protected by css_set_lock */
  1356. struct css_set __rcu *cgroups;
  1357. /* cg_list protected by css_set_lock and tsk->alloc_lock */
  1358. struct list_head cg_list;
  1359. #endif
  1360. #ifdef CONFIG_FUTEX
  1361. struct robust_list_head __user *robust_list;
  1362. #ifdef CONFIG_COMPAT
  1363. struct compat_robust_list_head __user *compat_robust_list;
  1364. #endif
  1365. struct list_head pi_state_list;
  1366. struct futex_pi_state *pi_state_cache;
  1367. #endif
  1368. #ifdef CONFIG_PERF_EVENTS
  1369. struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
  1370. struct mutex perf_event_mutex;
  1371. struct list_head perf_event_list;
  1372. #endif
  1373. #ifdef CONFIG_DEBUG_PREEMPT
  1374. unsigned long preempt_disable_ip;
  1375. #endif
  1376. #ifdef CONFIG_NUMA
  1377. struct mempolicy *mempolicy; /* Protected by alloc_lock */
  1378. short il_next;
  1379. short pref_node_fork;
  1380. #endif
  1381. #ifdef CONFIG_NUMA_BALANCING
  1382. int numa_scan_seq;
  1383. unsigned int numa_scan_period;
  1384. unsigned int numa_scan_period_max;
  1385. int numa_preferred_nid;
  1386. unsigned long numa_migrate_retry;
  1387. u64 node_stamp; /* migration stamp */
  1388. u64 last_task_numa_placement;
  1389. u64 last_sum_exec_runtime;
  1390. struct callback_head numa_work;
  1391. struct list_head numa_entry;
  1392. struct numa_group *numa_group;
  1393. /*
  1394. * numa_faults is an array split into four regions:
  1395. * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
  1396. * in this precise order.
  1397. *
  1398. * faults_memory: Exponential decaying average of faults on a per-node
  1399. * basis. Scheduling placement decisions are made based on these
  1400. * counts. The values remain static for the duration of a PTE scan.
  1401. * faults_cpu: Track the nodes the process was running on when a NUMA
  1402. * hinting fault was incurred.
  1403. * faults_memory_buffer and faults_cpu_buffer: Record faults per node
  1404. * during the current scan window. When the scan completes, the counts
  1405. * in faults_memory and faults_cpu decay and these values are copied.
  1406. */
  1407. unsigned long *numa_faults;
  1408. unsigned long total_numa_faults;
  1409. /*
  1410. * numa_faults_locality tracks if faults recorded during the last
  1411. * scan window were remote/local or failed to migrate. The task scan
  1412. * period is adapted based on the locality of the faults with different
  1413. * weights depending on whether they were shared or private faults
  1414. */
  1415. unsigned long numa_faults_locality[3];
  1416. unsigned long numa_pages_migrated;
  1417. #endif /* CONFIG_NUMA_BALANCING */
  1418. struct rcu_head rcu;
  1419. /*
  1420. * cache last used pipe for splice
  1421. */
  1422. struct pipe_inode_info *splice_pipe;
  1423. struct page_frag task_frag;
  1424. #ifdef CONFIG_TASK_DELAY_ACCT
  1425. struct task_delay_info *delays;
  1426. #endif
  1427. #ifdef CONFIG_FAULT_INJECTION
  1428. int make_it_fail;
  1429. #endif
  1430. /*
  1431. * when (nr_dirtied >= nr_dirtied_pause), it's time to call
  1432. * balance_dirty_pages() for some dirty throttling pause
  1433. */
  1434. int nr_dirtied;
  1435. int nr_dirtied_pause;
  1436. unsigned long dirty_paused_when; /* start of a write-and-pause period */
  1437. #ifdef CONFIG_LATENCYTOP
  1438. int latency_record_count;
  1439. struct latency_record latency_record[LT_SAVECOUNT];
  1440. #endif
  1441. /*
  1442. * time slack values; these are used to round up poll() and
  1443. * select() etc timeout values. These are in nanoseconds.
  1444. */
  1445. unsigned long timer_slack_ns;
  1446. unsigned long default_timer_slack_ns;
  1447. #ifdef CONFIG_KASAN
  1448. unsigned int kasan_depth;
  1449. #endif
  1450. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1451. /* Index of current stored address in ret_stack */
  1452. int curr_ret_stack;
  1453. /* Stack of return addresses for return function tracing */
  1454. struct ftrace_ret_stack *ret_stack;
  1455. /* time stamp for last schedule */
  1456. unsigned long long ftrace_timestamp;
  1457. /*
  1458. * Number of functions that haven't been traced
  1459. * because of depth overrun.
  1460. */
  1461. atomic_t trace_overrun;
  1462. /* Pause for the tracing */
  1463. atomic_t tracing_graph_pause;
  1464. #endif
  1465. #ifdef CONFIG_TRACING
  1466. /* state flags for use by tracers */
  1467. unsigned long trace;
  1468. /* bitmask and counter of trace recursion */
  1469. unsigned long trace_recursion;
  1470. #endif /* CONFIG_TRACING */
  1471. #ifdef CONFIG_MEMCG
  1472. struct memcg_oom_info {
  1473. struct mem_cgroup *memcg;
  1474. gfp_t gfp_mask;
  1475. int order;
  1476. unsigned int may_oom:1;
  1477. } memcg_oom;
  1478. #endif
  1479. #ifdef CONFIG_UPROBES
  1480. struct uprobe_task *utask;
  1481. #endif
  1482. #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
  1483. unsigned int sequential_io;
  1484. unsigned int sequential_io_avg;
  1485. #endif
  1486. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  1487. unsigned long task_state_change;
  1488. #endif
  1489. };
  1490. /* Future-safe accessor for struct task_struct's cpus_allowed. */
  1491. #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  1492. #define TNF_MIGRATED 0x01
  1493. #define TNF_NO_GROUP 0x02
  1494. #define TNF_SHARED 0x04
  1495. #define TNF_FAULT_LOCAL 0x08
  1496. #define TNF_MIGRATE_FAIL 0x10
  1497. #ifdef CONFIG_NUMA_BALANCING
  1498. extern void task_numa_fault(int last_node, int node, int pages, int flags);
  1499. extern pid_t task_numa_group_id(struct task_struct *p);
  1500. extern void set_numabalancing_state(bool enabled);
  1501. extern void task_numa_free(struct task_struct *p);
  1502. extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
  1503. int src_nid, int dst_cpu);
  1504. #else
  1505. static inline void task_numa_fault(int last_node, int node, int pages,
  1506. int flags)
  1507. {
  1508. }
  1509. static inline pid_t task_numa_group_id(struct task_struct *p)
  1510. {
  1511. return 0;
  1512. }
  1513. static inline void set_numabalancing_state(bool enabled)
  1514. {
  1515. }
  1516. static inline void task_numa_free(struct task_struct *p)
  1517. {
  1518. }
  1519. static inline bool should_numa_migrate_memory(struct task_struct *p,
  1520. struct page *page, int src_nid, int dst_cpu)
  1521. {
  1522. return true;
  1523. }
  1524. #endif
  1525. static inline struct pid *task_pid(struct task_struct *task)
  1526. {
  1527. return task->pids[PIDTYPE_PID].pid;
  1528. }
  1529. static inline struct pid *task_tgid(struct task_struct *task)
  1530. {
  1531. return task->group_leader->pids[PIDTYPE_PID].pid;
  1532. }
  1533. /*
  1534. * Without tasklist or rcu lock it is not safe to dereference
  1535. * the result of task_pgrp/task_session even if task == current,
  1536. * we can race with another thread doing sys_setsid/sys_setpgid.
  1537. */
  1538. static inline struct pid *task_pgrp(struct task_struct *task)
  1539. {
  1540. return task->group_leader->pids[PIDTYPE_PGID].pid;
  1541. }
  1542. static inline struct pid *task_session(struct task_struct *task)
  1543. {
  1544. return task->group_leader->pids[PIDTYPE_SID].pid;
  1545. }
  1546. struct pid_namespace;
  1547. /*
  1548. * the helpers to get the task's different pids as they are seen
  1549. * from various namespaces
  1550. *
  1551. * task_xid_nr() : global id, i.e. the id seen from the init namespace;
  1552. * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
  1553. * current.
  1554. * task_xid_nr_ns() : id seen from the ns specified;
  1555. *
  1556. * set_task_vxid() : assigns a virtual id to a task;
  1557. *
  1558. * see also pid_nr() etc in include/linux/pid.h
  1559. */
  1560. pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
  1561. struct pid_namespace *ns);
  1562. static inline pid_t task_pid_nr(struct task_struct *tsk)
  1563. {
  1564. return tsk->pid;
  1565. }
  1566. static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
  1567. struct pid_namespace *ns)
  1568. {
  1569. return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
  1570. }
  1571. static inline pid_t task_pid_vnr(struct task_struct *tsk)
  1572. {
  1573. return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
  1574. }
  1575. static inline pid_t task_tgid_nr(struct task_struct *tsk)
  1576. {
  1577. return tsk->tgid;
  1578. }
  1579. pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
  1580. static inline pid_t task_tgid_vnr(struct task_struct *tsk)
  1581. {
  1582. return pid_vnr(task_tgid(tsk));
  1583. }
  1584. static inline int pid_alive(const struct task_struct *p);
  1585. static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
  1586. {
  1587. pid_t pid = 0;
  1588. rcu_read_lock();
  1589. if (pid_alive(tsk))
  1590. pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
  1591. rcu_read_unlock();
  1592. return pid;
  1593. }
  1594. static inline pid_t task_ppid_nr(const struct task_struct *tsk)
  1595. {
  1596. return task_ppid_nr_ns(tsk, &init_pid_ns);
  1597. }
  1598. static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
  1599. struct pid_namespace *ns)
  1600. {
  1601. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
  1602. }
  1603. static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
  1604. {
  1605. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
  1606. }
  1607. static inline pid_t task_session_nr_ns(struct task_struct *tsk,
  1608. struct pid_namespace *ns)
  1609. {
  1610. return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
  1611. }
  1612. static inline pid_t task_session_vnr(struct task_struct *tsk)
  1613. {
  1614. return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
  1615. }
  1616. /* obsolete, do not use */
  1617. static inline pid_t task_pgrp_nr(struct task_struct *tsk)
  1618. {
  1619. return task_pgrp_nr_ns(tsk, &init_pid_ns);
  1620. }
  1621. /**
  1622. * pid_alive - check that a task structure is not stale
  1623. * @p: Task structure to be checked.
  1624. *
  1625. * Test if a process is not yet dead (at most zombie state)
  1626. * If pid_alive fails, then pointers within the task structure
  1627. * can be stale and must not be dereferenced.
  1628. *
  1629. * Return: 1 if the process is alive. 0 otherwise.
  1630. */
  1631. static inline int pid_alive(const struct task_struct *p)
  1632. {
  1633. return p->pids[PIDTYPE_PID].pid != NULL;
  1634. }
  1635. /**
  1636. * is_global_init - check if a task structure is init
  1637. * @tsk: Task structure to be checked.
  1638. *
  1639. * Check if a task structure is the first user space task the kernel created.
  1640. *
  1641. * Return: 1 if the task structure is init. 0 otherwise.
  1642. */
  1643. static inline int is_global_init(struct task_struct *tsk)
  1644. {
  1645. return tsk->pid == 1;
  1646. }
  1647. extern struct pid *cad_pid;
  1648. extern void free_task(struct task_struct *tsk);
  1649. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  1650. extern void __put_task_struct(struct task_struct *t);
  1651. static inline void put_task_struct(struct task_struct *t)
  1652. {
  1653. if (atomic_dec_and_test(&t->usage))
  1654. __put_task_struct(t);
  1655. }
  1656. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  1657. extern void task_cputime(struct task_struct *t,
  1658. cputime_t *utime, cputime_t *stime);
  1659. extern void task_cputime_scaled(struct task_struct *t,
  1660. cputime_t *utimescaled, cputime_t *stimescaled);
  1661. extern cputime_t task_gtime(struct task_struct *t);
  1662. #else
  1663. static inline void task_cputime(struct task_struct *t,
  1664. cputime_t *utime, cputime_t *stime)
  1665. {
  1666. if (utime)
  1667. *utime = t->utime;
  1668. if (stime)
  1669. *stime = t->stime;
  1670. }
  1671. static inline void task_cputime_scaled(struct task_struct *t,
  1672. cputime_t *utimescaled,
  1673. cputime_t *stimescaled)
  1674. {
  1675. if (utimescaled)
  1676. *utimescaled = t->utimescaled;
  1677. if (stimescaled)
  1678. *stimescaled = t->stimescaled;
  1679. }
  1680. static inline cputime_t task_gtime(struct task_struct *t)
  1681. {
  1682. return t->gtime;
  1683. }
  1684. #endif
  1685. extern void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
  1686. extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
  1687. /*
  1688. * Per process flags
  1689. */
  1690. #define PF_EXITING 0x00000004 /* getting shut down */
  1691. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  1692. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  1693. #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
  1694. #define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
  1695. #define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
  1696. #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
  1697. #define PF_DUMPCORE 0x00000200 /* dumped core */
  1698. #define PF_SIGNALED 0x00000400 /* killed by a signal */
  1699. #define PF_MEMALLOC 0x00000800 /* Allocating memory */
  1700. #define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
  1701. #define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
  1702. #define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
  1703. #define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
  1704. #define PF_FROZEN 0x00010000 /* frozen for system suspend */
  1705. #define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
  1706. #define PF_KSWAPD 0x00040000 /* I am kswapd */
  1707. #define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
  1708. #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
  1709. #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
  1710. #define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
  1711. #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
  1712. #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
  1713. #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
  1714. #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
  1715. #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
  1716. #define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
  1717. /*
  1718. * Only the _current_ task can read/write to tsk->flags, but other
  1719. * tasks can access tsk->flags in readonly mode for example
  1720. * with tsk_used_math (like during threaded core dumping).
  1721. * There is however an exception to this rule during ptrace
  1722. * or during fork: the ptracer task is allowed to write to the
  1723. * child->flags of its traced child (same goes for fork, the parent
  1724. * can write to the child->flags), because we're guaranteed the
  1725. * child is not running and in turn not changing child->flags
  1726. * at the same time the parent does it.
  1727. */
  1728. #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
  1729. #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
  1730. #define clear_used_math() clear_stopped_child_used_math(current)
  1731. #define set_used_math() set_stopped_child_used_math(current)
  1732. #define conditional_stopped_child_used_math(condition, child) \
  1733. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
  1734. #define conditional_used_math(condition) \
  1735. conditional_stopped_child_used_math(condition, current)
  1736. #define copy_to_stopped_child_used_math(child) \
  1737. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
  1738. /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
  1739. #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
  1740. #define used_math() tsk_used_math(current)
  1741. /* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
  1742. * __GFP_FS is also cleared as it implies __GFP_IO.
  1743. */
  1744. static inline gfp_t memalloc_noio_flags(gfp_t flags)
  1745. {
  1746. if (unlikely(current->flags & PF_MEMALLOC_NOIO))
  1747. flags &= ~(__GFP_IO | __GFP_FS);
  1748. return flags;
  1749. }
  1750. static inline unsigned int memalloc_noio_save(void)
  1751. {
  1752. unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
  1753. current->flags |= PF_MEMALLOC_NOIO;
  1754. return flags;
  1755. }
  1756. static inline void memalloc_noio_restore(unsigned int flags)
  1757. {
  1758. current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
  1759. }
  1760. /* Per-process atomic flags. */
  1761. #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
  1762. #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
  1763. #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
  1764. #define TASK_PFA_TEST(name, func) \
  1765. static inline bool task_##func(struct task_struct *p) \
  1766. { return test_bit(PFA_##name, &p->atomic_flags); }
  1767. #define TASK_PFA_SET(name, func) \
  1768. static inline void task_set_##func(struct task_struct *p) \
  1769. { set_bit(PFA_##name, &p->atomic_flags); }
  1770. #define TASK_PFA_CLEAR(name, func) \
  1771. static inline void task_clear_##func(struct task_struct *p) \
  1772. { clear_bit(PFA_##name, &p->atomic_flags); }
  1773. TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
  1774. TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
  1775. TASK_PFA_TEST(SPREAD_PAGE, spread_page)
  1776. TASK_PFA_SET(SPREAD_PAGE, spread_page)
  1777. TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
  1778. TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
  1779. TASK_PFA_SET(SPREAD_SLAB, spread_slab)
  1780. TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
  1781. /*
  1782. * task->jobctl flags
  1783. */
  1784. #define JOBCTL_STOP_SIGMASK 0xffff /* signr of the last group stop */
  1785. #define JOBCTL_STOP_DEQUEUED_BIT 16 /* stop signal dequeued */
  1786. #define JOBCTL_STOP_PENDING_BIT 17 /* task should stop for group stop */
  1787. #define JOBCTL_STOP_CONSUME_BIT 18 /* consume group stop count */
  1788. #define JOBCTL_TRAP_STOP_BIT 19 /* trap for STOP */
  1789. #define JOBCTL_TRAP_NOTIFY_BIT 20 /* trap for NOTIFY */
  1790. #define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */
  1791. #define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */
  1792. #define JOBCTL_STOP_DEQUEUED (1 << JOBCTL_STOP_DEQUEUED_BIT)
  1793. #define JOBCTL_STOP_PENDING (1 << JOBCTL_STOP_PENDING_BIT)
  1794. #define JOBCTL_STOP_CONSUME (1 << JOBCTL_STOP_CONSUME_BIT)
  1795. #define JOBCTL_TRAP_STOP (1 << JOBCTL_TRAP_STOP_BIT)
  1796. #define JOBCTL_TRAP_NOTIFY (1 << JOBCTL_TRAP_NOTIFY_BIT)
  1797. #define JOBCTL_TRAPPING (1 << JOBCTL_TRAPPING_BIT)
  1798. #define JOBCTL_LISTENING (1 << JOBCTL_LISTENING_BIT)
  1799. #define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
  1800. #define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
  1801. extern bool task_set_jobctl_pending(struct task_struct *task,
  1802. unsigned int mask);
  1803. extern void task_clear_jobctl_trapping(struct task_struct *task);
  1804. extern void task_clear_jobctl_pending(struct task_struct *task,
  1805. unsigned int mask);
  1806. static inline void rcu_copy_process(struct task_struct *p)
  1807. {
  1808. #ifdef CONFIG_PREEMPT_RCU
  1809. p->rcu_read_lock_nesting = 0;
  1810. p->rcu_read_unlock_special.s = 0;
  1811. p->rcu_blocked_node = NULL;
  1812. INIT_LIST_HEAD(&p->rcu_node_entry);
  1813. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1814. #ifdef CONFIG_TASKS_RCU
  1815. p->rcu_tasks_holdout = false;
  1816. INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
  1817. p->rcu_tasks_idle_cpu = -1;
  1818. #endif /* #ifdef CONFIG_TASKS_RCU */
  1819. }
  1820. static inline void tsk_restore_flags(struct task_struct *task,
  1821. unsigned long orig_flags, unsigned long flags)
  1822. {
  1823. task->flags &= ~flags;
  1824. task->flags |= orig_flags & flags;
  1825. }
  1826. extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
  1827. const struct cpumask *trial);
  1828. extern int task_can_attach(struct task_struct *p,
  1829. const struct cpumask *cs_cpus_allowed);
  1830. #ifdef CONFIG_SMP
  1831. extern void do_set_cpus_allowed(struct task_struct *p,
  1832. const struct cpumask *new_mask);
  1833. extern int set_cpus_allowed_ptr(struct task_struct *p,
  1834. const struct cpumask *new_mask);
  1835. #else
  1836. static inline void do_set_cpus_allowed(struct task_struct *p,
  1837. const struct cpumask *new_mask)
  1838. {
  1839. }
  1840. static inline int set_cpus_allowed_ptr(struct task_struct *p,
  1841. const struct cpumask *new_mask)
  1842. {
  1843. if (!cpumask_test_cpu(0, new_mask))
  1844. return -EINVAL;
  1845. return 0;
  1846. }
  1847. #endif
  1848. #ifdef CONFIG_NO_HZ_COMMON
  1849. void calc_load_enter_idle(void);
  1850. void calc_load_exit_idle(void);
  1851. #else
  1852. static inline void calc_load_enter_idle(void) { }
  1853. static inline void calc_load_exit_idle(void) { }
  1854. #endif /* CONFIG_NO_HZ_COMMON */
  1855. #ifndef CONFIG_CPUMASK_OFFSTACK
  1856. static inline int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
  1857. {
  1858. return set_cpus_allowed_ptr(p, &new_mask);
  1859. }
  1860. #endif
  1861. /*
  1862. * Do not use outside of architecture code which knows its limitations.
  1863. *
  1864. * sched_clock() has no promise of monotonicity or bounded drift between
  1865. * CPUs, use (which you should not) requires disabling IRQs.
  1866. *
  1867. * Please use one of the three interfaces below.
  1868. */
  1869. extern unsigned long long notrace sched_clock(void);
  1870. /*
  1871. * See the comment in kernel/sched/clock.c
  1872. */
  1873. extern u64 cpu_clock(int cpu);
  1874. extern u64 local_clock(void);
  1875. extern u64 running_clock(void);
  1876. extern u64 sched_clock_cpu(int cpu);
  1877. extern void sched_clock_init(void);
  1878. #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  1879. static inline void sched_clock_tick(void)
  1880. {
  1881. }
  1882. static inline void sched_clock_idle_sleep_event(void)
  1883. {
  1884. }
  1885. static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
  1886. {
  1887. }
  1888. #else
  1889. /*
  1890. * Architectures can set this to 1 if they have specified
  1891. * CONFIG_HAVE_UNSTABLE_SCHED_CLOCK in their arch Kconfig,
  1892. * but then during bootup it turns out that sched_clock()
  1893. * is reliable after all:
  1894. */
  1895. extern int sched_clock_stable(void);
  1896. extern void set_sched_clock_stable(void);
  1897. extern void clear_sched_clock_stable(void);
  1898. extern void sched_clock_tick(void);
  1899. extern void sched_clock_idle_sleep_event(void);
  1900. extern void sched_clock_idle_wakeup_event(u64 delta_ns);
  1901. #endif
  1902. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1903. /*
  1904. * An i/f to runtime opt-in for irq time accounting based off of sched_clock.
  1905. * The reason for this explicit opt-in is not to have perf penalty with
  1906. * slow sched_clocks.
  1907. */
  1908. extern void enable_sched_clock_irqtime(void);
  1909. extern void disable_sched_clock_irqtime(void);
  1910. #else
  1911. static inline void enable_sched_clock_irqtime(void) {}
  1912. static inline void disable_sched_clock_irqtime(void) {}
  1913. #endif
  1914. extern unsigned long long
  1915. task_sched_runtime(struct task_struct *task);
  1916. /* sched_exec is called by processes performing an exec */
  1917. #ifdef CONFIG_SMP
  1918. extern void sched_exec(void);
  1919. #else
  1920. #define sched_exec() {}
  1921. #endif
  1922. extern void sched_clock_idle_sleep_event(void);
  1923. extern void sched_clock_idle_wakeup_event(u64 delta_ns);
  1924. #ifdef CONFIG_HOTPLUG_CPU
  1925. extern void idle_task_exit(void);
  1926. #else
  1927. static inline void idle_task_exit(void) {}
  1928. #endif
  1929. #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
  1930. extern void wake_up_nohz_cpu(int cpu);
  1931. #else
  1932. static inline void wake_up_nohz_cpu(int cpu) { }
  1933. #endif
  1934. #ifdef CONFIG_NO_HZ_FULL
  1935. extern bool sched_can_stop_tick(void);
  1936. extern u64 scheduler_tick_max_deferment(void);
  1937. #else
  1938. static inline bool sched_can_stop_tick(void) { return false; }
  1939. #endif
  1940. #ifdef CONFIG_SCHED_AUTOGROUP
  1941. extern void sched_autogroup_create_attach(struct task_struct *p);
  1942. extern void sched_autogroup_detach(struct task_struct *p);
  1943. extern void sched_autogroup_fork(struct signal_struct *sig);
  1944. extern void sched_autogroup_exit(struct signal_struct *sig);
  1945. #ifdef CONFIG_PROC_FS
  1946. extern void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m);
  1947. extern int proc_sched_autogroup_set_nice(struct task_struct *p, int nice);
  1948. #endif
  1949. #else
  1950. static inline void sched_autogroup_create_attach(struct task_struct *p) { }
  1951. static inline void sched_autogroup_detach(struct task_struct *p) { }
  1952. static inline void sched_autogroup_fork(struct signal_struct *sig) { }
  1953. static inline void sched_autogroup_exit(struct signal_struct *sig) { }
  1954. #endif
  1955. extern int yield_to(struct task_struct *p, bool preempt);
  1956. extern void set_user_nice(struct task_struct *p, long nice);
  1957. extern int task_prio(const struct task_struct *p);
  1958. /**
  1959. * task_nice - return the nice value of a given task.
  1960. * @p: the task in question.
  1961. *
  1962. * Return: The nice value [ -20 ... 0 ... 19 ].
  1963. */
  1964. static inline int task_nice(const struct task_struct *p)
  1965. {
  1966. return PRIO_TO_NICE((p)->static_prio);
  1967. }
  1968. extern int can_nice(const struct task_struct *p, const int nice);
  1969. extern int task_curr(const struct task_struct *p);
  1970. extern int idle_cpu(int cpu);
  1971. extern int sched_setscheduler(struct task_struct *, int,
  1972. const struct sched_param *);
  1973. extern int sched_setscheduler_nocheck(struct task_struct *, int,
  1974. const struct sched_param *);
  1975. extern int sched_setattr(struct task_struct *,
  1976. const struct sched_attr *);
  1977. extern struct task_struct *idle_task(int cpu);
  1978. /**
  1979. * is_idle_task - is the specified task an idle task?
  1980. * @p: the task in question.
  1981. *
  1982. * Return: 1 if @p is an idle task. 0 otherwise.
  1983. */
  1984. static inline bool is_idle_task(const struct task_struct *p)
  1985. {
  1986. return p->pid == 0;
  1987. }
  1988. extern struct task_struct *curr_task(int cpu);
  1989. extern void set_curr_task(int cpu, struct task_struct *p);
  1990. void yield(void);
  1991. union thread_union {
  1992. struct thread_info thread_info;
  1993. unsigned long stack[THREAD_SIZE/sizeof(long)];
  1994. };
  1995. #ifndef __HAVE_ARCH_KSTACK_END
  1996. static inline int kstack_end(void *addr)
  1997. {
  1998. /* Reliable end of stack detection:
  1999. * Some APM bios versions misalign the stack
  2000. */
  2001. return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
  2002. }
  2003. #endif
  2004. extern union thread_union init_thread_union;
  2005. extern struct task_struct init_task;
  2006. extern struct mm_struct init_mm;
  2007. extern struct pid_namespace init_pid_ns;
  2008. /*
  2009. * find a task by one of its numerical ids
  2010. *
  2011. * find_task_by_pid_ns():
  2012. * finds a task by its pid in the specified namespace
  2013. * find_task_by_vpid():
  2014. * finds a task by its virtual pid
  2015. *
  2016. * see also find_vpid() etc in include/linux/pid.h
  2017. */
  2018. extern struct task_struct *find_task_by_vpid(pid_t nr);
  2019. extern struct task_struct *find_task_by_pid_ns(pid_t nr,
  2020. struct pid_namespace *ns);
  2021. /* per-UID process charging. */
  2022. extern struct user_struct * alloc_uid(kuid_t);
  2023. static inline struct user_struct *get_uid(struct user_struct *u)
  2024. {
  2025. atomic_inc(&u->__count);
  2026. return u;
  2027. }
  2028. extern void free_uid(struct user_struct *);
  2029. #include <asm/current.h>
  2030. extern void xtime_update(unsigned long ticks);
  2031. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  2032. extern int wake_up_process(struct task_struct *tsk);
  2033. extern void wake_up_new_task(struct task_struct *tsk);
  2034. #ifdef CONFIG_SMP
  2035. extern void kick_process(struct task_struct *tsk);
  2036. #else
  2037. static inline void kick_process(struct task_struct *tsk) { }
  2038. #endif
  2039. extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
  2040. extern void sched_dead(struct task_struct *p);
  2041. extern void proc_caches_init(void);
  2042. extern void flush_signals(struct task_struct *);
  2043. extern void __flush_signals(struct task_struct *);
  2044. extern void ignore_signals(struct task_struct *);
  2045. extern void flush_signal_handlers(struct task_struct *, int force_default);
  2046. extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
  2047. static inline int dequeue_signal_lock(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  2048. {
  2049. unsigned long flags;
  2050. int ret;
  2051. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  2052. ret = dequeue_signal(tsk, mask, info);
  2053. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  2054. return ret;
  2055. }
  2056. extern void block_all_signals(int (*notifier)(void *priv), void *priv,
  2057. sigset_t *mask);
  2058. extern void unblock_all_signals(void);
  2059. extern void release_task(struct task_struct * p);
  2060. extern int send_sig_info(int, struct siginfo *, struct task_struct *);
  2061. extern int force_sigsegv(int, struct task_struct *);
  2062. extern int force_sig_info(int, struct siginfo *, struct task_struct *);
  2063. extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
  2064. extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
  2065. extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
  2066. const struct cred *, u32);
  2067. extern int kill_pgrp(struct pid *pid, int sig, int priv);
  2068. extern int kill_pid(struct pid *pid, int sig, int priv);
  2069. extern int kill_proc_info(int, struct siginfo *, pid_t);
  2070. extern __must_check bool do_notify_parent(struct task_struct *, int);
  2071. extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
  2072. extern void force_sig(int, struct task_struct *);
  2073. extern int send_sig(int, struct task_struct *, int);
  2074. extern int zap_other_threads(struct task_struct *p);
  2075. extern struct sigqueue *sigqueue_alloc(void);
  2076. extern void sigqueue_free(struct sigqueue *);
  2077. extern int send_sigqueue(struct sigqueue *, struct task_struct *, int group);
  2078. extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
  2079. static inline void restore_saved_sigmask(void)
  2080. {
  2081. if (test_and_clear_restore_sigmask())
  2082. __set_current_blocked(&current->saved_sigmask);
  2083. }
  2084. static inline sigset_t *sigmask_to_save(void)
  2085. {
  2086. sigset_t *res = &current->blocked;
  2087. if (unlikely(test_restore_sigmask()))
  2088. res = &current->saved_sigmask;
  2089. return res;
  2090. }
  2091. static inline int kill_cad_pid(int sig, int priv)
  2092. {
  2093. return kill_pid(cad_pid, sig, priv);
  2094. }
  2095. /* These can be the second arg to send_sig_info/send_group_sig_info. */
  2096. #define SEND_SIG_NOINFO ((struct siginfo *) 0)
  2097. #define SEND_SIG_PRIV ((struct siginfo *) 1)
  2098. #define SEND_SIG_FORCED ((struct siginfo *) 2)
  2099. /*
  2100. * True if we are on the alternate signal stack.
  2101. */
  2102. static inline int on_sig_stack(unsigned long sp)
  2103. {
  2104. #ifdef CONFIG_STACK_GROWSUP
  2105. return sp >= current->sas_ss_sp &&
  2106. sp - current->sas_ss_sp < current->sas_ss_size;
  2107. #else
  2108. return sp > current->sas_ss_sp &&
  2109. sp - current->sas_ss_sp <= current->sas_ss_size;
  2110. #endif
  2111. }
  2112. static inline int sas_ss_flags(unsigned long sp)
  2113. {
  2114. if (!current->sas_ss_size)
  2115. return SS_DISABLE;
  2116. return on_sig_stack(sp) ? SS_ONSTACK : 0;
  2117. }
  2118. static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
  2119. {
  2120. if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
  2121. #ifdef CONFIG_STACK_GROWSUP
  2122. return current->sas_ss_sp;
  2123. #else
  2124. return current->sas_ss_sp + current->sas_ss_size;
  2125. #endif
  2126. return sp;
  2127. }
  2128. /*
  2129. * Routines for handling mm_structs
  2130. */
  2131. extern struct mm_struct * mm_alloc(void);
  2132. /* mmdrop drops the mm and the page tables */
  2133. extern void __mmdrop(struct mm_struct *);
  2134. static inline void mmdrop(struct mm_struct * mm)
  2135. {
  2136. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  2137. __mmdrop(mm);
  2138. }
  2139. /* mmput gets rid of the mappings and all user-space */
  2140. extern void mmput(struct mm_struct *);
  2141. /* Grab a reference to a task's mm, if it is not already going away */
  2142. extern struct mm_struct *get_task_mm(struct task_struct *task);
  2143. /*
  2144. * Grab a reference to a task's mm, if it is not already going away
  2145. * and ptrace_may_access with the mode parameter passed to it
  2146. * succeeds.
  2147. */
  2148. extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
  2149. /* Remove the current tasks stale references to the old mm_struct */
  2150. extern void mm_release(struct task_struct *, struct mm_struct *);
  2151. extern int copy_thread(unsigned long, unsigned long, unsigned long,
  2152. struct task_struct *);
  2153. extern void flush_thread(void);
  2154. extern void exit_thread(void);
  2155. extern void exit_files(struct task_struct *);
  2156. extern void __cleanup_sighand(struct sighand_struct *);
  2157. extern void exit_itimers(struct signal_struct *);
  2158. extern void flush_itimer_signals(void);
  2159. extern void do_group_exit(int);
  2160. extern int do_execve(struct filename *,
  2161. const char __user * const __user *,
  2162. const char __user * const __user *);
  2163. extern int do_execveat(int, struct filename *,
  2164. const char __user * const __user *,
  2165. const char __user * const __user *,
  2166. int);
  2167. extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
  2168. struct task_struct *fork_idle(int);
  2169. extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  2170. extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
  2171. static inline void set_task_comm(struct task_struct *tsk, const char *from)
  2172. {
  2173. __set_task_comm(tsk, from, false);
  2174. }
  2175. extern char *get_task_comm(char *to, struct task_struct *tsk);
  2176. #ifdef CONFIG_SMP
  2177. void scheduler_ipi(void);
  2178. extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
  2179. #else
  2180. static inline void scheduler_ipi(void) { }
  2181. static inline unsigned long wait_task_inactive(struct task_struct *p,
  2182. long match_state)
  2183. {
  2184. return 1;
  2185. }
  2186. #endif
  2187. #define tasklist_empty() \
  2188. list_empty(&init_task.tasks)
  2189. #define next_task(p) \
  2190. list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
  2191. #define for_each_process(p) \
  2192. for (p = &init_task ; (p = next_task(p)) != &init_task ; )
  2193. extern bool current_is_single_threaded(void);
  2194. /*
  2195. * Careful: do_each_thread/while_each_thread is a double loop so
  2196. * 'break' will not work as expected - use goto instead.
  2197. */
  2198. #define do_each_thread(g, t) \
  2199. for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
  2200. #define while_each_thread(g, t) \
  2201. while ((t = next_thread(t)) != g)
  2202. #define __for_each_thread(signal, t) \
  2203. list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
  2204. #define for_each_thread(p, t) \
  2205. __for_each_thread((p)->signal, t)
  2206. /* Careful: this is a double loop, 'break' won't work as expected. */
  2207. #define for_each_process_thread(p, t) \
  2208. for_each_process(p) for_each_thread(p, t)
  2209. static inline int get_nr_threads(struct task_struct *tsk)
  2210. {
  2211. return tsk->signal->nr_threads;
  2212. }
  2213. static inline bool thread_group_leader(struct task_struct *p)
  2214. {
  2215. return p->exit_signal >= 0;
  2216. }
  2217. /* Do to the insanities of de_thread it is possible for a process
  2218. * to have the pid of the thread group leader without actually being
  2219. * the thread group leader. For iteration through the pids in proc
  2220. * all we care about is that we have a task with the appropriate
  2221. * pid, we don't actually care if we have the right task.
  2222. */
  2223. static inline bool has_group_leader_pid(struct task_struct *p)
  2224. {
  2225. return task_pid(p) == p->signal->leader_pid;
  2226. }
  2227. static inline
  2228. bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
  2229. {
  2230. return p1->signal == p2->signal;
  2231. }
  2232. static inline struct task_struct *next_thread(const struct task_struct *p)
  2233. {
  2234. return list_entry_rcu(p->thread_group.next,
  2235. struct task_struct, thread_group);
  2236. }
  2237. static inline int thread_group_empty(struct task_struct *p)
  2238. {
  2239. return list_empty(&p->thread_group);
  2240. }
  2241. #define delay_group_leader(p) \
  2242. (thread_group_leader(p) && !thread_group_empty(p))
  2243. /*
  2244. * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
  2245. * subscriptions and synchronises with wait4(). Also used in procfs. Also
  2246. * pins the final release of task.io_context. Also protects ->cpuset and
  2247. * ->cgroup.subsys[]. And ->vfork_done.
  2248. *
  2249. * Nests both inside and outside of read_lock(&tasklist_lock).
  2250. * It must not be nested with write_lock_irq(&tasklist_lock),
  2251. * neither inside nor outside.
  2252. */
  2253. static inline void task_lock(struct task_struct *p)
  2254. {
  2255. spin_lock(&p->alloc_lock);
  2256. }
  2257. static inline void task_unlock(struct task_struct *p)
  2258. {
  2259. spin_unlock(&p->alloc_lock);
  2260. }
  2261. extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  2262. unsigned long *flags);
  2263. static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
  2264. unsigned long *flags)
  2265. {
  2266. struct sighand_struct *ret;
  2267. ret = __lock_task_sighand(tsk, flags);
  2268. (void)__cond_lock(&tsk->sighand->siglock, ret);
  2269. return ret;
  2270. }
  2271. static inline void unlock_task_sighand(struct task_struct *tsk,
  2272. unsigned long *flags)
  2273. {
  2274. spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
  2275. }
  2276. #ifdef CONFIG_CGROUPS
  2277. static inline void threadgroup_change_begin(struct task_struct *tsk)
  2278. {
  2279. down_read(&tsk->signal->group_rwsem);
  2280. }
  2281. static inline void threadgroup_change_end(struct task_struct *tsk)
  2282. {
  2283. up_read(&tsk->signal->group_rwsem);
  2284. }
  2285. /**
  2286. * threadgroup_lock - lock threadgroup
  2287. * @tsk: member task of the threadgroup to lock
  2288. *
  2289. * Lock the threadgroup @tsk belongs to. No new task is allowed to enter
  2290. * and member tasks aren't allowed to exit (as indicated by PF_EXITING) or
  2291. * change ->group_leader/pid. This is useful for cases where the threadgroup
  2292. * needs to stay stable across blockable operations.
  2293. *
  2294. * fork and exit paths explicitly call threadgroup_change_{begin|end}() for
  2295. * synchronization. While held, no new task will be added to threadgroup
  2296. * and no existing live task will have its PF_EXITING set.
  2297. *
  2298. * de_thread() does threadgroup_change_{begin|end}() when a non-leader
  2299. * sub-thread becomes a new leader.
  2300. */
  2301. static inline void threadgroup_lock(struct task_struct *tsk)
  2302. {
  2303. down_write(&tsk->signal->group_rwsem);
  2304. }
  2305. /**
  2306. * threadgroup_unlock - unlock threadgroup
  2307. * @tsk: member task of the threadgroup to unlock
  2308. *
  2309. * Reverse threadgroup_lock().
  2310. */
  2311. static inline void threadgroup_unlock(struct task_struct *tsk)
  2312. {
  2313. up_write(&tsk->signal->group_rwsem);
  2314. }
  2315. #else
  2316. static inline void threadgroup_change_begin(struct task_struct *tsk) {}
  2317. static inline void threadgroup_change_end(struct task_struct *tsk) {}
  2318. static inline void threadgroup_lock(struct task_struct *tsk) {}
  2319. static inline void threadgroup_unlock(struct task_struct *tsk) {}
  2320. #endif
  2321. #ifndef __HAVE_THREAD_FUNCTIONS
  2322. #define task_thread_info(task) ((struct thread_info *)(task)->stack)
  2323. #define task_stack_page(task) ((task)->stack)
  2324. static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
  2325. {
  2326. *task_thread_info(p) = *task_thread_info(org);
  2327. task_thread_info(p)->task = p;
  2328. }
  2329. /*
  2330. * Return the address of the last usable long on the stack.
  2331. *
  2332. * When the stack grows down, this is just above the thread
  2333. * info struct. Going any lower will corrupt the threadinfo.
  2334. *
  2335. * When the stack grows up, this is the highest address.
  2336. * Beyond that position, we corrupt data on the next page.
  2337. */
  2338. static inline unsigned long *end_of_stack(struct task_struct *p)
  2339. {
  2340. #ifdef CONFIG_STACK_GROWSUP
  2341. return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
  2342. #else
  2343. return (unsigned long *)(task_thread_info(p) + 1);
  2344. #endif
  2345. }
  2346. #endif
  2347. #define task_stack_end_corrupted(task) \
  2348. (*(end_of_stack(task)) != STACK_END_MAGIC)
  2349. static inline int object_is_on_stack(void *obj)
  2350. {
  2351. void *stack = task_stack_page(current);
  2352. return (obj >= stack) && (obj < (stack + THREAD_SIZE));
  2353. }
  2354. extern void thread_info_cache_init(void);
  2355. #ifdef CONFIG_DEBUG_STACK_USAGE
  2356. static inline unsigned long stack_not_used(struct task_struct *p)
  2357. {
  2358. unsigned long *n = end_of_stack(p);
  2359. do { /* Skip over canary */
  2360. n++;
  2361. } while (!*n);
  2362. return (unsigned long)n - (unsigned long)end_of_stack(p);
  2363. }
  2364. #endif
  2365. extern void set_task_stack_end_magic(struct task_struct *tsk);
  2366. /* set thread flags in other task's structures
  2367. * - see asm/thread_info.h for TIF_xxxx flags available
  2368. */
  2369. static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
  2370. {
  2371. set_ti_thread_flag(task_thread_info(tsk), flag);
  2372. }
  2373. static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  2374. {
  2375. clear_ti_thread_flag(task_thread_info(tsk), flag);
  2376. }
  2377. static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
  2378. {
  2379. return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
  2380. }
  2381. static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  2382. {
  2383. return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
  2384. }
  2385. static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
  2386. {
  2387. return test_ti_thread_flag(task_thread_info(tsk), flag);
  2388. }
  2389. static inline void set_tsk_need_resched(struct task_struct *tsk)
  2390. {
  2391. set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  2392. }
  2393. static inline void clear_tsk_need_resched(struct task_struct *tsk)
  2394. {
  2395. clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  2396. }
  2397. static inline int test_tsk_need_resched(struct task_struct *tsk)
  2398. {
  2399. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  2400. }
  2401. static inline int restart_syscall(void)
  2402. {
  2403. set_tsk_thread_flag(current, TIF_SIGPENDING);
  2404. return -ERESTARTNOINTR;
  2405. }
  2406. static inline int signal_pending(struct task_struct *p)
  2407. {
  2408. return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
  2409. }
  2410. static inline int __fatal_signal_pending(struct task_struct *p)
  2411. {
  2412. return unlikely(sigismember(&p->pending.signal, SIGKILL));
  2413. }
  2414. static inline int fatal_signal_pending(struct task_struct *p)
  2415. {
  2416. return signal_pending(p) && __fatal_signal_pending(p);
  2417. }
  2418. static inline int signal_pending_state(long state, struct task_struct *p)
  2419. {
  2420. if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
  2421. return 0;
  2422. if (!signal_pending(p))
  2423. return 0;
  2424. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  2425. }
  2426. /*
  2427. * cond_resched() and cond_resched_lock(): latency reduction via
  2428. * explicit rescheduling in places that are safe. The return
  2429. * value indicates whether a reschedule was done in fact.
  2430. * cond_resched_lock() will drop the spinlock before scheduling,
  2431. * cond_resched_softirq() will enable bhs before scheduling.
  2432. */
  2433. extern int _cond_resched(void);
  2434. #define cond_resched() ({ \
  2435. ___might_sleep(__FILE__, __LINE__, 0); \
  2436. _cond_resched(); \
  2437. })
  2438. extern int __cond_resched_lock(spinlock_t *lock);
  2439. #ifdef CONFIG_PREEMPT_COUNT
  2440. #define PREEMPT_LOCK_OFFSET PREEMPT_OFFSET
  2441. #else
  2442. #define PREEMPT_LOCK_OFFSET 0
  2443. #endif
  2444. #define cond_resched_lock(lock) ({ \
  2445. ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
  2446. __cond_resched_lock(lock); \
  2447. })
  2448. extern int __cond_resched_softirq(void);
  2449. #define cond_resched_softirq() ({ \
  2450. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  2451. __cond_resched_softirq(); \
  2452. })
  2453. static inline void cond_resched_rcu(void)
  2454. {
  2455. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
  2456. rcu_read_unlock();
  2457. cond_resched();
  2458. rcu_read_lock();
  2459. #endif
  2460. }
  2461. /*
  2462. * Does a critical section need to be broken due to another
  2463. * task waiting?: (technically does not depend on CONFIG_PREEMPT,
  2464. * but a general need for low latency)
  2465. */
  2466. static inline int spin_needbreak(spinlock_t *lock)
  2467. {
  2468. #ifdef CONFIG_PREEMPT
  2469. return spin_is_contended(lock);
  2470. #else
  2471. return 0;
  2472. #endif
  2473. }
  2474. /*
  2475. * Idle thread specific functions to determine the need_resched
  2476. * polling state.
  2477. */
  2478. #ifdef TIF_POLLING_NRFLAG
  2479. static inline int tsk_is_polling(struct task_struct *p)
  2480. {
  2481. return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG);
  2482. }
  2483. static inline void __current_set_polling(void)
  2484. {
  2485. set_thread_flag(TIF_POLLING_NRFLAG);
  2486. }
  2487. static inline bool __must_check current_set_polling_and_test(void)
  2488. {
  2489. __current_set_polling();
  2490. /*
  2491. * Polling state must be visible before we test NEED_RESCHED,
  2492. * paired by resched_curr()
  2493. */
  2494. smp_mb__after_atomic();
  2495. return unlikely(tif_need_resched());
  2496. }
  2497. static inline void __current_clr_polling(void)
  2498. {
  2499. clear_thread_flag(TIF_POLLING_NRFLAG);
  2500. }
  2501. static inline bool __must_check current_clr_polling_and_test(void)
  2502. {
  2503. __current_clr_polling();
  2504. /*
  2505. * Polling state must be visible before we test NEED_RESCHED,
  2506. * paired by resched_curr()
  2507. */
  2508. smp_mb__after_atomic();
  2509. return unlikely(tif_need_resched());
  2510. }
  2511. #else
  2512. static inline int tsk_is_polling(struct task_struct *p) { return 0; }
  2513. static inline void __current_set_polling(void) { }
  2514. static inline void __current_clr_polling(void) { }
  2515. static inline bool __must_check current_set_polling_and_test(void)
  2516. {
  2517. return unlikely(tif_need_resched());
  2518. }
  2519. static inline bool __must_check current_clr_polling_and_test(void)
  2520. {
  2521. return unlikely(tif_need_resched());
  2522. }
  2523. #endif
  2524. static inline void current_clr_polling(void)
  2525. {
  2526. __current_clr_polling();
  2527. /*
  2528. * Ensure we check TIF_NEED_RESCHED after we clear the polling bit.
  2529. * Once the bit is cleared, we'll get IPIs with every new
  2530. * TIF_NEED_RESCHED and the IPI handler, scheduler_ipi(), will also
  2531. * fold.
  2532. */
  2533. smp_mb(); /* paired with resched_curr() */
  2534. preempt_fold_need_resched();
  2535. }
  2536. static __always_inline bool need_resched(void)
  2537. {
  2538. return unlikely(tif_need_resched());
  2539. }
  2540. /*
  2541. * Thread group CPU time accounting.
  2542. */
  2543. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
  2544. void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
  2545. static inline void thread_group_cputime_init(struct signal_struct *sig)
  2546. {
  2547. raw_spin_lock_init(&sig->cputimer.lock);
  2548. }
  2549. /*
  2550. * Reevaluate whether the task has signals pending delivery.
  2551. * Wake the task if so.
  2552. * This is required every time the blocked sigset_t changes.
  2553. * callers must hold sighand->siglock.
  2554. */
  2555. extern void recalc_sigpending_and_wake(struct task_struct *t);
  2556. extern void recalc_sigpending(void);
  2557. extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
  2558. static inline void signal_wake_up(struct task_struct *t, bool resume)
  2559. {
  2560. signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
  2561. }
  2562. static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
  2563. {
  2564. signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
  2565. }
  2566. /*
  2567. * Wrappers for p->thread_info->cpu access. No-op on UP.
  2568. */
  2569. #ifdef CONFIG_SMP
  2570. static inline unsigned int task_cpu(const struct task_struct *p)
  2571. {
  2572. return task_thread_info(p)->cpu;
  2573. }
  2574. static inline int task_node(const struct task_struct *p)
  2575. {
  2576. return cpu_to_node(task_cpu(p));
  2577. }
  2578. extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
  2579. #else
  2580. static inline unsigned int task_cpu(const struct task_struct *p)
  2581. {
  2582. return 0;
  2583. }
  2584. static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  2585. {
  2586. }
  2587. #endif /* CONFIG_SMP */
  2588. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  2589. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  2590. #ifdef CONFIG_CGROUP_SCHED
  2591. extern struct task_group root_task_group;
  2592. #endif /* CONFIG_CGROUP_SCHED */
  2593. extern int task_can_switch_user(struct user_struct *up,
  2594. struct task_struct *tsk);
  2595. #ifdef CONFIG_TASK_XACCT
  2596. static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
  2597. {
  2598. tsk->ioac.rchar += amt;
  2599. }
  2600. static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
  2601. {
  2602. tsk->ioac.wchar += amt;
  2603. }
  2604. static inline void inc_syscr(struct task_struct *tsk)
  2605. {
  2606. tsk->ioac.syscr++;
  2607. }
  2608. static inline void inc_syscw(struct task_struct *tsk)
  2609. {
  2610. tsk->ioac.syscw++;
  2611. }
  2612. #else
  2613. static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
  2614. {
  2615. }
  2616. static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
  2617. {
  2618. }
  2619. static inline void inc_syscr(struct task_struct *tsk)
  2620. {
  2621. }
  2622. static inline void inc_syscw(struct task_struct *tsk)
  2623. {
  2624. }
  2625. #endif
  2626. #ifndef TASK_SIZE_OF
  2627. #define TASK_SIZE_OF(tsk) TASK_SIZE
  2628. #endif
  2629. #ifdef CONFIG_MEMCG
  2630. extern void mm_update_next_owner(struct mm_struct *mm);
  2631. #else
  2632. static inline void mm_update_next_owner(struct mm_struct *mm)
  2633. {
  2634. }
  2635. #endif /* CONFIG_MEMCG */
  2636. static inline unsigned long task_rlimit(const struct task_struct *tsk,
  2637. unsigned int limit)
  2638. {
  2639. return ACCESS_ONCE(tsk->signal->rlim[limit].rlim_cur);
  2640. }
  2641. static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
  2642. unsigned int limit)
  2643. {
  2644. return ACCESS_ONCE(tsk->signal->rlim[limit].rlim_max);
  2645. }
  2646. static inline unsigned long rlimit(unsigned int limit)
  2647. {
  2648. return task_rlimit(current, limit);
  2649. }
  2650. static inline unsigned long rlimit_max(unsigned int limit)
  2651. {
  2652. return task_rlimit_max(current, limit);
  2653. }
  2654. #endif