sched_fair.c 25 KB

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