sched.h 104 KB

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