sched_fair.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*
  2. * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Interactivity improvements by Mike Galbraith
  7. * (C) 2007 Mike Galbraith <efault@gmx.de>
  8. *
  9. * Various enhancements by Dmitry Adamushko.
  10. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  11. *
  12. * Group scheduling enhancements by Srivatsa Vaddagiri
  13. * Copyright IBM Corporation, 2007
  14. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  15. *
  16. * Scaled math optimizations by Thomas Gleixner
  17. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  18. *
  19. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  20. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  21. */
  22. #include <linux/latencytop.h>
  23. #include <linux/sched.h>
  24. /*
  25. * Targeted preemption latency for CPU-bound tasks:
  26. * (default: 5ms * (1 + ilog(ncpus)), units: nanoseconds)
  27. *
  28. * NOTE: this latency value is not the same as the concept of
  29. * 'timeslice length' - timeslices in CFS are of variable length
  30. * and have no persistent notion like in traditional, time-slice
  31. * based scheduling concepts.
  32. *
  33. * (to see the precise effective timeslice length of your workload,
  34. * run vmstat and monitor the context-switches (cs) field)
  35. */
  36. unsigned int sysctl_sched_latency = 5000000ULL;
  37. unsigned int normalized_sysctl_sched_latency = 5000000ULL;
  38. /*
  39. * The initial- and re-scaling of tunables is configurable
  40. * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
  41. *
  42. * Options are:
  43. * SCHED_TUNABLESCALING_NONE - unscaled, always *1
  44. * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
  45. * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
  46. */
  47. enum sched_tunable_scaling sysctl_sched_tunable_scaling
  48. = SCHED_TUNABLESCALING_LOG;
  49. /*
  50. * Minimal preemption granularity for CPU-bound tasks:
  51. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  52. */
  53. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  54. unsigned int normalized_sysctl_sched_min_granularity = 1000000ULL;
  55. /*
  56. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  57. */
  58. static unsigned int sched_nr_latency = 5;
  59. /*
  60. * After fork, child runs first. If set to 0 (default) then
  61. * parent will (try to) run first.
  62. */
  63. unsigned int sysctl_sched_child_runs_first __read_mostly;
  64. /*
  65. * sys_sched_yield() compat mode
  66. *
  67. * This option switches the agressive yield implementation of the
  68. * old scheduler back on.
  69. */
  70. unsigned int __read_mostly sysctl_sched_compat_yield;
  71. /*
  72. * SCHED_OTHER wake-up granularity.
  73. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  74. *
  75. * This option delays the preemption effects of decoupled workloads
  76. * and reduces their over-scheduling. Synchronous workloads will still
  77. * have immediate wakeup/sleep latencies.
  78. */
  79. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  80. unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
  81. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  82. static const struct sched_class fair_sched_class;
  83. /**************************************************************
  84. * CFS operations on generic schedulable entities:
  85. */
  86. #ifdef CONFIG_FAIR_GROUP_SCHED
  87. /* cpu runqueue to which this cfs_rq is attached */
  88. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  89. {
  90. return cfs_rq->rq;
  91. }
  92. /* An entity is a task if it doesn't "own" a runqueue */
  93. #define entity_is_task(se) (!se->my_q)
  94. static inline struct task_struct *task_of(struct sched_entity *se)
  95. {
  96. #ifdef CONFIG_SCHED_DEBUG
  97. WARN_ON_ONCE(!entity_is_task(se));
  98. #endif
  99. return container_of(se, struct task_struct, se);
  100. }
  101. /* Walk up scheduling entities hierarchy */
  102. #define for_each_sched_entity(se) \
  103. for (; se; se = se->parent)
  104. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  105. {
  106. return p->se.cfs_rq;
  107. }
  108. /* runqueue on which this entity is (to be) queued */
  109. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  110. {
  111. return se->cfs_rq;
  112. }
  113. /* runqueue "owned" by this group */
  114. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  115. {
  116. return grp->my_q;
  117. }
  118. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  119. * another cpu ('this_cpu')
  120. */
  121. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  122. {
  123. return cfs_rq->tg->cfs_rq[this_cpu];
  124. }
  125. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  126. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  127. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  128. /* Do the two (enqueued) entities belong to the same group ? */
  129. static inline int
  130. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  131. {
  132. if (se->cfs_rq == pse->cfs_rq)
  133. return 1;
  134. return 0;
  135. }
  136. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  137. {
  138. return se->parent;
  139. }
  140. /* return depth at which a sched entity is present in the hierarchy */
  141. static inline int depth_se(struct sched_entity *se)
  142. {
  143. int depth = 0;
  144. for_each_sched_entity(se)
  145. depth++;
  146. return depth;
  147. }
  148. static void
  149. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  150. {
  151. int se_depth, pse_depth;
  152. /*
  153. * preemption test can be made between sibling entities who are in the
  154. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  155. * both tasks until we find their ancestors who are siblings of common
  156. * parent.
  157. */
  158. /* First walk up until both entities are at same depth */
  159. se_depth = depth_se(*se);
  160. pse_depth = depth_se(*pse);
  161. while (se_depth > pse_depth) {
  162. se_depth--;
  163. *se = parent_entity(*se);
  164. }
  165. while (pse_depth > se_depth) {
  166. pse_depth--;
  167. *pse = parent_entity(*pse);
  168. }
  169. while (!is_same_group(*se, *pse)) {
  170. *se = parent_entity(*se);
  171. *pse = parent_entity(*pse);
  172. }
  173. }
  174. #else /* !CONFIG_FAIR_GROUP_SCHED */
  175. static inline struct task_struct *task_of(struct sched_entity *se)
  176. {
  177. return container_of(se, struct task_struct, se);
  178. }
  179. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  180. {
  181. return container_of(cfs_rq, struct rq, cfs);
  182. }
  183. #define entity_is_task(se) 1
  184. #define for_each_sched_entity(se) \
  185. for (; se; se = NULL)
  186. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  187. {
  188. return &task_rq(p)->cfs;
  189. }
  190. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  191. {
  192. struct task_struct *p = task_of(se);
  193. struct rq *rq = task_rq(p);
  194. return &rq->cfs;
  195. }
  196. /* runqueue "owned" by this group */
  197. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  198. {
  199. return NULL;
  200. }
  201. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  202. {
  203. return &cpu_rq(this_cpu)->cfs;
  204. }
  205. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  206. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  207. static inline int
  208. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  209. {
  210. return 1;
  211. }
  212. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  213. {
  214. return NULL;
  215. }
  216. static inline void
  217. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  218. {
  219. }
  220. #endif /* CONFIG_FAIR_GROUP_SCHED */
  221. /**************************************************************
  222. * Scheduling class tree data structure manipulation methods:
  223. */
  224. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  225. {
  226. s64 delta = (s64)(vruntime - min_vruntime);
  227. if (delta > 0)
  228. min_vruntime = vruntime;
  229. return min_vruntime;
  230. }
  231. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  232. {
  233. s64 delta = (s64)(vruntime - min_vruntime);
  234. if (delta < 0)
  235. min_vruntime = vruntime;
  236. return min_vruntime;
  237. }
  238. static inline int entity_before(struct sched_entity *a,
  239. struct sched_entity *b)
  240. {
  241. return (s64)(a->vruntime - b->vruntime) < 0;
  242. }
  243. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  244. {
  245. return se->vruntime - cfs_rq->min_vruntime;
  246. }
  247. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  248. {
  249. u64 vruntime = cfs_rq->min_vruntime;
  250. if (cfs_rq->curr)
  251. vruntime = cfs_rq->curr->vruntime;
  252. if (cfs_rq->rb_leftmost) {
  253. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  254. struct sched_entity,
  255. run_node);
  256. if (!cfs_rq->curr)
  257. vruntime = se->vruntime;
  258. else
  259. vruntime = min_vruntime(vruntime, se->vruntime);
  260. }
  261. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  262. }
  263. /*
  264. * Enqueue an entity into the rb-tree:
  265. */
  266. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  267. {
  268. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  269. struct rb_node *parent = NULL;
  270. struct sched_entity *entry;
  271. s64 key = entity_key(cfs_rq, se);
  272. int leftmost = 1;
  273. /*
  274. * Find the right place in the rbtree:
  275. */
  276. while (*link) {
  277. parent = *link;
  278. entry = rb_entry(parent, struct sched_entity, run_node);
  279. /*
  280. * We dont care about collisions. Nodes with
  281. * the same key stay together.
  282. */
  283. if (key < entity_key(cfs_rq, entry)) {
  284. link = &parent->rb_left;
  285. } else {
  286. link = &parent->rb_right;
  287. leftmost = 0;
  288. }
  289. }
  290. /*
  291. * Maintain a cache of leftmost tree entries (it is frequently
  292. * used):
  293. */
  294. if (leftmost)
  295. cfs_rq->rb_leftmost = &se->run_node;
  296. rb_link_node(&se->run_node, parent, link);
  297. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  298. }
  299. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  300. {
  301. if (cfs_rq->rb_leftmost == &se->run_node) {
  302. struct rb_node *next_node;
  303. next_node = rb_next(&se->run_node);
  304. cfs_rq->rb_leftmost = next_node;
  305. }
  306. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  307. }
  308. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  309. {
  310. struct rb_node *left = cfs_rq->rb_leftmost;
  311. if (!left)
  312. return NULL;
  313. return rb_entry(left, struct sched_entity, run_node);
  314. }
  315. static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  316. {
  317. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  318. if (!last)
  319. return NULL;
  320. return rb_entry(last, struct sched_entity, run_node);
  321. }
  322. /**************************************************************
  323. * Scheduling class statistics methods:
  324. */
  325. #ifdef CONFIG_SCHED_DEBUG
  326. int sched_proc_update_handler(struct ctl_table *table, int write,
  327. void __user *buffer, size_t *lenp,
  328. loff_t *ppos)
  329. {
  330. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  331. int factor = get_update_sysctl_factor();
  332. if (ret || !write)
  333. return ret;
  334. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  335. sysctl_sched_min_granularity);
  336. #define WRT_SYSCTL(name) \
  337. (normalized_sysctl_##name = sysctl_##name / (factor))
  338. WRT_SYSCTL(sched_min_granularity);
  339. WRT_SYSCTL(sched_latency);
  340. WRT_SYSCTL(sched_wakeup_granularity);
  341. WRT_SYSCTL(sched_shares_ratelimit);
  342. #undef WRT_SYSCTL
  343. return 0;
  344. }
  345. #endif
  346. /*
  347. * delta /= w
  348. */
  349. static inline unsigned long
  350. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  351. {
  352. if (unlikely(se->load.weight != NICE_0_LOAD))
  353. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  354. return delta;
  355. }
  356. /*
  357. * The idea is to set a period in which each task runs once.
  358. *
  359. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  360. * this period because otherwise the slices get too small.
  361. *
  362. * p = (nr <= nl) ? l : l*nr/nl
  363. */
  364. static u64 __sched_period(unsigned long nr_running)
  365. {
  366. u64 period = sysctl_sched_latency;
  367. unsigned long nr_latency = sched_nr_latency;
  368. if (unlikely(nr_running > nr_latency)) {
  369. period = sysctl_sched_min_granularity;
  370. period *= nr_running;
  371. }
  372. return period;
  373. }
  374. /*
  375. * We calculate the wall-time slice from the period by taking a part
  376. * proportional to the weight.
  377. *
  378. * s = p*P[w/rw]
  379. */
  380. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  381. {
  382. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  383. for_each_sched_entity(se) {
  384. struct load_weight *load;
  385. struct load_weight lw;
  386. cfs_rq = cfs_rq_of(se);
  387. load = &cfs_rq->load;
  388. if (unlikely(!se->on_rq)) {
  389. lw = cfs_rq->load;
  390. update_load_add(&lw, se->load.weight);
  391. load = &lw;
  392. }
  393. slice = calc_delta_mine(slice, se->load.weight, load);
  394. }
  395. return slice;
  396. }
  397. /*
  398. * We calculate the vruntime slice of a to be inserted task
  399. *
  400. * vs = s/w
  401. */
  402. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  403. {
  404. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  405. }
  406. /*
  407. * Update the current task's runtime statistics. Skip current tasks that
  408. * are not in our scheduling class.
  409. */
  410. static inline void
  411. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  412. unsigned long delta_exec)
  413. {
  414. unsigned long delta_exec_weighted;
  415. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  416. curr->sum_exec_runtime += delta_exec;
  417. schedstat_add(cfs_rq, exec_clock, delta_exec);
  418. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  419. curr->vruntime += delta_exec_weighted;
  420. update_min_vruntime(cfs_rq);
  421. }
  422. static void update_curr(struct cfs_rq *cfs_rq)
  423. {
  424. struct sched_entity *curr = cfs_rq->curr;
  425. u64 now = rq_of(cfs_rq)->clock;
  426. unsigned long delta_exec;
  427. if (unlikely(!curr))
  428. return;
  429. /*
  430. * Get the amount of time the current task was running
  431. * since the last time we changed load (this cannot
  432. * overflow on 32 bits):
  433. */
  434. delta_exec = (unsigned long)(now - curr->exec_start);
  435. if (!delta_exec)
  436. return;
  437. __update_curr(cfs_rq, curr, delta_exec);
  438. curr->exec_start = now;
  439. if (entity_is_task(curr)) {
  440. struct task_struct *curtask = task_of(curr);
  441. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  442. cpuacct_charge(curtask, delta_exec);
  443. account_group_exec_runtime(curtask, delta_exec);
  444. }
  445. }
  446. static inline void
  447. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  448. {
  449. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  450. }
  451. /*
  452. * Task is being enqueued - update stats:
  453. */
  454. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  455. {
  456. /*
  457. * Are we enqueueing a waiting task? (for current tasks
  458. * a dequeue/enqueue event is a NOP)
  459. */
  460. if (se != cfs_rq->curr)
  461. update_stats_wait_start(cfs_rq, se);
  462. }
  463. static void
  464. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  465. {
  466. schedstat_set(se->wait_max, max(se->wait_max,
  467. rq_of(cfs_rq)->clock - se->wait_start));
  468. schedstat_set(se->wait_count, se->wait_count + 1);
  469. schedstat_set(se->wait_sum, se->wait_sum +
  470. rq_of(cfs_rq)->clock - se->wait_start);
  471. #ifdef CONFIG_SCHEDSTATS
  472. if (entity_is_task(se)) {
  473. trace_sched_stat_wait(task_of(se),
  474. rq_of(cfs_rq)->clock - se->wait_start);
  475. }
  476. #endif
  477. schedstat_set(se->wait_start, 0);
  478. }
  479. static inline void
  480. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  481. {
  482. /*
  483. * Mark the end of the wait period if dequeueing a
  484. * waiting task:
  485. */
  486. if (se != cfs_rq->curr)
  487. update_stats_wait_end(cfs_rq, se);
  488. }
  489. /*
  490. * We are picking a new current task - update its stats:
  491. */
  492. static inline void
  493. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  494. {
  495. /*
  496. * We are starting a new run period:
  497. */
  498. se->exec_start = rq_of(cfs_rq)->clock;
  499. }
  500. /**************************************************
  501. * Scheduling class queueing methods:
  502. */
  503. #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
  504. static void
  505. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  506. {
  507. cfs_rq->task_weight += weight;
  508. }
  509. #else
  510. static inline void
  511. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  512. {
  513. }
  514. #endif
  515. static void
  516. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  517. {
  518. update_load_add(&cfs_rq->load, se->load.weight);
  519. if (!parent_entity(se))
  520. inc_cpu_load(rq_of(cfs_rq), se->load.weight);
  521. if (entity_is_task(se)) {
  522. add_cfs_task_weight(cfs_rq, se->load.weight);
  523. list_add(&se->group_node, &cfs_rq->tasks);
  524. }
  525. cfs_rq->nr_running++;
  526. se->on_rq = 1;
  527. }
  528. static void
  529. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  530. {
  531. update_load_sub(&cfs_rq->load, se->load.weight);
  532. if (!parent_entity(se))
  533. dec_cpu_load(rq_of(cfs_rq), se->load.weight);
  534. if (entity_is_task(se)) {
  535. add_cfs_task_weight(cfs_rq, -se->load.weight);
  536. list_del_init(&se->group_node);
  537. }
  538. cfs_rq->nr_running--;
  539. se->on_rq = 0;
  540. }
  541. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  542. {
  543. #ifdef CONFIG_SCHEDSTATS
  544. struct task_struct *tsk = NULL;
  545. if (entity_is_task(se))
  546. tsk = task_of(se);
  547. if (se->sleep_start) {
  548. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  549. if ((s64)delta < 0)
  550. delta = 0;
  551. if (unlikely(delta > se->sleep_max))
  552. se->sleep_max = delta;
  553. se->sleep_start = 0;
  554. se->sum_sleep_runtime += delta;
  555. if (tsk) {
  556. account_scheduler_latency(tsk, delta >> 10, 1);
  557. trace_sched_stat_sleep(tsk, delta);
  558. }
  559. }
  560. if (se->block_start) {
  561. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  562. if ((s64)delta < 0)
  563. delta = 0;
  564. if (unlikely(delta > se->block_max))
  565. se->block_max = delta;
  566. se->block_start = 0;
  567. se->sum_sleep_runtime += delta;
  568. if (tsk) {
  569. if (tsk->in_iowait) {
  570. se->iowait_sum += delta;
  571. se->iowait_count++;
  572. trace_sched_stat_iowait(tsk, delta);
  573. }
  574. /*
  575. * Blocking time is in units of nanosecs, so shift by
  576. * 20 to get a milliseconds-range estimation of the
  577. * amount of time that the task spent sleeping:
  578. */
  579. if (unlikely(prof_on == SLEEP_PROFILING)) {
  580. profile_hits(SLEEP_PROFILING,
  581. (void *)get_wchan(tsk),
  582. delta >> 20);
  583. }
  584. account_scheduler_latency(tsk, delta >> 10, 0);
  585. }
  586. }
  587. #endif
  588. }
  589. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  590. {
  591. #ifdef CONFIG_SCHED_DEBUG
  592. s64 d = se->vruntime - cfs_rq->min_vruntime;
  593. if (d < 0)
  594. d = -d;
  595. if (d > 3*sysctl_sched_latency)
  596. schedstat_inc(cfs_rq, nr_spread_over);
  597. #endif
  598. }
  599. static void
  600. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  601. {
  602. u64 vruntime = cfs_rq->min_vruntime;
  603. /*
  604. * The 'current' period is already promised to the current tasks,
  605. * however the extra weight of the new task will slow them down a
  606. * little, place the new task so that it fits in the slot that
  607. * stays open at the end.
  608. */
  609. if (initial && sched_feat(START_DEBIT))
  610. vruntime += sched_vslice(cfs_rq, se);
  611. /* sleeps up to a single latency don't count. */
  612. if (!initial && sched_feat(FAIR_SLEEPERS)) {
  613. unsigned long thresh = sysctl_sched_latency;
  614. /*
  615. * Convert the sleeper threshold into virtual time.
  616. * SCHED_IDLE is a special sub-class. We care about
  617. * fairness only relative to other SCHED_IDLE tasks,
  618. * all of which have the same weight.
  619. */
  620. if (sched_feat(NORMALIZED_SLEEPER) && (!entity_is_task(se) ||
  621. task_of(se)->policy != SCHED_IDLE))
  622. thresh = calc_delta_fair(thresh, se);
  623. /*
  624. * Halve their sleep time's effect, to allow
  625. * for a gentler effect of sleepers:
  626. */
  627. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  628. thresh >>= 1;
  629. vruntime -= thresh;
  630. }
  631. /* ensure we never gain time by being placed backwards. */
  632. vruntime = max_vruntime(se->vruntime, vruntime);
  633. se->vruntime = vruntime;
  634. }
  635. static void
  636. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  637. {
  638. /*
  639. * Update run-time statistics of the 'current'.
  640. */
  641. update_curr(cfs_rq);
  642. account_entity_enqueue(cfs_rq, se);
  643. if (wakeup) {
  644. place_entity(cfs_rq, se, 0);
  645. enqueue_sleeper(cfs_rq, se);
  646. }
  647. update_stats_enqueue(cfs_rq, se);
  648. check_spread(cfs_rq, se);
  649. if (se != cfs_rq->curr)
  650. __enqueue_entity(cfs_rq, se);
  651. }
  652. static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  653. {
  654. if (!se || cfs_rq->last == se)
  655. cfs_rq->last = NULL;
  656. if (!se || cfs_rq->next == se)
  657. cfs_rq->next = NULL;
  658. }
  659. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  660. {
  661. for_each_sched_entity(se)
  662. __clear_buddies(cfs_rq_of(se), se);
  663. }
  664. static void
  665. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  666. {
  667. /*
  668. * Update run-time statistics of the 'current'.
  669. */
  670. update_curr(cfs_rq);
  671. update_stats_dequeue(cfs_rq, se);
  672. if (sleep) {
  673. #ifdef CONFIG_SCHEDSTATS
  674. if (entity_is_task(se)) {
  675. struct task_struct *tsk = task_of(se);
  676. if (tsk->state & TASK_INTERRUPTIBLE)
  677. se->sleep_start = rq_of(cfs_rq)->clock;
  678. if (tsk->state & TASK_UNINTERRUPTIBLE)
  679. se->block_start = rq_of(cfs_rq)->clock;
  680. }
  681. #endif
  682. }
  683. clear_buddies(cfs_rq, se);
  684. if (se != cfs_rq->curr)
  685. __dequeue_entity(cfs_rq, se);
  686. account_entity_dequeue(cfs_rq, se);
  687. update_min_vruntime(cfs_rq);
  688. }
  689. /*
  690. * Preempt the current task with a newly woken task if needed:
  691. */
  692. static void
  693. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  694. {
  695. unsigned long ideal_runtime, delta_exec;
  696. ideal_runtime = sched_slice(cfs_rq, curr);
  697. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  698. if (delta_exec > ideal_runtime) {
  699. resched_task(rq_of(cfs_rq)->curr);
  700. /*
  701. * The current task ran long enough, ensure it doesn't get
  702. * re-elected due to buddy favours.
  703. */
  704. clear_buddies(cfs_rq, curr);
  705. return;
  706. }
  707. /*
  708. * Ensure that a task that missed wakeup preemption by a
  709. * narrow margin doesn't have to wait for a full slice.
  710. * This also mitigates buddy induced latencies under load.
  711. */
  712. if (!sched_feat(WAKEUP_PREEMPT))
  713. return;
  714. if (delta_exec < sysctl_sched_min_granularity)
  715. return;
  716. if (cfs_rq->nr_running > 1) {
  717. struct sched_entity *se = __pick_next_entity(cfs_rq);
  718. s64 delta = curr->vruntime - se->vruntime;
  719. if (delta > ideal_runtime)
  720. resched_task(rq_of(cfs_rq)->curr);
  721. }
  722. }
  723. static void
  724. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  725. {
  726. /* 'current' is not kept within the tree. */
  727. if (se->on_rq) {
  728. /*
  729. * Any task has to be enqueued before it get to execute on
  730. * a CPU. So account for the time it spent waiting on the
  731. * runqueue.
  732. */
  733. update_stats_wait_end(cfs_rq, se);
  734. __dequeue_entity(cfs_rq, se);
  735. }
  736. update_stats_curr_start(cfs_rq, se);
  737. cfs_rq->curr = se;
  738. #ifdef CONFIG_SCHEDSTATS
  739. /*
  740. * Track our maximum slice length, if the CPU's load is at
  741. * least twice that of our own weight (i.e. dont track it
  742. * when there are only lesser-weight tasks around):
  743. */
  744. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  745. se->slice_max = max(se->slice_max,
  746. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  747. }
  748. #endif
  749. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  750. }
  751. static int
  752. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  753. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  754. {
  755. struct sched_entity *se = __pick_next_entity(cfs_rq);
  756. struct sched_entity *left = se;
  757. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  758. se = cfs_rq->next;
  759. /*
  760. * Prefer last buddy, try to return the CPU to a preempted task.
  761. */
  762. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  763. se = cfs_rq->last;
  764. clear_buddies(cfs_rq, se);
  765. return se;
  766. }
  767. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  768. {
  769. /*
  770. * If still on the runqueue then deactivate_task()
  771. * was not called and update_curr() has to be done:
  772. */
  773. if (prev->on_rq)
  774. update_curr(cfs_rq);
  775. check_spread(cfs_rq, prev);
  776. if (prev->on_rq) {
  777. update_stats_wait_start(cfs_rq, prev);
  778. /* Put 'current' back into the tree. */
  779. __enqueue_entity(cfs_rq, prev);
  780. }
  781. cfs_rq->curr = NULL;
  782. }
  783. static void
  784. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  785. {
  786. /*
  787. * Update run-time statistics of the 'current'.
  788. */
  789. update_curr(cfs_rq);
  790. #ifdef CONFIG_SCHED_HRTICK
  791. /*
  792. * queued ticks are scheduled to match the slice, so don't bother
  793. * validating it and just reschedule.
  794. */
  795. if (queued) {
  796. resched_task(rq_of(cfs_rq)->curr);
  797. return;
  798. }
  799. /*
  800. * don't let the period tick interfere with the hrtick preemption
  801. */
  802. if (!sched_feat(DOUBLE_TICK) &&
  803. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  804. return;
  805. #endif
  806. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  807. check_preempt_tick(cfs_rq, curr);
  808. }
  809. /**************************************************
  810. * CFS operations on tasks:
  811. */
  812. #ifdef CONFIG_SCHED_HRTICK
  813. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  814. {
  815. struct sched_entity *se = &p->se;
  816. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  817. WARN_ON(task_rq(p) != rq);
  818. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  819. u64 slice = sched_slice(cfs_rq, se);
  820. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  821. s64 delta = slice - ran;
  822. if (delta < 0) {
  823. if (rq->curr == p)
  824. resched_task(p);
  825. return;
  826. }
  827. /*
  828. * Don't schedule slices shorter than 10000ns, that just
  829. * doesn't make sense. Rely on vruntime for fairness.
  830. */
  831. if (rq->curr != p)
  832. delta = max_t(s64, 10000LL, delta);
  833. hrtick_start(rq, delta);
  834. }
  835. }
  836. /*
  837. * called from enqueue/dequeue and updates the hrtick when the
  838. * current task is from our class and nr_running is low enough
  839. * to matter.
  840. */
  841. static void hrtick_update(struct rq *rq)
  842. {
  843. struct task_struct *curr = rq->curr;
  844. if (curr->sched_class != &fair_sched_class)
  845. return;
  846. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  847. hrtick_start_fair(rq, curr);
  848. }
  849. #else /* !CONFIG_SCHED_HRTICK */
  850. static inline void
  851. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  852. {
  853. }
  854. static inline void hrtick_update(struct rq *rq)
  855. {
  856. }
  857. #endif
  858. /*
  859. * The enqueue_task method is called before nr_running is
  860. * increased. Here we update the fair scheduling stats and
  861. * then put the task into the rbtree:
  862. */
  863. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  864. {
  865. struct cfs_rq *cfs_rq;
  866. struct sched_entity *se = &p->se;
  867. for_each_sched_entity(se) {
  868. if (se->on_rq)
  869. break;
  870. cfs_rq = cfs_rq_of(se);
  871. enqueue_entity(cfs_rq, se, wakeup);
  872. wakeup = 1;
  873. }
  874. hrtick_update(rq);
  875. }
  876. /*
  877. * The dequeue_task method is called before nr_running is
  878. * decreased. We remove the task from the rbtree and
  879. * update the fair scheduling stats:
  880. */
  881. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  882. {
  883. struct cfs_rq *cfs_rq;
  884. struct sched_entity *se = &p->se;
  885. for_each_sched_entity(se) {
  886. cfs_rq = cfs_rq_of(se);
  887. dequeue_entity(cfs_rq, se, sleep);
  888. /* Don't dequeue parent if it has other entities besides us */
  889. if (cfs_rq->load.weight)
  890. break;
  891. sleep = 1;
  892. }
  893. hrtick_update(rq);
  894. }
  895. /*
  896. * sched_yield() support is very simple - we dequeue and enqueue.
  897. *
  898. * If compat_yield is turned on then we requeue to the end of the tree.
  899. */
  900. static void yield_task_fair(struct rq *rq)
  901. {
  902. struct task_struct *curr = rq->curr;
  903. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  904. struct sched_entity *rightmost, *se = &curr->se;
  905. /*
  906. * Are we the only task in the tree?
  907. */
  908. if (unlikely(cfs_rq->nr_running == 1))
  909. return;
  910. clear_buddies(cfs_rq, se);
  911. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  912. update_rq_clock(rq);
  913. /*
  914. * Update run-time statistics of the 'current'.
  915. */
  916. update_curr(cfs_rq);
  917. return;
  918. }
  919. /*
  920. * Find the rightmost entry in the rbtree:
  921. */
  922. rightmost = __pick_last_entity(cfs_rq);
  923. /*
  924. * Already in the rightmost position?
  925. */
  926. if (unlikely(!rightmost || entity_before(rightmost, se)))
  927. return;
  928. /*
  929. * Minimally necessary key value to be last in the tree:
  930. * Upon rescheduling, sched_class::put_prev_task() will place
  931. * 'current' within the tree based on its new key value.
  932. */
  933. se->vruntime = rightmost->vruntime + 1;
  934. }
  935. #ifdef CONFIG_SMP
  936. #ifdef CONFIG_FAIR_GROUP_SCHED
  937. /*
  938. * effective_load() calculates the load change as seen from the root_task_group
  939. *
  940. * Adding load to a group doesn't make a group heavier, but can cause movement
  941. * of group shares between cpus. Assuming the shares were perfectly aligned one
  942. * can calculate the shift in shares.
  943. *
  944. * The problem is that perfectly aligning the shares is rather expensive, hence
  945. * we try to avoid doing that too often - see update_shares(), which ratelimits
  946. * this change.
  947. *
  948. * We compensate this by not only taking the current delta into account, but
  949. * also considering the delta between when the shares were last adjusted and
  950. * now.
  951. *
  952. * We still saw a performance dip, some tracing learned us that between
  953. * cgroup:/ and cgroup:/foo balancing the number of affine wakeups increased
  954. * significantly. Therefore try to bias the error in direction of failing
  955. * the affine wakeup.
  956. *
  957. */
  958. static long effective_load(struct task_group *tg, int cpu,
  959. long wl, long wg)
  960. {
  961. struct sched_entity *se = tg->se[cpu];
  962. if (!tg->parent)
  963. return wl;
  964. /*
  965. * By not taking the decrease of shares on the other cpu into
  966. * account our error leans towards reducing the affine wakeups.
  967. */
  968. if (!wl && sched_feat(ASYM_EFF_LOAD))
  969. return wl;
  970. for_each_sched_entity(se) {
  971. long S, rw, s, a, b;
  972. long more_w;
  973. /*
  974. * Instead of using this increment, also add the difference
  975. * between when the shares were last updated and now.
  976. */
  977. more_w = se->my_q->load.weight - se->my_q->rq_weight;
  978. wl += more_w;
  979. wg += more_w;
  980. S = se->my_q->tg->shares;
  981. s = se->my_q->shares;
  982. rw = se->my_q->rq_weight;
  983. a = S*(rw + wl);
  984. b = S*rw + s*wg;
  985. wl = s*(a-b);
  986. if (likely(b))
  987. wl /= b;
  988. /*
  989. * Assume the group is already running and will
  990. * thus already be accounted for in the weight.
  991. *
  992. * That is, moving shares between CPUs, does not
  993. * alter the group weight.
  994. */
  995. wg = 0;
  996. }
  997. return wl;
  998. }
  999. #else
  1000. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  1001. unsigned long wl, unsigned long wg)
  1002. {
  1003. return wl;
  1004. }
  1005. #endif
  1006. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  1007. {
  1008. struct task_struct *curr = current;
  1009. unsigned long this_load, load;
  1010. int idx, this_cpu, prev_cpu;
  1011. unsigned long tl_per_task;
  1012. unsigned int imbalance;
  1013. struct task_group *tg;
  1014. unsigned long weight;
  1015. int balanced;
  1016. idx = sd->wake_idx;
  1017. this_cpu = smp_processor_id();
  1018. prev_cpu = task_cpu(p);
  1019. load = source_load(prev_cpu, idx);
  1020. this_load = target_load(this_cpu, idx);
  1021. if (sync) {
  1022. if (sched_feat(SYNC_LESS) &&
  1023. (curr->se.avg_overlap > sysctl_sched_migration_cost ||
  1024. p->se.avg_overlap > sysctl_sched_migration_cost))
  1025. sync = 0;
  1026. } else {
  1027. if (sched_feat(SYNC_MORE) &&
  1028. (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  1029. p->se.avg_overlap < sysctl_sched_migration_cost))
  1030. sync = 1;
  1031. }
  1032. /*
  1033. * If sync wakeup then subtract the (maximum possible)
  1034. * effect of the currently running task from the load
  1035. * of the current CPU:
  1036. */
  1037. if (sync) {
  1038. tg = task_group(current);
  1039. weight = current->se.load.weight;
  1040. this_load += effective_load(tg, this_cpu, -weight, -weight);
  1041. load += effective_load(tg, prev_cpu, 0, -weight);
  1042. }
  1043. tg = task_group(p);
  1044. weight = p->se.load.weight;
  1045. imbalance = 100 + (sd->imbalance_pct - 100) / 2;
  1046. /*
  1047. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  1048. * due to the sync cause above having dropped this_load to 0, we'll
  1049. * always have an imbalance, but there's really nothing you can do
  1050. * about that, so that's good too.
  1051. *
  1052. * Otherwise check if either cpus are near enough in load to allow this
  1053. * task to be woken on this_cpu.
  1054. */
  1055. balanced = !this_load ||
  1056. 100*(this_load + effective_load(tg, this_cpu, weight, weight)) <=
  1057. imbalance*(load + effective_load(tg, prev_cpu, 0, weight));
  1058. /*
  1059. * If the currently running task will sleep within
  1060. * a reasonable amount of time then attract this newly
  1061. * woken task:
  1062. */
  1063. if (sync && balanced)
  1064. return 1;
  1065. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  1066. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1067. if (balanced ||
  1068. (this_load <= load &&
  1069. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  1070. /*
  1071. * This domain has SD_WAKE_AFFINE and
  1072. * p is cache cold in this domain, and
  1073. * there is no bad imbalance.
  1074. */
  1075. schedstat_inc(sd, ttwu_move_affine);
  1076. schedstat_inc(p, se.nr_wakeups_affine);
  1077. return 1;
  1078. }
  1079. return 0;
  1080. }
  1081. /*
  1082. * find_idlest_group finds and returns the least busy CPU group within the
  1083. * domain.
  1084. */
  1085. static struct sched_group *
  1086. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  1087. int this_cpu, int load_idx)
  1088. {
  1089. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1090. unsigned long min_load = ULONG_MAX, this_load = 0;
  1091. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1092. do {
  1093. unsigned long load, avg_load;
  1094. int local_group;
  1095. int i;
  1096. /* Skip over this group if it has no CPUs allowed */
  1097. if (!cpumask_intersects(sched_group_cpus(group),
  1098. &p->cpus_allowed))
  1099. continue;
  1100. local_group = cpumask_test_cpu(this_cpu,
  1101. sched_group_cpus(group));
  1102. /* Tally up the load of all CPUs in the group */
  1103. avg_load = 0;
  1104. for_each_cpu(i, sched_group_cpus(group)) {
  1105. /* Bias balancing toward cpus of our domain */
  1106. if (local_group)
  1107. load = source_load(i, load_idx);
  1108. else
  1109. load = target_load(i, load_idx);
  1110. avg_load += load;
  1111. }
  1112. /* Adjust by relative CPU power of the group */
  1113. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  1114. if (local_group) {
  1115. this_load = avg_load;
  1116. this = group;
  1117. } else if (avg_load < min_load) {
  1118. min_load = avg_load;
  1119. idlest = group;
  1120. }
  1121. } while (group = group->next, group != sd->groups);
  1122. if (!idlest || 100*this_load < imbalance*min_load)
  1123. return NULL;
  1124. return idlest;
  1125. }
  1126. /*
  1127. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1128. */
  1129. static int
  1130. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1131. {
  1132. unsigned long load, min_load = ULONG_MAX;
  1133. int idlest = -1;
  1134. int i;
  1135. /* Traverse only the allowed CPUs */
  1136. for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
  1137. load = weighted_cpuload(i);
  1138. if (load < min_load || (load == min_load && i == this_cpu)) {
  1139. min_load = load;
  1140. idlest = i;
  1141. }
  1142. }
  1143. return idlest;
  1144. }
  1145. /*
  1146. * Try and locate an idle CPU in the sched_domain.
  1147. */
  1148. static int
  1149. select_idle_sibling(struct task_struct *p, struct sched_domain *sd, int target)
  1150. {
  1151. int cpu = smp_processor_id();
  1152. int prev_cpu = task_cpu(p);
  1153. int i;
  1154. /*
  1155. * If this domain spans both cpu and prev_cpu (see the SD_WAKE_AFFINE
  1156. * test in select_task_rq_fair) and the prev_cpu is idle then that's
  1157. * always a better target than the current cpu.
  1158. */
  1159. if (target == cpu && !cpu_rq(prev_cpu)->cfs.nr_running)
  1160. return prev_cpu;
  1161. /*
  1162. * Otherwise, iterate the domain and find an elegible idle cpu.
  1163. */
  1164. for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) {
  1165. if (!cpu_rq(i)->cfs.nr_running) {
  1166. target = i;
  1167. break;
  1168. }
  1169. }
  1170. return target;
  1171. }
  1172. /*
  1173. * sched_balance_self: balance the current task (running on cpu) in domains
  1174. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1175. * SD_BALANCE_EXEC.
  1176. *
  1177. * Balance, ie. select the least loaded group.
  1178. *
  1179. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1180. *
  1181. * preempt must be disabled.
  1182. */
  1183. static int select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  1184. {
  1185. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  1186. int cpu = smp_processor_id();
  1187. int prev_cpu = task_cpu(p);
  1188. int new_cpu = cpu;
  1189. int want_affine = 0;
  1190. int want_sd = 1;
  1191. int sync = wake_flags & WF_SYNC;
  1192. if (sd_flag & SD_BALANCE_WAKE) {
  1193. if (sched_feat(AFFINE_WAKEUPS) &&
  1194. cpumask_test_cpu(cpu, &p->cpus_allowed))
  1195. want_affine = 1;
  1196. new_cpu = prev_cpu;
  1197. }
  1198. for_each_domain(cpu, tmp) {
  1199. /*
  1200. * If power savings logic is enabled for a domain, see if we
  1201. * are not overloaded, if so, don't balance wider.
  1202. */
  1203. if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
  1204. unsigned long power = 0;
  1205. unsigned long nr_running = 0;
  1206. unsigned long capacity;
  1207. int i;
  1208. for_each_cpu(i, sched_domain_span(tmp)) {
  1209. power += power_of(i);
  1210. nr_running += cpu_rq(i)->cfs.nr_running;
  1211. }
  1212. capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  1213. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1214. nr_running /= 2;
  1215. if (nr_running < capacity)
  1216. want_sd = 0;
  1217. }
  1218. /*
  1219. * While iterating the domains looking for a spanning
  1220. * WAKE_AFFINE domain, adjust the affine target to any idle cpu
  1221. * in cache sharing domains along the way.
  1222. */
  1223. if (want_affine) {
  1224. int target = -1;
  1225. /*
  1226. * If both cpu and prev_cpu are part of this domain,
  1227. * cpu is a valid SD_WAKE_AFFINE target.
  1228. */
  1229. if (cpumask_test_cpu(prev_cpu, sched_domain_span(tmp)))
  1230. target = cpu;
  1231. /*
  1232. * If there's an idle sibling in this domain, make that
  1233. * the wake_affine target instead of the current cpu.
  1234. */
  1235. if (tmp->flags & SD_PREFER_SIBLING)
  1236. target = select_idle_sibling(p, tmp, target);
  1237. if (target >= 0) {
  1238. if (tmp->flags & SD_WAKE_AFFINE) {
  1239. affine_sd = tmp;
  1240. want_affine = 0;
  1241. }
  1242. cpu = target;
  1243. }
  1244. }
  1245. if (!want_sd && !want_affine)
  1246. break;
  1247. if (!(tmp->flags & sd_flag))
  1248. continue;
  1249. if (want_sd)
  1250. sd = tmp;
  1251. }
  1252. if (sched_feat(LB_SHARES_UPDATE)) {
  1253. /*
  1254. * Pick the largest domain to update shares over
  1255. */
  1256. tmp = sd;
  1257. if (affine_sd && (!tmp ||
  1258. cpumask_weight(sched_domain_span(affine_sd)) >
  1259. cpumask_weight(sched_domain_span(sd))))
  1260. tmp = affine_sd;
  1261. if (tmp)
  1262. update_shares(tmp);
  1263. }
  1264. if (affine_sd && wake_affine(affine_sd, p, sync))
  1265. return cpu;
  1266. while (sd) {
  1267. int load_idx = sd->forkexec_idx;
  1268. struct sched_group *group;
  1269. int weight;
  1270. if (!(sd->flags & sd_flag)) {
  1271. sd = sd->child;
  1272. continue;
  1273. }
  1274. if (sd_flag & SD_BALANCE_WAKE)
  1275. load_idx = sd->wake_idx;
  1276. group = find_idlest_group(sd, p, cpu, load_idx);
  1277. if (!group) {
  1278. sd = sd->child;
  1279. continue;
  1280. }
  1281. new_cpu = find_idlest_cpu(group, p, cpu);
  1282. if (new_cpu == -1 || new_cpu == cpu) {
  1283. /* Now try balancing at a lower domain level of cpu */
  1284. sd = sd->child;
  1285. continue;
  1286. }
  1287. /* Now try balancing at a lower domain level of new_cpu */
  1288. cpu = new_cpu;
  1289. weight = cpumask_weight(sched_domain_span(sd));
  1290. sd = NULL;
  1291. for_each_domain(cpu, tmp) {
  1292. if (weight <= cpumask_weight(sched_domain_span(tmp)))
  1293. break;
  1294. if (tmp->flags & sd_flag)
  1295. sd = tmp;
  1296. }
  1297. /* while loop will break here if sd == NULL */
  1298. }
  1299. return new_cpu;
  1300. }
  1301. #endif /* CONFIG_SMP */
  1302. /*
  1303. * Adaptive granularity
  1304. *
  1305. * se->avg_wakeup gives the average time a task runs until it does a wakeup,
  1306. * with the limit of wakeup_gran -- when it never does a wakeup.
  1307. *
  1308. * So the smaller avg_wakeup is the faster we want this task to preempt,
  1309. * but we don't want to treat the preemptee unfairly and therefore allow it
  1310. * to run for at least the amount of time we'd like to run.
  1311. *
  1312. * NOTE: we use 2*avg_wakeup to increase the probability of actually doing one
  1313. *
  1314. * NOTE: we use *nr_running to scale with load, this nicely matches the
  1315. * degrading latency on load.
  1316. */
  1317. static unsigned long
  1318. adaptive_gran(struct sched_entity *curr, struct sched_entity *se)
  1319. {
  1320. u64 this_run = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1321. u64 expected_wakeup = 2*se->avg_wakeup * cfs_rq_of(se)->nr_running;
  1322. u64 gran = 0;
  1323. if (this_run < expected_wakeup)
  1324. gran = expected_wakeup - this_run;
  1325. return min_t(s64, gran, sysctl_sched_wakeup_granularity);
  1326. }
  1327. static unsigned long
  1328. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  1329. {
  1330. unsigned long gran = sysctl_sched_wakeup_granularity;
  1331. if (cfs_rq_of(curr)->curr && sched_feat(ADAPTIVE_GRAN))
  1332. gran = adaptive_gran(curr, se);
  1333. /*
  1334. * Since its curr running now, convert the gran from real-time
  1335. * to virtual-time in his units.
  1336. */
  1337. if (sched_feat(ASYM_GRAN)) {
  1338. /*
  1339. * By using 'se' instead of 'curr' we penalize light tasks, so
  1340. * they get preempted easier. That is, if 'se' < 'curr' then
  1341. * the resulting gran will be larger, therefore penalizing the
  1342. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  1343. * be smaller, again penalizing the lighter task.
  1344. *
  1345. * This is especially important for buddies when the leftmost
  1346. * task is higher priority than the buddy.
  1347. */
  1348. if (unlikely(se->load.weight != NICE_0_LOAD))
  1349. gran = calc_delta_fair(gran, se);
  1350. } else {
  1351. if (unlikely(curr->load.weight != NICE_0_LOAD))
  1352. gran = calc_delta_fair(gran, curr);
  1353. }
  1354. return gran;
  1355. }
  1356. /*
  1357. * Should 'se' preempt 'curr'.
  1358. *
  1359. * |s1
  1360. * |s2
  1361. * |s3
  1362. * g
  1363. * |<--->|c
  1364. *
  1365. * w(c, s1) = -1
  1366. * w(c, s2) = 0
  1367. * w(c, s3) = 1
  1368. *
  1369. */
  1370. static int
  1371. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  1372. {
  1373. s64 gran, vdiff = curr->vruntime - se->vruntime;
  1374. if (vdiff <= 0)
  1375. return -1;
  1376. gran = wakeup_gran(curr, se);
  1377. if (vdiff > gran)
  1378. return 1;
  1379. return 0;
  1380. }
  1381. static void set_last_buddy(struct sched_entity *se)
  1382. {
  1383. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1384. for_each_sched_entity(se)
  1385. cfs_rq_of(se)->last = se;
  1386. }
  1387. }
  1388. static void set_next_buddy(struct sched_entity *se)
  1389. {
  1390. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1391. for_each_sched_entity(se)
  1392. cfs_rq_of(se)->next = se;
  1393. }
  1394. }
  1395. /*
  1396. * Preempt the current task with a newly woken task if needed:
  1397. */
  1398. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1399. {
  1400. struct task_struct *curr = rq->curr;
  1401. struct sched_entity *se = &curr->se, *pse = &p->se;
  1402. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  1403. int sync = wake_flags & WF_SYNC;
  1404. int scale = cfs_rq->nr_running >= sched_nr_latency;
  1405. if (unlikely(rt_prio(p->prio)))
  1406. goto preempt;
  1407. if (unlikely(p->sched_class != &fair_sched_class))
  1408. return;
  1409. if (unlikely(se == pse))
  1410. return;
  1411. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK))
  1412. set_next_buddy(pse);
  1413. /*
  1414. * We can come here with TIF_NEED_RESCHED already set from new task
  1415. * wake up path.
  1416. */
  1417. if (test_tsk_need_resched(curr))
  1418. return;
  1419. /*
  1420. * Batch and idle tasks do not preempt (their preemption is driven by
  1421. * the tick):
  1422. */
  1423. if (unlikely(p->policy != SCHED_NORMAL))
  1424. return;
  1425. /* Idle tasks are by definition preempted by everybody. */
  1426. if (unlikely(curr->policy == SCHED_IDLE))
  1427. goto preempt;
  1428. if (sched_feat(WAKEUP_SYNC) && sync)
  1429. goto preempt;
  1430. if (sched_feat(WAKEUP_OVERLAP) &&
  1431. se->avg_overlap < sysctl_sched_migration_cost &&
  1432. pse->avg_overlap < sysctl_sched_migration_cost)
  1433. goto preempt;
  1434. if (!sched_feat(WAKEUP_PREEMPT))
  1435. return;
  1436. update_curr(cfs_rq);
  1437. find_matching_se(&se, &pse);
  1438. BUG_ON(!pse);
  1439. if (wakeup_preempt_entity(se, pse) == 1)
  1440. goto preempt;
  1441. return;
  1442. preempt:
  1443. resched_task(curr);
  1444. /*
  1445. * Only set the backward buddy when the current task is still
  1446. * on the rq. This can happen when a wakeup gets interleaved
  1447. * with schedule on the ->pre_schedule() or idle_balance()
  1448. * point, either of which can * drop the rq lock.
  1449. *
  1450. * Also, during early boot the idle thread is in the fair class,
  1451. * for obvious reasons its a bad idea to schedule back to it.
  1452. */
  1453. if (unlikely(!se->on_rq || curr == rq->idle))
  1454. return;
  1455. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  1456. set_last_buddy(se);
  1457. }
  1458. static struct task_struct *pick_next_task_fair(struct rq *rq)
  1459. {
  1460. struct task_struct *p;
  1461. struct cfs_rq *cfs_rq = &rq->cfs;
  1462. struct sched_entity *se;
  1463. if (!cfs_rq->nr_running)
  1464. return NULL;
  1465. do {
  1466. se = pick_next_entity(cfs_rq);
  1467. set_next_entity(cfs_rq, se);
  1468. cfs_rq = group_cfs_rq(se);
  1469. } while (cfs_rq);
  1470. p = task_of(se);
  1471. hrtick_start_fair(rq, p);
  1472. return p;
  1473. }
  1474. /*
  1475. * Account for a descheduled task:
  1476. */
  1477. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1478. {
  1479. struct sched_entity *se = &prev->se;
  1480. struct cfs_rq *cfs_rq;
  1481. for_each_sched_entity(se) {
  1482. cfs_rq = cfs_rq_of(se);
  1483. put_prev_entity(cfs_rq, se);
  1484. }
  1485. }
  1486. #ifdef CONFIG_SMP
  1487. /**************************************************
  1488. * Fair scheduling class load-balancing methods:
  1489. */
  1490. /*
  1491. * Load-balancing iterator. Note: while the runqueue stays locked
  1492. * during the whole iteration, the current task might be
  1493. * dequeued so the iterator has to be dequeue-safe. Here we
  1494. * achieve that by always pre-iterating before returning
  1495. * the current task:
  1496. */
  1497. static struct task_struct *
  1498. __load_balance_iterator(struct cfs_rq *cfs_rq, struct list_head *next)
  1499. {
  1500. struct task_struct *p = NULL;
  1501. struct sched_entity *se;
  1502. if (next == &cfs_rq->tasks)
  1503. return NULL;
  1504. se = list_entry(next, struct sched_entity, group_node);
  1505. p = task_of(se);
  1506. cfs_rq->balance_iterator = next->next;
  1507. return p;
  1508. }
  1509. static struct task_struct *load_balance_start_fair(void *arg)
  1510. {
  1511. struct cfs_rq *cfs_rq = arg;
  1512. return __load_balance_iterator(cfs_rq, cfs_rq->tasks.next);
  1513. }
  1514. static struct task_struct *load_balance_next_fair(void *arg)
  1515. {
  1516. struct cfs_rq *cfs_rq = arg;
  1517. return __load_balance_iterator(cfs_rq, cfs_rq->balance_iterator);
  1518. }
  1519. static unsigned long
  1520. __load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1521. unsigned long max_load_move, struct sched_domain *sd,
  1522. enum cpu_idle_type idle, int *all_pinned, int *this_best_prio,
  1523. struct cfs_rq *cfs_rq)
  1524. {
  1525. struct rq_iterator cfs_rq_iterator;
  1526. cfs_rq_iterator.start = load_balance_start_fair;
  1527. cfs_rq_iterator.next = load_balance_next_fair;
  1528. cfs_rq_iterator.arg = cfs_rq;
  1529. return balance_tasks(this_rq, this_cpu, busiest,
  1530. max_load_move, sd, idle, all_pinned,
  1531. this_best_prio, &cfs_rq_iterator);
  1532. }
  1533. #ifdef CONFIG_FAIR_GROUP_SCHED
  1534. static unsigned long
  1535. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1536. unsigned long max_load_move,
  1537. struct sched_domain *sd, enum cpu_idle_type idle,
  1538. int *all_pinned, int *this_best_prio)
  1539. {
  1540. long rem_load_move = max_load_move;
  1541. int busiest_cpu = cpu_of(busiest);
  1542. struct task_group *tg;
  1543. rcu_read_lock();
  1544. update_h_load(busiest_cpu);
  1545. list_for_each_entry_rcu(tg, &task_groups, list) {
  1546. struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
  1547. unsigned long busiest_h_load = busiest_cfs_rq->h_load;
  1548. unsigned long busiest_weight = busiest_cfs_rq->load.weight;
  1549. u64 rem_load, moved_load;
  1550. /*
  1551. * empty group
  1552. */
  1553. if (!busiest_cfs_rq->task_weight)
  1554. continue;
  1555. rem_load = (u64)rem_load_move * busiest_weight;
  1556. rem_load = div_u64(rem_load, busiest_h_load + 1);
  1557. moved_load = __load_balance_fair(this_rq, this_cpu, busiest,
  1558. rem_load, sd, idle, all_pinned, this_best_prio,
  1559. tg->cfs_rq[busiest_cpu]);
  1560. if (!moved_load)
  1561. continue;
  1562. moved_load *= busiest_h_load;
  1563. moved_load = div_u64(moved_load, busiest_weight + 1);
  1564. rem_load_move -= moved_load;
  1565. if (rem_load_move < 0)
  1566. break;
  1567. }
  1568. rcu_read_unlock();
  1569. return max_load_move - rem_load_move;
  1570. }
  1571. #else
  1572. static unsigned long
  1573. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1574. unsigned long max_load_move,
  1575. struct sched_domain *sd, enum cpu_idle_type idle,
  1576. int *all_pinned, int *this_best_prio)
  1577. {
  1578. return __load_balance_fair(this_rq, this_cpu, busiest,
  1579. max_load_move, sd, idle, all_pinned,
  1580. this_best_prio, &busiest->cfs);
  1581. }
  1582. #endif
  1583. static int
  1584. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1585. struct sched_domain *sd, enum cpu_idle_type idle)
  1586. {
  1587. struct cfs_rq *busy_cfs_rq;
  1588. struct rq_iterator cfs_rq_iterator;
  1589. cfs_rq_iterator.start = load_balance_start_fair;
  1590. cfs_rq_iterator.next = load_balance_next_fair;
  1591. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1592. /*
  1593. * pass busy_cfs_rq argument into
  1594. * load_balance_[start|next]_fair iterators
  1595. */
  1596. cfs_rq_iterator.arg = busy_cfs_rq;
  1597. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  1598. &cfs_rq_iterator))
  1599. return 1;
  1600. }
  1601. return 0;
  1602. }
  1603. static void rq_online_fair(struct rq *rq)
  1604. {
  1605. update_sysctl();
  1606. }
  1607. static void rq_offline_fair(struct rq *rq)
  1608. {
  1609. update_sysctl();
  1610. }
  1611. #endif /* CONFIG_SMP */
  1612. /*
  1613. * scheduler tick hitting a task of our scheduling class:
  1614. */
  1615. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  1616. {
  1617. struct cfs_rq *cfs_rq;
  1618. struct sched_entity *se = &curr->se;
  1619. for_each_sched_entity(se) {
  1620. cfs_rq = cfs_rq_of(se);
  1621. entity_tick(cfs_rq, se, queued);
  1622. }
  1623. }
  1624. /*
  1625. * called on fork with the child task as argument from the parent's context
  1626. * - child not yet on the tasklist
  1627. * - preemption disabled
  1628. */
  1629. static void task_fork_fair(struct task_struct *p)
  1630. {
  1631. struct cfs_rq *cfs_rq = task_cfs_rq(current);
  1632. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  1633. int this_cpu = smp_processor_id();
  1634. struct rq *rq = this_rq();
  1635. unsigned long flags;
  1636. spin_lock_irqsave(&rq->lock, flags);
  1637. if (unlikely(task_cpu(p) != this_cpu))
  1638. __set_task_cpu(p, this_cpu);
  1639. update_curr(cfs_rq);
  1640. if (curr)
  1641. se->vruntime = curr->vruntime;
  1642. place_entity(cfs_rq, se, 1);
  1643. if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
  1644. /*
  1645. * Upon rescheduling, sched_class::put_prev_task() will place
  1646. * 'current' within the tree based on its new key value.
  1647. */
  1648. swap(curr->vruntime, se->vruntime);
  1649. resched_task(rq->curr);
  1650. }
  1651. spin_unlock_irqrestore(&rq->lock, flags);
  1652. }
  1653. /*
  1654. * Priority of the task has changed. Check to see if we preempt
  1655. * the current task.
  1656. */
  1657. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  1658. int oldprio, int running)
  1659. {
  1660. /*
  1661. * Reschedule if we are currently running on this runqueue and
  1662. * our priority decreased, or if we are not currently running on
  1663. * this runqueue and our priority is higher than the current's
  1664. */
  1665. if (running) {
  1666. if (p->prio > oldprio)
  1667. resched_task(rq->curr);
  1668. } else
  1669. check_preempt_curr(rq, p, 0);
  1670. }
  1671. /*
  1672. * We switched to the sched_fair class.
  1673. */
  1674. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  1675. int running)
  1676. {
  1677. /*
  1678. * We were most likely switched from sched_rt, so
  1679. * kick off the schedule if running, otherwise just see
  1680. * if we can still preempt the current task.
  1681. */
  1682. if (running)
  1683. resched_task(rq->curr);
  1684. else
  1685. check_preempt_curr(rq, p, 0);
  1686. }
  1687. /* Account for a task changing its policy or group.
  1688. *
  1689. * This routine is mostly called to set cfs_rq->curr field when a task
  1690. * migrates between groups/classes.
  1691. */
  1692. static void set_curr_task_fair(struct rq *rq)
  1693. {
  1694. struct sched_entity *se = &rq->curr->se;
  1695. for_each_sched_entity(se)
  1696. set_next_entity(cfs_rq_of(se), se);
  1697. }
  1698. #ifdef CONFIG_FAIR_GROUP_SCHED
  1699. static void moved_group_fair(struct task_struct *p)
  1700. {
  1701. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1702. update_curr(cfs_rq);
  1703. place_entity(cfs_rq, &p->se, 1);
  1704. }
  1705. #endif
  1706. unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
  1707. {
  1708. struct sched_entity *se = &task->se;
  1709. unsigned int rr_interval = 0;
  1710. /*
  1711. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  1712. * idle runqueue:
  1713. */
  1714. if (rq->cfs.load.weight)
  1715. rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  1716. return rr_interval;
  1717. }
  1718. /*
  1719. * All the scheduling class methods:
  1720. */
  1721. static const struct sched_class fair_sched_class = {
  1722. .next = &idle_sched_class,
  1723. .enqueue_task = enqueue_task_fair,
  1724. .dequeue_task = dequeue_task_fair,
  1725. .yield_task = yield_task_fair,
  1726. .check_preempt_curr = check_preempt_wakeup,
  1727. .pick_next_task = pick_next_task_fair,
  1728. .put_prev_task = put_prev_task_fair,
  1729. #ifdef CONFIG_SMP
  1730. .select_task_rq = select_task_rq_fair,
  1731. .load_balance = load_balance_fair,
  1732. .move_one_task = move_one_task_fair,
  1733. .rq_online = rq_online_fair,
  1734. .rq_offline = rq_offline_fair,
  1735. #endif
  1736. .set_curr_task = set_curr_task_fair,
  1737. .task_tick = task_tick_fair,
  1738. .task_fork = task_fork_fair,
  1739. .prio_changed = prio_changed_fair,
  1740. .switched_to = switched_to_fair,
  1741. .get_rr_interval = get_rr_interval_fair,
  1742. #ifdef CONFIG_FAIR_GROUP_SCHED
  1743. .moved_group = moved_group_fair,
  1744. #endif
  1745. };
  1746. #ifdef CONFIG_SCHED_DEBUG
  1747. static void print_cfs_stats(struct seq_file *m, int cpu)
  1748. {
  1749. struct cfs_rq *cfs_rq;
  1750. rcu_read_lock();
  1751. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  1752. print_cfs_rq(m, cpu, cfs_rq);
  1753. rcu_read_unlock();
  1754. }
  1755. #endif