sched_rt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3. * policies)
  4. */
  5. #ifdef CONFIG_SMP
  6. static cpumask_t rt_overload_mask;
  7. static atomic_t rto_count;
  8. static inline int rt_overloaded(void)
  9. {
  10. return atomic_read(&rto_count);
  11. }
  12. static inline cpumask_t *rt_overload(void)
  13. {
  14. return &rt_overload_mask;
  15. }
  16. static inline void rt_set_overload(struct rq *rq)
  17. {
  18. rq->rt.overloaded = 1;
  19. cpu_set(rq->cpu, rt_overload_mask);
  20. /*
  21. * Make sure the mask is visible before we set
  22. * the overload count. That is checked to determine
  23. * if we should look at the mask. It would be a shame
  24. * if we looked at the mask, but the mask was not
  25. * updated yet.
  26. */
  27. wmb();
  28. atomic_inc(&rto_count);
  29. }
  30. static inline void rt_clear_overload(struct rq *rq)
  31. {
  32. /* the order here really doesn't matter */
  33. atomic_dec(&rto_count);
  34. cpu_clear(rq->cpu, rt_overload_mask);
  35. rq->rt.overloaded = 0;
  36. }
  37. static void update_rt_migration(struct rq *rq)
  38. {
  39. if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1))
  40. rt_set_overload(rq);
  41. else
  42. rt_clear_overload(rq);
  43. }
  44. #endif /* CONFIG_SMP */
  45. /*
  46. * Update the current task's runtime statistics. Skip current tasks that
  47. * are not in our scheduling class.
  48. */
  49. static void update_curr_rt(struct rq *rq)
  50. {
  51. struct task_struct *curr = rq->curr;
  52. u64 delta_exec;
  53. if (!task_has_rt_policy(curr))
  54. return;
  55. delta_exec = rq->clock - curr->se.exec_start;
  56. if (unlikely((s64)delta_exec < 0))
  57. delta_exec = 0;
  58. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  59. curr->se.sum_exec_runtime += delta_exec;
  60. curr->se.exec_start = rq->clock;
  61. cpuacct_charge(curr, delta_exec);
  62. }
  63. static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
  64. {
  65. WARN_ON(!rt_task(p));
  66. rq->rt.rt_nr_running++;
  67. #ifdef CONFIG_SMP
  68. if (p->prio < rq->rt.highest_prio)
  69. rq->rt.highest_prio = p->prio;
  70. if (p->nr_cpus_allowed > 1)
  71. rq->rt.rt_nr_migratory++;
  72. update_rt_migration(rq);
  73. #endif /* CONFIG_SMP */
  74. }
  75. static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
  76. {
  77. WARN_ON(!rt_task(p));
  78. WARN_ON(!rq->rt.rt_nr_running);
  79. rq->rt.rt_nr_running--;
  80. #ifdef CONFIG_SMP
  81. if (rq->rt.rt_nr_running) {
  82. struct rt_prio_array *array;
  83. WARN_ON(p->prio < rq->rt.highest_prio);
  84. if (p->prio == rq->rt.highest_prio) {
  85. /* recalculate */
  86. array = &rq->rt.active;
  87. rq->rt.highest_prio =
  88. sched_find_first_bit(array->bitmap);
  89. } /* otherwise leave rq->highest prio alone */
  90. } else
  91. rq->rt.highest_prio = MAX_RT_PRIO;
  92. if (p->nr_cpus_allowed > 1)
  93. rq->rt.rt_nr_migratory--;
  94. update_rt_migration(rq);
  95. #endif /* CONFIG_SMP */
  96. }
  97. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  98. {
  99. struct rt_prio_array *array = &rq->rt.active;
  100. list_add_tail(&p->run_list, array->queue + p->prio);
  101. __set_bit(p->prio, array->bitmap);
  102. inc_cpu_load(rq, p->se.load.weight);
  103. inc_rt_tasks(p, rq);
  104. }
  105. /*
  106. * Adding/removing a task to/from a priority array:
  107. */
  108. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  109. {
  110. struct rt_prio_array *array = &rq->rt.active;
  111. update_curr_rt(rq);
  112. list_del(&p->run_list);
  113. if (list_empty(array->queue + p->prio))
  114. __clear_bit(p->prio, array->bitmap);
  115. dec_cpu_load(rq, p->se.load.weight);
  116. dec_rt_tasks(p, rq);
  117. }
  118. /*
  119. * Put task to the end of the run list without the overhead of dequeue
  120. * followed by enqueue.
  121. */
  122. static void requeue_task_rt(struct rq *rq, struct task_struct *p)
  123. {
  124. struct rt_prio_array *array = &rq->rt.active;
  125. list_move_tail(&p->run_list, array->queue + p->prio);
  126. }
  127. static void
  128. yield_task_rt(struct rq *rq)
  129. {
  130. requeue_task_rt(rq, rq->curr);
  131. }
  132. #ifdef CONFIG_SMP
  133. static int find_lowest_rq(struct task_struct *task);
  134. static int select_task_rq_rt(struct task_struct *p, int sync)
  135. {
  136. struct rq *rq = task_rq(p);
  137. /*
  138. * If the task will not preempt the RQ, try to find a better RQ
  139. * before we even activate the task
  140. */
  141. if ((p->prio >= rq->rt.highest_prio)
  142. && (p->nr_cpus_allowed > 1)) {
  143. int cpu = find_lowest_rq(p);
  144. return (cpu == -1) ? task_cpu(p) : cpu;
  145. }
  146. /*
  147. * Otherwise, just let it ride on the affined RQ and the
  148. * post-schedule router will push the preempted task away
  149. */
  150. return task_cpu(p);
  151. }
  152. #endif /* CONFIG_SMP */
  153. /*
  154. * Preempt the current task with a newly woken task if needed:
  155. */
  156. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
  157. {
  158. if (p->prio < rq->curr->prio)
  159. resched_task(rq->curr);
  160. }
  161. static struct task_struct *pick_next_task_rt(struct rq *rq)
  162. {
  163. struct rt_prio_array *array = &rq->rt.active;
  164. struct task_struct *next;
  165. struct list_head *queue;
  166. int idx;
  167. idx = sched_find_first_bit(array->bitmap);
  168. if (idx >= MAX_RT_PRIO)
  169. return NULL;
  170. queue = array->queue + idx;
  171. next = list_entry(queue->next, struct task_struct, run_list);
  172. next->se.exec_start = rq->clock;
  173. return next;
  174. }
  175. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  176. {
  177. update_curr_rt(rq);
  178. p->se.exec_start = 0;
  179. }
  180. #ifdef CONFIG_SMP
  181. /* Only try algorithms three times */
  182. #define RT_MAX_TRIES 3
  183. static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
  184. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  185. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  186. {
  187. if (!task_running(rq, p) &&
  188. (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
  189. (p->nr_cpus_allowed > 1))
  190. return 1;
  191. return 0;
  192. }
  193. /* Return the second highest RT task, NULL otherwise */
  194. static struct task_struct *pick_next_highest_task_rt(struct rq *rq,
  195. int cpu)
  196. {
  197. struct rt_prio_array *array = &rq->rt.active;
  198. struct task_struct *next;
  199. struct list_head *queue;
  200. int idx;
  201. assert_spin_locked(&rq->lock);
  202. if (likely(rq->rt.rt_nr_running < 2))
  203. return NULL;
  204. idx = sched_find_first_bit(array->bitmap);
  205. if (unlikely(idx >= MAX_RT_PRIO)) {
  206. WARN_ON(1); /* rt_nr_running is bad */
  207. return NULL;
  208. }
  209. queue = array->queue + idx;
  210. BUG_ON(list_empty(queue));
  211. next = list_entry(queue->next, struct task_struct, run_list);
  212. if (unlikely(pick_rt_task(rq, next, cpu)))
  213. goto out;
  214. if (queue->next->next != queue) {
  215. /* same prio task */
  216. next = list_entry(queue->next->next, struct task_struct, run_list);
  217. if (pick_rt_task(rq, next, cpu))
  218. goto out;
  219. }
  220. retry:
  221. /* slower, but more flexible */
  222. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  223. if (unlikely(idx >= MAX_RT_PRIO))
  224. return NULL;
  225. queue = array->queue + idx;
  226. BUG_ON(list_empty(queue));
  227. list_for_each_entry(next, queue, run_list) {
  228. if (pick_rt_task(rq, next, cpu))
  229. goto out;
  230. }
  231. goto retry;
  232. out:
  233. return next;
  234. }
  235. static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
  236. static DEFINE_PER_CPU(cpumask_t, valid_cpu_mask);
  237. static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
  238. {
  239. int cpu;
  240. cpumask_t *valid_mask = &__get_cpu_var(valid_cpu_mask);
  241. int lowest_prio = -1;
  242. int ret = 0;
  243. cpus_clear(*lowest_mask);
  244. cpus_and(*valid_mask, cpu_online_map, task->cpus_allowed);
  245. /*
  246. * Scan each rq for the lowest prio.
  247. */
  248. for_each_cpu_mask(cpu, *valid_mask) {
  249. struct rq *rq = cpu_rq(cpu);
  250. /* We look for lowest RT prio or non-rt CPU */
  251. if (rq->rt.highest_prio >= MAX_RT_PRIO) {
  252. if (ret)
  253. cpus_clear(*lowest_mask);
  254. cpu_set(rq->cpu, *lowest_mask);
  255. return 1;
  256. }
  257. /* no locking for now */
  258. if ((rq->rt.highest_prio > task->prio)
  259. && (rq->rt.highest_prio >= lowest_prio)) {
  260. if (rq->rt.highest_prio > lowest_prio) {
  261. /* new low - clear old data */
  262. lowest_prio = rq->rt.highest_prio;
  263. cpus_clear(*lowest_mask);
  264. }
  265. cpu_set(rq->cpu, *lowest_mask);
  266. ret = 1;
  267. }
  268. }
  269. return ret;
  270. }
  271. static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
  272. {
  273. int first;
  274. /* "this_cpu" is cheaper to preempt than a remote processor */
  275. if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
  276. return this_cpu;
  277. first = first_cpu(*mask);
  278. if (first != NR_CPUS)
  279. return first;
  280. return -1;
  281. }
  282. static int find_lowest_rq(struct task_struct *task)
  283. {
  284. struct sched_domain *sd;
  285. cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
  286. int this_cpu = smp_processor_id();
  287. int cpu = task_cpu(task);
  288. if (!find_lowest_cpus(task, lowest_mask))
  289. return -1;
  290. /*
  291. * At this point we have built a mask of cpus representing the
  292. * lowest priority tasks in the system. Now we want to elect
  293. * the best one based on our affinity and topology.
  294. *
  295. * We prioritize the last cpu that the task executed on since
  296. * it is most likely cache-hot in that location.
  297. */
  298. if (cpu_isset(cpu, *lowest_mask))
  299. return cpu;
  300. /*
  301. * Otherwise, we consult the sched_domains span maps to figure
  302. * out which cpu is logically closest to our hot cache data.
  303. */
  304. if (this_cpu == cpu)
  305. this_cpu = -1; /* Skip this_cpu opt if the same */
  306. for_each_domain(cpu, sd) {
  307. if (sd->flags & SD_WAKE_AFFINE) {
  308. cpumask_t domain_mask;
  309. int best_cpu;
  310. cpus_and(domain_mask, sd->span, *lowest_mask);
  311. best_cpu = pick_optimal_cpu(this_cpu,
  312. &domain_mask);
  313. if (best_cpu != -1)
  314. return best_cpu;
  315. }
  316. }
  317. /*
  318. * And finally, if there were no matches within the domains
  319. * just give the caller *something* to work with from the compatible
  320. * locations.
  321. */
  322. return pick_optimal_cpu(this_cpu, lowest_mask);
  323. }
  324. /* Will lock the rq it finds */
  325. static struct rq *find_lock_lowest_rq(struct task_struct *task,
  326. struct rq *rq)
  327. {
  328. struct rq *lowest_rq = NULL;
  329. int cpu;
  330. int tries;
  331. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  332. cpu = find_lowest_rq(task);
  333. if ((cpu == -1) || (cpu == rq->cpu))
  334. break;
  335. lowest_rq = cpu_rq(cpu);
  336. /* if the prio of this runqueue changed, try again */
  337. if (double_lock_balance(rq, lowest_rq)) {
  338. /*
  339. * We had to unlock the run queue. In
  340. * the mean time, task could have
  341. * migrated already or had its affinity changed.
  342. * Also make sure that it wasn't scheduled on its rq.
  343. */
  344. if (unlikely(task_rq(task) != rq ||
  345. !cpu_isset(lowest_rq->cpu, task->cpus_allowed) ||
  346. task_running(rq, task) ||
  347. !task->se.on_rq)) {
  348. spin_unlock(&lowest_rq->lock);
  349. lowest_rq = NULL;
  350. break;
  351. }
  352. }
  353. /* If this rq is still suitable use it. */
  354. if (lowest_rq->rt.highest_prio > task->prio)
  355. break;
  356. /* try again */
  357. spin_unlock(&lowest_rq->lock);
  358. lowest_rq = NULL;
  359. }
  360. return lowest_rq;
  361. }
  362. /*
  363. * If the current CPU has more than one RT task, see if the non
  364. * running task can migrate over to a CPU that is running a task
  365. * of lesser priority.
  366. */
  367. static int push_rt_task(struct rq *rq)
  368. {
  369. struct task_struct *next_task;
  370. struct rq *lowest_rq;
  371. int ret = 0;
  372. int paranoid = RT_MAX_TRIES;
  373. assert_spin_locked(&rq->lock);
  374. if (!rq->rt.overloaded)
  375. return 0;
  376. next_task = pick_next_highest_task_rt(rq, -1);
  377. if (!next_task)
  378. return 0;
  379. retry:
  380. if (unlikely(next_task == rq->curr)) {
  381. WARN_ON(1);
  382. return 0;
  383. }
  384. /*
  385. * It's possible that the next_task slipped in of
  386. * higher priority than current. If that's the case
  387. * just reschedule current.
  388. */
  389. if (unlikely(next_task->prio < rq->curr->prio)) {
  390. resched_task(rq->curr);
  391. return 0;
  392. }
  393. /* We might release rq lock */
  394. get_task_struct(next_task);
  395. /* find_lock_lowest_rq locks the rq if found */
  396. lowest_rq = find_lock_lowest_rq(next_task, rq);
  397. if (!lowest_rq) {
  398. struct task_struct *task;
  399. /*
  400. * find lock_lowest_rq releases rq->lock
  401. * so it is possible that next_task has changed.
  402. * If it has, then try again.
  403. */
  404. task = pick_next_highest_task_rt(rq, -1);
  405. if (unlikely(task != next_task) && task && paranoid--) {
  406. put_task_struct(next_task);
  407. next_task = task;
  408. goto retry;
  409. }
  410. goto out;
  411. }
  412. assert_spin_locked(&lowest_rq->lock);
  413. deactivate_task(rq, next_task, 0);
  414. set_task_cpu(next_task, lowest_rq->cpu);
  415. activate_task(lowest_rq, next_task, 0);
  416. resched_task(lowest_rq->curr);
  417. spin_unlock(&lowest_rq->lock);
  418. ret = 1;
  419. out:
  420. put_task_struct(next_task);
  421. return ret;
  422. }
  423. /*
  424. * TODO: Currently we just use the second highest prio task on
  425. * the queue, and stop when it can't migrate (or there's
  426. * no more RT tasks). There may be a case where a lower
  427. * priority RT task has a different affinity than the
  428. * higher RT task. In this case the lower RT task could
  429. * possibly be able to migrate where as the higher priority
  430. * RT task could not. We currently ignore this issue.
  431. * Enhancements are welcome!
  432. */
  433. static void push_rt_tasks(struct rq *rq)
  434. {
  435. /* push_rt_task will return true if it moved an RT */
  436. while (push_rt_task(rq))
  437. ;
  438. }
  439. static int pull_rt_task(struct rq *this_rq)
  440. {
  441. struct task_struct *next;
  442. struct task_struct *p;
  443. struct rq *src_rq;
  444. cpumask_t *rto_cpumask;
  445. int this_cpu = this_rq->cpu;
  446. int cpu;
  447. int ret = 0;
  448. assert_spin_locked(&this_rq->lock);
  449. /*
  450. * If cpusets are used, and we have overlapping
  451. * run queue cpusets, then this algorithm may not catch all.
  452. * This is just the price you pay on trying to keep
  453. * dirtying caches down on large SMP machines.
  454. */
  455. if (likely(!rt_overloaded()))
  456. return 0;
  457. next = pick_next_task_rt(this_rq);
  458. rto_cpumask = rt_overload();
  459. for_each_cpu_mask(cpu, *rto_cpumask) {
  460. if (this_cpu == cpu)
  461. continue;
  462. src_rq = cpu_rq(cpu);
  463. if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
  464. /*
  465. * It is possible that overlapping cpusets
  466. * will miss clearing a non overloaded runqueue.
  467. * Clear it now.
  468. */
  469. if (double_lock_balance(this_rq, src_rq)) {
  470. /* unlocked our runqueue lock */
  471. struct task_struct *old_next = next;
  472. next = pick_next_task_rt(this_rq);
  473. if (next != old_next)
  474. ret = 1;
  475. }
  476. if (likely(src_rq->rt.rt_nr_running <= 1))
  477. /*
  478. * Small chance that this_rq->curr changed
  479. * but it's really harmless here.
  480. */
  481. rt_clear_overload(this_rq);
  482. else
  483. /*
  484. * Heh, the src_rq is now overloaded, since
  485. * we already have the src_rq lock, go straight
  486. * to pulling tasks from it.
  487. */
  488. goto try_pulling;
  489. spin_unlock(&src_rq->lock);
  490. continue;
  491. }
  492. /*
  493. * We can potentially drop this_rq's lock in
  494. * double_lock_balance, and another CPU could
  495. * steal our next task - hence we must cause
  496. * the caller to recalculate the next task
  497. * in that case:
  498. */
  499. if (double_lock_balance(this_rq, src_rq)) {
  500. struct task_struct *old_next = next;
  501. next = pick_next_task_rt(this_rq);
  502. if (next != old_next)
  503. ret = 1;
  504. }
  505. /*
  506. * Are there still pullable RT tasks?
  507. */
  508. if (src_rq->rt.rt_nr_running <= 1) {
  509. spin_unlock(&src_rq->lock);
  510. continue;
  511. }
  512. try_pulling:
  513. p = pick_next_highest_task_rt(src_rq, this_cpu);
  514. /*
  515. * Do we have an RT task that preempts
  516. * the to-be-scheduled task?
  517. */
  518. if (p && (!next || (p->prio < next->prio))) {
  519. WARN_ON(p == src_rq->curr);
  520. WARN_ON(!p->se.on_rq);
  521. /*
  522. * There's a chance that p is higher in priority
  523. * than what's currently running on its cpu.
  524. * This is just that p is wakeing up and hasn't
  525. * had a chance to schedule. We only pull
  526. * p if it is lower in priority than the
  527. * current task on the run queue or
  528. * this_rq next task is lower in prio than
  529. * the current task on that rq.
  530. */
  531. if (p->prio < src_rq->curr->prio ||
  532. (next && next->prio < src_rq->curr->prio))
  533. goto bail;
  534. ret = 1;
  535. deactivate_task(src_rq, p, 0);
  536. set_task_cpu(p, this_cpu);
  537. activate_task(this_rq, p, 0);
  538. /*
  539. * We continue with the search, just in
  540. * case there's an even higher prio task
  541. * in another runqueue. (low likelyhood
  542. * but possible)
  543. */
  544. /*
  545. * Update next so that we won't pick a task
  546. * on another cpu with a priority lower (or equal)
  547. * than the one we just picked.
  548. */
  549. next = p;
  550. }
  551. bail:
  552. spin_unlock(&src_rq->lock);
  553. }
  554. return ret;
  555. }
  556. static void schedule_balance_rt(struct rq *rq,
  557. struct task_struct *prev)
  558. {
  559. /* Try to pull RT tasks here if we lower this rq's prio */
  560. if (unlikely(rt_task(prev)) &&
  561. rq->rt.highest_prio > prev->prio)
  562. pull_rt_task(rq);
  563. }
  564. static void schedule_tail_balance_rt(struct rq *rq)
  565. {
  566. /*
  567. * If we have more than one rt_task queued, then
  568. * see if we can push the other rt_tasks off to other CPUS.
  569. * Note we may release the rq lock, and since
  570. * the lock was owned by prev, we need to release it
  571. * first via finish_lock_switch and then reaquire it here.
  572. */
  573. if (unlikely(rq->rt.overloaded)) {
  574. spin_lock_irq(&rq->lock);
  575. push_rt_tasks(rq);
  576. spin_unlock_irq(&rq->lock);
  577. }
  578. }
  579. static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
  580. {
  581. if (unlikely(rt_task(p)) &&
  582. !task_running(rq, p) &&
  583. (p->prio >= rq->rt.highest_prio) &&
  584. rq->rt.overloaded)
  585. push_rt_tasks(rq);
  586. }
  587. static unsigned long
  588. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  589. unsigned long max_load_move,
  590. struct sched_domain *sd, enum cpu_idle_type idle,
  591. int *all_pinned, int *this_best_prio)
  592. {
  593. /* don't touch RT tasks */
  594. return 0;
  595. }
  596. static int
  597. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  598. struct sched_domain *sd, enum cpu_idle_type idle)
  599. {
  600. /* don't touch RT tasks */
  601. return 0;
  602. }
  603. static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
  604. {
  605. int weight = cpus_weight(*new_mask);
  606. BUG_ON(!rt_task(p));
  607. /*
  608. * Update the migration status of the RQ if we have an RT task
  609. * which is running AND changing its weight value.
  610. */
  611. if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
  612. struct rq *rq = task_rq(p);
  613. if ((p->nr_cpus_allowed <= 1) && (weight > 1))
  614. rq->rt.rt_nr_migratory++;
  615. else if((p->nr_cpus_allowed > 1) && (weight <= 1)) {
  616. BUG_ON(!rq->rt.rt_nr_migratory);
  617. rq->rt.rt_nr_migratory--;
  618. }
  619. update_rt_migration(rq);
  620. }
  621. p->cpus_allowed = *new_mask;
  622. p->nr_cpus_allowed = weight;
  623. }
  624. #else /* CONFIG_SMP */
  625. # define schedule_tail_balance_rt(rq) do { } while (0)
  626. # define schedule_balance_rt(rq, prev) do { } while (0)
  627. # define wakeup_balance_rt(rq, p) do { } while (0)
  628. #endif /* CONFIG_SMP */
  629. static void task_tick_rt(struct rq *rq, struct task_struct *p)
  630. {
  631. update_curr_rt(rq);
  632. /*
  633. * RR tasks need a special form of timeslice management.
  634. * FIFO tasks have no timeslices.
  635. */
  636. if (p->policy != SCHED_RR)
  637. return;
  638. if (--p->time_slice)
  639. return;
  640. p->time_slice = DEF_TIMESLICE;
  641. /*
  642. * Requeue to the end of queue if we are not the only element
  643. * on the queue:
  644. */
  645. if (p->run_list.prev != p->run_list.next) {
  646. requeue_task_rt(rq, p);
  647. set_tsk_need_resched(p);
  648. }
  649. }
  650. static void set_curr_task_rt(struct rq *rq)
  651. {
  652. struct task_struct *p = rq->curr;
  653. p->se.exec_start = rq->clock;
  654. }
  655. const struct sched_class rt_sched_class = {
  656. .next = &fair_sched_class,
  657. .enqueue_task = enqueue_task_rt,
  658. .dequeue_task = dequeue_task_rt,
  659. .yield_task = yield_task_rt,
  660. #ifdef CONFIG_SMP
  661. .select_task_rq = select_task_rq_rt,
  662. #endif /* CONFIG_SMP */
  663. .check_preempt_curr = check_preempt_curr_rt,
  664. .pick_next_task = pick_next_task_rt,
  665. .put_prev_task = put_prev_task_rt,
  666. #ifdef CONFIG_SMP
  667. .load_balance = load_balance_rt,
  668. .move_one_task = move_one_task_rt,
  669. .set_cpus_allowed = set_cpus_allowed_rt,
  670. #endif
  671. .set_curr_task = set_curr_task_rt,
  672. .task_tick = task_tick_rt,
  673. };