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, 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. const_debug unsigned int sysctl_sched_latency = 20000000ULL;
  35. /*
  36. * Minimal preemption granularity for CPU-bound tasks:
  37. * (default: 1 msec, units: nanoseconds)
  38. */
  39. const_debug unsigned int sysctl_sched_min_granularity = 1000000ULL;
  40. /*
  41. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  42. */
  43. const_debug 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, 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. const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 10000000UL;
  65. /*
  66. * SCHED_OTHER wake-up granularity.
  67. * (default: 10 msec, 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. const_debug 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. se->peer_preempt = 0;
  458. #ifdef CONFIG_SCHEDSTATS
  459. if (entity_is_task(se)) {
  460. struct task_struct *tsk = task_of(se);
  461. if (tsk->state & TASK_INTERRUPTIBLE)
  462. se->sleep_start = rq_of(cfs_rq)->clock;
  463. if (tsk->state & TASK_UNINTERRUPTIBLE)
  464. se->block_start = rq_of(cfs_rq)->clock;
  465. }
  466. #endif
  467. }
  468. if (se != cfs_rq->curr)
  469. __dequeue_entity(cfs_rq, se);
  470. account_entity_dequeue(cfs_rq, se);
  471. }
  472. /*
  473. * Preempt the current task with a newly woken task if needed:
  474. */
  475. static void
  476. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  477. {
  478. unsigned long ideal_runtime, delta_exec;
  479. ideal_runtime = sched_slice(cfs_rq, curr);
  480. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  481. if (delta_exec > ideal_runtime ||
  482. (sched_feat(PREEMPT_RESTRICT) && curr->peer_preempt))
  483. resched_task(rq_of(cfs_rq)->curr);
  484. curr->peer_preempt = 0;
  485. }
  486. static void
  487. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  488. {
  489. /* 'current' is not kept within the tree. */
  490. if (se->on_rq) {
  491. /*
  492. * Any task has to be enqueued before it get to execute on
  493. * a CPU. So account for the time it spent waiting on the
  494. * runqueue.
  495. */
  496. update_stats_wait_end(cfs_rq, se);
  497. __dequeue_entity(cfs_rq, se);
  498. }
  499. update_stats_curr_start(cfs_rq, se);
  500. cfs_rq->curr = se;
  501. #ifdef CONFIG_SCHEDSTATS
  502. /*
  503. * Track our maximum slice length, if the CPU's load is at
  504. * least twice that of our own weight (i.e. dont track it
  505. * when there are only lesser-weight tasks around):
  506. */
  507. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  508. se->slice_max = max(se->slice_max,
  509. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  510. }
  511. #endif
  512. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  513. }
  514. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  515. {
  516. struct sched_entity *se = NULL;
  517. if (first_fair(cfs_rq)) {
  518. se = __pick_next_entity(cfs_rq);
  519. set_next_entity(cfs_rq, se);
  520. }
  521. return se;
  522. }
  523. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  524. {
  525. /*
  526. * If still on the runqueue then deactivate_task()
  527. * was not called and update_curr() has to be done:
  528. */
  529. if (prev->on_rq)
  530. update_curr(cfs_rq);
  531. check_spread(cfs_rq, prev);
  532. if (prev->on_rq) {
  533. update_stats_wait_start(cfs_rq, prev);
  534. /* Put 'current' back into the tree. */
  535. __enqueue_entity(cfs_rq, prev);
  536. }
  537. cfs_rq->curr = NULL;
  538. }
  539. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  540. {
  541. /*
  542. * Update run-time statistics of the 'current'.
  543. */
  544. update_curr(cfs_rq);
  545. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  546. check_preempt_tick(cfs_rq, curr);
  547. }
  548. /**************************************************
  549. * CFS operations on tasks:
  550. */
  551. #ifdef CONFIG_FAIR_GROUP_SCHED
  552. /* Walk up scheduling entities hierarchy */
  553. #define for_each_sched_entity(se) \
  554. for (; se; se = se->parent)
  555. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  556. {
  557. return p->se.cfs_rq;
  558. }
  559. /* runqueue on which this entity is (to be) queued */
  560. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  561. {
  562. return se->cfs_rq;
  563. }
  564. /* runqueue "owned" by this group */
  565. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  566. {
  567. return grp->my_q;
  568. }
  569. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  570. * another cpu ('this_cpu')
  571. */
  572. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  573. {
  574. return cfs_rq->tg->cfs_rq[this_cpu];
  575. }
  576. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  577. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  578. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  579. /* Do the two (enqueued) entities belong to the same group ? */
  580. static inline int
  581. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  582. {
  583. if (se->cfs_rq == pse->cfs_rq)
  584. return 1;
  585. return 0;
  586. }
  587. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  588. {
  589. return se->parent;
  590. }
  591. #else /* CONFIG_FAIR_GROUP_SCHED */
  592. #define for_each_sched_entity(se) \
  593. for (; se; se = NULL)
  594. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  595. {
  596. return &task_rq(p)->cfs;
  597. }
  598. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  599. {
  600. struct task_struct *p = task_of(se);
  601. struct rq *rq = task_rq(p);
  602. return &rq->cfs;
  603. }
  604. /* runqueue "owned" by this group */
  605. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  606. {
  607. return NULL;
  608. }
  609. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  610. {
  611. return &cpu_rq(this_cpu)->cfs;
  612. }
  613. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  614. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  615. static inline int
  616. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  617. {
  618. return 1;
  619. }
  620. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  621. {
  622. return NULL;
  623. }
  624. #endif /* CONFIG_FAIR_GROUP_SCHED */
  625. /*
  626. * The enqueue_task method is called before nr_running is
  627. * increased. Here we update the fair scheduling stats and
  628. * then put the task into the rbtree:
  629. */
  630. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  631. {
  632. struct cfs_rq *cfs_rq;
  633. struct sched_entity *se = &p->se;
  634. for_each_sched_entity(se) {
  635. if (se->on_rq)
  636. break;
  637. cfs_rq = cfs_rq_of(se);
  638. enqueue_entity(cfs_rq, se, wakeup);
  639. wakeup = 1;
  640. }
  641. }
  642. /*
  643. * The dequeue_task method is called before nr_running is
  644. * decreased. We remove the task from the rbtree and
  645. * update the fair scheduling stats:
  646. */
  647. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  648. {
  649. struct cfs_rq *cfs_rq;
  650. struct sched_entity *se = &p->se;
  651. for_each_sched_entity(se) {
  652. cfs_rq = cfs_rq_of(se);
  653. dequeue_entity(cfs_rq, se, sleep);
  654. /* Don't dequeue parent if it has other entities besides us */
  655. if (cfs_rq->load.weight)
  656. break;
  657. sleep = 1;
  658. }
  659. }
  660. /*
  661. * sched_yield() support is very simple - we dequeue and enqueue.
  662. *
  663. * If compat_yield is turned on then we requeue to the end of the tree.
  664. */
  665. static void yield_task_fair(struct rq *rq)
  666. {
  667. struct cfs_rq *cfs_rq = task_cfs_rq(rq->curr);
  668. struct sched_entity *rightmost, *se = &rq->curr->se;
  669. /*
  670. * Are we the only task in the tree?
  671. */
  672. if (unlikely(cfs_rq->nr_running == 1))
  673. return;
  674. if (likely(!sysctl_sched_compat_yield)) {
  675. __update_rq_clock(rq);
  676. /*
  677. * Update run-time statistics of the 'current'.
  678. */
  679. update_curr(cfs_rq);
  680. return;
  681. }
  682. /*
  683. * Find the rightmost entry in the rbtree:
  684. */
  685. rightmost = __pick_last_entity(cfs_rq);
  686. /*
  687. * Already in the rightmost position?
  688. */
  689. if (unlikely(rightmost->vruntime < se->vruntime))
  690. return;
  691. /*
  692. * Minimally necessary key value to be last in the tree:
  693. * Upon rescheduling, sched_class::put_prev_task() will place
  694. * 'current' within the tree based on its new key value.
  695. */
  696. se->vruntime = rightmost->vruntime + 1;
  697. }
  698. /*
  699. * Preempt the current task with a newly woken task if needed:
  700. */
  701. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  702. {
  703. struct task_struct *curr = rq->curr;
  704. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  705. struct sched_entity *se = &curr->se, *pse = &p->se;
  706. s64 delta, gran;
  707. if (unlikely(rt_prio(p->prio))) {
  708. update_rq_clock(rq);
  709. update_curr(cfs_rq);
  710. resched_task(curr);
  711. return;
  712. }
  713. /*
  714. * Batch tasks do not preempt (their preemption is driven by
  715. * the tick):
  716. */
  717. if (unlikely(p->policy == SCHED_BATCH))
  718. return;
  719. if (sched_feat(WAKEUP_PREEMPT)) {
  720. while (!is_same_group(se, pse)) {
  721. se = parent_entity(se);
  722. pse = parent_entity(pse);
  723. }
  724. delta = se->vruntime - pse->vruntime;
  725. gran = sysctl_sched_wakeup_granularity;
  726. if (unlikely(se->load.weight != NICE_0_LOAD))
  727. gran = calc_delta_fair(gran, &se->load);
  728. if (delta > gran) {
  729. int now = !sched_feat(PREEMPT_RESTRICT);
  730. if (now || p->prio < curr->prio || !se->peer_preempt++)
  731. resched_task(curr);
  732. }
  733. }
  734. }
  735. static struct task_struct *pick_next_task_fair(struct rq *rq)
  736. {
  737. struct cfs_rq *cfs_rq = &rq->cfs;
  738. struct sched_entity *se;
  739. if (unlikely(!cfs_rq->nr_running))
  740. return NULL;
  741. do {
  742. se = pick_next_entity(cfs_rq);
  743. cfs_rq = group_cfs_rq(se);
  744. } while (cfs_rq);
  745. return task_of(se);
  746. }
  747. /*
  748. * Account for a descheduled task:
  749. */
  750. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  751. {
  752. struct sched_entity *se = &prev->se;
  753. struct cfs_rq *cfs_rq;
  754. for_each_sched_entity(se) {
  755. cfs_rq = cfs_rq_of(se);
  756. put_prev_entity(cfs_rq, se);
  757. }
  758. }
  759. #ifdef CONFIG_SMP
  760. /**************************************************
  761. * Fair scheduling class load-balancing methods:
  762. */
  763. /*
  764. * Load-balancing iterator. Note: while the runqueue stays locked
  765. * during the whole iteration, the current task might be
  766. * dequeued so the iterator has to be dequeue-safe. Here we
  767. * achieve that by always pre-iterating before returning
  768. * the current task:
  769. */
  770. static struct task_struct *
  771. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  772. {
  773. struct task_struct *p;
  774. if (!curr)
  775. return NULL;
  776. p = rb_entry(curr, struct task_struct, se.run_node);
  777. cfs_rq->rb_load_balance_curr = rb_next(curr);
  778. return p;
  779. }
  780. static struct task_struct *load_balance_start_fair(void *arg)
  781. {
  782. struct cfs_rq *cfs_rq = arg;
  783. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  784. }
  785. static struct task_struct *load_balance_next_fair(void *arg)
  786. {
  787. struct cfs_rq *cfs_rq = arg;
  788. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  789. }
  790. #ifdef CONFIG_FAIR_GROUP_SCHED
  791. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  792. {
  793. struct sched_entity *curr;
  794. struct task_struct *p;
  795. if (!cfs_rq->nr_running)
  796. return MAX_PRIO;
  797. curr = cfs_rq->curr;
  798. if (!curr)
  799. curr = __pick_next_entity(cfs_rq);
  800. p = task_of(curr);
  801. return p->prio;
  802. }
  803. #endif
  804. static unsigned long
  805. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  806. unsigned long max_load_move,
  807. struct sched_domain *sd, enum cpu_idle_type idle,
  808. int *all_pinned, int *this_best_prio)
  809. {
  810. struct cfs_rq *busy_cfs_rq;
  811. long rem_load_move = max_load_move;
  812. struct rq_iterator cfs_rq_iterator;
  813. cfs_rq_iterator.start = load_balance_start_fair;
  814. cfs_rq_iterator.next = load_balance_next_fair;
  815. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  816. #ifdef CONFIG_FAIR_GROUP_SCHED
  817. struct cfs_rq *this_cfs_rq;
  818. long imbalance;
  819. unsigned long maxload;
  820. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  821. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  822. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  823. if (imbalance <= 0)
  824. continue;
  825. /* Don't pull more than imbalance/2 */
  826. imbalance /= 2;
  827. maxload = min(rem_load_move, imbalance);
  828. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  829. #else
  830. # define maxload rem_load_move
  831. #endif
  832. /*
  833. * pass busy_cfs_rq argument into
  834. * load_balance_[start|next]_fair iterators
  835. */
  836. cfs_rq_iterator.arg = busy_cfs_rq;
  837. rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  838. maxload, sd, idle, all_pinned,
  839. this_best_prio,
  840. &cfs_rq_iterator);
  841. if (rem_load_move <= 0)
  842. break;
  843. }
  844. return max_load_move - rem_load_move;
  845. }
  846. static int
  847. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  848. struct sched_domain *sd, enum cpu_idle_type idle)
  849. {
  850. struct cfs_rq *busy_cfs_rq;
  851. struct rq_iterator cfs_rq_iterator;
  852. cfs_rq_iterator.start = load_balance_start_fair;
  853. cfs_rq_iterator.next = load_balance_next_fair;
  854. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  855. /*
  856. * pass busy_cfs_rq argument into
  857. * load_balance_[start|next]_fair iterators
  858. */
  859. cfs_rq_iterator.arg = busy_cfs_rq;
  860. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  861. &cfs_rq_iterator))
  862. return 1;
  863. }
  864. return 0;
  865. }
  866. #endif
  867. /*
  868. * scheduler tick hitting a task of our scheduling class:
  869. */
  870. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  871. {
  872. struct cfs_rq *cfs_rq;
  873. struct sched_entity *se = &curr->se;
  874. for_each_sched_entity(se) {
  875. cfs_rq = cfs_rq_of(se);
  876. entity_tick(cfs_rq, se);
  877. }
  878. }
  879. #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  880. /*
  881. * Share the fairness runtime between parent and child, thus the
  882. * total amount of pressure for CPU stays equal - new tasks
  883. * get a chance to run but frequent forkers are not allowed to
  884. * monopolize the CPU. Note: the parent runqueue is locked,
  885. * the child is not running yet.
  886. */
  887. static void task_new_fair(struct rq *rq, struct task_struct *p)
  888. {
  889. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  890. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  891. int this_cpu = smp_processor_id();
  892. sched_info_queued(p);
  893. update_curr(cfs_rq);
  894. place_entity(cfs_rq, se, 1);
  895. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  896. curr->vruntime < se->vruntime) {
  897. /*
  898. * Upon rescheduling, sched_class::put_prev_task() will place
  899. * 'current' within the tree based on its new key value.
  900. */
  901. swap(curr->vruntime, se->vruntime);
  902. }
  903. se->peer_preempt = 0;
  904. enqueue_task_fair(rq, p, 0);
  905. resched_task(rq->curr);
  906. }
  907. /* Account for a task changing its policy or group.
  908. *
  909. * This routine is mostly called to set cfs_rq->curr field when a task
  910. * migrates between groups/classes.
  911. */
  912. static void set_curr_task_fair(struct rq *rq)
  913. {
  914. struct sched_entity *se = &rq->curr->se;
  915. for_each_sched_entity(se)
  916. set_next_entity(cfs_rq_of(se), se);
  917. }
  918. /*
  919. * All the scheduling class methods:
  920. */
  921. static const struct sched_class fair_sched_class = {
  922. .next = &idle_sched_class,
  923. .enqueue_task = enqueue_task_fair,
  924. .dequeue_task = dequeue_task_fair,
  925. .yield_task = yield_task_fair,
  926. .check_preempt_curr = check_preempt_wakeup,
  927. .pick_next_task = pick_next_task_fair,
  928. .put_prev_task = put_prev_task_fair,
  929. #ifdef CONFIG_SMP
  930. .load_balance = load_balance_fair,
  931. .move_one_task = move_one_task_fair,
  932. #endif
  933. .set_curr_task = set_curr_task_fair,
  934. .task_tick = task_tick_fair,
  935. .task_new = task_new_fair,
  936. };
  937. #ifdef CONFIG_SCHED_DEBUG
  938. static void print_cfs_stats(struct seq_file *m, int cpu)
  939. {
  940. struct cfs_rq *cfs_rq;
  941. #ifdef CONFIG_FAIR_GROUP_SCHED
  942. print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  943. #endif
  944. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  945. print_cfs_rq(m, cpu, cfs_rq);
  946. }
  947. #endif