sched_fair.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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: 20ms * (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 = 20000000ULL;
  36. /*
  37. * Minimal preemption granularity for CPU-bound tasks:
  38. * (default: 4 msec * (1 + ilog(ncpus)), units: nanoseconds)
  39. */
  40. unsigned int sysctl_sched_min_granularity = 4000000ULL;
  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. (default) If set to 0 then
  47. * parent will (try to) run first.
  48. */
  49. const_debug unsigned int sysctl_sched_child_runs_first = 1;
  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: 10 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 = 10000000UL;
  66. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  67. /**************************************************************
  68. * CFS operations on generic schedulable entities:
  69. */
  70. static inline struct task_struct *task_of(struct sched_entity *se)
  71. {
  72. return container_of(se, struct task_struct, se);
  73. }
  74. #ifdef CONFIG_FAIR_GROUP_SCHED
  75. /* cpu runqueue to which this cfs_rq is attached */
  76. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  77. {
  78. return cfs_rq->rq;
  79. }
  80. /* An entity is a task if it doesn't "own" a runqueue */
  81. #define entity_is_task(se) (!se->my_q)
  82. /* Walk up scheduling entities hierarchy */
  83. #define for_each_sched_entity(se) \
  84. for (; se; se = se->parent)
  85. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  86. {
  87. return p->se.cfs_rq;
  88. }
  89. /* runqueue on which this entity is (to be) queued */
  90. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  91. {
  92. return se->cfs_rq;
  93. }
  94. /* runqueue "owned" by this group */
  95. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  96. {
  97. return grp->my_q;
  98. }
  99. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  100. * another cpu ('this_cpu')
  101. */
  102. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  103. {
  104. return cfs_rq->tg->cfs_rq[this_cpu];
  105. }
  106. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  107. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  108. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  109. /* Do the two (enqueued) entities belong to the same group ? */
  110. static inline int
  111. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  112. {
  113. if (se->cfs_rq == pse->cfs_rq)
  114. return 1;
  115. return 0;
  116. }
  117. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  118. {
  119. return se->parent;
  120. }
  121. #else /* CONFIG_FAIR_GROUP_SCHED */
  122. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  123. {
  124. return container_of(cfs_rq, struct rq, cfs);
  125. }
  126. #define entity_is_task(se) 1
  127. #define for_each_sched_entity(se) \
  128. for (; se; se = NULL)
  129. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  130. {
  131. return &task_rq(p)->cfs;
  132. }
  133. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  134. {
  135. struct task_struct *p = task_of(se);
  136. struct rq *rq = task_rq(p);
  137. return &rq->cfs;
  138. }
  139. /* runqueue "owned" by this group */
  140. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  141. {
  142. return NULL;
  143. }
  144. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  145. {
  146. return &cpu_rq(this_cpu)->cfs;
  147. }
  148. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  149. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  150. static inline int
  151. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  152. {
  153. return 1;
  154. }
  155. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  156. {
  157. return NULL;
  158. }
  159. #endif /* CONFIG_FAIR_GROUP_SCHED */
  160. /**************************************************************
  161. * Scheduling class tree data structure manipulation methods:
  162. */
  163. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  164. {
  165. s64 delta = (s64)(vruntime - min_vruntime);
  166. if (delta > 0)
  167. min_vruntime = vruntime;
  168. return min_vruntime;
  169. }
  170. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  171. {
  172. s64 delta = (s64)(vruntime - min_vruntime);
  173. if (delta < 0)
  174. min_vruntime = vruntime;
  175. return min_vruntime;
  176. }
  177. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  178. {
  179. return se->vruntime - cfs_rq->min_vruntime;
  180. }
  181. /*
  182. * Enqueue an entity into the rb-tree:
  183. */
  184. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  185. {
  186. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  187. struct rb_node *parent = NULL;
  188. struct sched_entity *entry;
  189. s64 key = entity_key(cfs_rq, se);
  190. int leftmost = 1;
  191. /*
  192. * Find the right place in the rbtree:
  193. */
  194. while (*link) {
  195. parent = *link;
  196. entry = rb_entry(parent, struct sched_entity, run_node);
  197. /*
  198. * We dont care about collisions. Nodes with
  199. * the same key stay together.
  200. */
  201. if (key < entity_key(cfs_rq, entry)) {
  202. link = &parent->rb_left;
  203. } else {
  204. link = &parent->rb_right;
  205. leftmost = 0;
  206. }
  207. }
  208. /*
  209. * Maintain a cache of leftmost tree entries (it is frequently
  210. * used):
  211. */
  212. if (leftmost) {
  213. cfs_rq->rb_leftmost = &se->run_node;
  214. /*
  215. * maintain cfs_rq->min_vruntime to be a monotonic increasing
  216. * value tracking the leftmost vruntime in the tree.
  217. */
  218. cfs_rq->min_vruntime =
  219. max_vruntime(cfs_rq->min_vruntime, se->vruntime);
  220. }
  221. rb_link_node(&se->run_node, parent, link);
  222. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  223. }
  224. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  225. {
  226. if (cfs_rq->rb_leftmost == &se->run_node) {
  227. struct rb_node *next_node;
  228. struct sched_entity *next;
  229. next_node = rb_next(&se->run_node);
  230. cfs_rq->rb_leftmost = next_node;
  231. if (next_node) {
  232. next = rb_entry(next_node,
  233. struct sched_entity, run_node);
  234. cfs_rq->min_vruntime =
  235. max_vruntime(cfs_rq->min_vruntime,
  236. next->vruntime);
  237. }
  238. }
  239. if (cfs_rq->next == se)
  240. cfs_rq->next = NULL;
  241. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  242. }
  243. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  244. {
  245. return cfs_rq->rb_leftmost;
  246. }
  247. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  248. {
  249. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  250. }
  251. static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  252. {
  253. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  254. if (!last)
  255. return NULL;
  256. return rb_entry(last, struct sched_entity, run_node);
  257. }
  258. /**************************************************************
  259. * Scheduling class statistics methods:
  260. */
  261. #ifdef CONFIG_SCHED_DEBUG
  262. int sched_nr_latency_handler(struct ctl_table *table, int write,
  263. struct file *filp, void __user *buffer, size_t *lenp,
  264. loff_t *ppos)
  265. {
  266. int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  267. if (ret || !write)
  268. return ret;
  269. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  270. sysctl_sched_min_granularity);
  271. return 0;
  272. }
  273. #endif
  274. /*
  275. * The idea is to set a period in which each task runs once.
  276. *
  277. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  278. * this period because otherwise the slices get too small.
  279. *
  280. * p = (nr <= nl) ? l : l*nr/nl
  281. */
  282. static u64 __sched_period(unsigned long nr_running)
  283. {
  284. u64 period = sysctl_sched_latency;
  285. unsigned long nr_latency = sched_nr_latency;
  286. if (unlikely(nr_running > nr_latency)) {
  287. period = sysctl_sched_min_granularity;
  288. period *= nr_running;
  289. }
  290. return period;
  291. }
  292. /*
  293. * We calculate the wall-time slice from the period by taking a part
  294. * proportional to the weight.
  295. *
  296. * s = p*w/rw
  297. */
  298. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  299. {
  300. return calc_delta_mine(__sched_period(cfs_rq->nr_running),
  301. se->load.weight, &cfs_rq->load);
  302. }
  303. /*
  304. * We calculate the vruntime slice.
  305. *
  306. * vs = s/w = p/rw
  307. */
  308. static u64 __sched_vslice(unsigned long rq_weight, unsigned long nr_running)
  309. {
  310. u64 vslice = __sched_period(nr_running);
  311. vslice *= NICE_0_LOAD;
  312. do_div(vslice, rq_weight);
  313. return vslice;
  314. }
  315. static u64 sched_vslice_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
  316. {
  317. return __sched_vslice(cfs_rq->load.weight + se->load.weight,
  318. cfs_rq->nr_running + 1);
  319. }
  320. /*
  321. * Update the current task's runtime statistics. Skip current tasks that
  322. * are not in our scheduling class.
  323. */
  324. static inline void
  325. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  326. unsigned long delta_exec)
  327. {
  328. unsigned long delta_exec_weighted;
  329. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  330. curr->sum_exec_runtime += delta_exec;
  331. schedstat_add(cfs_rq, exec_clock, delta_exec);
  332. delta_exec_weighted = delta_exec;
  333. if (unlikely(curr->load.weight != NICE_0_LOAD)) {
  334. delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
  335. &curr->load);
  336. }
  337. curr->vruntime += delta_exec_weighted;
  338. }
  339. static void update_curr(struct cfs_rq *cfs_rq)
  340. {
  341. struct sched_entity *curr = cfs_rq->curr;
  342. u64 now = rq_of(cfs_rq)->clock;
  343. unsigned long delta_exec;
  344. if (unlikely(!curr))
  345. return;
  346. /*
  347. * Get the amount of time the current task was running
  348. * since the last time we changed load (this cannot
  349. * overflow on 32 bits):
  350. */
  351. delta_exec = (unsigned long)(now - curr->exec_start);
  352. __update_curr(cfs_rq, curr, delta_exec);
  353. curr->exec_start = now;
  354. if (entity_is_task(curr)) {
  355. struct task_struct *curtask = task_of(curr);
  356. cpuacct_charge(curtask, delta_exec);
  357. }
  358. }
  359. static inline void
  360. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  361. {
  362. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  363. }
  364. /*
  365. * Task is being enqueued - update stats:
  366. */
  367. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  368. {
  369. /*
  370. * Are we enqueueing a waiting task? (for current tasks
  371. * a dequeue/enqueue event is a NOP)
  372. */
  373. if (se != cfs_rq->curr)
  374. update_stats_wait_start(cfs_rq, se);
  375. }
  376. static void
  377. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  378. {
  379. schedstat_set(se->wait_max, max(se->wait_max,
  380. rq_of(cfs_rq)->clock - se->wait_start));
  381. schedstat_set(se->wait_count, se->wait_count + 1);
  382. schedstat_set(se->wait_sum, se->wait_sum +
  383. rq_of(cfs_rq)->clock - se->wait_start);
  384. schedstat_set(se->wait_start, 0);
  385. }
  386. static inline void
  387. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  388. {
  389. /*
  390. * Mark the end of the wait period if dequeueing a
  391. * waiting task:
  392. */
  393. if (se != cfs_rq->curr)
  394. update_stats_wait_end(cfs_rq, se);
  395. }
  396. /*
  397. * We are picking a new current task - update its stats:
  398. */
  399. static inline void
  400. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  401. {
  402. /*
  403. * We are starting a new run period:
  404. */
  405. se->exec_start = rq_of(cfs_rq)->clock;
  406. }
  407. /**************************************************
  408. * Scheduling class queueing methods:
  409. */
  410. static void
  411. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  412. {
  413. update_load_add(&cfs_rq->load, se->load.weight);
  414. cfs_rq->nr_running++;
  415. se->on_rq = 1;
  416. }
  417. static void
  418. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  419. {
  420. update_load_sub(&cfs_rq->load, se->load.weight);
  421. cfs_rq->nr_running--;
  422. se->on_rq = 0;
  423. }
  424. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  425. {
  426. #ifdef CONFIG_SCHEDSTATS
  427. if (se->sleep_start) {
  428. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  429. struct task_struct *tsk = task_of(se);
  430. if ((s64)delta < 0)
  431. delta = 0;
  432. if (unlikely(delta > se->sleep_max))
  433. se->sleep_max = delta;
  434. se->sleep_start = 0;
  435. se->sum_sleep_runtime += delta;
  436. account_scheduler_latency(tsk, delta >> 10, 1);
  437. }
  438. if (se->block_start) {
  439. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  440. struct task_struct *tsk = task_of(se);
  441. if ((s64)delta < 0)
  442. delta = 0;
  443. if (unlikely(delta > se->block_max))
  444. se->block_max = delta;
  445. se->block_start = 0;
  446. se->sum_sleep_runtime += delta;
  447. /*
  448. * Blocking time is in units of nanosecs, so shift by 20 to
  449. * get a milliseconds-range estimation of the amount of
  450. * time that the task spent sleeping:
  451. */
  452. if (unlikely(prof_on == SLEEP_PROFILING)) {
  453. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  454. delta >> 20);
  455. }
  456. account_scheduler_latency(tsk, delta >> 10, 0);
  457. }
  458. #endif
  459. }
  460. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  461. {
  462. #ifdef CONFIG_SCHED_DEBUG
  463. s64 d = se->vruntime - cfs_rq->min_vruntime;
  464. if (d < 0)
  465. d = -d;
  466. if (d > 3*sysctl_sched_latency)
  467. schedstat_inc(cfs_rq, nr_spread_over);
  468. #endif
  469. }
  470. static void
  471. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  472. {
  473. u64 vruntime;
  474. if (first_fair(cfs_rq)) {
  475. vruntime = min_vruntime(cfs_rq->min_vruntime,
  476. __pick_next_entity(cfs_rq)->vruntime);
  477. } else
  478. vruntime = cfs_rq->min_vruntime;
  479. /*
  480. * The 'current' period is already promised to the current tasks,
  481. * however the extra weight of the new task will slow them down a
  482. * little, place the new task so that it fits in the slot that
  483. * stays open at the end.
  484. */
  485. if (initial && sched_feat(START_DEBIT))
  486. vruntime += sched_vslice_add(cfs_rq, se);
  487. if (!initial) {
  488. /* sleeps upto a single latency don't count. */
  489. if (sched_feat(NEW_FAIR_SLEEPERS)) {
  490. if (sched_feat(NORMALIZED_SLEEPER))
  491. vruntime -= calc_delta_fair(sysctl_sched_latency,
  492. &cfs_rq->load);
  493. else
  494. vruntime -= sysctl_sched_latency;
  495. }
  496. /* ensure we never gain time by being placed backwards. */
  497. vruntime = max_vruntime(se->vruntime, vruntime);
  498. }
  499. se->vruntime = vruntime;
  500. }
  501. static void
  502. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  503. {
  504. /*
  505. * Update run-time statistics of the 'current'.
  506. */
  507. update_curr(cfs_rq);
  508. if (wakeup) {
  509. place_entity(cfs_rq, se, 0);
  510. enqueue_sleeper(cfs_rq, se);
  511. }
  512. update_stats_enqueue(cfs_rq, se);
  513. check_spread(cfs_rq, se);
  514. if (se != cfs_rq->curr)
  515. __enqueue_entity(cfs_rq, se);
  516. account_entity_enqueue(cfs_rq, se);
  517. }
  518. static void update_avg(u64 *avg, u64 sample)
  519. {
  520. s64 diff = sample - *avg;
  521. *avg += diff >> 3;
  522. }
  523. static void update_avg_stats(struct cfs_rq *cfs_rq, struct sched_entity *se)
  524. {
  525. if (!se->last_wakeup)
  526. return;
  527. update_avg(&se->avg_overlap, se->sum_exec_runtime - se->last_wakeup);
  528. se->last_wakeup = 0;
  529. }
  530. static void
  531. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  532. {
  533. /*
  534. * Update run-time statistics of the 'current'.
  535. */
  536. update_curr(cfs_rq);
  537. update_stats_dequeue(cfs_rq, se);
  538. if (sleep) {
  539. update_avg_stats(cfs_rq, se);
  540. #ifdef CONFIG_SCHEDSTATS
  541. if (entity_is_task(se)) {
  542. struct task_struct *tsk = task_of(se);
  543. if (tsk->state & TASK_INTERRUPTIBLE)
  544. se->sleep_start = rq_of(cfs_rq)->clock;
  545. if (tsk->state & TASK_UNINTERRUPTIBLE)
  546. se->block_start = rq_of(cfs_rq)->clock;
  547. }
  548. #endif
  549. }
  550. if (se != cfs_rq->curr)
  551. __dequeue_entity(cfs_rq, se);
  552. account_entity_dequeue(cfs_rq, se);
  553. }
  554. /*
  555. * Preempt the current task with a newly woken task if needed:
  556. */
  557. static void
  558. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  559. {
  560. unsigned long ideal_runtime, delta_exec;
  561. ideal_runtime = sched_slice(cfs_rq, curr);
  562. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  563. if (delta_exec > ideal_runtime)
  564. resched_task(rq_of(cfs_rq)->curr);
  565. }
  566. static void
  567. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  568. {
  569. /* 'current' is not kept within the tree. */
  570. if (se->on_rq) {
  571. /*
  572. * Any task has to be enqueued before it get to execute on
  573. * a CPU. So account for the time it spent waiting on the
  574. * runqueue.
  575. */
  576. update_stats_wait_end(cfs_rq, se);
  577. __dequeue_entity(cfs_rq, se);
  578. }
  579. update_stats_curr_start(cfs_rq, se);
  580. cfs_rq->curr = se;
  581. #ifdef CONFIG_SCHEDSTATS
  582. /*
  583. * Track our maximum slice length, if the CPU's load is at
  584. * least twice that of our own weight (i.e. dont track it
  585. * when there are only lesser-weight tasks around):
  586. */
  587. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  588. se->slice_max = max(se->slice_max,
  589. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  590. }
  591. #endif
  592. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  593. }
  594. static int
  595. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  596. static struct sched_entity *
  597. pick_next(struct cfs_rq *cfs_rq, struct sched_entity *se)
  598. {
  599. if (!cfs_rq->next)
  600. return se;
  601. if (wakeup_preempt_entity(cfs_rq->next, se) != 0)
  602. return se;
  603. return cfs_rq->next;
  604. }
  605. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  606. {
  607. struct sched_entity *se = NULL;
  608. if (first_fair(cfs_rq)) {
  609. se = __pick_next_entity(cfs_rq);
  610. se = pick_next(cfs_rq, se);
  611. set_next_entity(cfs_rq, se);
  612. }
  613. return se;
  614. }
  615. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  616. {
  617. /*
  618. * If still on the runqueue then deactivate_task()
  619. * was not called and update_curr() has to be done:
  620. */
  621. if (prev->on_rq)
  622. update_curr(cfs_rq);
  623. check_spread(cfs_rq, prev);
  624. if (prev->on_rq) {
  625. update_stats_wait_start(cfs_rq, prev);
  626. /* Put 'current' back into the tree. */
  627. __enqueue_entity(cfs_rq, prev);
  628. }
  629. cfs_rq->curr = NULL;
  630. }
  631. static void
  632. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  633. {
  634. /*
  635. * Update run-time statistics of the 'current'.
  636. */
  637. update_curr(cfs_rq);
  638. #ifdef CONFIG_SCHED_HRTICK
  639. /*
  640. * queued ticks are scheduled to match the slice, so don't bother
  641. * validating it and just reschedule.
  642. */
  643. if (queued)
  644. return resched_task(rq_of(cfs_rq)->curr);
  645. /*
  646. * don't let the period tick interfere with the hrtick preemption
  647. */
  648. if (!sched_feat(DOUBLE_TICK) &&
  649. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  650. return;
  651. #endif
  652. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  653. check_preempt_tick(cfs_rq, curr);
  654. }
  655. /**************************************************
  656. * CFS operations on tasks:
  657. */
  658. #ifdef CONFIG_SCHED_HRTICK
  659. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  660. {
  661. int requeue = rq->curr == p;
  662. struct sched_entity *se = &p->se;
  663. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  664. WARN_ON(task_rq(p) != rq);
  665. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  666. u64 slice = sched_slice(cfs_rq, se);
  667. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  668. s64 delta = slice - ran;
  669. if (delta < 0) {
  670. if (rq->curr == p)
  671. resched_task(p);
  672. return;
  673. }
  674. /*
  675. * Don't schedule slices shorter than 10000ns, that just
  676. * doesn't make sense. Rely on vruntime for fairness.
  677. */
  678. if (!requeue)
  679. delta = max(10000LL, delta);
  680. hrtick_start(rq, delta, requeue);
  681. }
  682. }
  683. #else
  684. static inline void
  685. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  686. {
  687. }
  688. #endif
  689. /*
  690. * The enqueue_task method is called before nr_running is
  691. * increased. Here we update the fair scheduling stats and
  692. * then put the task into the rbtree:
  693. */
  694. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  695. {
  696. struct cfs_rq *cfs_rq;
  697. struct sched_entity *se = &p->se;
  698. for_each_sched_entity(se) {
  699. if (se->on_rq)
  700. break;
  701. cfs_rq = cfs_rq_of(se);
  702. enqueue_entity(cfs_rq, se, wakeup);
  703. wakeup = 1;
  704. }
  705. hrtick_start_fair(rq, rq->curr);
  706. }
  707. /*
  708. * The dequeue_task method is called before nr_running is
  709. * decreased. We remove the task from the rbtree and
  710. * update the fair scheduling stats:
  711. */
  712. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  713. {
  714. struct cfs_rq *cfs_rq;
  715. struct sched_entity *se = &p->se;
  716. for_each_sched_entity(se) {
  717. cfs_rq = cfs_rq_of(se);
  718. dequeue_entity(cfs_rq, se, sleep);
  719. /* Don't dequeue parent if it has other entities besides us */
  720. if (cfs_rq->load.weight)
  721. break;
  722. sleep = 1;
  723. }
  724. hrtick_start_fair(rq, rq->curr);
  725. }
  726. /*
  727. * sched_yield() support is very simple - we dequeue and enqueue.
  728. *
  729. * If compat_yield is turned on then we requeue to the end of the tree.
  730. */
  731. static void yield_task_fair(struct rq *rq)
  732. {
  733. struct task_struct *curr = rq->curr;
  734. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  735. struct sched_entity *rightmost, *se = &curr->se;
  736. /*
  737. * Are we the only task in the tree?
  738. */
  739. if (unlikely(cfs_rq->nr_running == 1))
  740. return;
  741. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  742. __update_rq_clock(rq);
  743. /*
  744. * Update run-time statistics of the 'current'.
  745. */
  746. update_curr(cfs_rq);
  747. return;
  748. }
  749. /*
  750. * Find the rightmost entry in the rbtree:
  751. */
  752. rightmost = __pick_last_entity(cfs_rq);
  753. /*
  754. * Already in the rightmost position?
  755. */
  756. if (unlikely(!rightmost || rightmost->vruntime < se->vruntime))
  757. return;
  758. /*
  759. * Minimally necessary key value to be last in the tree:
  760. * Upon rescheduling, sched_class::put_prev_task() will place
  761. * 'current' within the tree based on its new key value.
  762. */
  763. se->vruntime = rightmost->vruntime + 1;
  764. }
  765. /*
  766. * wake_idle() will wake a task on an idle cpu if task->cpu is
  767. * not idle and an idle cpu is available. The span of cpus to
  768. * search starts with cpus closest then further out as needed,
  769. * so we always favor a closer, idle cpu.
  770. *
  771. * Returns the CPU we should wake onto.
  772. */
  773. #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
  774. static int wake_idle(int cpu, struct task_struct *p)
  775. {
  776. cpumask_t tmp;
  777. struct sched_domain *sd;
  778. int i;
  779. /*
  780. * If it is idle, then it is the best cpu to run this task.
  781. *
  782. * This cpu is also the best, if it has more than one task already.
  783. * Siblings must be also busy(in most cases) as they didn't already
  784. * pickup the extra load from this cpu and hence we need not check
  785. * sibling runqueue info. This will avoid the checks and cache miss
  786. * penalities associated with that.
  787. */
  788. if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
  789. return cpu;
  790. for_each_domain(cpu, sd) {
  791. if ((sd->flags & SD_WAKE_IDLE)
  792. || ((sd->flags & SD_WAKE_IDLE_FAR)
  793. && !task_hot(p, task_rq(p)->clock, sd))) {
  794. cpus_and(tmp, sd->span, p->cpus_allowed);
  795. for_each_cpu_mask(i, tmp) {
  796. if (idle_cpu(i)) {
  797. if (i != task_cpu(p)) {
  798. schedstat_inc(p,
  799. se.nr_wakeups_idle);
  800. }
  801. return i;
  802. }
  803. }
  804. } else {
  805. break;
  806. }
  807. }
  808. return cpu;
  809. }
  810. #else
  811. static inline int wake_idle(int cpu, struct task_struct *p)
  812. {
  813. return cpu;
  814. }
  815. #endif
  816. #ifdef CONFIG_SMP
  817. static const struct sched_class fair_sched_class;
  818. static int
  819. wake_affine(struct rq *rq, struct sched_domain *this_sd, struct rq *this_rq,
  820. struct task_struct *p, int prev_cpu, int this_cpu, int sync,
  821. int idx, unsigned long load, unsigned long this_load,
  822. unsigned int imbalance)
  823. {
  824. struct task_struct *curr = this_rq->curr;
  825. unsigned long tl = this_load;
  826. unsigned long tl_per_task;
  827. if (!(this_sd->flags & SD_WAKE_AFFINE))
  828. return 0;
  829. /*
  830. * If the currently running task will sleep within
  831. * a reasonable amount of time then attract this newly
  832. * woken task:
  833. */
  834. if (sync && curr->sched_class == &fair_sched_class) {
  835. if (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  836. p->se.avg_overlap < sysctl_sched_migration_cost)
  837. return 1;
  838. }
  839. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  840. tl_per_task = cpu_avg_load_per_task(this_cpu);
  841. /*
  842. * If sync wakeup then subtract the (maximum possible)
  843. * effect of the currently running task from the load
  844. * of the current CPU:
  845. */
  846. if (sync)
  847. tl -= current->se.load.weight;
  848. if ((tl <= load && tl + target_load(prev_cpu, idx) <= tl_per_task) ||
  849. 100*(tl + p->se.load.weight) <= imbalance*load) {
  850. /*
  851. * This domain has SD_WAKE_AFFINE and
  852. * p is cache cold in this domain, and
  853. * there is no bad imbalance.
  854. */
  855. schedstat_inc(this_sd, ttwu_move_affine);
  856. schedstat_inc(p, se.nr_wakeups_affine);
  857. return 1;
  858. }
  859. return 0;
  860. }
  861. static int select_task_rq_fair(struct task_struct *p, int sync)
  862. {
  863. struct sched_domain *sd, *this_sd = NULL;
  864. int prev_cpu, this_cpu, new_cpu;
  865. unsigned long load, this_load;
  866. struct rq *rq, *this_rq;
  867. unsigned int imbalance;
  868. int idx;
  869. prev_cpu = task_cpu(p);
  870. rq = task_rq(p);
  871. this_cpu = smp_processor_id();
  872. this_rq = cpu_rq(this_cpu);
  873. new_cpu = prev_cpu;
  874. /*
  875. * 'this_sd' is the first domain that both
  876. * this_cpu and prev_cpu are present in:
  877. */
  878. for_each_domain(this_cpu, sd) {
  879. if (cpu_isset(prev_cpu, sd->span)) {
  880. this_sd = sd;
  881. break;
  882. }
  883. }
  884. if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
  885. goto out;
  886. /*
  887. * Check for affine wakeup and passive balancing possibilities.
  888. */
  889. if (!this_sd)
  890. goto out;
  891. idx = this_sd->wake_idx;
  892. imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
  893. load = source_load(prev_cpu, idx);
  894. this_load = target_load(this_cpu, idx);
  895. if (wake_affine(rq, this_sd, this_rq, p, prev_cpu, this_cpu, sync, idx,
  896. load, this_load, imbalance))
  897. return this_cpu;
  898. if (prev_cpu == this_cpu)
  899. goto out;
  900. /*
  901. * Start passive balancing when half the imbalance_pct
  902. * limit is reached.
  903. */
  904. if (this_sd->flags & SD_WAKE_BALANCE) {
  905. if (imbalance*this_load <= 100*load) {
  906. schedstat_inc(this_sd, ttwu_move_balance);
  907. schedstat_inc(p, se.nr_wakeups_passive);
  908. return this_cpu;
  909. }
  910. }
  911. out:
  912. return wake_idle(new_cpu, p);
  913. }
  914. #endif /* CONFIG_SMP */
  915. static unsigned long wakeup_gran(struct sched_entity *se)
  916. {
  917. unsigned long gran = sysctl_sched_wakeup_granularity;
  918. /*
  919. * More easily preempt - nice tasks, while not making
  920. * it harder for + nice tasks.
  921. */
  922. if (unlikely(se->load.weight > NICE_0_LOAD))
  923. gran = calc_delta_fair(gran, &se->load);
  924. return gran;
  925. }
  926. /*
  927. * Should 'se' preempt 'curr'.
  928. *
  929. * |s1
  930. * |s2
  931. * |s3
  932. * g
  933. * |<--->|c
  934. *
  935. * w(c, s1) = -1
  936. * w(c, s2) = 0
  937. * w(c, s3) = 1
  938. *
  939. */
  940. static int
  941. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  942. {
  943. s64 gran, vdiff = curr->vruntime - se->vruntime;
  944. if (vdiff < 0)
  945. return -1;
  946. gran = wakeup_gran(curr);
  947. if (vdiff > gran)
  948. return 1;
  949. return 0;
  950. }
  951. /* return depth at which a sched entity is present in the hierarchy */
  952. static inline int depth_se(struct sched_entity *se)
  953. {
  954. int depth = 0;
  955. for_each_sched_entity(se)
  956. depth++;
  957. return depth;
  958. }
  959. /*
  960. * Preempt the current task with a newly woken task if needed:
  961. */
  962. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  963. {
  964. struct task_struct *curr = rq->curr;
  965. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  966. struct sched_entity *se = &curr->se, *pse = &p->se;
  967. int se_depth, pse_depth;
  968. if (unlikely(rt_prio(p->prio))) {
  969. update_rq_clock(rq);
  970. update_curr(cfs_rq);
  971. resched_task(curr);
  972. return;
  973. }
  974. se->last_wakeup = se->sum_exec_runtime;
  975. if (unlikely(se == pse))
  976. return;
  977. cfs_rq_of(pse)->next = pse;
  978. /*
  979. * Batch tasks do not preempt (their preemption is driven by
  980. * the tick):
  981. */
  982. if (unlikely(p->policy == SCHED_BATCH))
  983. return;
  984. if (!sched_feat(WAKEUP_PREEMPT))
  985. return;
  986. /*
  987. * preemption test can be made between sibling entities who are in the
  988. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  989. * both tasks until we find their ancestors who are siblings of common
  990. * parent.
  991. */
  992. /* First walk up until both entities are at same depth */
  993. se_depth = depth_se(se);
  994. pse_depth = depth_se(pse);
  995. while (se_depth > pse_depth) {
  996. se_depth--;
  997. se = parent_entity(se);
  998. }
  999. while (pse_depth > se_depth) {
  1000. pse_depth--;
  1001. pse = parent_entity(pse);
  1002. }
  1003. while (!is_same_group(se, pse)) {
  1004. se = parent_entity(se);
  1005. pse = parent_entity(pse);
  1006. }
  1007. if (wakeup_preempt_entity(se, pse) == 1)
  1008. resched_task(curr);
  1009. }
  1010. static struct task_struct *pick_next_task_fair(struct rq *rq)
  1011. {
  1012. struct task_struct *p;
  1013. struct cfs_rq *cfs_rq = &rq->cfs;
  1014. struct sched_entity *se;
  1015. if (unlikely(!cfs_rq->nr_running))
  1016. return NULL;
  1017. do {
  1018. se = pick_next_entity(cfs_rq);
  1019. cfs_rq = group_cfs_rq(se);
  1020. } while (cfs_rq);
  1021. p = task_of(se);
  1022. hrtick_start_fair(rq, p);
  1023. return p;
  1024. }
  1025. /*
  1026. * Account for a descheduled task:
  1027. */
  1028. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1029. {
  1030. struct sched_entity *se = &prev->se;
  1031. struct cfs_rq *cfs_rq;
  1032. for_each_sched_entity(se) {
  1033. cfs_rq = cfs_rq_of(se);
  1034. put_prev_entity(cfs_rq, se);
  1035. }
  1036. }
  1037. #ifdef CONFIG_SMP
  1038. /**************************************************
  1039. * Fair scheduling class load-balancing methods:
  1040. */
  1041. /*
  1042. * Load-balancing iterator. Note: while the runqueue stays locked
  1043. * during the whole iteration, the current task might be
  1044. * dequeued so the iterator has to be dequeue-safe. Here we
  1045. * achieve that by always pre-iterating before returning
  1046. * the current task:
  1047. */
  1048. static struct task_struct *
  1049. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  1050. {
  1051. struct task_struct *p = NULL;
  1052. struct sched_entity *se;
  1053. if (!curr)
  1054. return NULL;
  1055. /* Skip over entities that are not tasks */
  1056. do {
  1057. se = rb_entry(curr, struct sched_entity, run_node);
  1058. curr = rb_next(curr);
  1059. } while (curr && !entity_is_task(se));
  1060. cfs_rq->rb_load_balance_curr = curr;
  1061. if (entity_is_task(se))
  1062. p = task_of(se);
  1063. return p;
  1064. }
  1065. static struct task_struct *load_balance_start_fair(void *arg)
  1066. {
  1067. struct cfs_rq *cfs_rq = arg;
  1068. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  1069. }
  1070. static struct task_struct *load_balance_next_fair(void *arg)
  1071. {
  1072. struct cfs_rq *cfs_rq = arg;
  1073. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  1074. }
  1075. #ifdef CONFIG_FAIR_GROUP_SCHED
  1076. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  1077. {
  1078. struct sched_entity *curr;
  1079. struct task_struct *p;
  1080. if (!cfs_rq->nr_running || !first_fair(cfs_rq))
  1081. return MAX_PRIO;
  1082. curr = cfs_rq->curr;
  1083. if (!curr)
  1084. curr = __pick_next_entity(cfs_rq);
  1085. p = task_of(curr);
  1086. return p->prio;
  1087. }
  1088. #endif
  1089. static unsigned long
  1090. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1091. unsigned long max_load_move,
  1092. struct sched_domain *sd, enum cpu_idle_type idle,
  1093. int *all_pinned, int *this_best_prio)
  1094. {
  1095. struct cfs_rq *busy_cfs_rq;
  1096. long rem_load_move = max_load_move;
  1097. struct rq_iterator cfs_rq_iterator;
  1098. cfs_rq_iterator.start = load_balance_start_fair;
  1099. cfs_rq_iterator.next = load_balance_next_fair;
  1100. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1101. #ifdef CONFIG_FAIR_GROUP_SCHED
  1102. struct cfs_rq *this_cfs_rq;
  1103. long imbalance;
  1104. unsigned long maxload;
  1105. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  1106. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  1107. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  1108. if (imbalance <= 0)
  1109. continue;
  1110. /* Don't pull more than imbalance/2 */
  1111. imbalance /= 2;
  1112. maxload = min(rem_load_move, imbalance);
  1113. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  1114. #else
  1115. # define maxload rem_load_move
  1116. #endif
  1117. /*
  1118. * pass busy_cfs_rq argument into
  1119. * load_balance_[start|next]_fair iterators
  1120. */
  1121. cfs_rq_iterator.arg = busy_cfs_rq;
  1122. rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  1123. maxload, sd, idle, all_pinned,
  1124. this_best_prio,
  1125. &cfs_rq_iterator);
  1126. if (rem_load_move <= 0)
  1127. break;
  1128. }
  1129. return max_load_move - rem_load_move;
  1130. }
  1131. static int
  1132. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1133. struct sched_domain *sd, enum cpu_idle_type idle)
  1134. {
  1135. struct cfs_rq *busy_cfs_rq;
  1136. struct rq_iterator cfs_rq_iterator;
  1137. cfs_rq_iterator.start = load_balance_start_fair;
  1138. cfs_rq_iterator.next = load_balance_next_fair;
  1139. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1140. /*
  1141. * pass busy_cfs_rq argument into
  1142. * load_balance_[start|next]_fair iterators
  1143. */
  1144. cfs_rq_iterator.arg = busy_cfs_rq;
  1145. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  1146. &cfs_rq_iterator))
  1147. return 1;
  1148. }
  1149. return 0;
  1150. }
  1151. #endif
  1152. /*
  1153. * scheduler tick hitting a task of our scheduling class:
  1154. */
  1155. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  1156. {
  1157. struct cfs_rq *cfs_rq;
  1158. struct sched_entity *se = &curr->se;
  1159. for_each_sched_entity(se) {
  1160. cfs_rq = cfs_rq_of(se);
  1161. entity_tick(cfs_rq, se, queued);
  1162. }
  1163. }
  1164. #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  1165. /*
  1166. * Share the fairness runtime between parent and child, thus the
  1167. * total amount of pressure for CPU stays equal - new tasks
  1168. * get a chance to run but frequent forkers are not allowed to
  1169. * monopolize the CPU. Note: the parent runqueue is locked,
  1170. * the child is not running yet.
  1171. */
  1172. static void task_new_fair(struct rq *rq, struct task_struct *p)
  1173. {
  1174. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1175. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  1176. int this_cpu = smp_processor_id();
  1177. sched_info_queued(p);
  1178. update_curr(cfs_rq);
  1179. place_entity(cfs_rq, se, 1);
  1180. /* 'curr' will be NULL if the child belongs to a different group */
  1181. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  1182. curr && curr->vruntime < se->vruntime) {
  1183. /*
  1184. * Upon rescheduling, sched_class::put_prev_task() will place
  1185. * 'current' within the tree based on its new key value.
  1186. */
  1187. swap(curr->vruntime, se->vruntime);
  1188. }
  1189. enqueue_task_fair(rq, p, 0);
  1190. resched_task(rq->curr);
  1191. }
  1192. /*
  1193. * Priority of the task has changed. Check to see if we preempt
  1194. * the current task.
  1195. */
  1196. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  1197. int oldprio, int running)
  1198. {
  1199. /*
  1200. * Reschedule if we are currently running on this runqueue and
  1201. * our priority decreased, or if we are not currently running on
  1202. * this runqueue and our priority is higher than the current's
  1203. */
  1204. if (running) {
  1205. if (p->prio > oldprio)
  1206. resched_task(rq->curr);
  1207. } else
  1208. check_preempt_curr(rq, p);
  1209. }
  1210. /*
  1211. * We switched to the sched_fair class.
  1212. */
  1213. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  1214. int running)
  1215. {
  1216. /*
  1217. * We were most likely switched from sched_rt, so
  1218. * kick off the schedule if running, otherwise just see
  1219. * if we can still preempt the current task.
  1220. */
  1221. if (running)
  1222. resched_task(rq->curr);
  1223. else
  1224. check_preempt_curr(rq, p);
  1225. }
  1226. /* Account for a task changing its policy or group.
  1227. *
  1228. * This routine is mostly called to set cfs_rq->curr field when a task
  1229. * migrates between groups/classes.
  1230. */
  1231. static void set_curr_task_fair(struct rq *rq)
  1232. {
  1233. struct sched_entity *se = &rq->curr->se;
  1234. for_each_sched_entity(se)
  1235. set_next_entity(cfs_rq_of(se), se);
  1236. }
  1237. #ifdef CONFIG_FAIR_GROUP_SCHED
  1238. static void moved_group_fair(struct task_struct *p)
  1239. {
  1240. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1241. update_curr(cfs_rq);
  1242. place_entity(cfs_rq, &p->se, 1);
  1243. }
  1244. #endif
  1245. /*
  1246. * All the scheduling class methods:
  1247. */
  1248. static const struct sched_class fair_sched_class = {
  1249. .next = &idle_sched_class,
  1250. .enqueue_task = enqueue_task_fair,
  1251. .dequeue_task = dequeue_task_fair,
  1252. .yield_task = yield_task_fair,
  1253. #ifdef CONFIG_SMP
  1254. .select_task_rq = select_task_rq_fair,
  1255. #endif /* CONFIG_SMP */
  1256. .check_preempt_curr = check_preempt_wakeup,
  1257. .pick_next_task = pick_next_task_fair,
  1258. .put_prev_task = put_prev_task_fair,
  1259. #ifdef CONFIG_SMP
  1260. .load_balance = load_balance_fair,
  1261. .move_one_task = move_one_task_fair,
  1262. #endif
  1263. .set_curr_task = set_curr_task_fair,
  1264. .task_tick = task_tick_fair,
  1265. .task_new = task_new_fair,
  1266. .prio_changed = prio_changed_fair,
  1267. .switched_to = switched_to_fair,
  1268. #ifdef CONFIG_FAIR_GROUP_SCHED
  1269. .moved_group = moved_group_fair,
  1270. #endif
  1271. };
  1272. #ifdef CONFIG_SCHED_DEBUG
  1273. static void print_cfs_stats(struct seq_file *m, int cpu)
  1274. {
  1275. struct cfs_rq *cfs_rq;
  1276. rcu_read_lock();
  1277. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  1278. print_cfs_rq(m, cpu, cfs_rq);
  1279. rcu_read_unlock();
  1280. }
  1281. #endif