sched_rt.c 28 KB

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