sched_fair.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  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: 5ms * (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 = 5000000ULL;
  36. /*
  37. * Minimal preemption granularity for CPU-bound tasks:
  38. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  39. */
  40. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  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. If set to 0 (default) then
  47. * parent will (try to) run first.
  48. */
  49. unsigned int sysctl_sched_child_runs_first __read_mostly;
  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: 1 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 = 1000000UL;
  66. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  67. static const struct sched_class fair_sched_class;
  68. /**************************************************************
  69. * CFS operations on generic schedulable entities:
  70. */
  71. #ifdef CONFIG_FAIR_GROUP_SCHED
  72. /* cpu runqueue to which this cfs_rq is attached */
  73. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  74. {
  75. return cfs_rq->rq;
  76. }
  77. /* An entity is a task if it doesn't "own" a runqueue */
  78. #define entity_is_task(se) (!se->my_q)
  79. static inline struct task_struct *task_of(struct sched_entity *se)
  80. {
  81. #ifdef CONFIG_SCHED_DEBUG
  82. WARN_ON_ONCE(!entity_is_task(se));
  83. #endif
  84. return container_of(se, struct task_struct, se);
  85. }
  86. /* Walk up scheduling entities hierarchy */
  87. #define for_each_sched_entity(se) \
  88. for (; se; se = se->parent)
  89. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  90. {
  91. return p->se.cfs_rq;
  92. }
  93. /* runqueue on which this entity is (to be) queued */
  94. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  95. {
  96. return se->cfs_rq;
  97. }
  98. /* runqueue "owned" by this group */
  99. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  100. {
  101. return grp->my_q;
  102. }
  103. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  104. * another cpu ('this_cpu')
  105. */
  106. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  107. {
  108. return cfs_rq->tg->cfs_rq[this_cpu];
  109. }
  110. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  111. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  112. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  113. /* Do the two (enqueued) entities belong to the same group ? */
  114. static inline int
  115. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  116. {
  117. if (se->cfs_rq == pse->cfs_rq)
  118. return 1;
  119. return 0;
  120. }
  121. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  122. {
  123. return se->parent;
  124. }
  125. /* return depth at which a sched entity is present in the hierarchy */
  126. static inline int depth_se(struct sched_entity *se)
  127. {
  128. int depth = 0;
  129. for_each_sched_entity(se)
  130. depth++;
  131. return depth;
  132. }
  133. static void
  134. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  135. {
  136. int se_depth, pse_depth;
  137. /*
  138. * preemption test can be made between sibling entities who are in the
  139. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  140. * both tasks until we find their ancestors who are siblings of common
  141. * parent.
  142. */
  143. /* First walk up until both entities are at same depth */
  144. se_depth = depth_se(*se);
  145. pse_depth = depth_se(*pse);
  146. while (se_depth > pse_depth) {
  147. se_depth--;
  148. *se = parent_entity(*se);
  149. }
  150. while (pse_depth > se_depth) {
  151. pse_depth--;
  152. *pse = parent_entity(*pse);
  153. }
  154. while (!is_same_group(*se, *pse)) {
  155. *se = parent_entity(*se);
  156. *pse = parent_entity(*pse);
  157. }
  158. }
  159. #else /* !CONFIG_FAIR_GROUP_SCHED */
  160. static inline struct task_struct *task_of(struct sched_entity *se)
  161. {
  162. return container_of(se, struct task_struct, se);
  163. }
  164. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  165. {
  166. return container_of(cfs_rq, struct rq, cfs);
  167. }
  168. #define entity_is_task(se) 1
  169. #define for_each_sched_entity(se) \
  170. for (; se; se = NULL)
  171. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  172. {
  173. return &task_rq(p)->cfs;
  174. }
  175. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  176. {
  177. struct task_struct *p = task_of(se);
  178. struct rq *rq = task_rq(p);
  179. return &rq->cfs;
  180. }
  181. /* runqueue "owned" by this group */
  182. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  183. {
  184. return NULL;
  185. }
  186. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  187. {
  188. return &cpu_rq(this_cpu)->cfs;
  189. }
  190. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  191. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  192. static inline int
  193. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  194. {
  195. return 1;
  196. }
  197. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  198. {
  199. return NULL;
  200. }
  201. static inline void
  202. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  203. {
  204. }
  205. #endif /* CONFIG_FAIR_GROUP_SCHED */
  206. /**************************************************************
  207. * Scheduling class tree data structure manipulation methods:
  208. */
  209. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  210. {
  211. s64 delta = (s64)(vruntime - min_vruntime);
  212. if (delta > 0)
  213. min_vruntime = vruntime;
  214. return min_vruntime;
  215. }
  216. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  217. {
  218. s64 delta = (s64)(vruntime - min_vruntime);
  219. if (delta < 0)
  220. min_vruntime = vruntime;
  221. return min_vruntime;
  222. }
  223. static inline int entity_before(struct sched_entity *a,
  224. struct sched_entity *b)
  225. {
  226. return (s64)(a->vruntime - b->vruntime) < 0;
  227. }
  228. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  229. {
  230. return se->vruntime - cfs_rq->min_vruntime;
  231. }
  232. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  233. {
  234. u64 vruntime = cfs_rq->min_vruntime;
  235. if (cfs_rq->curr)
  236. vruntime = cfs_rq->curr->vruntime;
  237. if (cfs_rq->rb_leftmost) {
  238. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  239. struct sched_entity,
  240. run_node);
  241. if (!cfs_rq->curr)
  242. vruntime = se->vruntime;
  243. else
  244. vruntime = min_vruntime(vruntime, se->vruntime);
  245. }
  246. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  247. }
  248. /*
  249. * Enqueue an entity into the rb-tree:
  250. */
  251. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  252. {
  253. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  254. struct rb_node *parent = NULL;
  255. struct sched_entity *entry;
  256. s64 key = entity_key(cfs_rq, se);
  257. int leftmost = 1;
  258. /*
  259. * Find the right place in the rbtree:
  260. */
  261. while (*link) {
  262. parent = *link;
  263. entry = rb_entry(parent, struct sched_entity, run_node);
  264. /*
  265. * We dont care about collisions. Nodes with
  266. * the same key stay together.
  267. */
  268. if (key < entity_key(cfs_rq, entry)) {
  269. link = &parent->rb_left;
  270. } else {
  271. link = &parent->rb_right;
  272. leftmost = 0;
  273. }
  274. }
  275. /*
  276. * Maintain a cache of leftmost tree entries (it is frequently
  277. * used):
  278. */
  279. if (leftmost)
  280. cfs_rq->rb_leftmost = &se->run_node;
  281. rb_link_node(&se->run_node, parent, link);
  282. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  283. }
  284. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  285. {
  286. if (cfs_rq->rb_leftmost == &se->run_node) {
  287. struct rb_node *next_node;
  288. next_node = rb_next(&se->run_node);
  289. cfs_rq->rb_leftmost = next_node;
  290. }
  291. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  292. }
  293. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  294. {
  295. struct rb_node *left = cfs_rq->rb_leftmost;
  296. if (!left)
  297. return NULL;
  298. return rb_entry(left, struct sched_entity, run_node);
  299. }
  300. static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  301. {
  302. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  303. if (!last)
  304. return NULL;
  305. return rb_entry(last, struct sched_entity, run_node);
  306. }
  307. /**************************************************************
  308. * Scheduling class statistics methods:
  309. */
  310. #ifdef CONFIG_SCHED_DEBUG
  311. int sched_nr_latency_handler(struct ctl_table *table, int write,
  312. struct file *filp, void __user *buffer, size_t *lenp,
  313. loff_t *ppos)
  314. {
  315. int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  316. if (ret || !write)
  317. return ret;
  318. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  319. sysctl_sched_min_granularity);
  320. return 0;
  321. }
  322. #endif
  323. /*
  324. * delta /= w
  325. */
  326. static inline unsigned long
  327. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  328. {
  329. if (unlikely(se->load.weight != NICE_0_LOAD))
  330. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  331. return delta;
  332. }
  333. /*
  334. * The idea is to set a period in which each task runs once.
  335. *
  336. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  337. * this period because otherwise the slices get too small.
  338. *
  339. * p = (nr <= nl) ? l : l*nr/nl
  340. */
  341. static u64 __sched_period(unsigned long nr_running)
  342. {
  343. u64 period = sysctl_sched_latency;
  344. unsigned long nr_latency = sched_nr_latency;
  345. if (unlikely(nr_running > nr_latency)) {
  346. period = sysctl_sched_min_granularity;
  347. period *= nr_running;
  348. }
  349. return period;
  350. }
  351. /*
  352. * We calculate the wall-time slice from the period by taking a part
  353. * proportional to the weight.
  354. *
  355. * s = p*P[w/rw]
  356. */
  357. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  358. {
  359. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  360. for_each_sched_entity(se) {
  361. struct load_weight *load;
  362. struct load_weight lw;
  363. cfs_rq = cfs_rq_of(se);
  364. load = &cfs_rq->load;
  365. if (unlikely(!se->on_rq)) {
  366. lw = cfs_rq->load;
  367. update_load_add(&lw, se->load.weight);
  368. load = &lw;
  369. }
  370. slice = calc_delta_mine(slice, se->load.weight, load);
  371. }
  372. return slice;
  373. }
  374. /*
  375. * We calculate the vruntime slice of a to be inserted task
  376. *
  377. * vs = s/w
  378. */
  379. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  380. {
  381. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  382. }
  383. /*
  384. * Update the current task's runtime statistics. Skip current tasks that
  385. * are not in our scheduling class.
  386. */
  387. static inline void
  388. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  389. unsigned long delta_exec)
  390. {
  391. unsigned long delta_exec_weighted;
  392. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  393. curr->sum_exec_runtime += delta_exec;
  394. schedstat_add(cfs_rq, exec_clock, delta_exec);
  395. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  396. curr->vruntime += delta_exec_weighted;
  397. update_min_vruntime(cfs_rq);
  398. }
  399. static void update_curr(struct cfs_rq *cfs_rq)
  400. {
  401. struct sched_entity *curr = cfs_rq->curr;
  402. u64 now = rq_of(cfs_rq)->clock;
  403. unsigned long delta_exec;
  404. if (unlikely(!curr))
  405. return;
  406. /*
  407. * Get the amount of time the current task was running
  408. * since the last time we changed load (this cannot
  409. * overflow on 32 bits):
  410. */
  411. delta_exec = (unsigned long)(now - curr->exec_start);
  412. if (!delta_exec)
  413. return;
  414. __update_curr(cfs_rq, curr, delta_exec);
  415. curr->exec_start = now;
  416. if (entity_is_task(curr)) {
  417. struct task_struct *curtask = task_of(curr);
  418. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  419. cpuacct_charge(curtask, delta_exec);
  420. account_group_exec_runtime(curtask, delta_exec);
  421. }
  422. }
  423. static inline void
  424. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  425. {
  426. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  427. }
  428. /*
  429. * Task is being enqueued - update stats:
  430. */
  431. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  432. {
  433. /*
  434. * Are we enqueueing a waiting task? (for current tasks
  435. * a dequeue/enqueue event is a NOP)
  436. */
  437. if (se != cfs_rq->curr)
  438. update_stats_wait_start(cfs_rq, se);
  439. }
  440. static void
  441. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  442. {
  443. schedstat_set(se->wait_max, max(se->wait_max,
  444. rq_of(cfs_rq)->clock - se->wait_start));
  445. schedstat_set(se->wait_count, se->wait_count + 1);
  446. schedstat_set(se->wait_sum, se->wait_sum +
  447. rq_of(cfs_rq)->clock - se->wait_start);
  448. #ifdef CONFIG_SCHEDSTATS
  449. if (entity_is_task(se)) {
  450. trace_sched_stat_wait(task_of(se),
  451. rq_of(cfs_rq)->clock - se->wait_start);
  452. }
  453. #endif
  454. schedstat_set(se->wait_start, 0);
  455. }
  456. static inline void
  457. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  458. {
  459. /*
  460. * Mark the end of the wait period if dequeueing a
  461. * waiting task:
  462. */
  463. if (se != cfs_rq->curr)
  464. update_stats_wait_end(cfs_rq, se);
  465. }
  466. /*
  467. * We are picking a new current task - update its stats:
  468. */
  469. static inline void
  470. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  471. {
  472. /*
  473. * We are starting a new run period:
  474. */
  475. se->exec_start = rq_of(cfs_rq)->clock;
  476. }
  477. /**************************************************
  478. * Scheduling class queueing methods:
  479. */
  480. #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
  481. static void
  482. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  483. {
  484. cfs_rq->task_weight += weight;
  485. }
  486. #else
  487. static inline void
  488. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  489. {
  490. }
  491. #endif
  492. static void
  493. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  494. {
  495. update_load_add(&cfs_rq->load, se->load.weight);
  496. if (!parent_entity(se))
  497. inc_cpu_load(rq_of(cfs_rq), se->load.weight);
  498. if (entity_is_task(se)) {
  499. add_cfs_task_weight(cfs_rq, se->load.weight);
  500. list_add(&se->group_node, &cfs_rq->tasks);
  501. }
  502. cfs_rq->nr_running++;
  503. se->on_rq = 1;
  504. }
  505. static void
  506. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  507. {
  508. update_load_sub(&cfs_rq->load, se->load.weight);
  509. if (!parent_entity(se))
  510. dec_cpu_load(rq_of(cfs_rq), se->load.weight);
  511. if (entity_is_task(se)) {
  512. add_cfs_task_weight(cfs_rq, -se->load.weight);
  513. list_del_init(&se->group_node);
  514. }
  515. cfs_rq->nr_running--;
  516. se->on_rq = 0;
  517. }
  518. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  519. {
  520. #ifdef CONFIG_SCHEDSTATS
  521. struct task_struct *tsk = NULL;
  522. if (entity_is_task(se))
  523. tsk = task_of(se);
  524. if (se->sleep_start) {
  525. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  526. if ((s64)delta < 0)
  527. delta = 0;
  528. if (unlikely(delta > se->sleep_max))
  529. se->sleep_max = delta;
  530. se->sleep_start = 0;
  531. se->sum_sleep_runtime += delta;
  532. if (tsk) {
  533. account_scheduler_latency(tsk, delta >> 10, 1);
  534. trace_sched_stat_sleep(tsk, delta);
  535. }
  536. }
  537. if (se->block_start) {
  538. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  539. if ((s64)delta < 0)
  540. delta = 0;
  541. if (unlikely(delta > se->block_max))
  542. se->block_max = delta;
  543. se->block_start = 0;
  544. se->sum_sleep_runtime += delta;
  545. if (tsk) {
  546. if (tsk->in_iowait) {
  547. se->iowait_sum += delta;
  548. se->iowait_count++;
  549. trace_sched_stat_iowait(tsk, delta);
  550. }
  551. /*
  552. * Blocking time is in units of nanosecs, so shift by
  553. * 20 to get a milliseconds-range estimation of the
  554. * amount of time that the task spent sleeping:
  555. */
  556. if (unlikely(prof_on == SLEEP_PROFILING)) {
  557. profile_hits(SLEEP_PROFILING,
  558. (void *)get_wchan(tsk),
  559. delta >> 20);
  560. }
  561. account_scheduler_latency(tsk, delta >> 10, 0);
  562. }
  563. }
  564. #endif
  565. }
  566. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  567. {
  568. #ifdef CONFIG_SCHED_DEBUG
  569. s64 d = se->vruntime - cfs_rq->min_vruntime;
  570. if (d < 0)
  571. d = -d;
  572. if (d > 3*sysctl_sched_latency)
  573. schedstat_inc(cfs_rq, nr_spread_over);
  574. #endif
  575. }
  576. static void
  577. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  578. {
  579. u64 vruntime = cfs_rq->min_vruntime;
  580. /*
  581. * The 'current' period is already promised to the current tasks,
  582. * however the extra weight of the new task will slow them down a
  583. * little, place the new task so that it fits in the slot that
  584. * stays open at the end.
  585. */
  586. if (initial && sched_feat(START_DEBIT))
  587. vruntime += sched_vslice(cfs_rq, se);
  588. if (!initial) {
  589. /* sleeps upto a single latency don't count. */
  590. if (sched_feat(FAIR_SLEEPERS)) {
  591. unsigned long thresh = sysctl_sched_latency;
  592. /*
  593. * Convert the sleeper threshold into virtual time.
  594. * SCHED_IDLE is a special sub-class. We care about
  595. * fairness only relative to other SCHED_IDLE tasks,
  596. * all of which have the same weight.
  597. */
  598. if (sched_feat(NORMALIZED_SLEEPER) &&
  599. (!entity_is_task(se) ||
  600. task_of(se)->policy != SCHED_IDLE))
  601. thresh = calc_delta_fair(thresh, se);
  602. /*
  603. * Halve their sleep time's effect, to allow
  604. * for a gentler effect of sleepers:
  605. */
  606. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  607. thresh >>= 1;
  608. vruntime -= thresh;
  609. }
  610. }
  611. /* ensure we never gain time by being placed backwards. */
  612. vruntime = max_vruntime(se->vruntime, vruntime);
  613. se->vruntime = vruntime;
  614. }
  615. static void
  616. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  617. {
  618. /*
  619. * Update run-time statistics of the 'current'.
  620. */
  621. update_curr(cfs_rq);
  622. account_entity_enqueue(cfs_rq, se);
  623. if (wakeup) {
  624. place_entity(cfs_rq, se, 0);
  625. enqueue_sleeper(cfs_rq, se);
  626. }
  627. update_stats_enqueue(cfs_rq, se);
  628. check_spread(cfs_rq, se);
  629. if (se != cfs_rq->curr)
  630. __enqueue_entity(cfs_rq, se);
  631. }
  632. static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  633. {
  634. if (!se || cfs_rq->last == se)
  635. cfs_rq->last = NULL;
  636. if (!se || cfs_rq->next == se)
  637. cfs_rq->next = NULL;
  638. }
  639. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  640. {
  641. for_each_sched_entity(se)
  642. __clear_buddies(cfs_rq_of(se), se);
  643. }
  644. static void
  645. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  646. {
  647. /*
  648. * Update run-time statistics of the 'current'.
  649. */
  650. update_curr(cfs_rq);
  651. update_stats_dequeue(cfs_rq, se);
  652. if (sleep) {
  653. #ifdef CONFIG_SCHEDSTATS
  654. if (entity_is_task(se)) {
  655. struct task_struct *tsk = task_of(se);
  656. if (tsk->state & TASK_INTERRUPTIBLE)
  657. se->sleep_start = rq_of(cfs_rq)->clock;
  658. if (tsk->state & TASK_UNINTERRUPTIBLE)
  659. se->block_start = rq_of(cfs_rq)->clock;
  660. }
  661. #endif
  662. }
  663. clear_buddies(cfs_rq, se);
  664. if (se != cfs_rq->curr)
  665. __dequeue_entity(cfs_rq, se);
  666. account_entity_dequeue(cfs_rq, se);
  667. update_min_vruntime(cfs_rq);
  668. }
  669. /*
  670. * Preempt the current task with a newly woken task if needed:
  671. */
  672. static void
  673. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  674. {
  675. unsigned long ideal_runtime, delta_exec;
  676. ideal_runtime = sched_slice(cfs_rq, curr);
  677. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  678. if (delta_exec > ideal_runtime) {
  679. resched_task(rq_of(cfs_rq)->curr);
  680. /*
  681. * The current task ran long enough, ensure it doesn't get
  682. * re-elected due to buddy favours.
  683. */
  684. clear_buddies(cfs_rq, curr);
  685. }
  686. }
  687. static void
  688. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  689. {
  690. /* 'current' is not kept within the tree. */
  691. if (se->on_rq) {
  692. /*
  693. * Any task has to be enqueued before it get to execute on
  694. * a CPU. So account for the time it spent waiting on the
  695. * runqueue.
  696. */
  697. update_stats_wait_end(cfs_rq, se);
  698. __dequeue_entity(cfs_rq, se);
  699. }
  700. update_stats_curr_start(cfs_rq, se);
  701. cfs_rq->curr = se;
  702. #ifdef CONFIG_SCHEDSTATS
  703. /*
  704. * Track our maximum slice length, if the CPU's load is at
  705. * least twice that of our own weight (i.e. dont track it
  706. * when there are only lesser-weight tasks around):
  707. */
  708. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  709. se->slice_max = max(se->slice_max,
  710. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  711. }
  712. #endif
  713. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  714. }
  715. static int
  716. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  717. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  718. {
  719. struct sched_entity *se = __pick_next_entity(cfs_rq);
  720. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, se) < 1)
  721. return cfs_rq->next;
  722. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, se) < 1)
  723. return cfs_rq->last;
  724. return se;
  725. }
  726. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  727. {
  728. /*
  729. * If still on the runqueue then deactivate_task()
  730. * was not called and update_curr() has to be done:
  731. */
  732. if (prev->on_rq)
  733. update_curr(cfs_rq);
  734. check_spread(cfs_rq, prev);
  735. if (prev->on_rq) {
  736. update_stats_wait_start(cfs_rq, prev);
  737. /* Put 'current' back into the tree. */
  738. __enqueue_entity(cfs_rq, prev);
  739. }
  740. cfs_rq->curr = NULL;
  741. }
  742. static void
  743. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  744. {
  745. /*
  746. * Update run-time statistics of the 'current'.
  747. */
  748. update_curr(cfs_rq);
  749. #ifdef CONFIG_SCHED_HRTICK
  750. /*
  751. * queued ticks are scheduled to match the slice, so don't bother
  752. * validating it and just reschedule.
  753. */
  754. if (queued) {
  755. resched_task(rq_of(cfs_rq)->curr);
  756. return;
  757. }
  758. /*
  759. * don't let the period tick interfere with the hrtick preemption
  760. */
  761. if (!sched_feat(DOUBLE_TICK) &&
  762. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  763. return;
  764. #endif
  765. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  766. check_preempt_tick(cfs_rq, curr);
  767. }
  768. /**************************************************
  769. * CFS operations on tasks:
  770. */
  771. #ifdef CONFIG_SCHED_HRTICK
  772. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  773. {
  774. struct sched_entity *se = &p->se;
  775. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  776. WARN_ON(task_rq(p) != rq);
  777. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  778. u64 slice = sched_slice(cfs_rq, se);
  779. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  780. s64 delta = slice - ran;
  781. if (delta < 0) {
  782. if (rq->curr == p)
  783. resched_task(p);
  784. return;
  785. }
  786. /*
  787. * Don't schedule slices shorter than 10000ns, that just
  788. * doesn't make sense. Rely on vruntime for fairness.
  789. */
  790. if (rq->curr != p)
  791. delta = max_t(s64, 10000LL, delta);
  792. hrtick_start(rq, delta);
  793. }
  794. }
  795. /*
  796. * called from enqueue/dequeue and updates the hrtick when the
  797. * current task is from our class and nr_running is low enough
  798. * to matter.
  799. */
  800. static void hrtick_update(struct rq *rq)
  801. {
  802. struct task_struct *curr = rq->curr;
  803. if (curr->sched_class != &fair_sched_class)
  804. return;
  805. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  806. hrtick_start_fair(rq, curr);
  807. }
  808. #else /* !CONFIG_SCHED_HRTICK */
  809. static inline void
  810. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  811. {
  812. }
  813. static inline void hrtick_update(struct rq *rq)
  814. {
  815. }
  816. #endif
  817. /*
  818. * The enqueue_task method is called before nr_running is
  819. * increased. Here we update the fair scheduling stats and
  820. * then put the task into the rbtree:
  821. */
  822. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  823. {
  824. struct cfs_rq *cfs_rq;
  825. struct sched_entity *se = &p->se;
  826. for_each_sched_entity(se) {
  827. if (se->on_rq)
  828. break;
  829. cfs_rq = cfs_rq_of(se);
  830. enqueue_entity(cfs_rq, se, wakeup);
  831. wakeup = 1;
  832. }
  833. hrtick_update(rq);
  834. }
  835. /*
  836. * The dequeue_task method is called before nr_running is
  837. * decreased. We remove the task from the rbtree and
  838. * update the fair scheduling stats:
  839. */
  840. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  841. {
  842. struct cfs_rq *cfs_rq;
  843. struct sched_entity *se = &p->se;
  844. for_each_sched_entity(se) {
  845. cfs_rq = cfs_rq_of(se);
  846. dequeue_entity(cfs_rq, se, sleep);
  847. /* Don't dequeue parent if it has other entities besides us */
  848. if (cfs_rq->load.weight)
  849. break;
  850. sleep = 1;
  851. }
  852. hrtick_update(rq);
  853. }
  854. /*
  855. * sched_yield() support is very simple - we dequeue and enqueue.
  856. *
  857. * If compat_yield is turned on then we requeue to the end of the tree.
  858. */
  859. static void yield_task_fair(struct rq *rq)
  860. {
  861. struct task_struct *curr = rq->curr;
  862. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  863. struct sched_entity *rightmost, *se = &curr->se;
  864. /*
  865. * Are we the only task in the tree?
  866. */
  867. if (unlikely(cfs_rq->nr_running == 1))
  868. return;
  869. clear_buddies(cfs_rq, se);
  870. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  871. update_rq_clock(rq);
  872. /*
  873. * Update run-time statistics of the 'current'.
  874. */
  875. update_curr(cfs_rq);
  876. return;
  877. }
  878. /*
  879. * Find the rightmost entry in the rbtree:
  880. */
  881. rightmost = __pick_last_entity(cfs_rq);
  882. /*
  883. * Already in the rightmost position?
  884. */
  885. if (unlikely(!rightmost || entity_before(rightmost, se)))
  886. return;
  887. /*
  888. * Minimally necessary key value to be last in the tree:
  889. * Upon rescheduling, sched_class::put_prev_task() will place
  890. * 'current' within the tree based on its new key value.
  891. */
  892. se->vruntime = rightmost->vruntime + 1;
  893. }
  894. #ifdef CONFIG_SMP
  895. #ifdef CONFIG_FAIR_GROUP_SCHED
  896. /*
  897. * effective_load() calculates the load change as seen from the root_task_group
  898. *
  899. * Adding load to a group doesn't make a group heavier, but can cause movement
  900. * of group shares between cpus. Assuming the shares were perfectly aligned one
  901. * can calculate the shift in shares.
  902. *
  903. * The problem is that perfectly aligning the shares is rather expensive, hence
  904. * we try to avoid doing that too often - see update_shares(), which ratelimits
  905. * this change.
  906. *
  907. * We compensate this by not only taking the current delta into account, but
  908. * also considering the delta between when the shares were last adjusted and
  909. * now.
  910. *
  911. * We still saw a performance dip, some tracing learned us that between
  912. * cgroup:/ and cgroup:/foo balancing the number of affine wakeups increased
  913. * significantly. Therefore try to bias the error in direction of failing
  914. * the affine wakeup.
  915. *
  916. */
  917. static long effective_load(struct task_group *tg, int cpu,
  918. long wl, long wg)
  919. {
  920. struct sched_entity *se = tg->se[cpu];
  921. if (!tg->parent)
  922. return wl;
  923. /*
  924. * By not taking the decrease of shares on the other cpu into
  925. * account our error leans towards reducing the affine wakeups.
  926. */
  927. if (!wl && sched_feat(ASYM_EFF_LOAD))
  928. return wl;
  929. for_each_sched_entity(se) {
  930. long S, rw, s, a, b;
  931. long more_w;
  932. /*
  933. * Instead of using this increment, also add the difference
  934. * between when the shares were last updated and now.
  935. */
  936. more_w = se->my_q->load.weight - se->my_q->rq_weight;
  937. wl += more_w;
  938. wg += more_w;
  939. S = se->my_q->tg->shares;
  940. s = se->my_q->shares;
  941. rw = se->my_q->rq_weight;
  942. a = S*(rw + wl);
  943. b = S*rw + s*wg;
  944. wl = s*(a-b);
  945. if (likely(b))
  946. wl /= b;
  947. /*
  948. * Assume the group is already running and will
  949. * thus already be accounted for in the weight.
  950. *
  951. * That is, moving shares between CPUs, does not
  952. * alter the group weight.
  953. */
  954. wg = 0;
  955. }
  956. return wl;
  957. }
  958. #else
  959. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  960. unsigned long wl, unsigned long wg)
  961. {
  962. return wl;
  963. }
  964. #endif
  965. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  966. {
  967. struct task_struct *curr = current;
  968. unsigned long this_load, load;
  969. int idx, this_cpu, prev_cpu;
  970. unsigned long tl_per_task;
  971. unsigned int imbalance;
  972. struct task_group *tg;
  973. unsigned long weight;
  974. int balanced;
  975. idx = sd->wake_idx;
  976. this_cpu = smp_processor_id();
  977. prev_cpu = task_cpu(p);
  978. load = source_load(prev_cpu, idx);
  979. this_load = target_load(this_cpu, idx);
  980. if (sync) {
  981. if (sched_feat(SYNC_LESS) &&
  982. (curr->se.avg_overlap > sysctl_sched_migration_cost ||
  983. p->se.avg_overlap > sysctl_sched_migration_cost))
  984. sync = 0;
  985. } else {
  986. if (sched_feat(SYNC_MORE) &&
  987. (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  988. p->se.avg_overlap < sysctl_sched_migration_cost))
  989. sync = 1;
  990. }
  991. /*
  992. * If sync wakeup then subtract the (maximum possible)
  993. * effect of the currently running task from the load
  994. * of the current CPU:
  995. */
  996. if (sync) {
  997. tg = task_group(current);
  998. weight = current->se.load.weight;
  999. this_load += effective_load(tg, this_cpu, -weight, -weight);
  1000. load += effective_load(tg, prev_cpu, 0, -weight);
  1001. }
  1002. tg = task_group(p);
  1003. weight = p->se.load.weight;
  1004. imbalance = 100 + (sd->imbalance_pct - 100) / 2;
  1005. /*
  1006. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  1007. * due to the sync cause above having dropped this_load to 0, we'll
  1008. * always have an imbalance, but there's really nothing you can do
  1009. * about that, so that's good too.
  1010. *
  1011. * Otherwise check if either cpus are near enough in load to allow this
  1012. * task to be woken on this_cpu.
  1013. */
  1014. balanced = !this_load ||
  1015. 100*(this_load + effective_load(tg, this_cpu, weight, weight)) <=
  1016. imbalance*(load + effective_load(tg, prev_cpu, 0, weight));
  1017. /*
  1018. * If the currently running task will sleep within
  1019. * a reasonable amount of time then attract this newly
  1020. * woken task:
  1021. */
  1022. if (sync && balanced)
  1023. return 1;
  1024. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  1025. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1026. if (balanced ||
  1027. (this_load <= load &&
  1028. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  1029. /*
  1030. * This domain has SD_WAKE_AFFINE and
  1031. * p is cache cold in this domain, and
  1032. * there is no bad imbalance.
  1033. */
  1034. schedstat_inc(sd, ttwu_move_affine);
  1035. schedstat_inc(p, se.nr_wakeups_affine);
  1036. return 1;
  1037. }
  1038. return 0;
  1039. }
  1040. /*
  1041. * find_idlest_group finds and returns the least busy CPU group within the
  1042. * domain.
  1043. */
  1044. static struct sched_group *
  1045. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  1046. int this_cpu, int load_idx)
  1047. {
  1048. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1049. unsigned long min_load = ULONG_MAX, this_load = 0;
  1050. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1051. do {
  1052. unsigned long load, avg_load;
  1053. int local_group;
  1054. int i;
  1055. /* Skip over this group if it has no CPUs allowed */
  1056. if (!cpumask_intersects(sched_group_cpus(group),
  1057. &p->cpus_allowed))
  1058. continue;
  1059. local_group = cpumask_test_cpu(this_cpu,
  1060. sched_group_cpus(group));
  1061. /* Tally up the load of all CPUs in the group */
  1062. avg_load = 0;
  1063. for_each_cpu(i, sched_group_cpus(group)) {
  1064. /* Bias balancing toward cpus of our domain */
  1065. if (local_group)
  1066. load = source_load(i, load_idx);
  1067. else
  1068. load = target_load(i, load_idx);
  1069. avg_load += load;
  1070. }
  1071. /* Adjust by relative CPU power of the group */
  1072. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  1073. if (local_group) {
  1074. this_load = avg_load;
  1075. this = group;
  1076. } else if (avg_load < min_load) {
  1077. min_load = avg_load;
  1078. idlest = group;
  1079. }
  1080. } while (group = group->next, group != sd->groups);
  1081. if (!idlest || 100*this_load < imbalance*min_load)
  1082. return NULL;
  1083. return idlest;
  1084. }
  1085. /*
  1086. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1087. */
  1088. static int
  1089. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1090. {
  1091. unsigned long load, min_load = ULONG_MAX;
  1092. int idlest = -1;
  1093. int i;
  1094. /* Traverse only the allowed CPUs */
  1095. for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
  1096. load = weighted_cpuload(i);
  1097. if (load < min_load || (load == min_load && i == this_cpu)) {
  1098. min_load = load;
  1099. idlest = i;
  1100. }
  1101. }
  1102. return idlest;
  1103. }
  1104. /*
  1105. * sched_balance_self: balance the current task (running on cpu) in domains
  1106. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1107. * SD_BALANCE_EXEC.
  1108. *
  1109. * Balance, ie. select the least loaded group.
  1110. *
  1111. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1112. *
  1113. * preempt must be disabled.
  1114. */
  1115. static int select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  1116. {
  1117. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  1118. int cpu = smp_processor_id();
  1119. int prev_cpu = task_cpu(p);
  1120. int new_cpu = cpu;
  1121. int want_affine = 0;
  1122. int want_sd = 1;
  1123. int sync = wake_flags & WF_SYNC;
  1124. if (sd_flag & SD_BALANCE_WAKE) {
  1125. if (sched_feat(AFFINE_WAKEUPS))
  1126. want_affine = 1;
  1127. new_cpu = prev_cpu;
  1128. }
  1129. rcu_read_lock();
  1130. for_each_domain(cpu, tmp) {
  1131. /*
  1132. * If power savings logic is enabled for a domain, see if we
  1133. * are not overloaded, if so, don't balance wider.
  1134. */
  1135. if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
  1136. unsigned long power = 0;
  1137. unsigned long nr_running = 0;
  1138. unsigned long capacity;
  1139. int i;
  1140. for_each_cpu(i, sched_domain_span(tmp)) {
  1141. power += power_of(i);
  1142. nr_running += cpu_rq(i)->cfs.nr_running;
  1143. }
  1144. capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  1145. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1146. nr_running /= 2;
  1147. if (nr_running < capacity)
  1148. want_sd = 0;
  1149. }
  1150. if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
  1151. cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
  1152. affine_sd = tmp;
  1153. want_affine = 0;
  1154. }
  1155. if (!want_sd && !want_affine)
  1156. break;
  1157. if (!(tmp->flags & sd_flag))
  1158. continue;
  1159. if (want_sd)
  1160. sd = tmp;
  1161. }
  1162. if (sched_feat(LB_SHARES_UPDATE)) {
  1163. /*
  1164. * Pick the largest domain to update shares over
  1165. */
  1166. tmp = sd;
  1167. if (affine_sd && (!tmp ||
  1168. cpumask_weight(sched_domain_span(affine_sd)) >
  1169. cpumask_weight(sched_domain_span(sd))))
  1170. tmp = affine_sd;
  1171. if (tmp)
  1172. update_shares(tmp);
  1173. }
  1174. if (affine_sd && wake_affine(affine_sd, p, sync)) {
  1175. new_cpu = cpu;
  1176. goto out;
  1177. }
  1178. while (sd) {
  1179. int load_idx = sd->forkexec_idx;
  1180. struct sched_group *group;
  1181. int weight;
  1182. if (!(sd->flags & sd_flag)) {
  1183. sd = sd->child;
  1184. continue;
  1185. }
  1186. if (sd_flag & SD_BALANCE_WAKE)
  1187. load_idx = sd->wake_idx;
  1188. group = find_idlest_group(sd, p, cpu, load_idx);
  1189. if (!group) {
  1190. sd = sd->child;
  1191. continue;
  1192. }
  1193. new_cpu = find_idlest_cpu(group, p, cpu);
  1194. if (new_cpu == -1 || new_cpu == cpu) {
  1195. /* Now try balancing at a lower domain level of cpu */
  1196. sd = sd->child;
  1197. continue;
  1198. }
  1199. /* Now try balancing at a lower domain level of new_cpu */
  1200. cpu = new_cpu;
  1201. weight = cpumask_weight(sched_domain_span(sd));
  1202. sd = NULL;
  1203. for_each_domain(cpu, tmp) {
  1204. if (weight <= cpumask_weight(sched_domain_span(tmp)))
  1205. break;
  1206. if (tmp->flags & sd_flag)
  1207. sd = tmp;
  1208. }
  1209. /* while loop will break here if sd == NULL */
  1210. }
  1211. out:
  1212. rcu_read_unlock();
  1213. return new_cpu;
  1214. }
  1215. #endif /* CONFIG_SMP */
  1216. /*
  1217. * Adaptive granularity
  1218. *
  1219. * se->avg_wakeup gives the average time a task runs until it does a wakeup,
  1220. * with the limit of wakeup_gran -- when it never does a wakeup.
  1221. *
  1222. * So the smaller avg_wakeup is the faster we want this task to preempt,
  1223. * but we don't want to treat the preemptee unfairly and therefore allow it
  1224. * to run for at least the amount of time we'd like to run.
  1225. *
  1226. * NOTE: we use 2*avg_wakeup to increase the probability of actually doing one
  1227. *
  1228. * NOTE: we use *nr_running to scale with load, this nicely matches the
  1229. * degrading latency on load.
  1230. */
  1231. static unsigned long
  1232. adaptive_gran(struct sched_entity *curr, struct sched_entity *se)
  1233. {
  1234. u64 this_run = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1235. u64 expected_wakeup = 2*se->avg_wakeup * cfs_rq_of(se)->nr_running;
  1236. u64 gran = 0;
  1237. if (this_run < expected_wakeup)
  1238. gran = expected_wakeup - this_run;
  1239. return min_t(s64, gran, sysctl_sched_wakeup_granularity);
  1240. }
  1241. static unsigned long
  1242. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  1243. {
  1244. unsigned long gran = sysctl_sched_wakeup_granularity;
  1245. if (cfs_rq_of(curr)->curr && sched_feat(ADAPTIVE_GRAN))
  1246. gran = adaptive_gran(curr, se);
  1247. /*
  1248. * Since its curr running now, convert the gran from real-time
  1249. * to virtual-time in his units.
  1250. */
  1251. if (sched_feat(ASYM_GRAN)) {
  1252. /*
  1253. * By using 'se' instead of 'curr' we penalize light tasks, so
  1254. * they get preempted easier. That is, if 'se' < 'curr' then
  1255. * the resulting gran will be larger, therefore penalizing the
  1256. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  1257. * be smaller, again penalizing the lighter task.
  1258. *
  1259. * This is especially important for buddies when the leftmost
  1260. * task is higher priority than the buddy.
  1261. */
  1262. if (unlikely(se->load.weight != NICE_0_LOAD))
  1263. gran = calc_delta_fair(gran, se);
  1264. } else {
  1265. if (unlikely(curr->load.weight != NICE_0_LOAD))
  1266. gran = calc_delta_fair(gran, curr);
  1267. }
  1268. return gran;
  1269. }
  1270. /*
  1271. * Should 'se' preempt 'curr'.
  1272. *
  1273. * |s1
  1274. * |s2
  1275. * |s3
  1276. * g
  1277. * |<--->|c
  1278. *
  1279. * w(c, s1) = -1
  1280. * w(c, s2) = 0
  1281. * w(c, s3) = 1
  1282. *
  1283. */
  1284. static int
  1285. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  1286. {
  1287. s64 gran, vdiff = curr->vruntime - se->vruntime;
  1288. if (vdiff <= 0)
  1289. return -1;
  1290. gran = wakeup_gran(curr, se);
  1291. if (vdiff > gran)
  1292. return 1;
  1293. return 0;
  1294. }
  1295. static void set_last_buddy(struct sched_entity *se)
  1296. {
  1297. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1298. for_each_sched_entity(se)
  1299. cfs_rq_of(se)->last = se;
  1300. }
  1301. }
  1302. static void set_next_buddy(struct sched_entity *se)
  1303. {
  1304. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1305. for_each_sched_entity(se)
  1306. cfs_rq_of(se)->next = se;
  1307. }
  1308. }
  1309. /*
  1310. * Preempt the current task with a newly woken task if needed:
  1311. */
  1312. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1313. {
  1314. struct task_struct *curr = rq->curr;
  1315. struct sched_entity *se = &curr->se, *pse = &p->se;
  1316. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  1317. int sync = wake_flags & WF_SYNC;
  1318. update_curr(cfs_rq);
  1319. if (unlikely(rt_prio(p->prio))) {
  1320. resched_task(curr);
  1321. return;
  1322. }
  1323. if (unlikely(p->sched_class != &fair_sched_class))
  1324. return;
  1325. if (unlikely(se == pse))
  1326. return;
  1327. /*
  1328. * Only set the backward buddy when the current task is still on the
  1329. * rq. This can happen when a wakeup gets interleaved with schedule on
  1330. * the ->pre_schedule() or idle_balance() point, either of which can
  1331. * drop the rq lock.
  1332. *
  1333. * Also, during early boot the idle thread is in the fair class, for
  1334. * obvious reasons its a bad idea to schedule back to the idle thread.
  1335. */
  1336. if (sched_feat(LAST_BUDDY) && likely(se->on_rq && curr != rq->idle))
  1337. set_last_buddy(se);
  1338. if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK))
  1339. set_next_buddy(pse);
  1340. /*
  1341. * We can come here with TIF_NEED_RESCHED already set from new task
  1342. * wake up path.
  1343. */
  1344. if (test_tsk_need_resched(curr))
  1345. return;
  1346. /*
  1347. * Batch and idle tasks do not preempt (their preemption is driven by
  1348. * the tick):
  1349. */
  1350. if (unlikely(p->policy != SCHED_NORMAL))
  1351. return;
  1352. /* Idle tasks are by definition preempted by everybody. */
  1353. if (unlikely(curr->policy == SCHED_IDLE)) {
  1354. resched_task(curr);
  1355. return;
  1356. }
  1357. if ((sched_feat(WAKEUP_SYNC) && sync) ||
  1358. (sched_feat(WAKEUP_OVERLAP) &&
  1359. (se->avg_overlap < sysctl_sched_migration_cost &&
  1360. pse->avg_overlap < sysctl_sched_migration_cost))) {
  1361. resched_task(curr);
  1362. return;
  1363. }
  1364. if (sched_feat(WAKEUP_RUNNING)) {
  1365. if (pse->avg_running < se->avg_running) {
  1366. set_next_buddy(pse);
  1367. resched_task(curr);
  1368. return;
  1369. }
  1370. }
  1371. if (!sched_feat(WAKEUP_PREEMPT))
  1372. return;
  1373. find_matching_se(&se, &pse);
  1374. BUG_ON(!pse);
  1375. if (wakeup_preempt_entity(se, pse) == 1)
  1376. resched_task(curr);
  1377. }
  1378. static struct task_struct *pick_next_task_fair(struct rq *rq)
  1379. {
  1380. struct task_struct *p;
  1381. struct cfs_rq *cfs_rq = &rq->cfs;
  1382. struct sched_entity *se;
  1383. if (unlikely(!cfs_rq->nr_running))
  1384. return NULL;
  1385. do {
  1386. se = pick_next_entity(cfs_rq);
  1387. /*
  1388. * If se was a buddy, clear it so that it will have to earn
  1389. * the favour again.
  1390. *
  1391. * If se was not a buddy, clear the buddies because neither
  1392. * was elegible to run, let them earn it again.
  1393. *
  1394. * IOW. unconditionally clear buddies.
  1395. */
  1396. __clear_buddies(cfs_rq, NULL);
  1397. set_next_entity(cfs_rq, se);
  1398. cfs_rq = group_cfs_rq(se);
  1399. } while (cfs_rq);
  1400. p = task_of(se);
  1401. hrtick_start_fair(rq, p);
  1402. return p;
  1403. }
  1404. /*
  1405. * Account for a descheduled task:
  1406. */
  1407. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1408. {
  1409. struct sched_entity *se = &prev->se;
  1410. struct cfs_rq *cfs_rq;
  1411. for_each_sched_entity(se) {
  1412. cfs_rq = cfs_rq_of(se);
  1413. put_prev_entity(cfs_rq, se);
  1414. }
  1415. }
  1416. #ifdef CONFIG_SMP
  1417. /**************************************************
  1418. * Fair scheduling class load-balancing methods:
  1419. */
  1420. /*
  1421. * Load-balancing iterator. Note: while the runqueue stays locked
  1422. * during the whole iteration, the current task might be
  1423. * dequeued so the iterator has to be dequeue-safe. Here we
  1424. * achieve that by always pre-iterating before returning
  1425. * the current task:
  1426. */
  1427. static struct task_struct *
  1428. __load_balance_iterator(struct cfs_rq *cfs_rq, struct list_head *next)
  1429. {
  1430. struct task_struct *p = NULL;
  1431. struct sched_entity *se;
  1432. if (next == &cfs_rq->tasks)
  1433. return NULL;
  1434. se = list_entry(next, struct sched_entity, group_node);
  1435. p = task_of(se);
  1436. cfs_rq->balance_iterator = next->next;
  1437. return p;
  1438. }
  1439. static struct task_struct *load_balance_start_fair(void *arg)
  1440. {
  1441. struct cfs_rq *cfs_rq = arg;
  1442. return __load_balance_iterator(cfs_rq, cfs_rq->tasks.next);
  1443. }
  1444. static struct task_struct *load_balance_next_fair(void *arg)
  1445. {
  1446. struct cfs_rq *cfs_rq = arg;
  1447. return __load_balance_iterator(cfs_rq, cfs_rq->balance_iterator);
  1448. }
  1449. static unsigned long
  1450. __load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1451. unsigned long max_load_move, struct sched_domain *sd,
  1452. enum cpu_idle_type idle, int *all_pinned, int *this_best_prio,
  1453. struct cfs_rq *cfs_rq)
  1454. {
  1455. struct rq_iterator cfs_rq_iterator;
  1456. cfs_rq_iterator.start = load_balance_start_fair;
  1457. cfs_rq_iterator.next = load_balance_next_fair;
  1458. cfs_rq_iterator.arg = cfs_rq;
  1459. return balance_tasks(this_rq, this_cpu, busiest,
  1460. max_load_move, sd, idle, all_pinned,
  1461. this_best_prio, &cfs_rq_iterator);
  1462. }
  1463. #ifdef CONFIG_FAIR_GROUP_SCHED
  1464. static unsigned long
  1465. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1466. unsigned long max_load_move,
  1467. struct sched_domain *sd, enum cpu_idle_type idle,
  1468. int *all_pinned, int *this_best_prio)
  1469. {
  1470. long rem_load_move = max_load_move;
  1471. int busiest_cpu = cpu_of(busiest);
  1472. struct task_group *tg;
  1473. rcu_read_lock();
  1474. update_h_load(busiest_cpu);
  1475. list_for_each_entry_rcu(tg, &task_groups, list) {
  1476. struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
  1477. unsigned long busiest_h_load = busiest_cfs_rq->h_load;
  1478. unsigned long busiest_weight = busiest_cfs_rq->load.weight;
  1479. u64 rem_load, moved_load;
  1480. /*
  1481. * empty group
  1482. */
  1483. if (!busiest_cfs_rq->task_weight)
  1484. continue;
  1485. rem_load = (u64)rem_load_move * busiest_weight;
  1486. rem_load = div_u64(rem_load, busiest_h_load + 1);
  1487. moved_load = __load_balance_fair(this_rq, this_cpu, busiest,
  1488. rem_load, sd, idle, all_pinned, this_best_prio,
  1489. tg->cfs_rq[busiest_cpu]);
  1490. if (!moved_load)
  1491. continue;
  1492. moved_load *= busiest_h_load;
  1493. moved_load = div_u64(moved_load, busiest_weight + 1);
  1494. rem_load_move -= moved_load;
  1495. if (rem_load_move < 0)
  1496. break;
  1497. }
  1498. rcu_read_unlock();
  1499. return max_load_move - rem_load_move;
  1500. }
  1501. #else
  1502. static unsigned long
  1503. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1504. unsigned long max_load_move,
  1505. struct sched_domain *sd, enum cpu_idle_type idle,
  1506. int *all_pinned, int *this_best_prio)
  1507. {
  1508. return __load_balance_fair(this_rq, this_cpu, busiest,
  1509. max_load_move, sd, idle, all_pinned,
  1510. this_best_prio, &busiest->cfs);
  1511. }
  1512. #endif
  1513. static int
  1514. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1515. struct sched_domain *sd, enum cpu_idle_type idle)
  1516. {
  1517. struct cfs_rq *busy_cfs_rq;
  1518. struct rq_iterator cfs_rq_iterator;
  1519. cfs_rq_iterator.start = load_balance_start_fair;
  1520. cfs_rq_iterator.next = load_balance_next_fair;
  1521. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1522. /*
  1523. * pass busy_cfs_rq argument into
  1524. * load_balance_[start|next]_fair iterators
  1525. */
  1526. cfs_rq_iterator.arg = busy_cfs_rq;
  1527. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  1528. &cfs_rq_iterator))
  1529. return 1;
  1530. }
  1531. return 0;
  1532. }
  1533. #endif /* CONFIG_SMP */
  1534. /*
  1535. * scheduler tick hitting a task of our scheduling class:
  1536. */
  1537. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  1538. {
  1539. struct cfs_rq *cfs_rq;
  1540. struct sched_entity *se = &curr->se;
  1541. for_each_sched_entity(se) {
  1542. cfs_rq = cfs_rq_of(se);
  1543. entity_tick(cfs_rq, se, queued);
  1544. }
  1545. }
  1546. /*
  1547. * Share the fairness runtime between parent and child, thus the
  1548. * total amount of pressure for CPU stays equal - new tasks
  1549. * get a chance to run but frequent forkers are not allowed to
  1550. * monopolize the CPU. Note: the parent runqueue is locked,
  1551. * the child is not running yet.
  1552. */
  1553. static void task_new_fair(struct rq *rq, struct task_struct *p)
  1554. {
  1555. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1556. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  1557. int this_cpu = smp_processor_id();
  1558. sched_info_queued(p);
  1559. update_curr(cfs_rq);
  1560. if (curr)
  1561. se->vruntime = curr->vruntime;
  1562. place_entity(cfs_rq, se, 1);
  1563. /* 'curr' will be NULL if the child belongs to a different group */
  1564. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  1565. curr && entity_before(curr, se)) {
  1566. /*
  1567. * Upon rescheduling, sched_class::put_prev_task() will place
  1568. * 'current' within the tree based on its new key value.
  1569. */
  1570. swap(curr->vruntime, se->vruntime);
  1571. resched_task(rq->curr);
  1572. }
  1573. enqueue_task_fair(rq, p, 0);
  1574. }
  1575. /*
  1576. * Priority of the task has changed. Check to see if we preempt
  1577. * the current task.
  1578. */
  1579. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  1580. int oldprio, int running)
  1581. {
  1582. /*
  1583. * Reschedule if we are currently running on this runqueue and
  1584. * our priority decreased, or if we are not currently running on
  1585. * this runqueue and our priority is higher than the current's
  1586. */
  1587. if (running) {
  1588. if (p->prio > oldprio)
  1589. resched_task(rq->curr);
  1590. } else
  1591. check_preempt_curr(rq, p, 0);
  1592. }
  1593. /*
  1594. * We switched to the sched_fair class.
  1595. */
  1596. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  1597. int running)
  1598. {
  1599. /*
  1600. * We were most likely switched from sched_rt, so
  1601. * kick off the schedule if running, otherwise just see
  1602. * if we can still preempt the current task.
  1603. */
  1604. if (running)
  1605. resched_task(rq->curr);
  1606. else
  1607. check_preempt_curr(rq, p, 0);
  1608. }
  1609. /* Account for a task changing its policy or group.
  1610. *
  1611. * This routine is mostly called to set cfs_rq->curr field when a task
  1612. * migrates between groups/classes.
  1613. */
  1614. static void set_curr_task_fair(struct rq *rq)
  1615. {
  1616. struct sched_entity *se = &rq->curr->se;
  1617. for_each_sched_entity(se)
  1618. set_next_entity(cfs_rq_of(se), se);
  1619. }
  1620. #ifdef CONFIG_FAIR_GROUP_SCHED
  1621. static void moved_group_fair(struct task_struct *p)
  1622. {
  1623. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1624. update_curr(cfs_rq);
  1625. place_entity(cfs_rq, &p->se, 1);
  1626. }
  1627. #endif
  1628. /*
  1629. * All the scheduling class methods:
  1630. */
  1631. static const struct sched_class fair_sched_class = {
  1632. .next = &idle_sched_class,
  1633. .enqueue_task = enqueue_task_fair,
  1634. .dequeue_task = dequeue_task_fair,
  1635. .yield_task = yield_task_fair,
  1636. .check_preempt_curr = check_preempt_wakeup,
  1637. .pick_next_task = pick_next_task_fair,
  1638. .put_prev_task = put_prev_task_fair,
  1639. #ifdef CONFIG_SMP
  1640. .select_task_rq = select_task_rq_fair,
  1641. .load_balance = load_balance_fair,
  1642. .move_one_task = move_one_task_fair,
  1643. #endif
  1644. .set_curr_task = set_curr_task_fair,
  1645. .task_tick = task_tick_fair,
  1646. .task_new = task_new_fair,
  1647. .prio_changed = prio_changed_fair,
  1648. .switched_to = switched_to_fair,
  1649. #ifdef CONFIG_FAIR_GROUP_SCHED
  1650. .moved_group = moved_group_fair,
  1651. #endif
  1652. };
  1653. #ifdef CONFIG_SCHED_DEBUG
  1654. static void print_cfs_stats(struct seq_file *m, int cpu)
  1655. {
  1656. struct cfs_rq *cfs_rq;
  1657. rcu_read_lock();
  1658. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  1659. print_cfs_rq(m, cpu, cfs_rq);
  1660. rcu_read_unlock();
  1661. }
  1662. #endif