sched_fair.c 35 KB

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