sched_rt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. Note, the above pull_rt_task
  926. * can release the rq lock and p could migrate.
  927. * Only reschedule if p is still on the same runqueue.
  928. */
  929. if (p->prio > rq->rt.highest_prio && rq->curr == p)
  930. resched_task(p);
  931. #else
  932. /* For UP simply resched on drop of prio */
  933. if (oldprio < p->prio)
  934. resched_task(p);
  935. #endif /* CONFIG_SMP */
  936. } else {
  937. /*
  938. * This task is not running, but if it is
  939. * greater than the current running task
  940. * then reschedule.
  941. */
  942. if (p->prio < rq->curr->prio)
  943. resched_task(rq->curr);
  944. }
  945. }
  946. static void watchdog(struct rq *rq, struct task_struct *p)
  947. {
  948. unsigned long soft, hard;
  949. if (!p->signal)
  950. return;
  951. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  952. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  953. if (soft != RLIM_INFINITY) {
  954. unsigned long next;
  955. p->rt.timeout++;
  956. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  957. if (p->rt.timeout > next)
  958. p->it_sched_expires = p->se.sum_exec_runtime;
  959. }
  960. }
  961. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  962. {
  963. update_curr_rt(rq);
  964. watchdog(rq, p);
  965. /*
  966. * RR tasks need a special form of timeslice management.
  967. * FIFO tasks have no timeslices.
  968. */
  969. if (p->policy != SCHED_RR)
  970. return;
  971. if (--p->rt.time_slice)
  972. return;
  973. p->rt.time_slice = DEF_TIMESLICE;
  974. /*
  975. * Requeue to the end of queue if we are not the only element
  976. * on the queue:
  977. */
  978. if (p->rt.run_list.prev != p->rt.run_list.next) {
  979. requeue_task_rt(rq, p);
  980. set_tsk_need_resched(p);
  981. }
  982. }
  983. static void set_curr_task_rt(struct rq *rq)
  984. {
  985. struct task_struct *p = rq->curr;
  986. p->se.exec_start = rq->clock;
  987. }
  988. const struct sched_class rt_sched_class = {
  989. .next = &fair_sched_class,
  990. .enqueue_task = enqueue_task_rt,
  991. .dequeue_task = dequeue_task_rt,
  992. .yield_task = yield_task_rt,
  993. #ifdef CONFIG_SMP
  994. .select_task_rq = select_task_rq_rt,
  995. #endif /* CONFIG_SMP */
  996. .check_preempt_curr = check_preempt_curr_rt,
  997. .pick_next_task = pick_next_task_rt,
  998. .put_prev_task = put_prev_task_rt,
  999. #ifdef CONFIG_SMP
  1000. .load_balance = load_balance_rt,
  1001. .move_one_task = move_one_task_rt,
  1002. .set_cpus_allowed = set_cpus_allowed_rt,
  1003. .join_domain = join_domain_rt,
  1004. .leave_domain = leave_domain_rt,
  1005. .pre_schedule = pre_schedule_rt,
  1006. .post_schedule = post_schedule_rt,
  1007. .task_wake_up = task_wake_up_rt,
  1008. .switched_from = switched_from_rt,
  1009. #endif
  1010. .set_curr_task = set_curr_task_rt,
  1011. .task_tick = task_tick_rt,
  1012. .prio_changed = prio_changed_rt,
  1013. .switched_to = switched_to_rt,
  1014. };