sched_fair.c 48 KB

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