sched_rt.c 31 KB

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