sched_fair.c 25 KB

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