sched_fair.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. /*
  23. * Targeted preemption latency for CPU-bound tasks:
  24. * (default: 20ms * (1 + ilog(ncpus)), units: nanoseconds)
  25. *
  26. * NOTE: this latency value is not the same as the concept of
  27. * 'timeslice length' - timeslices in CFS are of variable length
  28. * and have no persistent notion like in traditional, time-slice
  29. * based scheduling concepts.
  30. *
  31. * (to see the precise effective timeslice length of your workload,
  32. * run vmstat and monitor the context-switches (cs) field)
  33. */
  34. unsigned int sysctl_sched_latency = 20000000ULL;
  35. /*
  36. * Minimal preemption granularity for CPU-bound tasks:
  37. * (default: 4 msec * (1 + ilog(ncpus)), units: nanoseconds)
  38. */
  39. unsigned int sysctl_sched_min_granularity = 4000000ULL;
  40. /*
  41. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  42. */
  43. static unsigned int sched_nr_latency = 5;
  44. /*
  45. * After fork, child runs first. (default) If set to 0 then
  46. * parent will (try to) run first.
  47. */
  48. const_debug unsigned int sysctl_sched_child_runs_first = 1;
  49. /*
  50. * sys_sched_yield() compat mode
  51. *
  52. * This option switches the agressive yield implementation of the
  53. * old scheduler back on.
  54. */
  55. unsigned int __read_mostly sysctl_sched_compat_yield;
  56. /*
  57. * SCHED_BATCH wake-up granularity.
  58. * (default: 10 msec * (1 + ilog(ncpus)), units: nanoseconds)
  59. *
  60. * This option delays the preemption effects of decoupled workloads
  61. * and reduces their over-scheduling. Synchronous workloads will still
  62. * have immediate wakeup/sleep latencies.
  63. */
  64. unsigned int sysctl_sched_batch_wakeup_granularity = 10000000UL;
  65. /*
  66. * SCHED_OTHER wake-up granularity.
  67. * (default: 10 msec * (1 + ilog(ncpus)), units: nanoseconds)
  68. *
  69. * This option delays the preemption effects of decoupled workloads
  70. * and reduces their over-scheduling. Synchronous workloads will still
  71. * have immediate wakeup/sleep latencies.
  72. */
  73. unsigned int sysctl_sched_wakeup_granularity = 10000000UL;
  74. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  75. /**************************************************************
  76. * CFS operations on generic schedulable entities:
  77. */
  78. #ifdef CONFIG_FAIR_GROUP_SCHED
  79. /* cpu runqueue to which this cfs_rq is attached */
  80. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  81. {
  82. return cfs_rq->rq;
  83. }
  84. /* An entity is a task if it doesn't "own" a runqueue */
  85. #define entity_is_task(se) (!se->my_q)
  86. #else /* CONFIG_FAIR_GROUP_SCHED */
  87. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  88. {
  89. return container_of(cfs_rq, struct rq, cfs);
  90. }
  91. #define entity_is_task(se) 1
  92. #endif /* CONFIG_FAIR_GROUP_SCHED */
  93. static inline struct task_struct *task_of(struct sched_entity *se)
  94. {
  95. return container_of(se, struct task_struct, se);
  96. }
  97. /**************************************************************
  98. * Scheduling class tree data structure manipulation methods:
  99. */
  100. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  101. {
  102. s64 delta = (s64)(vruntime - min_vruntime);
  103. if (delta > 0)
  104. min_vruntime = vruntime;
  105. return min_vruntime;
  106. }
  107. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  108. {
  109. s64 delta = (s64)(vruntime - min_vruntime);
  110. if (delta < 0)
  111. min_vruntime = vruntime;
  112. return min_vruntime;
  113. }
  114. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  115. {
  116. return se->vruntime - cfs_rq->min_vruntime;
  117. }
  118. /*
  119. * Enqueue an entity into the rb-tree:
  120. */
  121. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  122. {
  123. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  124. struct rb_node *parent = NULL;
  125. struct sched_entity *entry;
  126. s64 key = entity_key(cfs_rq, se);
  127. int leftmost = 1;
  128. /*
  129. * Find the right place in the rbtree:
  130. */
  131. while (*link) {
  132. parent = *link;
  133. entry = rb_entry(parent, struct sched_entity, run_node);
  134. /*
  135. * We dont care about collisions. Nodes with
  136. * the same key stay together.
  137. */
  138. if (key < entity_key(cfs_rq, entry)) {
  139. link = &parent->rb_left;
  140. } else {
  141. link = &parent->rb_right;
  142. leftmost = 0;
  143. }
  144. }
  145. /*
  146. * Maintain a cache of leftmost tree entries (it is frequently
  147. * used):
  148. */
  149. if (leftmost)
  150. cfs_rq->rb_leftmost = &se->run_node;
  151. rb_link_node(&se->run_node, parent, link);
  152. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  153. }
  154. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  155. {
  156. if (cfs_rq->rb_leftmost == &se->run_node)
  157. cfs_rq->rb_leftmost = rb_next(&se->run_node);
  158. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  159. }
  160. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  161. {
  162. return cfs_rq->rb_leftmost;
  163. }
  164. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  165. {
  166. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  167. }
  168. static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  169. {
  170. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  171. struct sched_entity *se = NULL;
  172. struct rb_node *parent;
  173. while (*link) {
  174. parent = *link;
  175. se = rb_entry(parent, struct sched_entity, run_node);
  176. link = &parent->rb_right;
  177. }
  178. return se;
  179. }
  180. /**************************************************************
  181. * Scheduling class statistics methods:
  182. */
  183. #ifdef CONFIG_SCHED_DEBUG
  184. int sched_nr_latency_handler(struct ctl_table *table, int write,
  185. struct file *filp, void __user *buffer, size_t *lenp,
  186. loff_t *ppos)
  187. {
  188. int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  189. if (ret || !write)
  190. return ret;
  191. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  192. sysctl_sched_min_granularity);
  193. return 0;
  194. }
  195. #endif
  196. /*
  197. * The idea is to set a period in which each task runs once.
  198. *
  199. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  200. * this period because otherwise the slices get too small.
  201. *
  202. * p = (nr <= nl) ? l : l*nr/nl
  203. */
  204. static u64 __sched_period(unsigned long nr_running)
  205. {
  206. u64 period = sysctl_sched_latency;
  207. unsigned long nr_latency = sched_nr_latency;
  208. if (unlikely(nr_running > nr_latency)) {
  209. period *= nr_running;
  210. do_div(period, nr_latency);
  211. }
  212. return period;
  213. }
  214. /*
  215. * We calculate the wall-time slice from the period by taking a part
  216. * proportional to the weight.
  217. *
  218. * s = p*w/rw
  219. */
  220. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  221. {
  222. u64 slice = __sched_period(cfs_rq->nr_running);
  223. slice *= se->load.weight;
  224. do_div(slice, cfs_rq->load.weight);
  225. return slice;
  226. }
  227. /*
  228. * We calculate the vruntime slice.
  229. *
  230. * vs = s/w = p/rw
  231. */
  232. static u64 __sched_vslice(unsigned long rq_weight, unsigned long nr_running)
  233. {
  234. u64 vslice = __sched_period(nr_running);
  235. vslice *= NICE_0_LOAD;
  236. do_div(vslice, rq_weight);
  237. return vslice;
  238. }
  239. static u64 sched_vslice(struct cfs_rq *cfs_rq)
  240. {
  241. return __sched_vslice(cfs_rq->load.weight, cfs_rq->nr_running);
  242. }
  243. static u64 sched_vslice_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
  244. {
  245. return __sched_vslice(cfs_rq->load.weight + se->load.weight,
  246. cfs_rq->nr_running + 1);
  247. }
  248. /*
  249. * Update the current task's runtime statistics. Skip current tasks that
  250. * are not in our scheduling class.
  251. */
  252. static inline void
  253. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  254. unsigned long delta_exec)
  255. {
  256. unsigned long delta_exec_weighted;
  257. u64 vruntime;
  258. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  259. curr->sum_exec_runtime += delta_exec;
  260. schedstat_add(cfs_rq, exec_clock, delta_exec);
  261. delta_exec_weighted = delta_exec;
  262. if (unlikely(curr->load.weight != NICE_0_LOAD)) {
  263. delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
  264. &curr->load);
  265. }
  266. curr->vruntime += delta_exec_weighted;
  267. /*
  268. * maintain cfs_rq->min_vruntime to be a monotonic increasing
  269. * value tracking the leftmost vruntime in the tree.
  270. */
  271. if (first_fair(cfs_rq)) {
  272. vruntime = min_vruntime(curr->vruntime,
  273. __pick_next_entity(cfs_rq)->vruntime);
  274. } else
  275. vruntime = curr->vruntime;
  276. cfs_rq->min_vruntime =
  277. max_vruntime(cfs_rq->min_vruntime, vruntime);
  278. }
  279. static void update_curr(struct cfs_rq *cfs_rq)
  280. {
  281. struct sched_entity *curr = cfs_rq->curr;
  282. u64 now = rq_of(cfs_rq)->clock;
  283. unsigned long delta_exec;
  284. if (unlikely(!curr))
  285. return;
  286. /*
  287. * Get the amount of time the current task was running
  288. * since the last time we changed load (this cannot
  289. * overflow on 32 bits):
  290. */
  291. delta_exec = (unsigned long)(now - curr->exec_start);
  292. __update_curr(cfs_rq, curr, delta_exec);
  293. curr->exec_start = now;
  294. if (entity_is_task(curr)) {
  295. struct task_struct *curtask = task_of(curr);
  296. cpuacct_charge(curtask, delta_exec);
  297. }
  298. }
  299. static inline void
  300. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  301. {
  302. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  303. }
  304. /*
  305. * Task is being enqueued - update stats:
  306. */
  307. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  308. {
  309. /*
  310. * Are we enqueueing a waiting task? (for current tasks
  311. * a dequeue/enqueue event is a NOP)
  312. */
  313. if (se != cfs_rq->curr)
  314. update_stats_wait_start(cfs_rq, se);
  315. }
  316. static void
  317. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  318. {
  319. schedstat_set(se->wait_max, max(se->wait_max,
  320. rq_of(cfs_rq)->clock - se->wait_start));
  321. schedstat_set(se->wait_start, 0);
  322. }
  323. static inline void
  324. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  325. {
  326. /*
  327. * Mark the end of the wait period if dequeueing a
  328. * waiting task:
  329. */
  330. if (se != cfs_rq->curr)
  331. update_stats_wait_end(cfs_rq, se);
  332. }
  333. /*
  334. * We are picking a new current task - update its stats:
  335. */
  336. static inline void
  337. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  338. {
  339. /*
  340. * We are starting a new run period:
  341. */
  342. se->exec_start = rq_of(cfs_rq)->clock;
  343. }
  344. /**************************************************
  345. * Scheduling class queueing methods:
  346. */
  347. static void
  348. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  349. {
  350. update_load_add(&cfs_rq->load, se->load.weight);
  351. cfs_rq->nr_running++;
  352. se->on_rq = 1;
  353. }
  354. static void
  355. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  356. {
  357. update_load_sub(&cfs_rq->load, se->load.weight);
  358. cfs_rq->nr_running--;
  359. se->on_rq = 0;
  360. }
  361. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  362. {
  363. #ifdef CONFIG_SCHEDSTATS
  364. if (se->sleep_start) {
  365. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  366. if ((s64)delta < 0)
  367. delta = 0;
  368. if (unlikely(delta > se->sleep_max))
  369. se->sleep_max = delta;
  370. se->sleep_start = 0;
  371. se->sum_sleep_runtime += delta;
  372. }
  373. if (se->block_start) {
  374. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  375. if ((s64)delta < 0)
  376. delta = 0;
  377. if (unlikely(delta > se->block_max))
  378. se->block_max = delta;
  379. se->block_start = 0;
  380. se->sum_sleep_runtime += delta;
  381. /*
  382. * Blocking time is in units of nanosecs, so shift by 20 to
  383. * get a milliseconds-range estimation of the amount of
  384. * time that the task spent sleeping:
  385. */
  386. if (unlikely(prof_on == SLEEP_PROFILING)) {
  387. struct task_struct *tsk = task_of(se);
  388. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  389. delta >> 20);
  390. }
  391. }
  392. #endif
  393. }
  394. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  395. {
  396. #ifdef CONFIG_SCHED_DEBUG
  397. s64 d = se->vruntime - cfs_rq->min_vruntime;
  398. if (d < 0)
  399. d = -d;
  400. if (d > 3*sysctl_sched_latency)
  401. schedstat_inc(cfs_rq, nr_spread_over);
  402. #endif
  403. }
  404. static void
  405. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  406. {
  407. u64 vruntime;
  408. vruntime = cfs_rq->min_vruntime;
  409. if (sched_feat(TREE_AVG)) {
  410. struct sched_entity *last = __pick_last_entity(cfs_rq);
  411. if (last) {
  412. vruntime += last->vruntime;
  413. vruntime >>= 1;
  414. }
  415. } else if (sched_feat(APPROX_AVG) && cfs_rq->nr_running)
  416. vruntime += sched_vslice(cfs_rq)/2;
  417. /*
  418. * The 'current' period is already promised to the current tasks,
  419. * however the extra weight of the new task will slow them down a
  420. * little, place the new task so that it fits in the slot that
  421. * stays open at the end.
  422. */
  423. if (initial && sched_feat(START_DEBIT))
  424. vruntime += sched_vslice_add(cfs_rq, se);
  425. if (!initial) {
  426. /* sleeps upto a single latency don't count. */
  427. if (sched_feat(NEW_FAIR_SLEEPERS) && entity_is_task(se) &&
  428. task_of(se)->policy != SCHED_BATCH)
  429. vruntime -= sysctl_sched_latency;
  430. /* ensure we never gain time by being placed backwards. */
  431. vruntime = max_vruntime(se->vruntime, vruntime);
  432. }
  433. se->vruntime = vruntime;
  434. }
  435. static void
  436. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  437. {
  438. /*
  439. * Update run-time statistics of the 'current'.
  440. */
  441. update_curr(cfs_rq);
  442. if (wakeup) {
  443. place_entity(cfs_rq, se, 0);
  444. enqueue_sleeper(cfs_rq, se);
  445. }
  446. update_stats_enqueue(cfs_rq, se);
  447. check_spread(cfs_rq, se);
  448. if (se != cfs_rq->curr)
  449. __enqueue_entity(cfs_rq, se);
  450. account_entity_enqueue(cfs_rq, se);
  451. }
  452. static void
  453. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  454. {
  455. /*
  456. * Update run-time statistics of the 'current'.
  457. */
  458. update_curr(cfs_rq);
  459. update_stats_dequeue(cfs_rq, se);
  460. if (sleep) {
  461. #ifdef CONFIG_SCHEDSTATS
  462. if (entity_is_task(se)) {
  463. struct task_struct *tsk = task_of(se);
  464. if (tsk->state & TASK_INTERRUPTIBLE)
  465. se->sleep_start = rq_of(cfs_rq)->clock;
  466. if (tsk->state & TASK_UNINTERRUPTIBLE)
  467. se->block_start = rq_of(cfs_rq)->clock;
  468. }
  469. #endif
  470. }
  471. if (se != cfs_rq->curr)
  472. __dequeue_entity(cfs_rq, se);
  473. account_entity_dequeue(cfs_rq, se);
  474. }
  475. /*
  476. * Preempt the current task with a newly woken task if needed:
  477. */
  478. static void
  479. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  480. {
  481. unsigned long ideal_runtime, delta_exec;
  482. ideal_runtime = sched_slice(cfs_rq, curr);
  483. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  484. if (delta_exec > ideal_runtime)
  485. resched_task(rq_of(cfs_rq)->curr);
  486. }
  487. static void
  488. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  489. {
  490. /* 'current' is not kept within the tree. */
  491. if (se->on_rq) {
  492. /*
  493. * Any task has to be enqueued before it get to execute on
  494. * a CPU. So account for the time it spent waiting on the
  495. * runqueue.
  496. */
  497. update_stats_wait_end(cfs_rq, se);
  498. __dequeue_entity(cfs_rq, se);
  499. }
  500. update_stats_curr_start(cfs_rq, se);
  501. cfs_rq->curr = se;
  502. #ifdef CONFIG_SCHEDSTATS
  503. /*
  504. * Track our maximum slice length, if the CPU's load is at
  505. * least twice that of our own weight (i.e. dont track it
  506. * when there are only lesser-weight tasks around):
  507. */
  508. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  509. se->slice_max = max(se->slice_max,
  510. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  511. }
  512. #endif
  513. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  514. }
  515. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  516. {
  517. struct sched_entity *se = NULL;
  518. if (first_fair(cfs_rq)) {
  519. se = __pick_next_entity(cfs_rq);
  520. set_next_entity(cfs_rq, se);
  521. }
  522. return se;
  523. }
  524. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  525. {
  526. /*
  527. * If still on the runqueue then deactivate_task()
  528. * was not called and update_curr() has to be done:
  529. */
  530. if (prev->on_rq)
  531. update_curr(cfs_rq);
  532. check_spread(cfs_rq, prev);
  533. if (prev->on_rq) {
  534. update_stats_wait_start(cfs_rq, prev);
  535. /* Put 'current' back into the tree. */
  536. __enqueue_entity(cfs_rq, prev);
  537. }
  538. cfs_rq->curr = NULL;
  539. }
  540. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  541. {
  542. /*
  543. * Update run-time statistics of the 'current'.
  544. */
  545. update_curr(cfs_rq);
  546. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  547. check_preempt_tick(cfs_rq, curr);
  548. }
  549. /**************************************************
  550. * CFS operations on tasks:
  551. */
  552. #ifdef CONFIG_FAIR_GROUP_SCHED
  553. /* Walk up scheduling entities hierarchy */
  554. #define for_each_sched_entity(se) \
  555. for (; se; se = se->parent)
  556. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  557. {
  558. return p->se.cfs_rq;
  559. }
  560. /* runqueue on which this entity is (to be) queued */
  561. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  562. {
  563. return se->cfs_rq;
  564. }
  565. /* runqueue "owned" by this group */
  566. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  567. {
  568. return grp->my_q;
  569. }
  570. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  571. * another cpu ('this_cpu')
  572. */
  573. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  574. {
  575. return cfs_rq->tg->cfs_rq[this_cpu];
  576. }
  577. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  578. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  579. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  580. /* Do the two (enqueued) entities belong to the same group ? */
  581. static inline int
  582. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  583. {
  584. if (se->cfs_rq == pse->cfs_rq)
  585. return 1;
  586. return 0;
  587. }
  588. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  589. {
  590. return se->parent;
  591. }
  592. #else /* CONFIG_FAIR_GROUP_SCHED */
  593. #define for_each_sched_entity(se) \
  594. for (; se; se = NULL)
  595. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  596. {
  597. return &task_rq(p)->cfs;
  598. }
  599. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  600. {
  601. struct task_struct *p = task_of(se);
  602. struct rq *rq = task_rq(p);
  603. return &rq->cfs;
  604. }
  605. /* runqueue "owned" by this group */
  606. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  607. {
  608. return NULL;
  609. }
  610. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  611. {
  612. return &cpu_rq(this_cpu)->cfs;
  613. }
  614. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  615. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  616. static inline int
  617. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  618. {
  619. return 1;
  620. }
  621. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  622. {
  623. return NULL;
  624. }
  625. #endif /* CONFIG_FAIR_GROUP_SCHED */
  626. /*
  627. * The enqueue_task method is called before nr_running is
  628. * increased. Here we update the fair scheduling stats and
  629. * then put the task into the rbtree:
  630. */
  631. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  632. {
  633. struct cfs_rq *cfs_rq;
  634. struct sched_entity *se = &p->se;
  635. for_each_sched_entity(se) {
  636. if (se->on_rq)
  637. break;
  638. cfs_rq = cfs_rq_of(se);
  639. enqueue_entity(cfs_rq, se, wakeup);
  640. wakeup = 1;
  641. }
  642. }
  643. /*
  644. * The dequeue_task method is called before nr_running is
  645. * decreased. We remove the task from the rbtree and
  646. * update the fair scheduling stats:
  647. */
  648. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  649. {
  650. struct cfs_rq *cfs_rq;
  651. struct sched_entity *se = &p->se;
  652. for_each_sched_entity(se) {
  653. cfs_rq = cfs_rq_of(se);
  654. dequeue_entity(cfs_rq, se, sleep);
  655. /* Don't dequeue parent if it has other entities besides us */
  656. if (cfs_rq->load.weight)
  657. break;
  658. sleep = 1;
  659. }
  660. }
  661. /*
  662. * sched_yield() support is very simple - we dequeue and enqueue.
  663. *
  664. * If compat_yield is turned on then we requeue to the end of the tree.
  665. */
  666. static void yield_task_fair(struct rq *rq)
  667. {
  668. struct task_struct *curr = rq->curr;
  669. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  670. struct sched_entity *rightmost, *se = &curr->se;
  671. /*
  672. * Are we the only task in the tree?
  673. */
  674. if (unlikely(cfs_rq->nr_running == 1))
  675. return;
  676. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  677. __update_rq_clock(rq);
  678. /*
  679. * Update run-time statistics of the 'current'.
  680. */
  681. update_curr(cfs_rq);
  682. return;
  683. }
  684. /*
  685. * Find the rightmost entry in the rbtree:
  686. */
  687. rightmost = __pick_last_entity(cfs_rq);
  688. /*
  689. * Already in the rightmost position?
  690. */
  691. if (unlikely(rightmost->vruntime < se->vruntime))
  692. return;
  693. /*
  694. * Minimally necessary key value to be last in the tree:
  695. * Upon rescheduling, sched_class::put_prev_task() will place
  696. * 'current' within the tree based on its new key value.
  697. */
  698. se->vruntime = rightmost->vruntime + 1;
  699. }
  700. /*
  701. * Preempt the current task with a newly woken task if needed:
  702. */
  703. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  704. {
  705. struct task_struct *curr = rq->curr;
  706. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  707. struct sched_entity *se = &curr->se, *pse = &p->se;
  708. unsigned long gran;
  709. if (unlikely(rt_prio(p->prio))) {
  710. update_rq_clock(rq);
  711. update_curr(cfs_rq);
  712. resched_task(curr);
  713. return;
  714. }
  715. /*
  716. * Batch tasks do not preempt (their preemption is driven by
  717. * the tick):
  718. */
  719. if (unlikely(p->policy == SCHED_BATCH))
  720. return;
  721. if (!sched_feat(WAKEUP_PREEMPT))
  722. return;
  723. while (!is_same_group(se, pse)) {
  724. se = parent_entity(se);
  725. pse = parent_entity(pse);
  726. }
  727. gran = sysctl_sched_wakeup_granularity;
  728. if (unlikely(se->load.weight != NICE_0_LOAD))
  729. gran = calc_delta_fair(gran, &se->load);
  730. if (pse->vruntime + gran < se->vruntime)
  731. resched_task(curr);
  732. }
  733. static struct task_struct *pick_next_task_fair(struct rq *rq)
  734. {
  735. struct cfs_rq *cfs_rq = &rq->cfs;
  736. struct sched_entity *se;
  737. if (unlikely(!cfs_rq->nr_running))
  738. return NULL;
  739. do {
  740. se = pick_next_entity(cfs_rq);
  741. cfs_rq = group_cfs_rq(se);
  742. } while (cfs_rq);
  743. return task_of(se);
  744. }
  745. /*
  746. * Account for a descheduled task:
  747. */
  748. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  749. {
  750. struct sched_entity *se = &prev->se;
  751. struct cfs_rq *cfs_rq;
  752. for_each_sched_entity(se) {
  753. cfs_rq = cfs_rq_of(se);
  754. put_prev_entity(cfs_rq, se);
  755. }
  756. }
  757. #ifdef CONFIG_SMP
  758. /**************************************************
  759. * Fair scheduling class load-balancing methods:
  760. */
  761. /*
  762. * Load-balancing iterator. Note: while the runqueue stays locked
  763. * during the whole iteration, the current task might be
  764. * dequeued so the iterator has to be dequeue-safe. Here we
  765. * achieve that by always pre-iterating before returning
  766. * the current task:
  767. */
  768. static struct task_struct *
  769. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  770. {
  771. struct task_struct *p;
  772. if (!curr)
  773. return NULL;
  774. p = rb_entry(curr, struct task_struct, se.run_node);
  775. cfs_rq->rb_load_balance_curr = rb_next(curr);
  776. return p;
  777. }
  778. static struct task_struct *load_balance_start_fair(void *arg)
  779. {
  780. struct cfs_rq *cfs_rq = arg;
  781. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  782. }
  783. static struct task_struct *load_balance_next_fair(void *arg)
  784. {
  785. struct cfs_rq *cfs_rq = arg;
  786. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  787. }
  788. #ifdef CONFIG_FAIR_GROUP_SCHED
  789. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  790. {
  791. struct sched_entity *curr;
  792. struct task_struct *p;
  793. if (!cfs_rq->nr_running)
  794. return MAX_PRIO;
  795. curr = cfs_rq->curr;
  796. if (!curr)
  797. curr = __pick_next_entity(cfs_rq);
  798. p = task_of(curr);
  799. return p->prio;
  800. }
  801. #endif
  802. static unsigned long
  803. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  804. unsigned long max_load_move,
  805. struct sched_domain *sd, enum cpu_idle_type idle,
  806. int *all_pinned, int *this_best_prio)
  807. {
  808. struct cfs_rq *busy_cfs_rq;
  809. long rem_load_move = max_load_move;
  810. struct rq_iterator cfs_rq_iterator;
  811. cfs_rq_iterator.start = load_balance_start_fair;
  812. cfs_rq_iterator.next = load_balance_next_fair;
  813. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  814. #ifdef CONFIG_FAIR_GROUP_SCHED
  815. struct cfs_rq *this_cfs_rq;
  816. long imbalance;
  817. unsigned long maxload;
  818. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  819. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  820. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  821. if (imbalance <= 0)
  822. continue;
  823. /* Don't pull more than imbalance/2 */
  824. imbalance /= 2;
  825. maxload = min(rem_load_move, imbalance);
  826. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  827. #else
  828. # define maxload rem_load_move
  829. #endif
  830. /*
  831. * pass busy_cfs_rq argument into
  832. * load_balance_[start|next]_fair iterators
  833. */
  834. cfs_rq_iterator.arg = busy_cfs_rq;
  835. rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  836. maxload, sd, idle, all_pinned,
  837. this_best_prio,
  838. &cfs_rq_iterator);
  839. if (rem_load_move <= 0)
  840. break;
  841. }
  842. return max_load_move - rem_load_move;
  843. }
  844. static int
  845. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  846. struct sched_domain *sd, enum cpu_idle_type idle)
  847. {
  848. struct cfs_rq *busy_cfs_rq;
  849. struct rq_iterator cfs_rq_iterator;
  850. cfs_rq_iterator.start = load_balance_start_fair;
  851. cfs_rq_iterator.next = load_balance_next_fair;
  852. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  853. /*
  854. * pass busy_cfs_rq argument into
  855. * load_balance_[start|next]_fair iterators
  856. */
  857. cfs_rq_iterator.arg = busy_cfs_rq;
  858. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  859. &cfs_rq_iterator))
  860. return 1;
  861. }
  862. return 0;
  863. }
  864. #endif
  865. /*
  866. * scheduler tick hitting a task of our scheduling class:
  867. */
  868. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  869. {
  870. struct cfs_rq *cfs_rq;
  871. struct sched_entity *se = &curr->se;
  872. for_each_sched_entity(se) {
  873. cfs_rq = cfs_rq_of(se);
  874. entity_tick(cfs_rq, se);
  875. }
  876. }
  877. #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  878. /*
  879. * Share the fairness runtime between parent and child, thus the
  880. * total amount of pressure for CPU stays equal - new tasks
  881. * get a chance to run but frequent forkers are not allowed to
  882. * monopolize the CPU. Note: the parent runqueue is locked,
  883. * the child is not running yet.
  884. */
  885. static void task_new_fair(struct rq *rq, struct task_struct *p)
  886. {
  887. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  888. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  889. int this_cpu = smp_processor_id();
  890. sched_info_queued(p);
  891. update_curr(cfs_rq);
  892. place_entity(cfs_rq, se, 1);
  893. /* 'curr' will be NULL if the child belongs to a different group */
  894. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  895. curr && curr->vruntime < se->vruntime) {
  896. /*
  897. * Upon rescheduling, sched_class::put_prev_task() will place
  898. * 'current' within the tree based on its new key value.
  899. */
  900. swap(curr->vruntime, se->vruntime);
  901. }
  902. enqueue_task_fair(rq, p, 0);
  903. resched_task(rq->curr);
  904. }
  905. /* Account for a task changing its policy or group.
  906. *
  907. * This routine is mostly called to set cfs_rq->curr field when a task
  908. * migrates between groups/classes.
  909. */
  910. static void set_curr_task_fair(struct rq *rq)
  911. {
  912. struct sched_entity *se = &rq->curr->se;
  913. for_each_sched_entity(se)
  914. set_next_entity(cfs_rq_of(se), se);
  915. }
  916. /*
  917. * All the scheduling class methods:
  918. */
  919. static const struct sched_class fair_sched_class = {
  920. .next = &idle_sched_class,
  921. .enqueue_task = enqueue_task_fair,
  922. .dequeue_task = dequeue_task_fair,
  923. .yield_task = yield_task_fair,
  924. .check_preempt_curr = check_preempt_wakeup,
  925. .pick_next_task = pick_next_task_fair,
  926. .put_prev_task = put_prev_task_fair,
  927. #ifdef CONFIG_SMP
  928. .load_balance = load_balance_fair,
  929. .move_one_task = move_one_task_fair,
  930. #endif
  931. .set_curr_task = set_curr_task_fair,
  932. .task_tick = task_tick_fair,
  933. .task_new = task_new_fair,
  934. };
  935. #ifdef CONFIG_SCHED_DEBUG
  936. static void print_cfs_stats(struct seq_file *m, int cpu)
  937. {
  938. struct cfs_rq *cfs_rq;
  939. #ifdef CONFIG_FAIR_GROUP_SCHED
  940. print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  941. #endif
  942. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  943. print_cfs_rq(m, cpu, cfs_rq);
  944. }
  945. #endif