sched_fair.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. /*
  23. * Targeted preemption latency for CPU-bound tasks:
  24. * (default: 20ms, units: nanoseconds)
  25. *
  26. * NOTE: this latency value is not the same as the concept of
  27. * 'timeslice length' - timeslices in CFS are of variable length.
  28. * (to see the precise effective timeslice length of your workload,
  29. * run vmstat and monitor the context-switches field)
  30. *
  31. * On SMP systems the value of this is multiplied by the log2 of the
  32. * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
  33. * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
  34. * Targeted preemption latency for CPU-bound tasks:
  35. */
  36. const_debug unsigned int sysctl_sched_latency = 20000000ULL;
  37. /*
  38. * After fork, child runs first. (default) If set to 0 then
  39. * parent will (try to) run first.
  40. */
  41. const_debug unsigned int sysctl_sched_child_runs_first = 1;
  42. /*
  43. * Minimal preemption granularity for CPU-bound tasks:
  44. * (default: 2 msec, units: nanoseconds)
  45. */
  46. unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
  47. /*
  48. * sys_sched_yield() compat mode
  49. *
  50. * This option switches the agressive yield implementation of the
  51. * old scheduler back on.
  52. */
  53. unsigned int __read_mostly sysctl_sched_compat_yield;
  54. /*
  55. * SCHED_BATCH wake-up granularity.
  56. * (default: 25 msec, units: nanoseconds)
  57. *
  58. * This option delays the preemption effects of decoupled workloads
  59. * and reduces their over-scheduling. Synchronous workloads will still
  60. * have immediate wakeup/sleep latencies.
  61. */
  62. const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL;
  63. /*
  64. * SCHED_OTHER wake-up granularity.
  65. * (default: 1 msec, units: nanoseconds)
  66. *
  67. * This option delays the preemption effects of decoupled workloads
  68. * and reduces their over-scheduling. Synchronous workloads will still
  69. * have immediate wakeup/sleep latencies.
  70. */
  71. const_debug unsigned int sysctl_sched_wakeup_granularity = 2000000UL;
  72. unsigned int sysctl_sched_runtime_limit __read_mostly;
  73. extern struct sched_class fair_sched_class;
  74. /**************************************************************
  75. * CFS operations on generic schedulable entities:
  76. */
  77. #ifdef CONFIG_FAIR_GROUP_SCHED
  78. /* cpu runqueue to which this cfs_rq is attached */
  79. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  80. {
  81. return cfs_rq->rq;
  82. }
  83. /* An entity is a task if it doesn't "own" a runqueue */
  84. #define entity_is_task(se) (!se->my_q)
  85. #else /* CONFIG_FAIR_GROUP_SCHED */
  86. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  87. {
  88. return container_of(cfs_rq, struct rq, cfs);
  89. }
  90. #define entity_is_task(se) 1
  91. #endif /* CONFIG_FAIR_GROUP_SCHED */
  92. static inline struct task_struct *task_of(struct sched_entity *se)
  93. {
  94. return container_of(se, struct task_struct, se);
  95. }
  96. /**************************************************************
  97. * Scheduling class tree data structure manipulation methods:
  98. */
  99. static inline u64
  100. max_vruntime(u64 min_vruntime, u64 vruntime)
  101. {
  102. if ((vruntime > min_vruntime) ||
  103. (min_vruntime > (1ULL << 61) && vruntime < (1ULL << 50)))
  104. min_vruntime = vruntime;
  105. return min_vruntime;
  106. }
  107. static inline void
  108. set_leftmost(struct cfs_rq *cfs_rq, struct rb_node *leftmost)
  109. {
  110. struct sched_entity *se;
  111. cfs_rq->rb_leftmost = leftmost;
  112. if (leftmost)
  113. se = rb_entry(leftmost, struct sched_entity, run_node);
  114. }
  115. static inline s64
  116. entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  117. {
  118. return se->fair_key - cfs_rq->min_vruntime;
  119. }
  120. /*
  121. * Enqueue an entity into the rb-tree:
  122. */
  123. static void
  124. __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  125. {
  126. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  127. struct rb_node *parent = NULL;
  128. struct sched_entity *entry;
  129. s64 key = entity_key(cfs_rq, se);
  130. int leftmost = 1;
  131. /*
  132. * Find the right place in the rbtree:
  133. */
  134. while (*link) {
  135. parent = *link;
  136. entry = rb_entry(parent, struct sched_entity, run_node);
  137. /*
  138. * We dont care about collisions. Nodes with
  139. * the same key stay together.
  140. */
  141. if (key < entity_key(cfs_rq, entry)) {
  142. link = &parent->rb_left;
  143. } else {
  144. link = &parent->rb_right;
  145. leftmost = 0;
  146. }
  147. }
  148. /*
  149. * Maintain a cache of leftmost tree entries (it is frequently
  150. * used):
  151. */
  152. if (leftmost)
  153. set_leftmost(cfs_rq, &se->run_node);
  154. rb_link_node(&se->run_node, parent, link);
  155. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  156. update_load_add(&cfs_rq->load, se->load.weight);
  157. cfs_rq->nr_running++;
  158. se->on_rq = 1;
  159. }
  160. static void
  161. __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  162. {
  163. if (cfs_rq->rb_leftmost == &se->run_node)
  164. set_leftmost(cfs_rq, rb_next(&se->run_node));
  165. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  166. update_load_sub(&cfs_rq->load, se->load.weight);
  167. cfs_rq->nr_running--;
  168. se->on_rq = 0;
  169. }
  170. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  171. {
  172. return cfs_rq->rb_leftmost;
  173. }
  174. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  175. {
  176. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  177. }
  178. static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  179. {
  180. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  181. struct sched_entity *se = NULL;
  182. struct rb_node *parent;
  183. while (*link) {
  184. parent = *link;
  185. se = rb_entry(parent, struct sched_entity, run_node);
  186. link = &parent->rb_right;
  187. }
  188. return se;
  189. }
  190. /**************************************************************
  191. * Scheduling class statistics methods:
  192. */
  193. static u64 __sched_period(unsigned long nr_running)
  194. {
  195. u64 period = sysctl_sched_latency;
  196. unsigned long nr_latency =
  197. sysctl_sched_latency / sysctl_sched_min_granularity;
  198. if (unlikely(nr_running > nr_latency)) {
  199. period *= nr_running;
  200. do_div(period, nr_latency);
  201. }
  202. return period;
  203. }
  204. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  205. {
  206. u64 period = __sched_period(cfs_rq->nr_running);
  207. period *= se->load.weight;
  208. do_div(period, cfs_rq->load.weight);
  209. return period;
  210. }
  211. /*
  212. * Update the current task's runtime statistics. Skip current tasks that
  213. * are not in our scheduling class.
  214. */
  215. static inline void
  216. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  217. unsigned long delta_exec)
  218. {
  219. unsigned long delta_exec_weighted;
  220. u64 next_vruntime, min_vruntime;
  221. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  222. curr->sum_exec_runtime += delta_exec;
  223. schedstat_add(cfs_rq, exec_clock, delta_exec);
  224. delta_exec_weighted = delta_exec;
  225. if (unlikely(curr->load.weight != NICE_0_LOAD)) {
  226. delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
  227. &curr->load);
  228. }
  229. curr->vruntime += delta_exec_weighted;
  230. /*
  231. * maintain cfs_rq->min_vruntime to be a monotonic increasing
  232. * value tracking the leftmost vruntime in the tree.
  233. */
  234. if (first_fair(cfs_rq)) {
  235. next_vruntime = __pick_next_entity(cfs_rq)->vruntime;
  236. /* min_vruntime() := !max_vruntime() */
  237. min_vruntime = max_vruntime(curr->vruntime, next_vruntime);
  238. if (min_vruntime == next_vruntime)
  239. min_vruntime = curr->vruntime;
  240. else
  241. min_vruntime = next_vruntime;
  242. } else
  243. min_vruntime = curr->vruntime;
  244. cfs_rq->min_vruntime =
  245. max_vruntime(cfs_rq->min_vruntime, min_vruntime);
  246. }
  247. static void update_curr(struct cfs_rq *cfs_rq)
  248. {
  249. struct sched_entity *curr = cfs_rq->curr;
  250. u64 now = rq_of(cfs_rq)->clock;
  251. unsigned long delta_exec;
  252. if (unlikely(!curr))
  253. return;
  254. /*
  255. * Get the amount of time the current task was running
  256. * since the last time we changed load (this cannot
  257. * overflow on 32 bits):
  258. */
  259. delta_exec = (unsigned long)(now - curr->exec_start);
  260. __update_curr(cfs_rq, curr, delta_exec);
  261. curr->exec_start = now;
  262. }
  263. static inline void
  264. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  265. {
  266. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  267. }
  268. static inline unsigned long
  269. calc_weighted(unsigned long delta, struct sched_entity *se)
  270. {
  271. unsigned long weight = se->load.weight;
  272. if (unlikely(weight != NICE_0_LOAD))
  273. return (u64)delta * se->load.weight >> NICE_0_SHIFT;
  274. else
  275. return delta;
  276. }
  277. /*
  278. * Task is being enqueued - update stats:
  279. */
  280. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  281. {
  282. /*
  283. * Are we enqueueing a waiting task? (for current tasks
  284. * a dequeue/enqueue event is a NOP)
  285. */
  286. if (se != cfs_rq->curr)
  287. update_stats_wait_start(cfs_rq, se);
  288. /*
  289. * Update the key:
  290. */
  291. se->fair_key = se->vruntime;
  292. }
  293. static void
  294. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  295. {
  296. schedstat_set(se->wait_max, max(se->wait_max,
  297. rq_of(cfs_rq)->clock - se->wait_start));
  298. schedstat_set(se->wait_start, 0);
  299. }
  300. static inline void
  301. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  302. {
  303. update_curr(cfs_rq);
  304. /*
  305. * Mark the end of the wait period if dequeueing a
  306. * waiting task:
  307. */
  308. if (se != cfs_rq->curr)
  309. update_stats_wait_end(cfs_rq, se);
  310. }
  311. /*
  312. * We are picking a new current task - update its stats:
  313. */
  314. static inline void
  315. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  316. {
  317. /*
  318. * We are starting a new run period:
  319. */
  320. se->exec_start = rq_of(cfs_rq)->clock;
  321. }
  322. /*
  323. * We are descheduling a task - update its stats:
  324. */
  325. static inline void
  326. update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  327. {
  328. se->exec_start = 0;
  329. }
  330. /**************************************************
  331. * Scheduling class queueing methods:
  332. */
  333. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  334. {
  335. #ifdef CONFIG_SCHEDSTATS
  336. if (se->sleep_start) {
  337. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  338. if ((s64)delta < 0)
  339. delta = 0;
  340. if (unlikely(delta > se->sleep_max))
  341. se->sleep_max = delta;
  342. se->sleep_start = 0;
  343. se->sum_sleep_runtime += delta;
  344. }
  345. if (se->block_start) {
  346. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  347. if ((s64)delta < 0)
  348. delta = 0;
  349. if (unlikely(delta > se->block_max))
  350. se->block_max = delta;
  351. se->block_start = 0;
  352. se->sum_sleep_runtime += delta;
  353. /*
  354. * Blocking time is in units of nanosecs, so shift by 20 to
  355. * get a milliseconds-range estimation of the amount of
  356. * time that the task spent sleeping:
  357. */
  358. if (unlikely(prof_on == SLEEP_PROFILING)) {
  359. struct task_struct *tsk = task_of(se);
  360. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  361. delta >> 20);
  362. }
  363. }
  364. #endif
  365. }
  366. static void
  367. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  368. {
  369. u64 min_runtime, latency;
  370. min_runtime = cfs_rq->min_vruntime;
  371. if (sched_feat(USE_TREE_AVG)) {
  372. struct sched_entity *last = __pick_last_entity(cfs_rq);
  373. if (last) {
  374. min_runtime = __pick_next_entity(cfs_rq)->vruntime;
  375. min_runtime += last->vruntime;
  376. min_runtime >>= 1;
  377. }
  378. } else if (sched_feat(APPROX_AVG))
  379. min_runtime += sysctl_sched_latency/2;
  380. if (initial && sched_feat(START_DEBIT))
  381. min_runtime += sched_slice(cfs_rq, se);
  382. if (!initial && sched_feat(NEW_FAIR_SLEEPERS)) {
  383. latency = sysctl_sched_latency;
  384. if (min_runtime > latency)
  385. min_runtime -= latency;
  386. else
  387. min_runtime = 0;
  388. }
  389. se->vruntime = max(se->vruntime, min_runtime);
  390. }
  391. static void
  392. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  393. {
  394. /*
  395. * Update the fair clock.
  396. */
  397. update_curr(cfs_rq);
  398. if (wakeup) {
  399. place_entity(cfs_rq, se, 0);
  400. enqueue_sleeper(cfs_rq, se);
  401. }
  402. update_stats_enqueue(cfs_rq, se);
  403. __enqueue_entity(cfs_rq, se);
  404. }
  405. static void
  406. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  407. {
  408. update_stats_dequeue(cfs_rq, se);
  409. #ifdef CONFIG_SCHEDSTATS
  410. if (sleep) {
  411. if (entity_is_task(se)) {
  412. struct task_struct *tsk = task_of(se);
  413. if (tsk->state & TASK_INTERRUPTIBLE)
  414. se->sleep_start = rq_of(cfs_rq)->clock;
  415. if (tsk->state & TASK_UNINTERRUPTIBLE)
  416. se->block_start = rq_of(cfs_rq)->clock;
  417. }
  418. }
  419. #endif
  420. __dequeue_entity(cfs_rq, se);
  421. }
  422. /*
  423. * Preempt the current task with a newly woken task if needed:
  424. */
  425. static void
  426. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  427. {
  428. unsigned long ideal_runtime, delta_exec;
  429. ideal_runtime = sched_slice(cfs_rq, curr);
  430. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  431. if (delta_exec > ideal_runtime)
  432. resched_task(rq_of(cfs_rq)->curr);
  433. }
  434. static inline void
  435. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  436. {
  437. /*
  438. * Any task has to be enqueued before it get to execute on
  439. * a CPU. So account for the time it spent waiting on the
  440. * runqueue.
  441. */
  442. update_stats_wait_end(cfs_rq, se);
  443. update_stats_curr_start(cfs_rq, se);
  444. cfs_rq->curr = se;
  445. #ifdef CONFIG_SCHEDSTATS
  446. /*
  447. * Track our maximum slice length, if the CPU's load is at
  448. * least twice that of our own weight (i.e. dont track it
  449. * when there are only lesser-weight tasks around):
  450. */
  451. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  452. se->slice_max = max(se->slice_max,
  453. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  454. }
  455. #endif
  456. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  457. }
  458. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  459. {
  460. struct sched_entity *se = __pick_next_entity(cfs_rq);
  461. set_next_entity(cfs_rq, se);
  462. return se;
  463. }
  464. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  465. {
  466. /*
  467. * If still on the runqueue then deactivate_task()
  468. * was not called and update_curr() has to be done:
  469. */
  470. if (prev->on_rq)
  471. update_curr(cfs_rq);
  472. update_stats_curr_end(cfs_rq, prev);
  473. if (prev->on_rq)
  474. update_stats_wait_start(cfs_rq, prev);
  475. cfs_rq->curr = NULL;
  476. }
  477. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  478. {
  479. /*
  480. * Dequeue and enqueue the task to update its
  481. * position within the tree:
  482. */
  483. dequeue_entity(cfs_rq, curr, 0);
  484. enqueue_entity(cfs_rq, curr, 0);
  485. if (cfs_rq->nr_running > 1)
  486. check_preempt_tick(cfs_rq, curr);
  487. }
  488. /**************************************************
  489. * CFS operations on tasks:
  490. */
  491. #ifdef CONFIG_FAIR_GROUP_SCHED
  492. /* Walk up scheduling entities hierarchy */
  493. #define for_each_sched_entity(se) \
  494. for (; se; se = se->parent)
  495. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  496. {
  497. return p->se.cfs_rq;
  498. }
  499. /* runqueue on which this entity is (to be) queued */
  500. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  501. {
  502. return se->cfs_rq;
  503. }
  504. /* runqueue "owned" by this group */
  505. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  506. {
  507. return grp->my_q;
  508. }
  509. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  510. * another cpu ('this_cpu')
  511. */
  512. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  513. {
  514. /* A later patch will take group into account */
  515. return &cpu_rq(this_cpu)->cfs;
  516. }
  517. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  518. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  519. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  520. /* Do the two (enqueued) tasks belong to the same group ? */
  521. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  522. {
  523. if (curr->se.cfs_rq == p->se.cfs_rq)
  524. return 1;
  525. return 0;
  526. }
  527. #else /* CONFIG_FAIR_GROUP_SCHED */
  528. #define for_each_sched_entity(se) \
  529. for (; se; se = NULL)
  530. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  531. {
  532. return &task_rq(p)->cfs;
  533. }
  534. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  535. {
  536. struct task_struct *p = task_of(se);
  537. struct rq *rq = task_rq(p);
  538. return &rq->cfs;
  539. }
  540. /* runqueue "owned" by this group */
  541. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  542. {
  543. return NULL;
  544. }
  545. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  546. {
  547. return &cpu_rq(this_cpu)->cfs;
  548. }
  549. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  550. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  551. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  552. {
  553. return 1;
  554. }
  555. #endif /* CONFIG_FAIR_GROUP_SCHED */
  556. /*
  557. * The enqueue_task method is called before nr_running is
  558. * increased. Here we update the fair scheduling stats and
  559. * then put the task into the rbtree:
  560. */
  561. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  562. {
  563. struct cfs_rq *cfs_rq;
  564. struct sched_entity *se = &p->se;
  565. for_each_sched_entity(se) {
  566. if (se->on_rq)
  567. break;
  568. cfs_rq = cfs_rq_of(se);
  569. enqueue_entity(cfs_rq, se, wakeup);
  570. }
  571. }
  572. /*
  573. * The dequeue_task method is called before nr_running is
  574. * decreased. We remove the task from the rbtree and
  575. * update the fair scheduling stats:
  576. */
  577. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  578. {
  579. struct cfs_rq *cfs_rq;
  580. struct sched_entity *se = &p->se;
  581. for_each_sched_entity(se) {
  582. cfs_rq = cfs_rq_of(se);
  583. dequeue_entity(cfs_rq, se, sleep);
  584. /* Don't dequeue parent if it has other entities besides us */
  585. if (cfs_rq->load.weight)
  586. break;
  587. }
  588. }
  589. /*
  590. * sched_yield() support is very simple - we dequeue and enqueue.
  591. *
  592. * If compat_yield is turned on then we requeue to the end of the tree.
  593. */
  594. static void yield_task_fair(struct rq *rq, struct task_struct *p)
  595. {
  596. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  597. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  598. struct sched_entity *rightmost, *se = &p->se;
  599. struct rb_node *parent;
  600. /*
  601. * Are we the only task in the tree?
  602. */
  603. if (unlikely(cfs_rq->nr_running == 1))
  604. return;
  605. if (likely(!sysctl_sched_compat_yield)) {
  606. __update_rq_clock(rq);
  607. /*
  608. * Dequeue and enqueue the task to update its
  609. * position within the tree:
  610. */
  611. dequeue_entity(cfs_rq, &p->se, 0);
  612. enqueue_entity(cfs_rq, &p->se, 0);
  613. return;
  614. }
  615. /*
  616. * Find the rightmost entry in the rbtree:
  617. */
  618. do {
  619. parent = *link;
  620. link = &parent->rb_right;
  621. } while (*link);
  622. rightmost = rb_entry(parent, struct sched_entity, run_node);
  623. /*
  624. * Already in the rightmost position?
  625. */
  626. if (unlikely(rightmost == se))
  627. return;
  628. /*
  629. * Minimally necessary key value to be last in the tree:
  630. */
  631. se->fair_key = rightmost->fair_key + 1;
  632. if (cfs_rq->rb_leftmost == &se->run_node)
  633. cfs_rq->rb_leftmost = rb_next(&se->run_node);
  634. /*
  635. * Relink the task to the rightmost position:
  636. */
  637. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  638. rb_link_node(&se->run_node, parent, link);
  639. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  640. }
  641. /*
  642. * Preempt the current task with a newly woken task if needed:
  643. */
  644. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  645. {
  646. struct task_struct *curr = rq->curr;
  647. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  648. if (unlikely(rt_prio(p->prio))) {
  649. update_rq_clock(rq);
  650. update_curr(cfs_rq);
  651. resched_task(curr);
  652. return;
  653. }
  654. if (is_same_group(curr, p)) {
  655. s64 delta = curr->se.vruntime - p->se.vruntime;
  656. if (delta > (s64)sysctl_sched_wakeup_granularity)
  657. resched_task(curr);
  658. }
  659. }
  660. static struct task_struct *pick_next_task_fair(struct rq *rq)
  661. {
  662. struct cfs_rq *cfs_rq = &rq->cfs;
  663. struct sched_entity *se;
  664. if (unlikely(!cfs_rq->nr_running))
  665. return NULL;
  666. do {
  667. se = pick_next_entity(cfs_rq);
  668. cfs_rq = group_cfs_rq(se);
  669. } while (cfs_rq);
  670. return task_of(se);
  671. }
  672. /*
  673. * Account for a descheduled task:
  674. */
  675. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  676. {
  677. struct sched_entity *se = &prev->se;
  678. struct cfs_rq *cfs_rq;
  679. for_each_sched_entity(se) {
  680. cfs_rq = cfs_rq_of(se);
  681. put_prev_entity(cfs_rq, se);
  682. }
  683. }
  684. /**************************************************
  685. * Fair scheduling class load-balancing methods:
  686. */
  687. /*
  688. * Load-balancing iterator. Note: while the runqueue stays locked
  689. * during the whole iteration, the current task might be
  690. * dequeued so the iterator has to be dequeue-safe. Here we
  691. * achieve that by always pre-iterating before returning
  692. * the current task:
  693. */
  694. static inline struct task_struct *
  695. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  696. {
  697. struct task_struct *p;
  698. if (!curr)
  699. return NULL;
  700. p = rb_entry(curr, struct task_struct, se.run_node);
  701. cfs_rq->rb_load_balance_curr = rb_next(curr);
  702. return p;
  703. }
  704. static struct task_struct *load_balance_start_fair(void *arg)
  705. {
  706. struct cfs_rq *cfs_rq = arg;
  707. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  708. }
  709. static struct task_struct *load_balance_next_fair(void *arg)
  710. {
  711. struct cfs_rq *cfs_rq = arg;
  712. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  713. }
  714. #ifdef CONFIG_FAIR_GROUP_SCHED
  715. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  716. {
  717. struct sched_entity *curr;
  718. struct task_struct *p;
  719. if (!cfs_rq->nr_running)
  720. return MAX_PRIO;
  721. curr = __pick_next_entity(cfs_rq);
  722. p = task_of(curr);
  723. return p->prio;
  724. }
  725. #endif
  726. static unsigned long
  727. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  728. unsigned long max_nr_move, unsigned long max_load_move,
  729. struct sched_domain *sd, enum cpu_idle_type idle,
  730. int *all_pinned, int *this_best_prio)
  731. {
  732. struct cfs_rq *busy_cfs_rq;
  733. unsigned long load_moved, total_nr_moved = 0, nr_moved;
  734. long rem_load_move = max_load_move;
  735. struct rq_iterator cfs_rq_iterator;
  736. cfs_rq_iterator.start = load_balance_start_fair;
  737. cfs_rq_iterator.next = load_balance_next_fair;
  738. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  739. #ifdef CONFIG_FAIR_GROUP_SCHED
  740. struct cfs_rq *this_cfs_rq;
  741. long imbalance;
  742. unsigned long maxload;
  743. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  744. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  745. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  746. if (imbalance <= 0)
  747. continue;
  748. /* Don't pull more than imbalance/2 */
  749. imbalance /= 2;
  750. maxload = min(rem_load_move, imbalance);
  751. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  752. #else
  753. # define maxload rem_load_move
  754. #endif
  755. /* pass busy_cfs_rq argument into
  756. * load_balance_[start|next]_fair iterators
  757. */
  758. cfs_rq_iterator.arg = busy_cfs_rq;
  759. nr_moved = balance_tasks(this_rq, this_cpu, busiest,
  760. max_nr_move, maxload, sd, idle, all_pinned,
  761. &load_moved, this_best_prio, &cfs_rq_iterator);
  762. total_nr_moved += nr_moved;
  763. max_nr_move -= nr_moved;
  764. rem_load_move -= load_moved;
  765. if (max_nr_move <= 0 || rem_load_move <= 0)
  766. break;
  767. }
  768. return max_load_move - rem_load_move;
  769. }
  770. /*
  771. * scheduler tick hitting a task of our scheduling class:
  772. */
  773. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  774. {
  775. struct cfs_rq *cfs_rq;
  776. struct sched_entity *se = &curr->se;
  777. for_each_sched_entity(se) {
  778. cfs_rq = cfs_rq_of(se);
  779. entity_tick(cfs_rq, se);
  780. }
  781. }
  782. #define swap(a,b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  783. /*
  784. * Share the fairness runtime between parent and child, thus the
  785. * total amount of pressure for CPU stays equal - new tasks
  786. * get a chance to run but frequent forkers are not allowed to
  787. * monopolize the CPU. Note: the parent runqueue is locked,
  788. * the child is not running yet.
  789. */
  790. static void task_new_fair(struct rq *rq, struct task_struct *p)
  791. {
  792. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  793. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  794. sched_info_queued(p);
  795. update_curr(cfs_rq);
  796. place_entity(cfs_rq, se, 1);
  797. if (sysctl_sched_child_runs_first &&
  798. curr->vruntime < se->vruntime) {
  799. dequeue_entity(cfs_rq, curr, 0);
  800. swap(curr->vruntime, se->vruntime);
  801. enqueue_entity(cfs_rq, curr, 0);
  802. }
  803. update_stats_enqueue(cfs_rq, se);
  804. __enqueue_entity(cfs_rq, se);
  805. resched_task(rq->curr);
  806. }
  807. #ifdef CONFIG_FAIR_GROUP_SCHED
  808. /* Account for a task changing its policy or group.
  809. *
  810. * This routine is mostly called to set cfs_rq->curr field when a task
  811. * migrates between groups/classes.
  812. */
  813. static void set_curr_task_fair(struct rq *rq)
  814. {
  815. struct sched_entity *se = &rq->curr->se;
  816. for_each_sched_entity(se)
  817. set_next_entity(cfs_rq_of(se), se);
  818. }
  819. #else
  820. static void set_curr_task_fair(struct rq *rq)
  821. {
  822. }
  823. #endif
  824. /*
  825. * All the scheduling class methods:
  826. */
  827. struct sched_class fair_sched_class __read_mostly = {
  828. .enqueue_task = enqueue_task_fair,
  829. .dequeue_task = dequeue_task_fair,
  830. .yield_task = yield_task_fair,
  831. .check_preempt_curr = check_preempt_wakeup,
  832. .pick_next_task = pick_next_task_fair,
  833. .put_prev_task = put_prev_task_fair,
  834. .load_balance = load_balance_fair,
  835. .set_curr_task = set_curr_task_fair,
  836. .task_tick = task_tick_fair,
  837. .task_new = task_new_fair,
  838. };
  839. #ifdef CONFIG_SCHED_DEBUG
  840. static void print_cfs_stats(struct seq_file *m, int cpu)
  841. {
  842. struct cfs_rq *cfs_rq;
  843. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  844. print_cfs_rq(m, cpu, cfs_rq);
  845. }
  846. #endif