sched_rt.c 41 KB

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