sched_rt.c 41 KB

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