sched_fair.c 39 KB

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