sched_rt.c 27 KB

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