sched_rt.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. if (group_rq && rt_rq_throttled(group_rq))
  373. return;
  374. list_add_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
  375. __set_bit(rt_se_prio(rt_se), array->bitmap);
  376. inc_rt_tasks(rt_se, rt_rq);
  377. }
  378. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  379. {
  380. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  381. struct rt_prio_array *array = &rt_rq->active;
  382. list_del_init(&rt_se->run_list);
  383. if (list_empty(array->queue + rt_se_prio(rt_se)))
  384. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  385. dec_rt_tasks(rt_se, rt_rq);
  386. }
  387. /*
  388. * Because the prio of an upper entry depends on the lower
  389. * entries, we must remove entries top - down.
  390. */
  391. static void dequeue_rt_stack(struct task_struct *p)
  392. {
  393. struct sched_rt_entity *rt_se, *back = NULL;
  394. rt_se = &p->rt;
  395. for_each_sched_rt_entity(rt_se) {
  396. rt_se->back = back;
  397. back = rt_se;
  398. }
  399. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  400. if (on_rt_rq(rt_se))
  401. dequeue_rt_entity(rt_se);
  402. }
  403. }
  404. /*
  405. * Adding/removing a task to/from a priority array:
  406. */
  407. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  408. {
  409. struct sched_rt_entity *rt_se = &p->rt;
  410. if (wakeup)
  411. rt_se->timeout = 0;
  412. dequeue_rt_stack(p);
  413. /*
  414. * enqueue everybody, bottom - up.
  415. */
  416. for_each_sched_rt_entity(rt_se)
  417. enqueue_rt_entity(rt_se);
  418. }
  419. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  420. {
  421. struct sched_rt_entity *rt_se = &p->rt;
  422. struct rt_rq *rt_rq;
  423. update_curr_rt(rq);
  424. dequeue_rt_stack(p);
  425. /*
  426. * re-enqueue all non-empty rt_rq entities.
  427. */
  428. for_each_sched_rt_entity(rt_se) {
  429. rt_rq = group_rt_rq(rt_se);
  430. if (rt_rq && rt_rq->rt_nr_running)
  431. enqueue_rt_entity(rt_se);
  432. }
  433. }
  434. /*
  435. * Put task to the end of the run list without the overhead of dequeue
  436. * followed by enqueue.
  437. */
  438. static
  439. void requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
  440. {
  441. struct rt_prio_array *array = &rt_rq->active;
  442. list_move_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
  443. }
  444. static void requeue_task_rt(struct rq *rq, struct task_struct *p)
  445. {
  446. struct sched_rt_entity *rt_se = &p->rt;
  447. struct rt_rq *rt_rq;
  448. for_each_sched_rt_entity(rt_se) {
  449. rt_rq = rt_rq_of_se(rt_se);
  450. requeue_rt_entity(rt_rq, rt_se);
  451. }
  452. }
  453. static void yield_task_rt(struct rq *rq)
  454. {
  455. requeue_task_rt(rq, rq->curr);
  456. }
  457. #ifdef CONFIG_SMP
  458. static int find_lowest_rq(struct task_struct *task);
  459. static int select_task_rq_rt(struct task_struct *p, int sync)
  460. {
  461. struct rq *rq = task_rq(p);
  462. /*
  463. * If the current task is an RT task, then
  464. * try to see if we can wake this RT task up on another
  465. * runqueue. Otherwise simply start this RT task
  466. * on its current runqueue.
  467. *
  468. * We want to avoid overloading runqueues. Even if
  469. * the RT task is of higher priority than the current RT task.
  470. * RT tasks behave differently than other tasks. If
  471. * one gets preempted, we try to push it off to another queue.
  472. * So trying to keep a preempting RT task on the same
  473. * cache hot CPU will force the running RT task to
  474. * a cold CPU. So we waste all the cache for the lower
  475. * RT task in hopes of saving some of a RT task
  476. * that is just being woken and probably will have
  477. * cold cache anyway.
  478. */
  479. if (unlikely(rt_task(rq->curr)) &&
  480. (p->rt.nr_cpus_allowed > 1)) {
  481. int cpu = find_lowest_rq(p);
  482. return (cpu == -1) ? task_cpu(p) : cpu;
  483. }
  484. /*
  485. * Otherwise, just let it ride on the affined RQ and the
  486. * post-schedule router will push the preempted task away
  487. */
  488. return task_cpu(p);
  489. }
  490. #endif /* CONFIG_SMP */
  491. /*
  492. * Preempt the current task with a newly woken task if needed:
  493. */
  494. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
  495. {
  496. if (p->prio < rq->curr->prio)
  497. resched_task(rq->curr);
  498. }
  499. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  500. struct rt_rq *rt_rq)
  501. {
  502. struct rt_prio_array *array = &rt_rq->active;
  503. struct sched_rt_entity *next = NULL;
  504. struct list_head *queue;
  505. int idx;
  506. idx = sched_find_first_bit(array->bitmap);
  507. BUG_ON(idx >= MAX_RT_PRIO);
  508. queue = array->queue + idx;
  509. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  510. return next;
  511. }
  512. static struct task_struct *pick_next_task_rt(struct rq *rq)
  513. {
  514. struct sched_rt_entity *rt_se;
  515. struct task_struct *p;
  516. struct rt_rq *rt_rq;
  517. rt_rq = &rq->rt;
  518. if (unlikely(!rt_rq->rt_nr_running))
  519. return NULL;
  520. if (rt_rq_throttled(rt_rq))
  521. return NULL;
  522. do {
  523. rt_se = pick_next_rt_entity(rq, rt_rq);
  524. BUG_ON(!rt_se);
  525. rt_rq = group_rt_rq(rt_se);
  526. } while (rt_rq);
  527. p = rt_task_of(rt_se);
  528. p->se.exec_start = rq->clock;
  529. return p;
  530. }
  531. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  532. {
  533. update_curr_rt(rq);
  534. p->se.exec_start = 0;
  535. }
  536. #ifdef CONFIG_SMP
  537. /* Only try algorithms three times */
  538. #define RT_MAX_TRIES 3
  539. static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
  540. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  541. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  542. {
  543. if (!task_running(rq, p) &&
  544. (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
  545. (p->rt.nr_cpus_allowed > 1))
  546. return 1;
  547. return 0;
  548. }
  549. /* Return the second highest RT task, NULL otherwise */
  550. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  551. {
  552. struct task_struct *next = NULL;
  553. struct sched_rt_entity *rt_se;
  554. struct rt_prio_array *array;
  555. struct rt_rq *rt_rq;
  556. int idx;
  557. for_each_leaf_rt_rq(rt_rq, rq) {
  558. array = &rt_rq->active;
  559. idx = sched_find_first_bit(array->bitmap);
  560. next_idx:
  561. if (idx >= MAX_RT_PRIO)
  562. continue;
  563. if (next && next->prio < idx)
  564. continue;
  565. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  566. struct task_struct *p = rt_task_of(rt_se);
  567. if (pick_rt_task(rq, p, cpu)) {
  568. next = p;
  569. break;
  570. }
  571. }
  572. if (!next) {
  573. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  574. goto next_idx;
  575. }
  576. }
  577. return next;
  578. }
  579. static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
  580. static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
  581. {
  582. int lowest_prio = -1;
  583. int lowest_cpu = -1;
  584. int count = 0;
  585. int cpu;
  586. cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
  587. /*
  588. * Scan each rq for the lowest prio.
  589. */
  590. for_each_cpu_mask(cpu, *lowest_mask) {
  591. struct rq *rq = cpu_rq(cpu);
  592. /* We look for lowest RT prio or non-rt CPU */
  593. if (rq->rt.highest_prio >= MAX_RT_PRIO) {
  594. /*
  595. * if we already found a low RT queue
  596. * and now we found this non-rt queue
  597. * clear the mask and set our bit.
  598. * Otherwise just return the queue as is
  599. * and the count==1 will cause the algorithm
  600. * to use the first bit found.
  601. */
  602. if (lowest_cpu != -1) {
  603. cpus_clear(*lowest_mask);
  604. cpu_set(rq->cpu, *lowest_mask);
  605. }
  606. return 1;
  607. }
  608. /* no locking for now */
  609. if ((rq->rt.highest_prio > task->prio)
  610. && (rq->rt.highest_prio >= lowest_prio)) {
  611. if (rq->rt.highest_prio > lowest_prio) {
  612. /* new low - clear old data */
  613. lowest_prio = rq->rt.highest_prio;
  614. lowest_cpu = cpu;
  615. count = 0;
  616. }
  617. count++;
  618. } else
  619. cpu_clear(cpu, *lowest_mask);
  620. }
  621. /*
  622. * Clear out all the set bits that represent
  623. * runqueues that were of higher prio than
  624. * the lowest_prio.
  625. */
  626. if (lowest_cpu > 0) {
  627. /*
  628. * Perhaps we could add another cpumask op to
  629. * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
  630. * Then that could be optimized to use memset and such.
  631. */
  632. for_each_cpu_mask(cpu, *lowest_mask) {
  633. if (cpu >= lowest_cpu)
  634. break;
  635. cpu_clear(cpu, *lowest_mask);
  636. }
  637. }
  638. return count;
  639. }
  640. static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
  641. {
  642. int first;
  643. /* "this_cpu" is cheaper to preempt than a remote processor */
  644. if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
  645. return this_cpu;
  646. first = first_cpu(*mask);
  647. if (first != NR_CPUS)
  648. return first;
  649. return -1;
  650. }
  651. static int find_lowest_rq(struct task_struct *task)
  652. {
  653. struct sched_domain *sd;
  654. cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
  655. int this_cpu = smp_processor_id();
  656. int cpu = task_cpu(task);
  657. int count = find_lowest_cpus(task, lowest_mask);
  658. if (!count)
  659. return -1; /* No targets found */
  660. /*
  661. * There is no sense in performing an optimal search if only one
  662. * target is found.
  663. */
  664. if (count == 1)
  665. return first_cpu(*lowest_mask);
  666. /*
  667. * At this point we have built a mask of cpus representing the
  668. * lowest priority tasks in the system. Now we want to elect
  669. * the best one based on our affinity and topology.
  670. *
  671. * We prioritize the last cpu that the task executed on since
  672. * it is most likely cache-hot in that location.
  673. */
  674. if (cpu_isset(cpu, *lowest_mask))
  675. return cpu;
  676. /*
  677. * Otherwise, we consult the sched_domains span maps to figure
  678. * out which cpu is logically closest to our hot cache data.
  679. */
  680. if (this_cpu == cpu)
  681. this_cpu = -1; /* Skip this_cpu opt if the same */
  682. for_each_domain(cpu, sd) {
  683. if (sd->flags & SD_WAKE_AFFINE) {
  684. cpumask_t domain_mask;
  685. int best_cpu;
  686. cpus_and(domain_mask, sd->span, *lowest_mask);
  687. best_cpu = pick_optimal_cpu(this_cpu,
  688. &domain_mask);
  689. if (best_cpu != -1)
  690. return best_cpu;
  691. }
  692. }
  693. /*
  694. * And finally, if there were no matches within the domains
  695. * just give the caller *something* to work with from the compatible
  696. * locations.
  697. */
  698. return pick_optimal_cpu(this_cpu, lowest_mask);
  699. }
  700. /* Will lock the rq it finds */
  701. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  702. {
  703. struct rq *lowest_rq = NULL;
  704. int tries;
  705. int cpu;
  706. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  707. cpu = find_lowest_rq(task);
  708. if ((cpu == -1) || (cpu == rq->cpu))
  709. break;
  710. lowest_rq = cpu_rq(cpu);
  711. /* if the prio of this runqueue changed, try again */
  712. if (double_lock_balance(rq, lowest_rq)) {
  713. /*
  714. * We had to unlock the run queue. In
  715. * the mean time, task could have
  716. * migrated already or had its affinity changed.
  717. * Also make sure that it wasn't scheduled on its rq.
  718. */
  719. if (unlikely(task_rq(task) != rq ||
  720. !cpu_isset(lowest_rq->cpu,
  721. task->cpus_allowed) ||
  722. task_running(rq, task) ||
  723. !task->se.on_rq)) {
  724. spin_unlock(&lowest_rq->lock);
  725. lowest_rq = NULL;
  726. break;
  727. }
  728. }
  729. /* If this rq is still suitable use it. */
  730. if (lowest_rq->rt.highest_prio > task->prio)
  731. break;
  732. /* try again */
  733. spin_unlock(&lowest_rq->lock);
  734. lowest_rq = NULL;
  735. }
  736. return lowest_rq;
  737. }
  738. /*
  739. * If the current CPU has more than one RT task, see if the non
  740. * running task can migrate over to a CPU that is running a task
  741. * of lesser priority.
  742. */
  743. static int push_rt_task(struct rq *rq)
  744. {
  745. struct task_struct *next_task;
  746. struct rq *lowest_rq;
  747. int ret = 0;
  748. int paranoid = RT_MAX_TRIES;
  749. if (!rq->rt.overloaded)
  750. return 0;
  751. next_task = pick_next_highest_task_rt(rq, -1);
  752. if (!next_task)
  753. return 0;
  754. retry:
  755. if (unlikely(next_task == rq->curr)) {
  756. WARN_ON(1);
  757. return 0;
  758. }
  759. /*
  760. * It's possible that the next_task slipped in of
  761. * higher priority than current. If that's the case
  762. * just reschedule current.
  763. */
  764. if (unlikely(next_task->prio < rq->curr->prio)) {
  765. resched_task(rq->curr);
  766. return 0;
  767. }
  768. /* We might release rq lock */
  769. get_task_struct(next_task);
  770. /* find_lock_lowest_rq locks the rq if found */
  771. lowest_rq = find_lock_lowest_rq(next_task, rq);
  772. if (!lowest_rq) {
  773. struct task_struct *task;
  774. /*
  775. * find lock_lowest_rq releases rq->lock
  776. * so it is possible that next_task has changed.
  777. * If it has, then try again.
  778. */
  779. task = pick_next_highest_task_rt(rq, -1);
  780. if (unlikely(task != next_task) && task && paranoid--) {
  781. put_task_struct(next_task);
  782. next_task = task;
  783. goto retry;
  784. }
  785. goto out;
  786. }
  787. deactivate_task(rq, next_task, 0);
  788. set_task_cpu(next_task, lowest_rq->cpu);
  789. activate_task(lowest_rq, next_task, 0);
  790. resched_task(lowest_rq->curr);
  791. spin_unlock(&lowest_rq->lock);
  792. ret = 1;
  793. out:
  794. put_task_struct(next_task);
  795. return ret;
  796. }
  797. /*
  798. * TODO: Currently we just use the second highest prio task on
  799. * the queue, and stop when it can't migrate (or there's
  800. * no more RT tasks). There may be a case where a lower
  801. * priority RT task has a different affinity than the
  802. * higher RT task. In this case the lower RT task could
  803. * possibly be able to migrate where as the higher priority
  804. * RT task could not. We currently ignore this issue.
  805. * Enhancements are welcome!
  806. */
  807. static void push_rt_tasks(struct rq *rq)
  808. {
  809. /* push_rt_task will return true if it moved an RT */
  810. while (push_rt_task(rq))
  811. ;
  812. }
  813. static int pull_rt_task(struct rq *this_rq)
  814. {
  815. int this_cpu = this_rq->cpu, ret = 0, cpu;
  816. struct task_struct *p, *next;
  817. struct rq *src_rq;
  818. if (likely(!rt_overloaded(this_rq)))
  819. return 0;
  820. next = pick_next_task_rt(this_rq);
  821. for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
  822. if (this_cpu == cpu)
  823. continue;
  824. src_rq = cpu_rq(cpu);
  825. /*
  826. * We can potentially drop this_rq's lock in
  827. * double_lock_balance, and another CPU could
  828. * steal our next task - hence we must cause
  829. * the caller to recalculate the next task
  830. * in that case:
  831. */
  832. if (double_lock_balance(this_rq, src_rq)) {
  833. struct task_struct *old_next = next;
  834. next = pick_next_task_rt(this_rq);
  835. if (next != old_next)
  836. ret = 1;
  837. }
  838. /*
  839. * Are there still pullable RT tasks?
  840. */
  841. if (src_rq->rt.rt_nr_running <= 1)
  842. goto skip;
  843. p = pick_next_highest_task_rt(src_rq, this_cpu);
  844. /*
  845. * Do we have an RT task that preempts
  846. * the to-be-scheduled task?
  847. */
  848. if (p && (!next || (p->prio < next->prio))) {
  849. WARN_ON(p == src_rq->curr);
  850. WARN_ON(!p->se.on_rq);
  851. /*
  852. * There's a chance that p is higher in priority
  853. * than what's currently running on its cpu.
  854. * This is just that p is wakeing up and hasn't
  855. * had a chance to schedule. We only pull
  856. * p if it is lower in priority than the
  857. * current task on the run queue or
  858. * this_rq next task is lower in prio than
  859. * the current task on that rq.
  860. */
  861. if (p->prio < src_rq->curr->prio ||
  862. (next && next->prio < src_rq->curr->prio))
  863. goto skip;
  864. ret = 1;
  865. deactivate_task(src_rq, p, 0);
  866. set_task_cpu(p, this_cpu);
  867. activate_task(this_rq, p, 0);
  868. /*
  869. * We continue with the search, just in
  870. * case there's an even higher prio task
  871. * in another runqueue. (low likelyhood
  872. * but possible)
  873. *
  874. * Update next so that we won't pick a task
  875. * on another cpu with a priority lower (or equal)
  876. * than the one we just picked.
  877. */
  878. next = p;
  879. }
  880. skip:
  881. spin_unlock(&src_rq->lock);
  882. }
  883. return ret;
  884. }
  885. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  886. {
  887. /* Try to pull RT tasks here if we lower this rq's prio */
  888. if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
  889. pull_rt_task(rq);
  890. }
  891. static void post_schedule_rt(struct rq *rq)
  892. {
  893. /*
  894. * If we have more than one rt_task queued, then
  895. * see if we can push the other rt_tasks off to other CPUS.
  896. * Note we may release the rq lock, and since
  897. * the lock was owned by prev, we need to release it
  898. * first via finish_lock_switch and then reaquire it here.
  899. */
  900. if (unlikely(rq->rt.overloaded)) {
  901. spin_lock_irq(&rq->lock);
  902. push_rt_tasks(rq);
  903. spin_unlock_irq(&rq->lock);
  904. }
  905. }
  906. /*
  907. * If we are not running and we are not going to reschedule soon, we should
  908. * try to push tasks away now
  909. */
  910. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  911. {
  912. if (!task_running(rq, p) &&
  913. !test_tsk_need_resched(rq->curr) &&
  914. rq->rt.overloaded)
  915. push_rt_tasks(rq);
  916. }
  917. static unsigned long
  918. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  919. unsigned long max_load_move,
  920. struct sched_domain *sd, enum cpu_idle_type idle,
  921. int *all_pinned, int *this_best_prio)
  922. {
  923. /* don't touch RT tasks */
  924. return 0;
  925. }
  926. static int
  927. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  928. struct sched_domain *sd, enum cpu_idle_type idle)
  929. {
  930. /* don't touch RT tasks */
  931. return 0;
  932. }
  933. static void set_cpus_allowed_rt(struct task_struct *p,
  934. const cpumask_t *new_mask)
  935. {
  936. int weight = cpus_weight(*new_mask);
  937. BUG_ON(!rt_task(p));
  938. /*
  939. * Update the migration status of the RQ if we have an RT task
  940. * which is running AND changing its weight value.
  941. */
  942. if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
  943. struct rq *rq = task_rq(p);
  944. if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
  945. rq->rt.rt_nr_migratory++;
  946. } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
  947. BUG_ON(!rq->rt.rt_nr_migratory);
  948. rq->rt.rt_nr_migratory--;
  949. }
  950. update_rt_migration(rq);
  951. }
  952. p->cpus_allowed = *new_mask;
  953. p->rt.nr_cpus_allowed = weight;
  954. }
  955. /* Assumes rq->lock is held */
  956. static void join_domain_rt(struct rq *rq)
  957. {
  958. if (rq->rt.overloaded)
  959. rt_set_overload(rq);
  960. }
  961. /* Assumes rq->lock is held */
  962. static void leave_domain_rt(struct rq *rq)
  963. {
  964. if (rq->rt.overloaded)
  965. rt_clear_overload(rq);
  966. }
  967. /*
  968. * When switch from the rt queue, we bring ourselves to a position
  969. * that we might want to pull RT tasks from other runqueues.
  970. */
  971. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  972. int running)
  973. {
  974. /*
  975. * If there are other RT tasks then we will reschedule
  976. * and the scheduling of the other RT tasks will handle
  977. * the balancing. But if we are the last RT task
  978. * we may need to handle the pulling of RT tasks
  979. * now.
  980. */
  981. if (!rq->rt.rt_nr_running)
  982. pull_rt_task(rq);
  983. }
  984. #endif /* CONFIG_SMP */
  985. /*
  986. * When switching a task to RT, we may overload the runqueue
  987. * with RT tasks. In this case we try to push them off to
  988. * other runqueues.
  989. */
  990. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  991. int running)
  992. {
  993. int check_resched = 1;
  994. /*
  995. * If we are already running, then there's nothing
  996. * that needs to be done. But if we are not running
  997. * we may need to preempt the current running task.
  998. * If that current running task is also an RT task
  999. * then see if we can move to another run queue.
  1000. */
  1001. if (!running) {
  1002. #ifdef CONFIG_SMP
  1003. if (rq->rt.overloaded && push_rt_task(rq) &&
  1004. /* Don't resched if we changed runqueues */
  1005. rq != task_rq(p))
  1006. check_resched = 0;
  1007. #endif /* CONFIG_SMP */
  1008. if (check_resched && p->prio < rq->curr->prio)
  1009. resched_task(rq->curr);
  1010. }
  1011. }
  1012. /*
  1013. * Priority of the task has changed. This may cause
  1014. * us to initiate a push or pull.
  1015. */
  1016. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  1017. int oldprio, int running)
  1018. {
  1019. if (running) {
  1020. #ifdef CONFIG_SMP
  1021. /*
  1022. * If our priority decreases while running, we
  1023. * may need to pull tasks to this runqueue.
  1024. */
  1025. if (oldprio < p->prio)
  1026. pull_rt_task(rq);
  1027. /*
  1028. * If there's a higher priority task waiting to run
  1029. * then reschedule. Note, the above pull_rt_task
  1030. * can release the rq lock and p could migrate.
  1031. * Only reschedule if p is still on the same runqueue.
  1032. */
  1033. if (p->prio > rq->rt.highest_prio && rq->curr == p)
  1034. resched_task(p);
  1035. #else
  1036. /* For UP simply resched on drop of prio */
  1037. if (oldprio < p->prio)
  1038. resched_task(p);
  1039. #endif /* CONFIG_SMP */
  1040. } else {
  1041. /*
  1042. * This task is not running, but if it is
  1043. * greater than the current running task
  1044. * then reschedule.
  1045. */
  1046. if (p->prio < rq->curr->prio)
  1047. resched_task(rq->curr);
  1048. }
  1049. }
  1050. static void watchdog(struct rq *rq, struct task_struct *p)
  1051. {
  1052. unsigned long soft, hard;
  1053. if (!p->signal)
  1054. return;
  1055. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  1056. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  1057. if (soft != RLIM_INFINITY) {
  1058. unsigned long next;
  1059. p->rt.timeout++;
  1060. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1061. if (p->rt.timeout > next)
  1062. p->it_sched_expires = p->se.sum_exec_runtime;
  1063. }
  1064. }
  1065. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1066. {
  1067. update_curr_rt(rq);
  1068. watchdog(rq, p);
  1069. /*
  1070. * RR tasks need a special form of timeslice management.
  1071. * FIFO tasks have no timeslices.
  1072. */
  1073. if (p->policy != SCHED_RR)
  1074. return;
  1075. if (--p->rt.time_slice)
  1076. return;
  1077. p->rt.time_slice = DEF_TIMESLICE;
  1078. /*
  1079. * Requeue to the end of queue if we are not the only element
  1080. * on the queue:
  1081. */
  1082. if (p->rt.run_list.prev != p->rt.run_list.next) {
  1083. requeue_task_rt(rq, p);
  1084. set_tsk_need_resched(p);
  1085. }
  1086. }
  1087. static void set_curr_task_rt(struct rq *rq)
  1088. {
  1089. struct task_struct *p = rq->curr;
  1090. p->se.exec_start = rq->clock;
  1091. }
  1092. static const struct sched_class rt_sched_class = {
  1093. .next = &fair_sched_class,
  1094. .enqueue_task = enqueue_task_rt,
  1095. .dequeue_task = dequeue_task_rt,
  1096. .yield_task = yield_task_rt,
  1097. #ifdef CONFIG_SMP
  1098. .select_task_rq = select_task_rq_rt,
  1099. #endif /* CONFIG_SMP */
  1100. .check_preempt_curr = check_preempt_curr_rt,
  1101. .pick_next_task = pick_next_task_rt,
  1102. .put_prev_task = put_prev_task_rt,
  1103. #ifdef CONFIG_SMP
  1104. .load_balance = load_balance_rt,
  1105. .move_one_task = move_one_task_rt,
  1106. .set_cpus_allowed = set_cpus_allowed_rt,
  1107. .join_domain = join_domain_rt,
  1108. .leave_domain = leave_domain_rt,
  1109. .pre_schedule = pre_schedule_rt,
  1110. .post_schedule = post_schedule_rt,
  1111. .task_wake_up = task_wake_up_rt,
  1112. .switched_from = switched_from_rt,
  1113. #endif
  1114. .set_curr_task = set_curr_task_rt,
  1115. .task_tick = task_tick_rt,
  1116. .prio_changed = prio_changed_rt,
  1117. .switched_to = switched_to_rt,
  1118. };