sched_rt.c 38 KB

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