sched_rt.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. /*
  2. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3. * policies)
  4. */
  5. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  6. {
  7. return container_of(rt_se, struct task_struct, rt);
  8. }
  9. #ifdef CONFIG_RT_GROUP_SCHED
  10. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  11. {
  12. return rt_rq->rq;
  13. }
  14. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  15. {
  16. return rt_se->rt_rq;
  17. }
  18. #else /* CONFIG_RT_GROUP_SCHED */
  19. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  20. {
  21. return container_of(rt_rq, struct rq, rt);
  22. }
  23. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  24. {
  25. struct task_struct *p = rt_task_of(rt_se);
  26. struct rq *rq = task_rq(p);
  27. return &rq->rt;
  28. }
  29. #endif /* CONFIG_RT_GROUP_SCHED */
  30. #ifdef CONFIG_SMP
  31. static inline int rt_overloaded(struct rq *rq)
  32. {
  33. return atomic_read(&rq->rd->rto_count);
  34. }
  35. static inline void rt_set_overload(struct rq *rq)
  36. {
  37. if (!rq->online)
  38. return;
  39. cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
  40. /*
  41. * Make sure the mask is visible before we set
  42. * the overload count. That is checked to determine
  43. * if we should look at the mask. It would be a shame
  44. * if we looked at the mask, but the mask was not
  45. * updated yet.
  46. */
  47. wmb();
  48. atomic_inc(&rq->rd->rto_count);
  49. }
  50. static inline void rt_clear_overload(struct rq *rq)
  51. {
  52. if (!rq->online)
  53. return;
  54. /* the order here really doesn't matter */
  55. atomic_dec(&rq->rd->rto_count);
  56. cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
  57. }
  58. static void update_rt_migration(struct rt_rq *rt_rq)
  59. {
  60. if (rt_rq->rt_nr_migratory && (rt_rq->rt_nr_running > 1)) {
  61. if (!rt_rq->overloaded) {
  62. rt_set_overload(rq_of_rt_rq(rt_rq));
  63. rt_rq->overloaded = 1;
  64. }
  65. } else if (rt_rq->overloaded) {
  66. rt_clear_overload(rq_of_rt_rq(rt_rq));
  67. rt_rq->overloaded = 0;
  68. }
  69. }
  70. static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  71. {
  72. if (rt_se->nr_cpus_allowed > 1)
  73. rt_rq->rt_nr_migratory++;
  74. update_rt_migration(rt_rq);
  75. }
  76. static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  77. {
  78. if (rt_se->nr_cpus_allowed > 1)
  79. rt_rq->rt_nr_migratory--;
  80. update_rt_migration(rt_rq);
  81. }
  82. static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  83. {
  84. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  85. plist_node_init(&p->pushable_tasks, p->prio);
  86. plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
  87. }
  88. static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  89. {
  90. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  91. }
  92. #else
  93. static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  94. {
  95. }
  96. static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  97. {
  98. }
  99. static inline
  100. void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  101. {
  102. }
  103. static inline
  104. void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  105. {
  106. }
  107. #endif /* CONFIG_SMP */
  108. static inline int on_rt_rq(struct sched_rt_entity *rt_se)
  109. {
  110. return !list_empty(&rt_se->run_list);
  111. }
  112. #ifdef CONFIG_RT_GROUP_SCHED
  113. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  114. {
  115. if (!rt_rq->tg)
  116. return RUNTIME_INF;
  117. return rt_rq->rt_runtime;
  118. }
  119. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  120. {
  121. return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
  122. }
  123. #define for_each_leaf_rt_rq(rt_rq, rq) \
  124. list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
  125. #define for_each_sched_rt_entity(rt_se) \
  126. for (; rt_se; rt_se = rt_se->parent)
  127. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  128. {
  129. return rt_se->my_q;
  130. }
  131. static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
  132. static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
  133. static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  134. {
  135. struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
  136. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  137. if (rt_rq->rt_nr_running) {
  138. if (rt_se && !on_rt_rq(rt_se))
  139. enqueue_rt_entity(rt_se);
  140. if (rt_rq->highest_prio.curr < curr->prio)
  141. resched_task(curr);
  142. }
  143. }
  144. static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  145. {
  146. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  147. if (rt_se && on_rt_rq(rt_se))
  148. dequeue_rt_entity(rt_se);
  149. }
  150. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  151. {
  152. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  153. }
  154. static int rt_se_boosted(struct sched_rt_entity *rt_se)
  155. {
  156. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  157. struct task_struct *p;
  158. if (rt_rq)
  159. return !!rt_rq->rt_nr_boosted;
  160. p = rt_task_of(rt_se);
  161. return p->prio != p->normal_prio;
  162. }
  163. #ifdef CONFIG_SMP
  164. static inline const struct cpumask *sched_rt_period_mask(void)
  165. {
  166. return cpu_rq(smp_processor_id())->rd->span;
  167. }
  168. #else
  169. static inline const struct cpumask *sched_rt_period_mask(void)
  170. {
  171. return cpu_online_mask;
  172. }
  173. #endif
  174. static inline
  175. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  176. {
  177. return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
  178. }
  179. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  180. {
  181. return &rt_rq->tg->rt_bandwidth;
  182. }
  183. #else /* !CONFIG_RT_GROUP_SCHED */
  184. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  185. {
  186. return rt_rq->rt_runtime;
  187. }
  188. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  189. {
  190. return ktime_to_ns(def_rt_bandwidth.rt_period);
  191. }
  192. #define for_each_leaf_rt_rq(rt_rq, rq) \
  193. for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
  194. #define for_each_sched_rt_entity(rt_se) \
  195. for (; rt_se; rt_se = NULL)
  196. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  197. {
  198. return NULL;
  199. }
  200. static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  201. {
  202. if (rt_rq->rt_nr_running)
  203. resched_task(rq_of_rt_rq(rt_rq)->curr);
  204. }
  205. static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  206. {
  207. }
  208. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  209. {
  210. return rt_rq->rt_throttled;
  211. }
  212. static inline const struct cpumask *sched_rt_period_mask(void)
  213. {
  214. return cpu_online_mask;
  215. }
  216. static inline
  217. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  218. {
  219. return &cpu_rq(cpu)->rt;
  220. }
  221. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  222. {
  223. return &def_rt_bandwidth;
  224. }
  225. #endif /* CONFIG_RT_GROUP_SCHED */
  226. #ifdef CONFIG_SMP
  227. /*
  228. * We ran out of runtime, see if we can borrow some from our neighbours.
  229. */
  230. static int do_balance_runtime(struct rt_rq *rt_rq)
  231. {
  232. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  233. struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
  234. int i, weight, more = 0;
  235. u64 rt_period;
  236. weight = cpumask_weight(rd->span);
  237. spin_lock(&rt_b->rt_runtime_lock);
  238. rt_period = ktime_to_ns(rt_b->rt_period);
  239. for_each_cpu(i, rd->span) {
  240. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  241. s64 diff;
  242. if (iter == rt_rq)
  243. continue;
  244. spin_lock(&iter->rt_runtime_lock);
  245. /*
  246. * Either all rqs have inf runtime and there's nothing to steal
  247. * or __disable_runtime() below sets a specific rq to inf to
  248. * indicate its been disabled and disalow stealing.
  249. */
  250. if (iter->rt_runtime == RUNTIME_INF)
  251. goto next;
  252. /*
  253. * From runqueues with spare time, take 1/n part of their
  254. * spare time, but no more than our period.
  255. */
  256. diff = iter->rt_runtime - iter->rt_time;
  257. if (diff > 0) {
  258. diff = div_u64((u64)diff, weight);
  259. if (rt_rq->rt_runtime + diff > rt_period)
  260. diff = rt_period - rt_rq->rt_runtime;
  261. iter->rt_runtime -= diff;
  262. rt_rq->rt_runtime += diff;
  263. more = 1;
  264. if (rt_rq->rt_runtime == rt_period) {
  265. spin_unlock(&iter->rt_runtime_lock);
  266. break;
  267. }
  268. }
  269. next:
  270. spin_unlock(&iter->rt_runtime_lock);
  271. }
  272. spin_unlock(&rt_b->rt_runtime_lock);
  273. return more;
  274. }
  275. /*
  276. * Ensure this RQ takes back all the runtime it lend to its neighbours.
  277. */
  278. static void __disable_runtime(struct rq *rq)
  279. {
  280. struct root_domain *rd = rq->rd;
  281. struct rt_rq *rt_rq;
  282. if (unlikely(!scheduler_running))
  283. return;
  284. for_each_leaf_rt_rq(rt_rq, rq) {
  285. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  286. s64 want;
  287. int i;
  288. spin_lock(&rt_b->rt_runtime_lock);
  289. spin_lock(&rt_rq->rt_runtime_lock);
  290. /*
  291. * Either we're all inf and nobody needs to borrow, or we're
  292. * already disabled and thus have nothing to do, or we have
  293. * exactly the right amount of runtime to take out.
  294. */
  295. if (rt_rq->rt_runtime == RUNTIME_INF ||
  296. rt_rq->rt_runtime == rt_b->rt_runtime)
  297. goto balanced;
  298. spin_unlock(&rt_rq->rt_runtime_lock);
  299. /*
  300. * Calculate the difference between what we started out with
  301. * and what we current have, that's the amount of runtime
  302. * we lend and now have to reclaim.
  303. */
  304. want = rt_b->rt_runtime - rt_rq->rt_runtime;
  305. /*
  306. * Greedy reclaim, take back as much as we can.
  307. */
  308. for_each_cpu(i, rd->span) {
  309. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  310. s64 diff;
  311. /*
  312. * Can't reclaim from ourselves or disabled runqueues.
  313. */
  314. if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
  315. continue;
  316. spin_lock(&iter->rt_runtime_lock);
  317. if (want > 0) {
  318. diff = min_t(s64, iter->rt_runtime, want);
  319. iter->rt_runtime -= diff;
  320. want -= diff;
  321. } else {
  322. iter->rt_runtime -= want;
  323. want -= want;
  324. }
  325. spin_unlock(&iter->rt_runtime_lock);
  326. if (!want)
  327. break;
  328. }
  329. spin_lock(&rt_rq->rt_runtime_lock);
  330. /*
  331. * We cannot be left wanting - that would mean some runtime
  332. * leaked out of the system.
  333. */
  334. BUG_ON(want);
  335. balanced:
  336. /*
  337. * Disable all the borrow logic by pretending we have inf
  338. * runtime - in which case borrowing doesn't make sense.
  339. */
  340. rt_rq->rt_runtime = RUNTIME_INF;
  341. spin_unlock(&rt_rq->rt_runtime_lock);
  342. spin_unlock(&rt_b->rt_runtime_lock);
  343. }
  344. }
  345. static void disable_runtime(struct rq *rq)
  346. {
  347. unsigned long flags;
  348. spin_lock_irqsave(&rq->lock, flags);
  349. __disable_runtime(rq);
  350. spin_unlock_irqrestore(&rq->lock, flags);
  351. }
  352. static void __enable_runtime(struct rq *rq)
  353. {
  354. struct rt_rq *rt_rq;
  355. if (unlikely(!scheduler_running))
  356. return;
  357. /*
  358. * Reset each runqueue's bandwidth settings
  359. */
  360. for_each_leaf_rt_rq(rt_rq, rq) {
  361. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  362. spin_lock(&rt_b->rt_runtime_lock);
  363. spin_lock(&rt_rq->rt_runtime_lock);
  364. rt_rq->rt_runtime = rt_b->rt_runtime;
  365. rt_rq->rt_time = 0;
  366. rt_rq->rt_throttled = 0;
  367. spin_unlock(&rt_rq->rt_runtime_lock);
  368. spin_unlock(&rt_b->rt_runtime_lock);
  369. }
  370. }
  371. static void enable_runtime(struct rq *rq)
  372. {
  373. unsigned long flags;
  374. spin_lock_irqsave(&rq->lock, flags);
  375. __enable_runtime(rq);
  376. spin_unlock_irqrestore(&rq->lock, flags);
  377. }
  378. static int balance_runtime(struct rt_rq *rt_rq)
  379. {
  380. int more = 0;
  381. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  382. spin_unlock(&rt_rq->rt_runtime_lock);
  383. more = do_balance_runtime(rt_rq);
  384. spin_lock(&rt_rq->rt_runtime_lock);
  385. }
  386. return more;
  387. }
  388. #else /* !CONFIG_SMP */
  389. static inline int balance_runtime(struct rt_rq *rt_rq)
  390. {
  391. return 0;
  392. }
  393. #endif /* CONFIG_SMP */
  394. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  395. {
  396. int i, idle = 1;
  397. const struct cpumask *span;
  398. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  399. return 1;
  400. span = sched_rt_period_mask();
  401. for_each_cpu(i, span) {
  402. int enqueue = 0;
  403. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  404. struct rq *rq = rq_of_rt_rq(rt_rq);
  405. spin_lock(&rq->lock);
  406. if (rt_rq->rt_time) {
  407. u64 runtime;
  408. spin_lock(&rt_rq->rt_runtime_lock);
  409. if (rt_rq->rt_throttled)
  410. balance_runtime(rt_rq);
  411. runtime = rt_rq->rt_runtime;
  412. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  413. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  414. rt_rq->rt_throttled = 0;
  415. enqueue = 1;
  416. }
  417. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  418. idle = 0;
  419. spin_unlock(&rt_rq->rt_runtime_lock);
  420. } else if (rt_rq->rt_nr_running)
  421. idle = 0;
  422. if (enqueue)
  423. sched_rt_rq_enqueue(rt_rq);
  424. spin_unlock(&rq->lock);
  425. }
  426. return idle;
  427. }
  428. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  429. {
  430. #ifdef CONFIG_RT_GROUP_SCHED
  431. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  432. if (rt_rq)
  433. return rt_rq->highest_prio.curr;
  434. #endif
  435. return rt_task_of(rt_se)->prio;
  436. }
  437. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  438. {
  439. u64 runtime = sched_rt_runtime(rt_rq);
  440. if (rt_rq->rt_throttled)
  441. return rt_rq_throttled(rt_rq);
  442. if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq))
  443. return 0;
  444. balance_runtime(rt_rq);
  445. runtime = sched_rt_runtime(rt_rq);
  446. if (runtime == RUNTIME_INF)
  447. return 0;
  448. if (rt_rq->rt_time > runtime) {
  449. rt_rq->rt_throttled = 1;
  450. if (rt_rq_throttled(rt_rq)) {
  451. sched_rt_rq_dequeue(rt_rq);
  452. return 1;
  453. }
  454. }
  455. return 0;
  456. }
  457. /*
  458. * Update the current task's runtime statistics. Skip current tasks that
  459. * are not in our scheduling class.
  460. */
  461. static void update_curr_rt(struct rq *rq)
  462. {
  463. struct task_struct *curr = rq->curr;
  464. struct sched_rt_entity *rt_se = &curr->rt;
  465. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  466. u64 delta_exec;
  467. if (!task_has_rt_policy(curr))
  468. return;
  469. delta_exec = rq->clock - curr->se.exec_start;
  470. if (unlikely((s64)delta_exec < 0))
  471. delta_exec = 0;
  472. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  473. curr->se.sum_exec_runtime += delta_exec;
  474. account_group_exec_runtime(curr, delta_exec);
  475. curr->se.exec_start = rq->clock;
  476. cpuacct_charge(curr, delta_exec);
  477. if (!rt_bandwidth_enabled())
  478. return;
  479. for_each_sched_rt_entity(rt_se) {
  480. rt_rq = rt_rq_of_se(rt_se);
  481. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  482. spin_lock(&rt_rq->rt_runtime_lock);
  483. rt_rq->rt_time += delta_exec;
  484. if (sched_rt_runtime_exceeded(rt_rq))
  485. resched_task(curr);
  486. spin_unlock(&rt_rq->rt_runtime_lock);
  487. }
  488. }
  489. }
  490. #if defined CONFIG_SMP
  491. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu);
  492. static inline int next_prio(struct rq *rq)
  493. {
  494. struct task_struct *next = pick_next_highest_task_rt(rq, rq->cpu);
  495. if (next && rt_prio(next->prio))
  496. return next->prio;
  497. else
  498. return MAX_RT_PRIO;
  499. }
  500. static void
  501. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  502. {
  503. struct rq *rq = rq_of_rt_rq(rt_rq);
  504. if (prio < prev_prio) {
  505. /*
  506. * If the new task is higher in priority than anything on the
  507. * run-queue, we know that the previous high becomes our
  508. * next-highest.
  509. */
  510. rt_rq->highest_prio.next = prev_prio;
  511. if (rq->online)
  512. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  513. } else if (prio == rt_rq->highest_prio.curr)
  514. /*
  515. * If the next task is equal in priority to the highest on
  516. * the run-queue, then we implicitly know that the next highest
  517. * task cannot be any lower than current
  518. */
  519. rt_rq->highest_prio.next = prio;
  520. else if (prio < rt_rq->highest_prio.next)
  521. /*
  522. * Otherwise, we need to recompute next-highest
  523. */
  524. rt_rq->highest_prio.next = next_prio(rq);
  525. }
  526. static void
  527. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  528. {
  529. struct rq *rq = rq_of_rt_rq(rt_rq);
  530. if (rt_rq->rt_nr_running && (prio <= rt_rq->highest_prio.next))
  531. rt_rq->highest_prio.next = next_prio(rq);
  532. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  533. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  534. }
  535. #else /* CONFIG_SMP */
  536. static inline
  537. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  538. static inline
  539. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  540. #endif /* CONFIG_SMP */
  541. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  542. static void
  543. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  544. {
  545. int prev_prio = rt_rq->highest_prio.curr;
  546. if (prio < prev_prio)
  547. rt_rq->highest_prio.curr = prio;
  548. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  549. }
  550. static void
  551. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  552. {
  553. int prev_prio = rt_rq->highest_prio.curr;
  554. if (rt_rq->rt_nr_running) {
  555. WARN_ON(prio < prev_prio);
  556. /*
  557. * This may have been our highest task, and therefore
  558. * we may have some recomputation to do
  559. */
  560. if (prio == prev_prio) {
  561. struct rt_prio_array *array = &rt_rq->active;
  562. rt_rq->highest_prio.curr =
  563. sched_find_first_bit(array->bitmap);
  564. }
  565. } else
  566. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  567. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  568. }
  569. #else
  570. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  571. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  572. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  573. #ifdef CONFIG_RT_GROUP_SCHED
  574. static void
  575. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  576. {
  577. if (rt_se_boosted(rt_se))
  578. rt_rq->rt_nr_boosted++;
  579. if (rt_rq->tg)
  580. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  581. }
  582. static void
  583. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  584. {
  585. if (rt_se_boosted(rt_se))
  586. rt_rq->rt_nr_boosted--;
  587. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  588. }
  589. #else /* CONFIG_RT_GROUP_SCHED */
  590. static void
  591. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  592. {
  593. start_rt_bandwidth(&def_rt_bandwidth);
  594. }
  595. static inline
  596. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  597. #endif /* CONFIG_RT_GROUP_SCHED */
  598. static inline
  599. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  600. {
  601. int prio = rt_se_prio(rt_se);
  602. WARN_ON(!rt_prio(prio));
  603. rt_rq->rt_nr_running++;
  604. inc_rt_prio(rt_rq, prio);
  605. inc_rt_migration(rt_se, rt_rq);
  606. inc_rt_group(rt_se, rt_rq);
  607. }
  608. static inline
  609. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  610. {
  611. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  612. WARN_ON(!rt_rq->rt_nr_running);
  613. rt_rq->rt_nr_running--;
  614. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  615. dec_rt_migration(rt_se, rt_rq);
  616. dec_rt_group(rt_se, rt_rq);
  617. }
  618. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
  619. {
  620. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  621. struct rt_prio_array *array = &rt_rq->active;
  622. struct rt_rq *group_rq = group_rt_rq(rt_se);
  623. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  624. /*
  625. * Don't enqueue the group if its throttled, or when empty.
  626. * The latter is a consequence of the former when a child group
  627. * get throttled and the current group doesn't have any other
  628. * active members.
  629. */
  630. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  631. return;
  632. list_add_tail(&rt_se->run_list, queue);
  633. __set_bit(rt_se_prio(rt_se), array->bitmap);
  634. inc_rt_tasks(rt_se, rt_rq);
  635. }
  636. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  637. {
  638. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  639. struct rt_prio_array *array = &rt_rq->active;
  640. list_del_init(&rt_se->run_list);
  641. if (list_empty(array->queue + rt_se_prio(rt_se)))
  642. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  643. dec_rt_tasks(rt_se, rt_rq);
  644. }
  645. /*
  646. * Because the prio of an upper entry depends on the lower
  647. * entries, we must remove entries top - down.
  648. */
  649. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  650. {
  651. struct sched_rt_entity *back = NULL;
  652. for_each_sched_rt_entity(rt_se) {
  653. rt_se->back = back;
  654. back = rt_se;
  655. }
  656. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  657. if (on_rt_rq(rt_se))
  658. __dequeue_rt_entity(rt_se);
  659. }
  660. }
  661. static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
  662. {
  663. dequeue_rt_stack(rt_se);
  664. for_each_sched_rt_entity(rt_se)
  665. __enqueue_rt_entity(rt_se);
  666. }
  667. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  668. {
  669. dequeue_rt_stack(rt_se);
  670. for_each_sched_rt_entity(rt_se) {
  671. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  672. if (rt_rq && rt_rq->rt_nr_running)
  673. __enqueue_rt_entity(rt_se);
  674. }
  675. }
  676. /*
  677. * Adding/removing a task to/from a priority array:
  678. */
  679. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  680. {
  681. struct sched_rt_entity *rt_se = &p->rt;
  682. if (wakeup)
  683. rt_se->timeout = 0;
  684. enqueue_rt_entity(rt_se);
  685. if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1)
  686. enqueue_pushable_task(rq, p);
  687. inc_cpu_load(rq, p->se.load.weight);
  688. }
  689. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  690. {
  691. struct sched_rt_entity *rt_se = &p->rt;
  692. update_curr_rt(rq);
  693. dequeue_rt_entity(rt_se);
  694. dequeue_pushable_task(rq, p);
  695. dec_cpu_load(rq, p->se.load.weight);
  696. }
  697. /*
  698. * Put task to the end of the run list without the overhead of dequeue
  699. * followed by enqueue.
  700. */
  701. static void
  702. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  703. {
  704. if (on_rt_rq(rt_se)) {
  705. struct rt_prio_array *array = &rt_rq->active;
  706. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  707. if (head)
  708. list_move(&rt_se->run_list, queue);
  709. else
  710. list_move_tail(&rt_se->run_list, queue);
  711. }
  712. }
  713. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  714. {
  715. struct sched_rt_entity *rt_se = &p->rt;
  716. struct rt_rq *rt_rq;
  717. for_each_sched_rt_entity(rt_se) {
  718. rt_rq = rt_rq_of_se(rt_se);
  719. requeue_rt_entity(rt_rq, rt_se, head);
  720. }
  721. }
  722. static void yield_task_rt(struct rq *rq)
  723. {
  724. requeue_task_rt(rq, rq->curr, 0);
  725. }
  726. #ifdef CONFIG_SMP
  727. static int find_lowest_rq(struct task_struct *task);
  728. static int select_task_rq_rt(struct task_struct *p, int sync)
  729. {
  730. struct rq *rq = task_rq(p);
  731. /*
  732. * If the current task is an RT task, then
  733. * try to see if we can wake this RT task up on another
  734. * runqueue. Otherwise simply start this RT task
  735. * on its current runqueue.
  736. *
  737. * We want to avoid overloading runqueues. Even if
  738. * the RT task is of higher priority than the current RT task.
  739. * RT tasks behave differently than other tasks. If
  740. * one gets preempted, we try to push it off to another queue.
  741. * So trying to keep a preempting RT task on the same
  742. * cache hot CPU will force the running RT task to
  743. * a cold CPU. So we waste all the cache for the lower
  744. * RT task in hopes of saving some of a RT task
  745. * that is just being woken and probably will have
  746. * cold cache anyway.
  747. */
  748. if (unlikely(rt_task(rq->curr)) &&
  749. (p->rt.nr_cpus_allowed > 1)) {
  750. int cpu = find_lowest_rq(p);
  751. return (cpu == -1) ? task_cpu(p) : cpu;
  752. }
  753. /*
  754. * Otherwise, just let it ride on the affined RQ and the
  755. * post-schedule router will push the preempted task away
  756. */
  757. return task_cpu(p);
  758. }
  759. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  760. {
  761. if (rq->curr->rt.nr_cpus_allowed == 1)
  762. return;
  763. if (p->rt.nr_cpus_allowed != 1
  764. && cpupri_find(&rq->rd->cpupri, p, NULL))
  765. return;
  766. if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  767. return;
  768. /*
  769. * There appears to be other cpus that can accept
  770. * current and none to run 'p', so lets reschedule
  771. * to try and push current away:
  772. */
  773. requeue_task_rt(rq, p, 1);
  774. resched_task(rq->curr);
  775. }
  776. #endif /* CONFIG_SMP */
  777. /*
  778. * Preempt the current task with a newly woken task if needed:
  779. */
  780. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync)
  781. {
  782. if (p->prio < rq->curr->prio) {
  783. resched_task(rq->curr);
  784. return;
  785. }
  786. #ifdef CONFIG_SMP
  787. /*
  788. * If:
  789. *
  790. * - the newly woken task is of equal priority to the current task
  791. * - the newly woken task is non-migratable while current is migratable
  792. * - current will be preempted on the next reschedule
  793. *
  794. * we should check to see if current can readily move to a different
  795. * cpu. If so, we will reschedule to allow the push logic to try
  796. * to move current somewhere else, making room for our non-migratable
  797. * task.
  798. */
  799. if (p->prio == rq->curr->prio && !need_resched())
  800. check_preempt_equal_prio(rq, p);
  801. #endif
  802. }
  803. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  804. struct rt_rq *rt_rq)
  805. {
  806. struct rt_prio_array *array = &rt_rq->active;
  807. struct sched_rt_entity *next = NULL;
  808. struct list_head *queue;
  809. int idx;
  810. idx = sched_find_first_bit(array->bitmap);
  811. BUG_ON(idx >= MAX_RT_PRIO);
  812. queue = array->queue + idx;
  813. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  814. return next;
  815. }
  816. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  817. {
  818. struct sched_rt_entity *rt_se;
  819. struct task_struct *p;
  820. struct rt_rq *rt_rq;
  821. rt_rq = &rq->rt;
  822. if (unlikely(!rt_rq->rt_nr_running))
  823. return NULL;
  824. if (rt_rq_throttled(rt_rq))
  825. return NULL;
  826. do {
  827. rt_se = pick_next_rt_entity(rq, rt_rq);
  828. BUG_ON(!rt_se);
  829. rt_rq = group_rt_rq(rt_se);
  830. } while (rt_rq);
  831. p = rt_task_of(rt_se);
  832. p->se.exec_start = rq->clock;
  833. return p;
  834. }
  835. static struct task_struct *pick_next_task_rt(struct rq *rq)
  836. {
  837. struct task_struct *p = _pick_next_task_rt(rq);
  838. /* The running task is never eligible for pushing */
  839. if (p)
  840. dequeue_pushable_task(rq, p);
  841. return p;
  842. }
  843. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  844. {
  845. update_curr_rt(rq);
  846. p->se.exec_start = 0;
  847. /*
  848. * The previous task needs to be made eligible for pushing
  849. * if it is still active
  850. */
  851. if (p->se.on_rq && p->rt.nr_cpus_allowed > 1)
  852. enqueue_pushable_task(rq, p);
  853. }
  854. #ifdef CONFIG_SMP
  855. /* Only try algorithms three times */
  856. #define RT_MAX_TRIES 3
  857. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  858. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  859. {
  860. if (!task_running(rq, p) &&
  861. (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
  862. (p->rt.nr_cpus_allowed > 1))
  863. return 1;
  864. return 0;
  865. }
  866. /* Return the second highest RT task, NULL otherwise */
  867. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  868. {
  869. struct task_struct *next = NULL;
  870. struct sched_rt_entity *rt_se;
  871. struct rt_prio_array *array;
  872. struct rt_rq *rt_rq;
  873. int idx;
  874. for_each_leaf_rt_rq(rt_rq, rq) {
  875. array = &rt_rq->active;
  876. idx = sched_find_first_bit(array->bitmap);
  877. next_idx:
  878. if (idx >= MAX_RT_PRIO)
  879. continue;
  880. if (next && next->prio < idx)
  881. continue;
  882. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  883. struct task_struct *p = rt_task_of(rt_se);
  884. if (pick_rt_task(rq, p, cpu)) {
  885. next = p;
  886. break;
  887. }
  888. }
  889. if (!next) {
  890. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  891. goto next_idx;
  892. }
  893. }
  894. return next;
  895. }
  896. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  897. static inline int pick_optimal_cpu(int this_cpu,
  898. const struct cpumask *mask)
  899. {
  900. int first;
  901. /* "this_cpu" is cheaper to preempt than a remote processor */
  902. if ((this_cpu != -1) && cpumask_test_cpu(this_cpu, mask))
  903. return this_cpu;
  904. first = cpumask_first(mask);
  905. if (first < nr_cpu_ids)
  906. return first;
  907. return -1;
  908. }
  909. static int find_lowest_rq(struct task_struct *task)
  910. {
  911. struct sched_domain *sd;
  912. struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
  913. int this_cpu = smp_processor_id();
  914. int cpu = task_cpu(task);
  915. cpumask_var_t domain_mask;
  916. if (task->rt.nr_cpus_allowed == 1)
  917. return -1; /* No other targets possible */
  918. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  919. return -1; /* No targets found */
  920. /*
  921. * Only consider CPUs that are usable for migration.
  922. * I guess we might want to change cpupri_find() to ignore those
  923. * in the first place.
  924. */
  925. cpumask_and(lowest_mask, lowest_mask, cpu_active_mask);
  926. /*
  927. * At this point we have built a mask of cpus representing the
  928. * lowest priority tasks in the system. Now we want to elect
  929. * the best one based on our affinity and topology.
  930. *
  931. * We prioritize the last cpu that the task executed on since
  932. * it is most likely cache-hot in that location.
  933. */
  934. if (cpumask_test_cpu(cpu, lowest_mask))
  935. return cpu;
  936. /*
  937. * Otherwise, we consult the sched_domains span maps to figure
  938. * out which cpu is logically closest to our hot cache data.
  939. */
  940. if (this_cpu == cpu)
  941. this_cpu = -1; /* Skip this_cpu opt if the same */
  942. if (alloc_cpumask_var(&domain_mask, GFP_ATOMIC)) {
  943. for_each_domain(cpu, sd) {
  944. if (sd->flags & SD_WAKE_AFFINE) {
  945. int best_cpu;
  946. cpumask_and(domain_mask,
  947. sched_domain_span(sd),
  948. lowest_mask);
  949. best_cpu = pick_optimal_cpu(this_cpu,
  950. domain_mask);
  951. if (best_cpu != -1) {
  952. free_cpumask_var(domain_mask);
  953. return best_cpu;
  954. }
  955. }
  956. }
  957. free_cpumask_var(domain_mask);
  958. }
  959. /*
  960. * And finally, if there were no matches within the domains
  961. * just give the caller *something* to work with from the compatible
  962. * locations.
  963. */
  964. return pick_optimal_cpu(this_cpu, lowest_mask);
  965. }
  966. /* Will lock the rq it finds */
  967. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  968. {
  969. struct rq *lowest_rq = NULL;
  970. int tries;
  971. int cpu;
  972. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  973. cpu = find_lowest_rq(task);
  974. if ((cpu == -1) || (cpu == rq->cpu))
  975. break;
  976. lowest_rq = cpu_rq(cpu);
  977. /* if the prio of this runqueue changed, try again */
  978. if (double_lock_balance(rq, lowest_rq)) {
  979. /*
  980. * We had to unlock the run queue. In
  981. * the mean time, task could have
  982. * migrated already or had its affinity changed.
  983. * Also make sure that it wasn't scheduled on its rq.
  984. */
  985. if (unlikely(task_rq(task) != rq ||
  986. !cpumask_test_cpu(lowest_rq->cpu,
  987. &task->cpus_allowed) ||
  988. task_running(rq, task) ||
  989. !task->se.on_rq)) {
  990. spin_unlock(&lowest_rq->lock);
  991. lowest_rq = NULL;
  992. break;
  993. }
  994. }
  995. /* If this rq is still suitable use it. */
  996. if (lowest_rq->rt.highest_prio.curr > task->prio)
  997. break;
  998. /* try again */
  999. double_unlock_balance(rq, lowest_rq);
  1000. lowest_rq = NULL;
  1001. }
  1002. return lowest_rq;
  1003. }
  1004. static inline int has_pushable_tasks(struct rq *rq)
  1005. {
  1006. return !plist_head_empty(&rq->rt.pushable_tasks);
  1007. }
  1008. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1009. {
  1010. struct task_struct *p;
  1011. if (!has_pushable_tasks(rq))
  1012. return NULL;
  1013. p = plist_first_entry(&rq->rt.pushable_tasks,
  1014. struct task_struct, pushable_tasks);
  1015. BUG_ON(rq->cpu != task_cpu(p));
  1016. BUG_ON(task_current(rq, p));
  1017. BUG_ON(p->rt.nr_cpus_allowed <= 1);
  1018. BUG_ON(!p->se.on_rq);
  1019. BUG_ON(!rt_task(p));
  1020. return p;
  1021. }
  1022. /*
  1023. * If the current CPU has more than one RT task, see if the non
  1024. * running task can migrate over to a CPU that is running a task
  1025. * of lesser priority.
  1026. */
  1027. static int push_rt_task(struct rq *rq)
  1028. {
  1029. struct task_struct *next_task;
  1030. struct rq *lowest_rq;
  1031. if (!rq->rt.overloaded)
  1032. return 0;
  1033. next_task = pick_next_pushable_task(rq);
  1034. if (!next_task)
  1035. return 0;
  1036. retry:
  1037. if (unlikely(next_task == rq->curr)) {
  1038. WARN_ON(1);
  1039. return 0;
  1040. }
  1041. /*
  1042. * It's possible that the next_task slipped in of
  1043. * higher priority than current. If that's the case
  1044. * just reschedule current.
  1045. */
  1046. if (unlikely(next_task->prio < rq->curr->prio)) {
  1047. resched_task(rq->curr);
  1048. return 0;
  1049. }
  1050. /* We might release rq lock */
  1051. get_task_struct(next_task);
  1052. /* find_lock_lowest_rq locks the rq if found */
  1053. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1054. if (!lowest_rq) {
  1055. struct task_struct *task;
  1056. /*
  1057. * find lock_lowest_rq releases rq->lock
  1058. * so it is possible that next_task has migrated.
  1059. *
  1060. * We need to make sure that the task is still on the same
  1061. * run-queue and is also still the next task eligible for
  1062. * pushing.
  1063. */
  1064. task = pick_next_pushable_task(rq);
  1065. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1066. /*
  1067. * If we get here, the task hasnt moved at all, but
  1068. * it has failed to push. We will not try again,
  1069. * since the other cpus will pull from us when they
  1070. * are ready.
  1071. */
  1072. dequeue_pushable_task(rq, next_task);
  1073. goto out;
  1074. }
  1075. if (!task)
  1076. /* No more tasks, just exit */
  1077. goto out;
  1078. /*
  1079. * Something has shifted, try again.
  1080. */
  1081. put_task_struct(next_task);
  1082. next_task = task;
  1083. goto retry;
  1084. }
  1085. deactivate_task(rq, next_task, 0);
  1086. set_task_cpu(next_task, lowest_rq->cpu);
  1087. activate_task(lowest_rq, next_task, 0);
  1088. resched_task(lowest_rq->curr);
  1089. double_unlock_balance(rq, lowest_rq);
  1090. out:
  1091. put_task_struct(next_task);
  1092. return 1;
  1093. }
  1094. static void push_rt_tasks(struct rq *rq)
  1095. {
  1096. /* push_rt_task will return true if it moved an RT */
  1097. while (push_rt_task(rq))
  1098. ;
  1099. }
  1100. static int pull_rt_task(struct rq *this_rq)
  1101. {
  1102. int this_cpu = this_rq->cpu, ret = 0, cpu;
  1103. struct task_struct *p;
  1104. struct rq *src_rq;
  1105. if (likely(!rt_overloaded(this_rq)))
  1106. return 0;
  1107. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1108. if (this_cpu == cpu)
  1109. continue;
  1110. src_rq = cpu_rq(cpu);
  1111. /*
  1112. * Don't bother taking the src_rq->lock if the next highest
  1113. * task is known to be lower-priority than our current task.
  1114. * This may look racy, but if this value is about to go
  1115. * logically higher, the src_rq will push this task away.
  1116. * And if its going logically lower, we do not care
  1117. */
  1118. if (src_rq->rt.highest_prio.next >=
  1119. this_rq->rt.highest_prio.curr)
  1120. continue;
  1121. /*
  1122. * We can potentially drop this_rq's lock in
  1123. * double_lock_balance, and another CPU could
  1124. * alter this_rq
  1125. */
  1126. double_lock_balance(this_rq, src_rq);
  1127. /*
  1128. * Are there still pullable RT tasks?
  1129. */
  1130. if (src_rq->rt.rt_nr_running <= 1)
  1131. goto skip;
  1132. p = pick_next_highest_task_rt(src_rq, this_cpu);
  1133. /*
  1134. * Do we have an RT task that preempts
  1135. * the to-be-scheduled task?
  1136. */
  1137. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1138. WARN_ON(p == src_rq->curr);
  1139. WARN_ON(!p->se.on_rq);
  1140. /*
  1141. * There's a chance that p is higher in priority
  1142. * than what's currently running on its cpu.
  1143. * This is just that p is wakeing up and hasn't
  1144. * had a chance to schedule. We only pull
  1145. * p if it is lower in priority than the
  1146. * current task on the run queue
  1147. */
  1148. if (p->prio < src_rq->curr->prio)
  1149. goto skip;
  1150. ret = 1;
  1151. deactivate_task(src_rq, p, 0);
  1152. set_task_cpu(p, this_cpu);
  1153. activate_task(this_rq, p, 0);
  1154. /*
  1155. * We continue with the search, just in
  1156. * case there's an even higher prio task
  1157. * in another runqueue. (low likelyhood
  1158. * but possible)
  1159. */
  1160. }
  1161. skip:
  1162. double_unlock_balance(this_rq, src_rq);
  1163. }
  1164. return ret;
  1165. }
  1166. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  1167. {
  1168. /* Try to pull RT tasks here if we lower this rq's prio */
  1169. if (unlikely(rt_task(prev)) && rq->rt.highest_prio.curr > prev->prio)
  1170. pull_rt_task(rq);
  1171. }
  1172. /*
  1173. * assumes rq->lock is held
  1174. */
  1175. static int needs_post_schedule_rt(struct rq *rq)
  1176. {
  1177. return has_pushable_tasks(rq);
  1178. }
  1179. static void post_schedule_rt(struct rq *rq)
  1180. {
  1181. /*
  1182. * This is only called if needs_post_schedule_rt() indicates that
  1183. * we need to push tasks away
  1184. */
  1185. spin_lock_irq(&rq->lock);
  1186. push_rt_tasks(rq);
  1187. spin_unlock_irq(&rq->lock);
  1188. }
  1189. /*
  1190. * If we are not running and we are not going to reschedule soon, we should
  1191. * try to push tasks away now
  1192. */
  1193. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  1194. {
  1195. if (!task_running(rq, p) &&
  1196. !test_tsk_need_resched(rq->curr) &&
  1197. has_pushable_tasks(rq) &&
  1198. p->rt.nr_cpus_allowed > 1)
  1199. push_rt_tasks(rq);
  1200. }
  1201. static unsigned long
  1202. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1203. unsigned long max_load_move,
  1204. struct sched_domain *sd, enum cpu_idle_type idle,
  1205. int *all_pinned, int *this_best_prio)
  1206. {
  1207. /* don't touch RT tasks */
  1208. return 0;
  1209. }
  1210. static int
  1211. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1212. struct sched_domain *sd, enum cpu_idle_type idle)
  1213. {
  1214. /* don't touch RT tasks */
  1215. return 0;
  1216. }
  1217. static void set_cpus_allowed_rt(struct task_struct *p,
  1218. const struct cpumask *new_mask)
  1219. {
  1220. int weight = cpumask_weight(new_mask);
  1221. BUG_ON(!rt_task(p));
  1222. /*
  1223. * Update the migration status of the RQ if we have an RT task
  1224. * which is running AND changing its weight value.
  1225. */
  1226. if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
  1227. struct rq *rq = task_rq(p);
  1228. if (!task_current(rq, p)) {
  1229. /*
  1230. * Make sure we dequeue this task from the pushable list
  1231. * before going further. It will either remain off of
  1232. * the list because we are no longer pushable, or it
  1233. * will be requeued.
  1234. */
  1235. if (p->rt.nr_cpus_allowed > 1)
  1236. dequeue_pushable_task(rq, p);
  1237. /*
  1238. * Requeue if our weight is changing and still > 1
  1239. */
  1240. if (weight > 1)
  1241. enqueue_pushable_task(rq, p);
  1242. }
  1243. if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
  1244. rq->rt.rt_nr_migratory++;
  1245. } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
  1246. BUG_ON(!rq->rt.rt_nr_migratory);
  1247. rq->rt.rt_nr_migratory--;
  1248. }
  1249. update_rt_migration(&rq->rt);
  1250. }
  1251. cpumask_copy(&p->cpus_allowed, new_mask);
  1252. p->rt.nr_cpus_allowed = weight;
  1253. }
  1254. /* Assumes rq->lock is held */
  1255. static void rq_online_rt(struct rq *rq)
  1256. {
  1257. if (rq->rt.overloaded)
  1258. rt_set_overload(rq);
  1259. __enable_runtime(rq);
  1260. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1261. }
  1262. /* Assumes rq->lock is held */
  1263. static void rq_offline_rt(struct rq *rq)
  1264. {
  1265. if (rq->rt.overloaded)
  1266. rt_clear_overload(rq);
  1267. __disable_runtime(rq);
  1268. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1269. }
  1270. /*
  1271. * When switch from the rt queue, we bring ourselves to a position
  1272. * that we might want to pull RT tasks from other runqueues.
  1273. */
  1274. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  1275. int running)
  1276. {
  1277. /*
  1278. * If there are other RT tasks then we will reschedule
  1279. * and the scheduling of the other RT tasks will handle
  1280. * the balancing. But if we are the last RT task
  1281. * we may need to handle the pulling of RT tasks
  1282. * now.
  1283. */
  1284. if (!rq->rt.rt_nr_running)
  1285. pull_rt_task(rq);
  1286. }
  1287. static inline void init_sched_rt_class(void)
  1288. {
  1289. unsigned int i;
  1290. for_each_possible_cpu(i)
  1291. alloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  1292. GFP_KERNEL, cpu_to_node(i));
  1293. }
  1294. #endif /* CONFIG_SMP */
  1295. /*
  1296. * When switching a task to RT, we may overload the runqueue
  1297. * with RT tasks. In this case we try to push them off to
  1298. * other runqueues.
  1299. */
  1300. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  1301. int running)
  1302. {
  1303. int check_resched = 1;
  1304. /*
  1305. * If we are already running, then there's nothing
  1306. * that needs to be done. But if we are not running
  1307. * we may need to preempt the current running task.
  1308. * If that current running task is also an RT task
  1309. * then see if we can move to another run queue.
  1310. */
  1311. if (!running) {
  1312. #ifdef CONFIG_SMP
  1313. if (rq->rt.overloaded && push_rt_task(rq) &&
  1314. /* Don't resched if we changed runqueues */
  1315. rq != task_rq(p))
  1316. check_resched = 0;
  1317. #endif /* CONFIG_SMP */
  1318. if (check_resched && p->prio < rq->curr->prio)
  1319. resched_task(rq->curr);
  1320. }
  1321. }
  1322. /*
  1323. * Priority of the task has changed. This may cause
  1324. * us to initiate a push or pull.
  1325. */
  1326. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  1327. int oldprio, int running)
  1328. {
  1329. if (running) {
  1330. #ifdef CONFIG_SMP
  1331. /*
  1332. * If our priority decreases while running, we
  1333. * may need to pull tasks to this runqueue.
  1334. */
  1335. if (oldprio < p->prio)
  1336. pull_rt_task(rq);
  1337. /*
  1338. * If there's a higher priority task waiting to run
  1339. * then reschedule. Note, the above pull_rt_task
  1340. * can release the rq lock and p could migrate.
  1341. * Only reschedule if p is still on the same runqueue.
  1342. */
  1343. if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
  1344. resched_task(p);
  1345. #else
  1346. /* For UP simply resched on drop of prio */
  1347. if (oldprio < p->prio)
  1348. resched_task(p);
  1349. #endif /* CONFIG_SMP */
  1350. } else {
  1351. /*
  1352. * This task is not running, but if it is
  1353. * greater than the current running task
  1354. * then reschedule.
  1355. */
  1356. if (p->prio < rq->curr->prio)
  1357. resched_task(rq->curr);
  1358. }
  1359. }
  1360. static void watchdog(struct rq *rq, struct task_struct *p)
  1361. {
  1362. unsigned long soft, hard;
  1363. if (!p->signal)
  1364. return;
  1365. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  1366. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  1367. if (soft != RLIM_INFINITY) {
  1368. unsigned long next;
  1369. p->rt.timeout++;
  1370. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1371. if (p->rt.timeout > next)
  1372. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1373. }
  1374. }
  1375. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1376. {
  1377. update_curr_rt(rq);
  1378. watchdog(rq, p);
  1379. /*
  1380. * RR tasks need a special form of timeslice management.
  1381. * FIFO tasks have no timeslices.
  1382. */
  1383. if (p->policy != SCHED_RR)
  1384. return;
  1385. if (--p->rt.time_slice)
  1386. return;
  1387. p->rt.time_slice = DEF_TIMESLICE;
  1388. /*
  1389. * Requeue to the end of queue if we are not the only element
  1390. * on the queue:
  1391. */
  1392. if (p->rt.run_list.prev != p->rt.run_list.next) {
  1393. requeue_task_rt(rq, p, 0);
  1394. set_tsk_need_resched(p);
  1395. }
  1396. }
  1397. static void set_curr_task_rt(struct rq *rq)
  1398. {
  1399. struct task_struct *p = rq->curr;
  1400. p->se.exec_start = rq->clock;
  1401. /* The running task is never eligible for pushing */
  1402. dequeue_pushable_task(rq, p);
  1403. }
  1404. static const struct sched_class rt_sched_class = {
  1405. .next = &fair_sched_class,
  1406. .enqueue_task = enqueue_task_rt,
  1407. .dequeue_task = dequeue_task_rt,
  1408. .yield_task = yield_task_rt,
  1409. .check_preempt_curr = check_preempt_curr_rt,
  1410. .pick_next_task = pick_next_task_rt,
  1411. .put_prev_task = put_prev_task_rt,
  1412. #ifdef CONFIG_SMP
  1413. .select_task_rq = select_task_rq_rt,
  1414. .load_balance = load_balance_rt,
  1415. .move_one_task = move_one_task_rt,
  1416. .set_cpus_allowed = set_cpus_allowed_rt,
  1417. .rq_online = rq_online_rt,
  1418. .rq_offline = rq_offline_rt,
  1419. .pre_schedule = pre_schedule_rt,
  1420. .needs_post_schedule = needs_post_schedule_rt,
  1421. .post_schedule = post_schedule_rt,
  1422. .task_wake_up = task_wake_up_rt,
  1423. .switched_from = switched_from_rt,
  1424. #endif
  1425. .set_curr_task = set_curr_task_rt,
  1426. .task_tick = task_tick_rt,
  1427. .prio_changed = prio_changed_rt,
  1428. .switched_to = switched_to_rt,
  1429. };
  1430. #ifdef CONFIG_SCHED_DEBUG
  1431. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1432. static void print_rt_stats(struct seq_file *m, int cpu)
  1433. {
  1434. struct rt_rq *rt_rq;
  1435. rcu_read_lock();
  1436. for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu))
  1437. print_rt_rq(m, cpu, rt_rq);
  1438. rcu_read_unlock();
  1439. }
  1440. #endif /* CONFIG_SCHED_DEBUG */