sched_rt.c 34 KB

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