sched_rt.c 42 KB

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