sched.h 90 KB

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