sched_fair.c 24 KB

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