sched_rt.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. /*
  43. * Update the current task's runtime statistics. Skip current tasks that
  44. * are not in our scheduling class.
  45. */
  46. static void update_curr_rt(struct rq *rq)
  47. {
  48. struct task_struct *curr = rq->curr;
  49. u64 delta_exec;
  50. if (!task_has_rt_policy(curr))
  51. return;
  52. delta_exec = rq->clock - curr->se.exec_start;
  53. if (unlikely((s64)delta_exec < 0))
  54. delta_exec = 0;
  55. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  56. curr->se.sum_exec_runtime += delta_exec;
  57. curr->se.exec_start = rq->clock;
  58. cpuacct_charge(curr, delta_exec);
  59. }
  60. static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
  61. {
  62. WARN_ON(!rt_task(p));
  63. rq->rt.rt_nr_running++;
  64. #ifdef CONFIG_SMP
  65. if (p->prio < rq->rt.highest_prio)
  66. rq->rt.highest_prio = p->prio;
  67. if (p->nr_cpus_allowed > 1)
  68. rq->rt.rt_nr_migratory++;
  69. update_rt_migration(rq);
  70. #endif /* CONFIG_SMP */
  71. }
  72. static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
  73. {
  74. WARN_ON(!rt_task(p));
  75. WARN_ON(!rq->rt.rt_nr_running);
  76. rq->rt.rt_nr_running--;
  77. #ifdef CONFIG_SMP
  78. if (rq->rt.rt_nr_running) {
  79. struct rt_prio_array *array;
  80. WARN_ON(p->prio < rq->rt.highest_prio);
  81. if (p->prio == rq->rt.highest_prio) {
  82. /* recalculate */
  83. array = &rq->rt.active;
  84. rq->rt.highest_prio =
  85. sched_find_first_bit(array->bitmap);
  86. } /* otherwise leave rq->highest prio alone */
  87. } else
  88. rq->rt.highest_prio = MAX_RT_PRIO;
  89. if (p->nr_cpus_allowed > 1)
  90. rq->rt.rt_nr_migratory--;
  91. update_rt_migration(rq);
  92. #endif /* CONFIG_SMP */
  93. }
  94. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  95. {
  96. struct rt_prio_array *array = &rq->rt.active;
  97. list_add_tail(&p->run_list, array->queue + p->prio);
  98. __set_bit(p->prio, array->bitmap);
  99. inc_cpu_load(rq, p->se.load.weight);
  100. inc_rt_tasks(p, rq);
  101. }
  102. /*
  103. * Adding/removing a task to/from a priority array:
  104. */
  105. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  106. {
  107. struct rt_prio_array *array = &rq->rt.active;
  108. update_curr_rt(rq);
  109. list_del(&p->run_list);
  110. if (list_empty(array->queue + p->prio))
  111. __clear_bit(p->prio, array->bitmap);
  112. dec_cpu_load(rq, p->se.load.weight);
  113. dec_rt_tasks(p, rq);
  114. }
  115. /*
  116. * Put task to the end of the run list without the overhead of dequeue
  117. * followed by enqueue.
  118. */
  119. static void requeue_task_rt(struct rq *rq, struct task_struct *p)
  120. {
  121. struct rt_prio_array *array = &rq->rt.active;
  122. list_move_tail(&p->run_list, array->queue + p->prio);
  123. }
  124. static void
  125. yield_task_rt(struct rq *rq)
  126. {
  127. requeue_task_rt(rq, rq->curr);
  128. }
  129. #ifdef CONFIG_SMP
  130. static int find_lowest_rq(struct task_struct *task);
  131. static int select_task_rq_rt(struct task_struct *p, int sync)
  132. {
  133. struct rq *rq = task_rq(p);
  134. /*
  135. * If the current task is an RT task, then
  136. * try to see if we can wake this RT task up on another
  137. * runqueue. Otherwise simply start this RT task
  138. * on its current runqueue.
  139. *
  140. * We want to avoid overloading runqueues. Even if
  141. * the RT task is of higher priority than the current RT task.
  142. * RT tasks behave differently than other tasks. If
  143. * one gets preempted, we try to push it off to another queue.
  144. * So trying to keep a preempting RT task on the same
  145. * cache hot CPU will force the running RT task to
  146. * a cold CPU. So we waste all the cache for the lower
  147. * RT task in hopes of saving some of a RT task
  148. * that is just being woken and probably will have
  149. * cold cache anyway.
  150. */
  151. if (unlikely(rt_task(rq->curr)) &&
  152. (p->nr_cpus_allowed > 1)) {
  153. int cpu = find_lowest_rq(p);
  154. return (cpu == -1) ? task_cpu(p) : cpu;
  155. }
  156. /*
  157. * Otherwise, just let it ride on the affined RQ and the
  158. * post-schedule router will push the preempted task away
  159. */
  160. return task_cpu(p);
  161. }
  162. #endif /* CONFIG_SMP */
  163. /*
  164. * Preempt the current task with a newly woken task if needed:
  165. */
  166. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
  167. {
  168. if (p->prio < rq->curr->prio)
  169. resched_task(rq->curr);
  170. }
  171. static struct task_struct *pick_next_task_rt(struct rq *rq)
  172. {
  173. struct rt_prio_array *array = &rq->rt.active;
  174. struct task_struct *next;
  175. struct list_head *queue;
  176. int idx;
  177. idx = sched_find_first_bit(array->bitmap);
  178. if (idx >= MAX_RT_PRIO)
  179. return NULL;
  180. queue = array->queue + idx;
  181. next = list_entry(queue->next, struct task_struct, run_list);
  182. next->se.exec_start = rq->clock;
  183. return next;
  184. }
  185. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  186. {
  187. update_curr_rt(rq);
  188. p->se.exec_start = 0;
  189. }
  190. #ifdef CONFIG_SMP
  191. /* Only try algorithms three times */
  192. #define RT_MAX_TRIES 3
  193. static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
  194. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  195. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  196. {
  197. if (!task_running(rq, p) &&
  198. (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
  199. (p->nr_cpus_allowed > 1))
  200. return 1;
  201. return 0;
  202. }
  203. /* Return the second highest RT task, NULL otherwise */
  204. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  205. {
  206. struct rt_prio_array *array = &rq->rt.active;
  207. struct task_struct *next;
  208. struct list_head *queue;
  209. int idx;
  210. if (likely(rq->rt.rt_nr_running < 2))
  211. return NULL;
  212. idx = sched_find_first_bit(array->bitmap);
  213. if (unlikely(idx >= MAX_RT_PRIO)) {
  214. WARN_ON(1); /* rt_nr_running is bad */
  215. return NULL;
  216. }
  217. queue = array->queue + idx;
  218. BUG_ON(list_empty(queue));
  219. next = list_entry(queue->next, struct task_struct, run_list);
  220. if (unlikely(pick_rt_task(rq, next, cpu)))
  221. goto out;
  222. if (queue->next->next != queue) {
  223. /* same prio task */
  224. next = list_entry(queue->next->next, struct task_struct,
  225. run_list);
  226. if (pick_rt_task(rq, next, cpu))
  227. goto out;
  228. }
  229. retry:
  230. /* slower, but more flexible */
  231. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  232. if (unlikely(idx >= MAX_RT_PRIO))
  233. return NULL;
  234. queue = array->queue + idx;
  235. BUG_ON(list_empty(queue));
  236. list_for_each_entry(next, queue, run_list) {
  237. if (pick_rt_task(rq, next, cpu))
  238. goto out;
  239. }
  240. goto retry;
  241. out:
  242. return next;
  243. }
  244. static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
  245. static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
  246. {
  247. int lowest_prio = -1;
  248. int lowest_cpu = -1;
  249. int count = 0;
  250. int cpu;
  251. cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
  252. /*
  253. * Scan each rq for the lowest prio.
  254. */
  255. for_each_cpu_mask(cpu, *lowest_mask) {
  256. struct rq *rq = cpu_rq(cpu);
  257. /* We look for lowest RT prio or non-rt CPU */
  258. if (rq->rt.highest_prio >= MAX_RT_PRIO) {
  259. /*
  260. * if we already found a low RT queue
  261. * and now we found this non-rt queue
  262. * clear the mask and set our bit.
  263. * Otherwise just return the queue as is
  264. * and the count==1 will cause the algorithm
  265. * to use the first bit found.
  266. */
  267. if (lowest_cpu != -1) {
  268. cpus_clear(*lowest_mask);
  269. cpu_set(rq->cpu, *lowest_mask);
  270. }
  271. return 1;
  272. }
  273. /* no locking for now */
  274. if ((rq->rt.highest_prio > task->prio)
  275. && (rq->rt.highest_prio >= lowest_prio)) {
  276. if (rq->rt.highest_prio > lowest_prio) {
  277. /* new low - clear old data */
  278. lowest_prio = rq->rt.highest_prio;
  279. lowest_cpu = cpu;
  280. count = 0;
  281. }
  282. count++;
  283. } else
  284. cpu_clear(cpu, *lowest_mask);
  285. }
  286. /*
  287. * Clear out all the set bits that represent
  288. * runqueues that were of higher prio than
  289. * the lowest_prio.
  290. */
  291. if (lowest_cpu > 0) {
  292. /*
  293. * Perhaps we could add another cpumask op to
  294. * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
  295. * Then that could be optimized to use memset and such.
  296. */
  297. for_each_cpu_mask(cpu, *lowest_mask) {
  298. if (cpu >= lowest_cpu)
  299. break;
  300. cpu_clear(cpu, *lowest_mask);
  301. }
  302. }
  303. return count;
  304. }
  305. static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
  306. {
  307. int first;
  308. /* "this_cpu" is cheaper to preempt than a remote processor */
  309. if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
  310. return this_cpu;
  311. first = first_cpu(*mask);
  312. if (first != NR_CPUS)
  313. return first;
  314. return -1;
  315. }
  316. static int find_lowest_rq(struct task_struct *task)
  317. {
  318. struct sched_domain *sd;
  319. cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
  320. int this_cpu = smp_processor_id();
  321. int cpu = task_cpu(task);
  322. int count = find_lowest_cpus(task, lowest_mask);
  323. if (!count)
  324. return -1; /* No targets found */
  325. /*
  326. * There is no sense in performing an optimal search if only one
  327. * target is found.
  328. */
  329. if (count == 1)
  330. return first_cpu(*lowest_mask);
  331. /*
  332. * At this point we have built a mask of cpus representing the
  333. * lowest priority tasks in the system. Now we want to elect
  334. * the best one based on our affinity and topology.
  335. *
  336. * We prioritize the last cpu that the task executed on since
  337. * it is most likely cache-hot in that location.
  338. */
  339. if (cpu_isset(cpu, *lowest_mask))
  340. return cpu;
  341. /*
  342. * Otherwise, we consult the sched_domains span maps to figure
  343. * out which cpu is logically closest to our hot cache data.
  344. */
  345. if (this_cpu == cpu)
  346. this_cpu = -1; /* Skip this_cpu opt if the same */
  347. for_each_domain(cpu, sd) {
  348. if (sd->flags & SD_WAKE_AFFINE) {
  349. cpumask_t domain_mask;
  350. int best_cpu;
  351. cpus_and(domain_mask, sd->span, *lowest_mask);
  352. best_cpu = pick_optimal_cpu(this_cpu,
  353. &domain_mask);
  354. if (best_cpu != -1)
  355. return best_cpu;
  356. }
  357. }
  358. /*
  359. * And finally, if there were no matches within the domains
  360. * just give the caller *something* to work with from the compatible
  361. * locations.
  362. */
  363. return pick_optimal_cpu(this_cpu, lowest_mask);
  364. }
  365. /* Will lock the rq it finds */
  366. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  367. {
  368. struct rq *lowest_rq = NULL;
  369. int tries;
  370. int cpu;
  371. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  372. cpu = find_lowest_rq(task);
  373. if ((cpu == -1) || (cpu == rq->cpu))
  374. break;
  375. lowest_rq = cpu_rq(cpu);
  376. /* if the prio of this runqueue changed, try again */
  377. if (double_lock_balance(rq, lowest_rq)) {
  378. /*
  379. * We had to unlock the run queue. In
  380. * the mean time, task could have
  381. * migrated already or had its affinity changed.
  382. * Also make sure that it wasn't scheduled on its rq.
  383. */
  384. if (unlikely(task_rq(task) != rq ||
  385. !cpu_isset(lowest_rq->cpu,
  386. task->cpus_allowed) ||
  387. task_running(rq, task) ||
  388. !task->se.on_rq)) {
  389. spin_unlock(&lowest_rq->lock);
  390. lowest_rq = NULL;
  391. break;
  392. }
  393. }
  394. /* If this rq is still suitable use it. */
  395. if (lowest_rq->rt.highest_prio > task->prio)
  396. break;
  397. /* try again */
  398. spin_unlock(&lowest_rq->lock);
  399. lowest_rq = NULL;
  400. }
  401. return lowest_rq;
  402. }
  403. /*
  404. * If the current CPU has more than one RT task, see if the non
  405. * running task can migrate over to a CPU that is running a task
  406. * of lesser priority.
  407. */
  408. static int push_rt_task(struct rq *rq)
  409. {
  410. struct task_struct *next_task;
  411. struct rq *lowest_rq;
  412. int ret = 0;
  413. int paranoid = RT_MAX_TRIES;
  414. if (!rq->rt.overloaded)
  415. return 0;
  416. next_task = pick_next_highest_task_rt(rq, -1);
  417. if (!next_task)
  418. return 0;
  419. retry:
  420. if (unlikely(next_task == rq->curr)) {
  421. WARN_ON(1);
  422. return 0;
  423. }
  424. /*
  425. * It's possible that the next_task slipped in of
  426. * higher priority than current. If that's the case
  427. * just reschedule current.
  428. */
  429. if (unlikely(next_task->prio < rq->curr->prio)) {
  430. resched_task(rq->curr);
  431. return 0;
  432. }
  433. /* We might release rq lock */
  434. get_task_struct(next_task);
  435. /* find_lock_lowest_rq locks the rq if found */
  436. lowest_rq = find_lock_lowest_rq(next_task, rq);
  437. if (!lowest_rq) {
  438. struct task_struct *task;
  439. /*
  440. * find lock_lowest_rq releases rq->lock
  441. * so it is possible that next_task has changed.
  442. * If it has, then try again.
  443. */
  444. task = pick_next_highest_task_rt(rq, -1);
  445. if (unlikely(task != next_task) && task && paranoid--) {
  446. put_task_struct(next_task);
  447. next_task = task;
  448. goto retry;
  449. }
  450. goto out;
  451. }
  452. deactivate_task(rq, next_task, 0);
  453. set_task_cpu(next_task, lowest_rq->cpu);
  454. activate_task(lowest_rq, next_task, 0);
  455. resched_task(lowest_rq->curr);
  456. spin_unlock(&lowest_rq->lock);
  457. ret = 1;
  458. out:
  459. put_task_struct(next_task);
  460. return ret;
  461. }
  462. /*
  463. * TODO: Currently we just use the second highest prio task on
  464. * the queue, and stop when it can't migrate (or there's
  465. * no more RT tasks). There may be a case where a lower
  466. * priority RT task has a different affinity than the
  467. * higher RT task. In this case the lower RT task could
  468. * possibly be able to migrate where as the higher priority
  469. * RT task could not. We currently ignore this issue.
  470. * Enhancements are welcome!
  471. */
  472. static void push_rt_tasks(struct rq *rq)
  473. {
  474. /* push_rt_task will return true if it moved an RT */
  475. while (push_rt_task(rq))
  476. ;
  477. }
  478. static int pull_rt_task(struct rq *this_rq)
  479. {
  480. int this_cpu = this_rq->cpu, ret = 0, cpu;
  481. struct task_struct *p, *next;
  482. struct rq *src_rq;
  483. if (likely(!rt_overloaded(this_rq)))
  484. return 0;
  485. next = pick_next_task_rt(this_rq);
  486. for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
  487. if (this_cpu == cpu)
  488. continue;
  489. src_rq = cpu_rq(cpu);
  490. /*
  491. * We can potentially drop this_rq's lock in
  492. * double_lock_balance, and another CPU could
  493. * steal our next task - hence we must cause
  494. * the caller to recalculate the next task
  495. * in that case:
  496. */
  497. if (double_lock_balance(this_rq, src_rq)) {
  498. struct task_struct *old_next = next;
  499. next = pick_next_task_rt(this_rq);
  500. if (next != old_next)
  501. ret = 1;
  502. }
  503. /*
  504. * Are there still pullable RT tasks?
  505. */
  506. if (src_rq->rt.rt_nr_running <= 1) {
  507. spin_unlock(&src_rq->lock);
  508. continue;
  509. }
  510. p = pick_next_highest_task_rt(src_rq, this_cpu);
  511. /*
  512. * Do we have an RT task that preempts
  513. * the to-be-scheduled task?
  514. */
  515. if (p && (!next || (p->prio < next->prio))) {
  516. WARN_ON(p == src_rq->curr);
  517. WARN_ON(!p->se.on_rq);
  518. /*
  519. * There's a chance that p is higher in priority
  520. * than what's currently running on its cpu.
  521. * This is just that p is wakeing up and hasn't
  522. * had a chance to schedule. We only pull
  523. * p if it is lower in priority than the
  524. * current task on the run queue or
  525. * this_rq next task is lower in prio than
  526. * the current task on that rq.
  527. */
  528. if (p->prio < src_rq->curr->prio ||
  529. (next && next->prio < src_rq->curr->prio))
  530. goto out;
  531. ret = 1;
  532. deactivate_task(src_rq, p, 0);
  533. set_task_cpu(p, this_cpu);
  534. activate_task(this_rq, p, 0);
  535. /*
  536. * We continue with the search, just in
  537. * case there's an even higher prio task
  538. * in another runqueue. (low likelyhood
  539. * but possible)
  540. *
  541. * Update next so that we won't pick a task
  542. * on another cpu with a priority lower (or equal)
  543. * than the one we just picked.
  544. */
  545. next = p;
  546. }
  547. out:
  548. spin_unlock(&src_rq->lock);
  549. }
  550. return ret;
  551. }
  552. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  553. {
  554. /* Try to pull RT tasks here if we lower this rq's prio */
  555. if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
  556. pull_rt_task(rq);
  557. }
  558. static void post_schedule_rt(struct rq *rq)
  559. {
  560. /*
  561. * If we have more than one rt_task queued, then
  562. * see if we can push the other rt_tasks off to other CPUS.
  563. * Note we may release the rq lock, and since
  564. * the lock was owned by prev, we need to release it
  565. * first via finish_lock_switch and then reaquire it here.
  566. */
  567. if (unlikely(rq->rt.overloaded)) {
  568. spin_lock_irq(&rq->lock);
  569. push_rt_tasks(rq);
  570. spin_unlock_irq(&rq->lock);
  571. }
  572. }
  573. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  574. {
  575. if (!task_running(rq, p) &&
  576. (p->prio >= rq->rt.highest_prio) &&
  577. rq->rt.overloaded)
  578. push_rt_tasks(rq);
  579. }
  580. static unsigned long
  581. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  582. unsigned long max_load_move,
  583. struct sched_domain *sd, enum cpu_idle_type idle,
  584. int *all_pinned, int *this_best_prio)
  585. {
  586. /* don't touch RT tasks */
  587. return 0;
  588. }
  589. static int
  590. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  591. struct sched_domain *sd, enum cpu_idle_type idle)
  592. {
  593. /* don't touch RT tasks */
  594. return 0;
  595. }
  596. static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
  597. {
  598. int weight = cpus_weight(*new_mask);
  599. BUG_ON(!rt_task(p));
  600. /*
  601. * Update the migration status of the RQ if we have an RT task
  602. * which is running AND changing its weight value.
  603. */
  604. if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
  605. struct rq *rq = task_rq(p);
  606. if ((p->nr_cpus_allowed <= 1) && (weight > 1)) {
  607. rq->rt.rt_nr_migratory++;
  608. } else if ((p->nr_cpus_allowed > 1) && (weight <= 1)) {
  609. BUG_ON(!rq->rt.rt_nr_migratory);
  610. rq->rt.rt_nr_migratory--;
  611. }
  612. update_rt_migration(rq);
  613. }
  614. p->cpus_allowed = *new_mask;
  615. p->nr_cpus_allowed = weight;
  616. }
  617. /* Assumes rq->lock is held */
  618. static void join_domain_rt(struct rq *rq)
  619. {
  620. if (rq->rt.overloaded)
  621. rt_set_overload(rq);
  622. }
  623. /* Assumes rq->lock is held */
  624. static void leave_domain_rt(struct rq *rq)
  625. {
  626. if (rq->rt.overloaded)
  627. rt_clear_overload(rq);
  628. }
  629. /*
  630. * When switch from the rt queue, we bring ourselves to a position
  631. * that we might want to pull RT tasks from other runqueues.
  632. */
  633. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  634. int running)
  635. {
  636. /*
  637. * If there are other RT tasks then we will reschedule
  638. * and the scheduling of the other RT tasks will handle
  639. * the balancing. But if we are the last RT task
  640. * we may need to handle the pulling of RT tasks
  641. * now.
  642. */
  643. if (!rq->rt.rt_nr_running)
  644. pull_rt_task(rq);
  645. }
  646. #endif /* CONFIG_SMP */
  647. /*
  648. * When switching a task to RT, we may overload the runqueue
  649. * with RT tasks. In this case we try to push them off to
  650. * other runqueues.
  651. */
  652. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  653. int running)
  654. {
  655. int check_resched = 1;
  656. /*
  657. * If we are already running, then there's nothing
  658. * that needs to be done. But if we are not running
  659. * we may need to preempt the current running task.
  660. * If that current running task is also an RT task
  661. * then see if we can move to another run queue.
  662. */
  663. if (!running) {
  664. #ifdef CONFIG_SMP
  665. if (rq->rt.overloaded && push_rt_task(rq) &&
  666. /* Don't resched if we changed runqueues */
  667. rq != task_rq(p))
  668. check_resched = 0;
  669. #endif /* CONFIG_SMP */
  670. if (check_resched && p->prio < rq->curr->prio)
  671. resched_task(rq->curr);
  672. }
  673. }
  674. /*
  675. * Priority of the task has changed. This may cause
  676. * us to initiate a push or pull.
  677. */
  678. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  679. int oldprio, int running)
  680. {
  681. if (running) {
  682. #ifdef CONFIG_SMP
  683. /*
  684. * If our priority decreases while running, we
  685. * may need to pull tasks to this runqueue.
  686. */
  687. if (oldprio < p->prio)
  688. pull_rt_task(rq);
  689. /*
  690. * If there's a higher priority task waiting to run
  691. * then reschedule.
  692. */
  693. if (p->prio > rq->rt.highest_prio)
  694. resched_task(p);
  695. #else
  696. /* For UP simply resched on drop of prio */
  697. if (oldprio < p->prio)
  698. resched_task(p);
  699. #endif /* CONFIG_SMP */
  700. } else {
  701. /*
  702. * This task is not running, but if it is
  703. * greater than the current running task
  704. * then reschedule.
  705. */
  706. if (p->prio < rq->curr->prio)
  707. resched_task(rq->curr);
  708. }
  709. }
  710. static void task_tick_rt(struct rq *rq, struct task_struct *p)
  711. {
  712. update_curr_rt(rq);
  713. /*
  714. * RR tasks need a special form of timeslice management.
  715. * FIFO tasks have no timeslices.
  716. */
  717. if (p->policy != SCHED_RR)
  718. return;
  719. if (--p->time_slice)
  720. return;
  721. p->time_slice = DEF_TIMESLICE;
  722. /*
  723. * Requeue to the end of queue if we are not the only element
  724. * on the queue:
  725. */
  726. if (p->run_list.prev != p->run_list.next) {
  727. requeue_task_rt(rq, p);
  728. set_tsk_need_resched(p);
  729. }
  730. }
  731. static void set_curr_task_rt(struct rq *rq)
  732. {
  733. struct task_struct *p = rq->curr;
  734. p->se.exec_start = rq->clock;
  735. }
  736. const struct sched_class rt_sched_class = {
  737. .next = &fair_sched_class,
  738. .enqueue_task = enqueue_task_rt,
  739. .dequeue_task = dequeue_task_rt,
  740. .yield_task = yield_task_rt,
  741. #ifdef CONFIG_SMP
  742. .select_task_rq = select_task_rq_rt,
  743. #endif /* CONFIG_SMP */
  744. .check_preempt_curr = check_preempt_curr_rt,
  745. .pick_next_task = pick_next_task_rt,
  746. .put_prev_task = put_prev_task_rt,
  747. #ifdef CONFIG_SMP
  748. .load_balance = load_balance_rt,
  749. .move_one_task = move_one_task_rt,
  750. .set_cpus_allowed = set_cpus_allowed_rt,
  751. .join_domain = join_domain_rt,
  752. .leave_domain = leave_domain_rt,
  753. .pre_schedule = pre_schedule_rt,
  754. .post_schedule = post_schedule_rt,
  755. .task_wake_up = task_wake_up_rt,
  756. .switched_from = switched_from_rt,
  757. #endif
  758. .set_curr_task = set_curr_task_rt,
  759. .task_tick = task_tick_rt,
  760. .prio_changed = prio_changed_rt,
  761. .switched_to = switched_to_rt,
  762. };