sched_fair.c 34 KB

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