sched_fair.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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 * 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: 1 msec * ilog(ncpus), units: nanoseconds)
  38. */
  39. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  40. /*
  41. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  42. */
  43. unsigned int sched_nr_latency = 20;
  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 * 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 * 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. }
  295. static inline void
  296. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  297. {
  298. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  299. }
  300. /*
  301. * Task is being enqueued - update stats:
  302. */
  303. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  304. {
  305. /*
  306. * Are we enqueueing a waiting task? (for current tasks
  307. * a dequeue/enqueue event is a NOP)
  308. */
  309. if (se != cfs_rq->curr)
  310. update_stats_wait_start(cfs_rq, se);
  311. }
  312. static void
  313. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  314. {
  315. schedstat_set(se->wait_max, max(se->wait_max,
  316. rq_of(cfs_rq)->clock - se->wait_start));
  317. schedstat_set(se->wait_start, 0);
  318. }
  319. static inline void
  320. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  321. {
  322. /*
  323. * Mark the end of the wait period if dequeueing a
  324. * waiting task:
  325. */
  326. if (se != cfs_rq->curr)
  327. update_stats_wait_end(cfs_rq, se);
  328. }
  329. /*
  330. * We are picking a new current task - update its stats:
  331. */
  332. static inline void
  333. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  334. {
  335. /*
  336. * We are starting a new run period:
  337. */
  338. se->exec_start = rq_of(cfs_rq)->clock;
  339. }
  340. /**************************************************
  341. * Scheduling class queueing methods:
  342. */
  343. static void
  344. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  345. {
  346. update_load_add(&cfs_rq->load, se->load.weight);
  347. cfs_rq->nr_running++;
  348. se->on_rq = 1;
  349. }
  350. static void
  351. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  352. {
  353. update_load_sub(&cfs_rq->load, se->load.weight);
  354. cfs_rq->nr_running--;
  355. se->on_rq = 0;
  356. }
  357. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  358. {
  359. #ifdef CONFIG_SCHEDSTATS
  360. if (se->sleep_start) {
  361. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  362. if ((s64)delta < 0)
  363. delta = 0;
  364. if (unlikely(delta > se->sleep_max))
  365. se->sleep_max = delta;
  366. se->sleep_start = 0;
  367. se->sum_sleep_runtime += delta;
  368. }
  369. if (se->block_start) {
  370. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  371. if ((s64)delta < 0)
  372. delta = 0;
  373. if (unlikely(delta > se->block_max))
  374. se->block_max = delta;
  375. se->block_start = 0;
  376. se->sum_sleep_runtime += delta;
  377. /*
  378. * Blocking time is in units of nanosecs, so shift by 20 to
  379. * get a milliseconds-range estimation of the amount of
  380. * time that the task spent sleeping:
  381. */
  382. if (unlikely(prof_on == SLEEP_PROFILING)) {
  383. struct task_struct *tsk = task_of(se);
  384. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  385. delta >> 20);
  386. }
  387. }
  388. #endif
  389. }
  390. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  391. {
  392. #ifdef CONFIG_SCHED_DEBUG
  393. s64 d = se->vruntime - cfs_rq->min_vruntime;
  394. if (d < 0)
  395. d = -d;
  396. if (d > 3*sysctl_sched_latency)
  397. schedstat_inc(cfs_rq, nr_spread_over);
  398. #endif
  399. }
  400. static void
  401. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  402. {
  403. u64 vruntime;
  404. vruntime = cfs_rq->min_vruntime;
  405. if (sched_feat(TREE_AVG)) {
  406. struct sched_entity *last = __pick_last_entity(cfs_rq);
  407. if (last) {
  408. vruntime += last->vruntime;
  409. vruntime >>= 1;
  410. }
  411. } else if (sched_feat(APPROX_AVG) && cfs_rq->nr_running)
  412. vruntime += sched_vslice(cfs_rq)/2;
  413. /*
  414. * The 'current' period is already promised to the current tasks,
  415. * however the extra weight of the new task will slow them down a
  416. * little, place the new task so that it fits in the slot that
  417. * stays open at the end.
  418. */
  419. if (initial && sched_feat(START_DEBIT))
  420. vruntime += sched_vslice_add(cfs_rq, se);
  421. if (!initial) {
  422. /* sleeps upto a single latency don't count. */
  423. if (sched_feat(NEW_FAIR_SLEEPERS) && entity_is_task(se) &&
  424. task_of(se)->policy != SCHED_BATCH)
  425. vruntime -= sysctl_sched_latency;
  426. /* ensure we never gain time by being placed backwards. */
  427. vruntime = max_vruntime(se->vruntime, vruntime);
  428. }
  429. se->vruntime = vruntime;
  430. }
  431. static void
  432. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  433. {
  434. /*
  435. * Update run-time statistics of the 'current'.
  436. */
  437. update_curr(cfs_rq);
  438. if (wakeup) {
  439. place_entity(cfs_rq, se, 0);
  440. enqueue_sleeper(cfs_rq, se);
  441. }
  442. update_stats_enqueue(cfs_rq, se);
  443. check_spread(cfs_rq, se);
  444. if (se != cfs_rq->curr)
  445. __enqueue_entity(cfs_rq, se);
  446. account_entity_enqueue(cfs_rq, se);
  447. }
  448. static void
  449. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  450. {
  451. /*
  452. * Update run-time statistics of the 'current'.
  453. */
  454. update_curr(cfs_rq);
  455. update_stats_dequeue(cfs_rq, se);
  456. if (sleep) {
  457. #ifdef CONFIG_SCHEDSTATS
  458. if (entity_is_task(se)) {
  459. struct task_struct *tsk = task_of(se);
  460. if (tsk->state & TASK_INTERRUPTIBLE)
  461. se->sleep_start = rq_of(cfs_rq)->clock;
  462. if (tsk->state & TASK_UNINTERRUPTIBLE)
  463. se->block_start = rq_of(cfs_rq)->clock;
  464. }
  465. #endif
  466. }
  467. if (se != cfs_rq->curr)
  468. __dequeue_entity(cfs_rq, se);
  469. account_entity_dequeue(cfs_rq, se);
  470. }
  471. /*
  472. * Preempt the current task with a newly woken task if needed:
  473. */
  474. static void
  475. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  476. {
  477. unsigned long ideal_runtime, delta_exec;
  478. ideal_runtime = sched_slice(cfs_rq, curr);
  479. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  480. if (delta_exec > ideal_runtime)
  481. resched_task(rq_of(cfs_rq)->curr);
  482. }
  483. static void
  484. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  485. {
  486. /* 'current' is not kept within the tree. */
  487. if (se->on_rq) {
  488. /*
  489. * Any task has to be enqueued before it get to execute on
  490. * a CPU. So account for the time it spent waiting on the
  491. * runqueue.
  492. */
  493. update_stats_wait_end(cfs_rq, se);
  494. __dequeue_entity(cfs_rq, se);
  495. }
  496. update_stats_curr_start(cfs_rq, se);
  497. cfs_rq->curr = se;
  498. #ifdef CONFIG_SCHEDSTATS
  499. /*
  500. * Track our maximum slice length, if the CPU's load is at
  501. * least twice that of our own weight (i.e. dont track it
  502. * when there are only lesser-weight tasks around):
  503. */
  504. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  505. se->slice_max = max(se->slice_max,
  506. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  507. }
  508. #endif
  509. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  510. }
  511. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  512. {
  513. struct sched_entity *se = NULL;
  514. if (first_fair(cfs_rq)) {
  515. se = __pick_next_entity(cfs_rq);
  516. set_next_entity(cfs_rq, se);
  517. }
  518. return se;
  519. }
  520. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  521. {
  522. /*
  523. * If still on the runqueue then deactivate_task()
  524. * was not called and update_curr() has to be done:
  525. */
  526. if (prev->on_rq)
  527. update_curr(cfs_rq);
  528. check_spread(cfs_rq, prev);
  529. if (prev->on_rq) {
  530. update_stats_wait_start(cfs_rq, prev);
  531. /* Put 'current' back into the tree. */
  532. __enqueue_entity(cfs_rq, prev);
  533. }
  534. cfs_rq->curr = NULL;
  535. }
  536. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  537. {
  538. /*
  539. * Update run-time statistics of the 'current'.
  540. */
  541. update_curr(cfs_rq);
  542. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  543. check_preempt_tick(cfs_rq, curr);
  544. }
  545. /**************************************************
  546. * CFS operations on tasks:
  547. */
  548. #ifdef CONFIG_FAIR_GROUP_SCHED
  549. /* Walk up scheduling entities hierarchy */
  550. #define for_each_sched_entity(se) \
  551. for (; se; se = se->parent)
  552. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  553. {
  554. return p->se.cfs_rq;
  555. }
  556. /* runqueue on which this entity is (to be) queued */
  557. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  558. {
  559. return se->cfs_rq;
  560. }
  561. /* runqueue "owned" by this group */
  562. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  563. {
  564. return grp->my_q;
  565. }
  566. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  567. * another cpu ('this_cpu')
  568. */
  569. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  570. {
  571. return cfs_rq->tg->cfs_rq[this_cpu];
  572. }
  573. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  574. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  575. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  576. /* Do the two (enqueued) entities belong to the same group ? */
  577. static inline int
  578. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  579. {
  580. if (se->cfs_rq == pse->cfs_rq)
  581. return 1;
  582. return 0;
  583. }
  584. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  585. {
  586. return se->parent;
  587. }
  588. #else /* CONFIG_FAIR_GROUP_SCHED */
  589. #define for_each_sched_entity(se) \
  590. for (; se; se = NULL)
  591. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  592. {
  593. return &task_rq(p)->cfs;
  594. }
  595. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  596. {
  597. struct task_struct *p = task_of(se);
  598. struct rq *rq = task_rq(p);
  599. return &rq->cfs;
  600. }
  601. /* runqueue "owned" by this group */
  602. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  603. {
  604. return NULL;
  605. }
  606. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  607. {
  608. return &cpu_rq(this_cpu)->cfs;
  609. }
  610. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  611. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  612. static inline int
  613. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  614. {
  615. return 1;
  616. }
  617. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  618. {
  619. return NULL;
  620. }
  621. #endif /* CONFIG_FAIR_GROUP_SCHED */
  622. /*
  623. * The enqueue_task method is called before nr_running is
  624. * increased. Here we update the fair scheduling stats and
  625. * then put the task into the rbtree:
  626. */
  627. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  628. {
  629. struct cfs_rq *cfs_rq;
  630. struct sched_entity *se = &p->se;
  631. for_each_sched_entity(se) {
  632. if (se->on_rq)
  633. break;
  634. cfs_rq = cfs_rq_of(se);
  635. enqueue_entity(cfs_rq, se, wakeup);
  636. wakeup = 1;
  637. }
  638. }
  639. /*
  640. * The dequeue_task method is called before nr_running is
  641. * decreased. We remove the task from the rbtree and
  642. * update the fair scheduling stats:
  643. */
  644. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  645. {
  646. struct cfs_rq *cfs_rq;
  647. struct sched_entity *se = &p->se;
  648. for_each_sched_entity(se) {
  649. cfs_rq = cfs_rq_of(se);
  650. dequeue_entity(cfs_rq, se, sleep);
  651. /* Don't dequeue parent if it has other entities besides us */
  652. if (cfs_rq->load.weight)
  653. break;
  654. sleep = 1;
  655. }
  656. }
  657. /*
  658. * sched_yield() support is very simple - we dequeue and enqueue.
  659. *
  660. * If compat_yield is turned on then we requeue to the end of the tree.
  661. */
  662. static void yield_task_fair(struct rq *rq)
  663. {
  664. struct cfs_rq *cfs_rq = task_cfs_rq(rq->curr);
  665. struct sched_entity *rightmost, *se = &rq->curr->se;
  666. /*
  667. * Are we the only task in the tree?
  668. */
  669. if (unlikely(cfs_rq->nr_running == 1))
  670. return;
  671. if (likely(!sysctl_sched_compat_yield)) {
  672. __update_rq_clock(rq);
  673. /*
  674. * Update run-time statistics of the 'current'.
  675. */
  676. update_curr(cfs_rq);
  677. return;
  678. }
  679. /*
  680. * Find the rightmost entry in the rbtree:
  681. */
  682. rightmost = __pick_last_entity(cfs_rq);
  683. /*
  684. * Already in the rightmost position?
  685. */
  686. if (unlikely(rightmost->vruntime < se->vruntime))
  687. return;
  688. /*
  689. * Minimally necessary key value to be last in the tree:
  690. * Upon rescheduling, sched_class::put_prev_task() will place
  691. * 'current' within the tree based on its new key value.
  692. */
  693. se->vruntime = rightmost->vruntime + 1;
  694. }
  695. /*
  696. * Preempt the current task with a newly woken task if needed:
  697. */
  698. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  699. {
  700. struct task_struct *curr = rq->curr;
  701. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  702. struct sched_entity *se = &curr->se, *pse = &p->se;
  703. s64 delta, gran;
  704. if (unlikely(rt_prio(p->prio))) {
  705. update_rq_clock(rq);
  706. update_curr(cfs_rq);
  707. resched_task(curr);
  708. return;
  709. }
  710. /*
  711. * Batch tasks do not preempt (their preemption is driven by
  712. * the tick):
  713. */
  714. if (unlikely(p->policy == SCHED_BATCH))
  715. return;
  716. if (sched_feat(WAKEUP_PREEMPT)) {
  717. while (!is_same_group(se, pse)) {
  718. se = parent_entity(se);
  719. pse = parent_entity(pse);
  720. }
  721. delta = se->vruntime - pse->vruntime;
  722. gran = sysctl_sched_wakeup_granularity;
  723. if (unlikely(se->load.weight != NICE_0_LOAD))
  724. gran = calc_delta_fair(gran, &se->load);
  725. if (delta > gran) {
  726. if (p->prio < curr->prio)
  727. resched_task(curr);
  728. }
  729. }
  730. }
  731. static struct task_struct *pick_next_task_fair(struct rq *rq)
  732. {
  733. struct cfs_rq *cfs_rq = &rq->cfs;
  734. struct sched_entity *se;
  735. if (unlikely(!cfs_rq->nr_running))
  736. return NULL;
  737. do {
  738. se = pick_next_entity(cfs_rq);
  739. cfs_rq = group_cfs_rq(se);
  740. } while (cfs_rq);
  741. return task_of(se);
  742. }
  743. /*
  744. * Account for a descheduled task:
  745. */
  746. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  747. {
  748. struct sched_entity *se = &prev->se;
  749. struct cfs_rq *cfs_rq;
  750. for_each_sched_entity(se) {
  751. cfs_rq = cfs_rq_of(se);
  752. put_prev_entity(cfs_rq, se);
  753. }
  754. }
  755. #ifdef CONFIG_SMP
  756. /**************************************************
  757. * Fair scheduling class load-balancing methods:
  758. */
  759. /*
  760. * Load-balancing iterator. Note: while the runqueue stays locked
  761. * during the whole iteration, the current task might be
  762. * dequeued so the iterator has to be dequeue-safe. Here we
  763. * achieve that by always pre-iterating before returning
  764. * the current task:
  765. */
  766. static struct task_struct *
  767. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  768. {
  769. struct task_struct *p;
  770. if (!curr)
  771. return NULL;
  772. p = rb_entry(curr, struct task_struct, se.run_node);
  773. cfs_rq->rb_load_balance_curr = rb_next(curr);
  774. return p;
  775. }
  776. static struct task_struct *load_balance_start_fair(void *arg)
  777. {
  778. struct cfs_rq *cfs_rq = arg;
  779. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  780. }
  781. static struct task_struct *load_balance_next_fair(void *arg)
  782. {
  783. struct cfs_rq *cfs_rq = arg;
  784. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  785. }
  786. #ifdef CONFIG_FAIR_GROUP_SCHED
  787. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  788. {
  789. struct sched_entity *curr;
  790. struct task_struct *p;
  791. if (!cfs_rq->nr_running)
  792. return MAX_PRIO;
  793. curr = cfs_rq->curr;
  794. if (!curr)
  795. curr = __pick_next_entity(cfs_rq);
  796. p = task_of(curr);
  797. return p->prio;
  798. }
  799. #endif
  800. static unsigned long
  801. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  802. unsigned long max_load_move,
  803. struct sched_domain *sd, enum cpu_idle_type idle,
  804. int *all_pinned, int *this_best_prio)
  805. {
  806. struct cfs_rq *busy_cfs_rq;
  807. long rem_load_move = max_load_move;
  808. struct rq_iterator cfs_rq_iterator;
  809. cfs_rq_iterator.start = load_balance_start_fair;
  810. cfs_rq_iterator.next = load_balance_next_fair;
  811. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  812. #ifdef CONFIG_FAIR_GROUP_SCHED
  813. struct cfs_rq *this_cfs_rq;
  814. long imbalance;
  815. unsigned long maxload;
  816. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  817. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  818. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  819. if (imbalance <= 0)
  820. continue;
  821. /* Don't pull more than imbalance/2 */
  822. imbalance /= 2;
  823. maxload = min(rem_load_move, imbalance);
  824. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  825. #else
  826. # define maxload rem_load_move
  827. #endif
  828. /*
  829. * pass busy_cfs_rq argument into
  830. * load_balance_[start|next]_fair iterators
  831. */
  832. cfs_rq_iterator.arg = busy_cfs_rq;
  833. rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  834. maxload, sd, idle, all_pinned,
  835. this_best_prio,
  836. &cfs_rq_iterator);
  837. if (rem_load_move <= 0)
  838. break;
  839. }
  840. return max_load_move - rem_load_move;
  841. }
  842. static int
  843. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  844. struct sched_domain *sd, enum cpu_idle_type idle)
  845. {
  846. struct cfs_rq *busy_cfs_rq;
  847. struct rq_iterator cfs_rq_iterator;
  848. cfs_rq_iterator.start = load_balance_start_fair;
  849. cfs_rq_iterator.next = load_balance_next_fair;
  850. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  851. /*
  852. * pass busy_cfs_rq argument into
  853. * load_balance_[start|next]_fair iterators
  854. */
  855. cfs_rq_iterator.arg = busy_cfs_rq;
  856. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  857. &cfs_rq_iterator))
  858. return 1;
  859. }
  860. return 0;
  861. }
  862. #endif
  863. /*
  864. * scheduler tick hitting a task of our scheduling class:
  865. */
  866. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  867. {
  868. struct cfs_rq *cfs_rq;
  869. struct sched_entity *se = &curr->se;
  870. for_each_sched_entity(se) {
  871. cfs_rq = cfs_rq_of(se);
  872. entity_tick(cfs_rq, se);
  873. }
  874. }
  875. #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  876. /*
  877. * Share the fairness runtime between parent and child, thus the
  878. * total amount of pressure for CPU stays equal - new tasks
  879. * get a chance to run but frequent forkers are not allowed to
  880. * monopolize the CPU. Note: the parent runqueue is locked,
  881. * the child is not running yet.
  882. */
  883. static void task_new_fair(struct rq *rq, struct task_struct *p)
  884. {
  885. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  886. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  887. int this_cpu = smp_processor_id();
  888. sched_info_queued(p);
  889. update_curr(cfs_rq);
  890. place_entity(cfs_rq, se, 1);
  891. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  892. curr->vruntime < se->vruntime) {
  893. /*
  894. * Upon rescheduling, sched_class::put_prev_task() will place
  895. * 'current' within the tree based on its new key value.
  896. */
  897. swap(curr->vruntime, se->vruntime);
  898. }
  899. enqueue_task_fair(rq, p, 0);
  900. resched_task(rq->curr);
  901. }
  902. /* Account for a task changing its policy or group.
  903. *
  904. * This routine is mostly called to set cfs_rq->curr field when a task
  905. * migrates between groups/classes.
  906. */
  907. static void set_curr_task_fair(struct rq *rq)
  908. {
  909. struct sched_entity *se = &rq->curr->se;
  910. for_each_sched_entity(se)
  911. set_next_entity(cfs_rq_of(se), se);
  912. }
  913. /*
  914. * All the scheduling class methods:
  915. */
  916. static const struct sched_class fair_sched_class = {
  917. .next = &idle_sched_class,
  918. .enqueue_task = enqueue_task_fair,
  919. .dequeue_task = dequeue_task_fair,
  920. .yield_task = yield_task_fair,
  921. .check_preempt_curr = check_preempt_wakeup,
  922. .pick_next_task = pick_next_task_fair,
  923. .put_prev_task = put_prev_task_fair,
  924. #ifdef CONFIG_SMP
  925. .load_balance = load_balance_fair,
  926. .move_one_task = move_one_task_fair,
  927. #endif
  928. .set_curr_task = set_curr_task_fair,
  929. .task_tick = task_tick_fair,
  930. .task_new = task_new_fair,
  931. };
  932. #ifdef CONFIG_SCHED_DEBUG
  933. static void print_cfs_stats(struct seq_file *m, int cpu)
  934. {
  935. struct cfs_rq *cfs_rq;
  936. #ifdef CONFIG_FAIR_GROUP_SCHED
  937. print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  938. #endif
  939. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  940. print_cfs_rq(m, cpu, cfs_rq);
  941. }
  942. #endif