sched_rt.c 37 KB

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