sched_fair.c 51 KB

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