sched_rt.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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. if (!rq->online)
  13. return;
  14. cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
  15. /*
  16. * Make sure the mask is visible before we set
  17. * the overload count. That is checked to determine
  18. * if we should look at the mask. It would be a shame
  19. * if we looked at the mask, but the mask was not
  20. * updated yet.
  21. */
  22. wmb();
  23. atomic_inc(&rq->rd->rto_count);
  24. }
  25. static inline void rt_clear_overload(struct rq *rq)
  26. {
  27. if (!rq->online)
  28. return;
  29. /* the order here really doesn't matter */
  30. atomic_dec(&rq->rd->rto_count);
  31. cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
  32. }
  33. static void update_rt_migration(struct rq *rq)
  34. {
  35. if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
  36. if (!rq->rt.overloaded) {
  37. rt_set_overload(rq);
  38. rq->rt.overloaded = 1;
  39. }
  40. } else if (rq->rt.overloaded) {
  41. rt_clear_overload(rq);
  42. rq->rt.overloaded = 0;
  43. }
  44. }
  45. #endif /* CONFIG_SMP */
  46. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  47. {
  48. return container_of(rt_se, struct task_struct, rt);
  49. }
  50. static inline int on_rt_rq(struct sched_rt_entity *rt_se)
  51. {
  52. return !list_empty(&rt_se->run_list);
  53. }
  54. #ifdef CONFIG_RT_GROUP_SCHED
  55. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  56. {
  57. if (!rt_rq->tg)
  58. return RUNTIME_INF;
  59. return rt_rq->rt_runtime;
  60. }
  61. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  62. {
  63. return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
  64. }
  65. #define for_each_leaf_rt_rq(rt_rq, rq) \
  66. list_for_each_entry(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
  67. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  68. {
  69. return rt_rq->rq;
  70. }
  71. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  72. {
  73. return rt_se->rt_rq;
  74. }
  75. #define for_each_sched_rt_entity(rt_se) \
  76. for (; rt_se; rt_se = rt_se->parent)
  77. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  78. {
  79. return rt_se->my_q;
  80. }
  81. static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
  82. static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
  83. static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  84. {
  85. struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
  86. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  87. if (rt_rq->rt_nr_running) {
  88. if (rt_se && !on_rt_rq(rt_se))
  89. enqueue_rt_entity(rt_se);
  90. if (rt_rq->highest_prio < curr->prio)
  91. resched_task(curr);
  92. }
  93. }
  94. static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  95. {
  96. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  97. if (rt_se && on_rt_rq(rt_se))
  98. dequeue_rt_entity(rt_se);
  99. }
  100. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  101. {
  102. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  103. }
  104. static int rt_se_boosted(struct sched_rt_entity *rt_se)
  105. {
  106. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  107. struct task_struct *p;
  108. if (rt_rq)
  109. return !!rt_rq->rt_nr_boosted;
  110. p = rt_task_of(rt_se);
  111. return p->prio != p->normal_prio;
  112. }
  113. #ifdef CONFIG_SMP
  114. static inline const struct cpumask *sched_rt_period_mask(void)
  115. {
  116. return cpu_rq(smp_processor_id())->rd->span;
  117. }
  118. #else
  119. static inline const struct cpumask *sched_rt_period_mask(void)
  120. {
  121. return cpu_online_mask;
  122. }
  123. #endif
  124. static inline
  125. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  126. {
  127. return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
  128. }
  129. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  130. {
  131. return &rt_rq->tg->rt_bandwidth;
  132. }
  133. #else /* !CONFIG_RT_GROUP_SCHED */
  134. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  135. {
  136. return rt_rq->rt_runtime;
  137. }
  138. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  139. {
  140. return ktime_to_ns(def_rt_bandwidth.rt_period);
  141. }
  142. #define for_each_leaf_rt_rq(rt_rq, rq) \
  143. for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
  144. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  145. {
  146. return container_of(rt_rq, struct rq, rt);
  147. }
  148. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  149. {
  150. struct task_struct *p = rt_task_of(rt_se);
  151. struct rq *rq = task_rq(p);
  152. return &rq->rt;
  153. }
  154. #define for_each_sched_rt_entity(rt_se) \
  155. for (; rt_se; rt_se = NULL)
  156. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  157. {
  158. return NULL;
  159. }
  160. static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  161. {
  162. if (rt_rq->rt_nr_running)
  163. resched_task(rq_of_rt_rq(rt_rq)->curr);
  164. }
  165. static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  166. {
  167. }
  168. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  169. {
  170. return rt_rq->rt_throttled;
  171. }
  172. static inline const struct cpumask *sched_rt_period_mask(void)
  173. {
  174. return cpu_online_mask;
  175. }
  176. static inline
  177. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  178. {
  179. return &cpu_rq(cpu)->rt;
  180. }
  181. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  182. {
  183. return &def_rt_bandwidth;
  184. }
  185. #endif /* CONFIG_RT_GROUP_SCHED */
  186. #ifdef CONFIG_SMP
  187. /*
  188. * We ran out of runtime, see if we can borrow some from our neighbours.
  189. */
  190. static int do_balance_runtime(struct rt_rq *rt_rq)
  191. {
  192. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  193. struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
  194. int i, weight, more = 0;
  195. u64 rt_period;
  196. weight = cpumask_weight(rd->span);
  197. spin_lock(&rt_b->rt_runtime_lock);
  198. rt_period = ktime_to_ns(rt_b->rt_period);
  199. for_each_cpu(i, rd->span) {
  200. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  201. s64 diff;
  202. if (iter == rt_rq)
  203. continue;
  204. spin_lock(&iter->rt_runtime_lock);
  205. /*
  206. * Either all rqs have inf runtime and there's nothing to steal
  207. * or __disable_runtime() below sets a specific rq to inf to
  208. * indicate its been disabled and disalow stealing.
  209. */
  210. if (iter->rt_runtime == RUNTIME_INF)
  211. goto next;
  212. /*
  213. * From runqueues with spare time, take 1/n part of their
  214. * spare time, but no more than our period.
  215. */
  216. diff = iter->rt_runtime - iter->rt_time;
  217. if (diff > 0) {
  218. diff = div_u64((u64)diff, weight);
  219. if (rt_rq->rt_runtime + diff > rt_period)
  220. diff = rt_period - rt_rq->rt_runtime;
  221. iter->rt_runtime -= diff;
  222. rt_rq->rt_runtime += diff;
  223. more = 1;
  224. if (rt_rq->rt_runtime == rt_period) {
  225. spin_unlock(&iter->rt_runtime_lock);
  226. break;
  227. }
  228. }
  229. next:
  230. spin_unlock(&iter->rt_runtime_lock);
  231. }
  232. spin_unlock(&rt_b->rt_runtime_lock);
  233. return more;
  234. }
  235. /*
  236. * Ensure this RQ takes back all the runtime it lend to its neighbours.
  237. */
  238. static void __disable_runtime(struct rq *rq)
  239. {
  240. struct root_domain *rd = rq->rd;
  241. struct rt_rq *rt_rq;
  242. if (unlikely(!scheduler_running))
  243. return;
  244. for_each_leaf_rt_rq(rt_rq, rq) {
  245. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  246. s64 want;
  247. int i;
  248. spin_lock(&rt_b->rt_runtime_lock);
  249. spin_lock(&rt_rq->rt_runtime_lock);
  250. /*
  251. * Either we're all inf and nobody needs to borrow, or we're
  252. * already disabled and thus have nothing to do, or we have
  253. * exactly the right amount of runtime to take out.
  254. */
  255. if (rt_rq->rt_runtime == RUNTIME_INF ||
  256. rt_rq->rt_runtime == rt_b->rt_runtime)
  257. goto balanced;
  258. spin_unlock(&rt_rq->rt_runtime_lock);
  259. /*
  260. * Calculate the difference between what we started out with
  261. * and what we current have, that's the amount of runtime
  262. * we lend and now have to reclaim.
  263. */
  264. want = rt_b->rt_runtime - rt_rq->rt_runtime;
  265. /*
  266. * Greedy reclaim, take back as much as we can.
  267. */
  268. for_each_cpu(i, rd->span) {
  269. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  270. s64 diff;
  271. /*
  272. * Can't reclaim from ourselves or disabled runqueues.
  273. */
  274. if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
  275. continue;
  276. spin_lock(&iter->rt_runtime_lock);
  277. if (want > 0) {
  278. diff = min_t(s64, iter->rt_runtime, want);
  279. iter->rt_runtime -= diff;
  280. want -= diff;
  281. } else {
  282. iter->rt_runtime -= want;
  283. want -= want;
  284. }
  285. spin_unlock(&iter->rt_runtime_lock);
  286. if (!want)
  287. break;
  288. }
  289. spin_lock(&rt_rq->rt_runtime_lock);
  290. /*
  291. * We cannot be left wanting - that would mean some runtime
  292. * leaked out of the system.
  293. */
  294. BUG_ON(want);
  295. balanced:
  296. /*
  297. * Disable all the borrow logic by pretending we have inf
  298. * runtime - in which case borrowing doesn't make sense.
  299. */
  300. rt_rq->rt_runtime = RUNTIME_INF;
  301. spin_unlock(&rt_rq->rt_runtime_lock);
  302. spin_unlock(&rt_b->rt_runtime_lock);
  303. }
  304. }
  305. static void disable_runtime(struct rq *rq)
  306. {
  307. unsigned long flags;
  308. spin_lock_irqsave(&rq->lock, flags);
  309. __disable_runtime(rq);
  310. spin_unlock_irqrestore(&rq->lock, flags);
  311. }
  312. static void __enable_runtime(struct rq *rq)
  313. {
  314. struct rt_rq *rt_rq;
  315. if (unlikely(!scheduler_running))
  316. return;
  317. /*
  318. * Reset each runqueue's bandwidth settings
  319. */
  320. for_each_leaf_rt_rq(rt_rq, rq) {
  321. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  322. spin_lock(&rt_b->rt_runtime_lock);
  323. spin_lock(&rt_rq->rt_runtime_lock);
  324. rt_rq->rt_runtime = rt_b->rt_runtime;
  325. rt_rq->rt_time = 0;
  326. rt_rq->rt_throttled = 0;
  327. spin_unlock(&rt_rq->rt_runtime_lock);
  328. spin_unlock(&rt_b->rt_runtime_lock);
  329. }
  330. }
  331. static void enable_runtime(struct rq *rq)
  332. {
  333. unsigned long flags;
  334. spin_lock_irqsave(&rq->lock, flags);
  335. __enable_runtime(rq);
  336. spin_unlock_irqrestore(&rq->lock, flags);
  337. }
  338. static int balance_runtime(struct rt_rq *rt_rq)
  339. {
  340. int more = 0;
  341. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  342. spin_unlock(&rt_rq->rt_runtime_lock);
  343. more = do_balance_runtime(rt_rq);
  344. spin_lock(&rt_rq->rt_runtime_lock);
  345. }
  346. return more;
  347. }
  348. #else /* !CONFIG_SMP */
  349. static inline int balance_runtime(struct rt_rq *rt_rq)
  350. {
  351. return 0;
  352. }
  353. #endif /* CONFIG_SMP */
  354. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  355. {
  356. int i, idle = 1;
  357. const struct cpumask *span;
  358. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  359. return 1;
  360. span = sched_rt_period_mask();
  361. for_each_cpu(i, span) {
  362. int enqueue = 0;
  363. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  364. struct rq *rq = rq_of_rt_rq(rt_rq);
  365. spin_lock(&rq->lock);
  366. if (rt_rq->rt_time) {
  367. u64 runtime;
  368. spin_lock(&rt_rq->rt_runtime_lock);
  369. if (rt_rq->rt_throttled)
  370. balance_runtime(rt_rq);
  371. runtime = rt_rq->rt_runtime;
  372. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  373. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  374. rt_rq->rt_throttled = 0;
  375. enqueue = 1;
  376. }
  377. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  378. idle = 0;
  379. spin_unlock(&rt_rq->rt_runtime_lock);
  380. } else if (rt_rq->rt_nr_running)
  381. idle = 0;
  382. if (enqueue)
  383. sched_rt_rq_enqueue(rt_rq);
  384. spin_unlock(&rq->lock);
  385. }
  386. return idle;
  387. }
  388. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  389. {
  390. #ifdef CONFIG_RT_GROUP_SCHED
  391. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  392. if (rt_rq)
  393. return rt_rq->highest_prio;
  394. #endif
  395. return rt_task_of(rt_se)->prio;
  396. }
  397. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  398. {
  399. u64 runtime = sched_rt_runtime(rt_rq);
  400. if (rt_rq->rt_throttled)
  401. return rt_rq_throttled(rt_rq);
  402. if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq))
  403. return 0;
  404. balance_runtime(rt_rq);
  405. runtime = sched_rt_runtime(rt_rq);
  406. if (runtime == RUNTIME_INF)
  407. return 0;
  408. if (rt_rq->rt_time > runtime) {
  409. rt_rq->rt_throttled = 1;
  410. if (rt_rq_throttled(rt_rq)) {
  411. sched_rt_rq_dequeue(rt_rq);
  412. return 1;
  413. }
  414. }
  415. return 0;
  416. }
  417. /*
  418. * Update the current task's runtime statistics. Skip current tasks that
  419. * are not in our scheduling class.
  420. */
  421. static void update_curr_rt(struct rq *rq)
  422. {
  423. struct task_struct *curr = rq->curr;
  424. struct sched_rt_entity *rt_se = &curr->rt;
  425. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  426. u64 delta_exec;
  427. if (!task_has_rt_policy(curr))
  428. return;
  429. delta_exec = rq->clock - curr->se.exec_start;
  430. if (unlikely((s64)delta_exec < 0))
  431. delta_exec = 0;
  432. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  433. curr->se.sum_exec_runtime += delta_exec;
  434. account_group_exec_runtime(curr, delta_exec);
  435. curr->se.exec_start = rq->clock;
  436. cpuacct_charge(curr, delta_exec);
  437. if (!rt_bandwidth_enabled())
  438. return;
  439. for_each_sched_rt_entity(rt_se) {
  440. rt_rq = rt_rq_of_se(rt_se);
  441. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  442. spin_lock(&rt_rq->rt_runtime_lock);
  443. rt_rq->rt_time += delta_exec;
  444. if (sched_rt_runtime_exceeded(rt_rq))
  445. resched_task(curr);
  446. spin_unlock(&rt_rq->rt_runtime_lock);
  447. }
  448. }
  449. }
  450. static inline
  451. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  452. {
  453. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  454. rt_rq->rt_nr_running++;
  455. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  456. if (rt_se_prio(rt_se) < rt_rq->highest_prio) {
  457. #ifdef CONFIG_SMP
  458. struct rq *rq = rq_of_rt_rq(rt_rq);
  459. #endif
  460. rt_rq->highest_prio = rt_se_prio(rt_se);
  461. #ifdef CONFIG_SMP
  462. if (rq->online)
  463. cpupri_set(&rq->rd->cpupri, rq->cpu,
  464. rt_se_prio(rt_se));
  465. #endif
  466. }
  467. #endif
  468. #ifdef CONFIG_SMP
  469. if (rt_se->nr_cpus_allowed > 1) {
  470. struct rq *rq = rq_of_rt_rq(rt_rq);
  471. rq->rt.rt_nr_migratory++;
  472. }
  473. update_rt_migration(rq_of_rt_rq(rt_rq));
  474. #endif
  475. #ifdef CONFIG_RT_GROUP_SCHED
  476. if (rt_se_boosted(rt_se))
  477. rt_rq->rt_nr_boosted++;
  478. if (rt_rq->tg)
  479. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  480. #else
  481. start_rt_bandwidth(&def_rt_bandwidth);
  482. #endif
  483. }
  484. static inline
  485. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  486. {
  487. #ifdef CONFIG_SMP
  488. int highest_prio = rt_rq->highest_prio;
  489. #endif
  490. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  491. WARN_ON(!rt_rq->rt_nr_running);
  492. rt_rq->rt_nr_running--;
  493. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  494. if (rt_rq->rt_nr_running) {
  495. struct rt_prio_array *array;
  496. WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
  497. if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
  498. /* recalculate */
  499. array = &rt_rq->active;
  500. rt_rq->highest_prio =
  501. sched_find_first_bit(array->bitmap);
  502. } /* otherwise leave rq->highest prio alone */
  503. } else
  504. rt_rq->highest_prio = MAX_RT_PRIO;
  505. #endif
  506. #ifdef CONFIG_SMP
  507. if (rt_se->nr_cpus_allowed > 1) {
  508. struct rq *rq = rq_of_rt_rq(rt_rq);
  509. rq->rt.rt_nr_migratory--;
  510. }
  511. if (rt_rq->highest_prio != highest_prio) {
  512. struct rq *rq = rq_of_rt_rq(rt_rq);
  513. if (rq->online)
  514. cpupri_set(&rq->rd->cpupri, rq->cpu,
  515. rt_rq->highest_prio);
  516. }
  517. update_rt_migration(rq_of_rt_rq(rt_rq));
  518. #endif /* CONFIG_SMP */
  519. #ifdef CONFIG_RT_GROUP_SCHED
  520. if (rt_se_boosted(rt_se))
  521. rt_rq->rt_nr_boosted--;
  522. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  523. #endif
  524. }
  525. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
  526. {
  527. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  528. struct rt_prio_array *array = &rt_rq->active;
  529. struct rt_rq *group_rq = group_rt_rq(rt_se);
  530. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  531. /*
  532. * Don't enqueue the group if its throttled, or when empty.
  533. * The latter is a consequence of the former when a child group
  534. * get throttled and the current group doesn't have any other
  535. * active members.
  536. */
  537. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  538. return;
  539. list_add_tail(&rt_se->run_list, queue);
  540. __set_bit(rt_se_prio(rt_se), array->bitmap);
  541. inc_rt_tasks(rt_se, rt_rq);
  542. }
  543. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  544. {
  545. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  546. struct rt_prio_array *array = &rt_rq->active;
  547. list_del_init(&rt_se->run_list);
  548. if (list_empty(array->queue + rt_se_prio(rt_se)))
  549. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  550. dec_rt_tasks(rt_se, rt_rq);
  551. }
  552. /*
  553. * Because the prio of an upper entry depends on the lower
  554. * entries, we must remove entries top - down.
  555. */
  556. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  557. {
  558. struct sched_rt_entity *back = NULL;
  559. for_each_sched_rt_entity(rt_se) {
  560. rt_se->back = back;
  561. back = rt_se;
  562. }
  563. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  564. if (on_rt_rq(rt_se))
  565. __dequeue_rt_entity(rt_se);
  566. }
  567. }
  568. static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
  569. {
  570. dequeue_rt_stack(rt_se);
  571. for_each_sched_rt_entity(rt_se)
  572. __enqueue_rt_entity(rt_se);
  573. }
  574. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  575. {
  576. dequeue_rt_stack(rt_se);
  577. for_each_sched_rt_entity(rt_se) {
  578. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  579. if (rt_rq && rt_rq->rt_nr_running)
  580. __enqueue_rt_entity(rt_se);
  581. }
  582. }
  583. /*
  584. * Adding/removing a task to/from a priority array:
  585. */
  586. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  587. {
  588. struct sched_rt_entity *rt_se = &p->rt;
  589. if (wakeup)
  590. rt_se->timeout = 0;
  591. enqueue_rt_entity(rt_se);
  592. inc_cpu_load(rq, p->se.load.weight);
  593. }
  594. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  595. {
  596. struct sched_rt_entity *rt_se = &p->rt;
  597. update_curr_rt(rq);
  598. dequeue_rt_entity(rt_se);
  599. dec_cpu_load(rq, p->se.load.weight);
  600. }
  601. /*
  602. * Put task to the end of the run list without the overhead of dequeue
  603. * followed by enqueue.
  604. */
  605. static void
  606. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  607. {
  608. if (on_rt_rq(rt_se)) {
  609. struct rt_prio_array *array = &rt_rq->active;
  610. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  611. if (head)
  612. list_move(&rt_se->run_list, queue);
  613. else
  614. list_move_tail(&rt_se->run_list, queue);
  615. }
  616. }
  617. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  618. {
  619. struct sched_rt_entity *rt_se = &p->rt;
  620. struct rt_rq *rt_rq;
  621. for_each_sched_rt_entity(rt_se) {
  622. rt_rq = rt_rq_of_se(rt_se);
  623. requeue_rt_entity(rt_rq, rt_se, head);
  624. }
  625. }
  626. static void yield_task_rt(struct rq *rq)
  627. {
  628. requeue_task_rt(rq, rq->curr, 0);
  629. }
  630. #ifdef CONFIG_SMP
  631. static int find_lowest_rq(struct task_struct *task);
  632. static int select_task_rq_rt(struct task_struct *p, int sync)
  633. {
  634. struct rq *rq = task_rq(p);
  635. /*
  636. * If the current task is an RT task, then
  637. * try to see if we can wake this RT task up on another
  638. * runqueue. Otherwise simply start this RT task
  639. * on its current runqueue.
  640. *
  641. * We want to avoid overloading runqueues. Even if
  642. * the RT task is of higher priority than the current RT task.
  643. * RT tasks behave differently than other tasks. If
  644. * one gets preempted, we try to push it off to another queue.
  645. * So trying to keep a preempting RT task on the same
  646. * cache hot CPU will force the running RT task to
  647. * a cold CPU. So we waste all the cache for the lower
  648. * RT task in hopes of saving some of a RT task
  649. * that is just being woken and probably will have
  650. * cold cache anyway.
  651. */
  652. if (unlikely(rt_task(rq->curr)) &&
  653. (p->rt.nr_cpus_allowed > 1)) {
  654. int cpu = find_lowest_rq(p);
  655. return (cpu == -1) ? task_cpu(p) : cpu;
  656. }
  657. /*
  658. * Otherwise, just let it ride on the affined RQ and the
  659. * post-schedule router will push the preempted task away
  660. */
  661. return task_cpu(p);
  662. }
  663. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  664. {
  665. cpumask_t mask;
  666. if (rq->curr->rt.nr_cpus_allowed == 1)
  667. return;
  668. if (p->rt.nr_cpus_allowed != 1
  669. && cpupri_find(&rq->rd->cpupri, p, &mask))
  670. return;
  671. if (!cpupri_find(&rq->rd->cpupri, rq->curr, &mask))
  672. return;
  673. /*
  674. * There appears to be other cpus that can accept
  675. * current and none to run 'p', so lets reschedule
  676. * to try and push current away:
  677. */
  678. requeue_task_rt(rq, p, 1);
  679. resched_task(rq->curr);
  680. }
  681. #endif /* CONFIG_SMP */
  682. /*
  683. * Preempt the current task with a newly woken task if needed:
  684. */
  685. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync)
  686. {
  687. if (p->prio < rq->curr->prio) {
  688. resched_task(rq->curr);
  689. return;
  690. }
  691. #ifdef CONFIG_SMP
  692. /*
  693. * If:
  694. *
  695. * - the newly woken task is of equal priority to the current task
  696. * - the newly woken task is non-migratable while current is migratable
  697. * - current will be preempted on the next reschedule
  698. *
  699. * we should check to see if current can readily move to a different
  700. * cpu. If so, we will reschedule to allow the push logic to try
  701. * to move current somewhere else, making room for our non-migratable
  702. * task.
  703. */
  704. if (p->prio == rq->curr->prio && !need_resched())
  705. check_preempt_equal_prio(rq, p);
  706. #endif
  707. }
  708. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  709. struct rt_rq *rt_rq)
  710. {
  711. struct rt_prio_array *array = &rt_rq->active;
  712. struct sched_rt_entity *next = NULL;
  713. struct list_head *queue;
  714. int idx;
  715. idx = sched_find_first_bit(array->bitmap);
  716. BUG_ON(idx >= MAX_RT_PRIO);
  717. queue = array->queue + idx;
  718. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  719. return next;
  720. }
  721. static struct task_struct *pick_next_task_rt(struct rq *rq)
  722. {
  723. struct sched_rt_entity *rt_se;
  724. struct task_struct *p;
  725. struct rt_rq *rt_rq;
  726. rt_rq = &rq->rt;
  727. if (unlikely(!rt_rq->rt_nr_running))
  728. return NULL;
  729. if (rt_rq_throttled(rt_rq))
  730. return NULL;
  731. do {
  732. rt_se = pick_next_rt_entity(rq, rt_rq);
  733. BUG_ON(!rt_se);
  734. rt_rq = group_rt_rq(rt_se);
  735. } while (rt_rq);
  736. p = rt_task_of(rt_se);
  737. p->se.exec_start = rq->clock;
  738. return p;
  739. }
  740. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  741. {
  742. update_curr_rt(rq);
  743. p->se.exec_start = 0;
  744. }
  745. #ifdef CONFIG_SMP
  746. /* Only try algorithms three times */
  747. #define RT_MAX_TRIES 3
  748. static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
  749. static inline void double_unlock_balance(struct rq *this_rq,
  750. struct rq *busiest);
  751. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  752. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  753. {
  754. if (!task_running(rq, p) &&
  755. (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
  756. (p->rt.nr_cpus_allowed > 1))
  757. return 1;
  758. return 0;
  759. }
  760. /* Return the second highest RT task, NULL otherwise */
  761. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  762. {
  763. struct task_struct *next = NULL;
  764. struct sched_rt_entity *rt_se;
  765. struct rt_prio_array *array;
  766. struct rt_rq *rt_rq;
  767. int idx;
  768. for_each_leaf_rt_rq(rt_rq, rq) {
  769. array = &rt_rq->active;
  770. idx = sched_find_first_bit(array->bitmap);
  771. next_idx:
  772. if (idx >= MAX_RT_PRIO)
  773. continue;
  774. if (next && next->prio < idx)
  775. continue;
  776. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  777. struct task_struct *p = rt_task_of(rt_se);
  778. if (pick_rt_task(rq, p, cpu)) {
  779. next = p;
  780. break;
  781. }
  782. }
  783. if (!next) {
  784. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  785. goto next_idx;
  786. }
  787. }
  788. return next;
  789. }
  790. static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
  791. static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
  792. {
  793. int first;
  794. /* "this_cpu" is cheaper to preempt than a remote processor */
  795. if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
  796. return this_cpu;
  797. first = first_cpu(*mask);
  798. if (first != NR_CPUS)
  799. return first;
  800. return -1;
  801. }
  802. static int find_lowest_rq(struct task_struct *task)
  803. {
  804. struct sched_domain *sd;
  805. cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
  806. int this_cpu = smp_processor_id();
  807. int cpu = task_cpu(task);
  808. if (task->rt.nr_cpus_allowed == 1)
  809. return -1; /* No other targets possible */
  810. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  811. return -1; /* No targets found */
  812. /*
  813. * Only consider CPUs that are usable for migration.
  814. * I guess we might want to change cpupri_find() to ignore those
  815. * in the first place.
  816. */
  817. cpus_and(*lowest_mask, *lowest_mask, cpu_active_map);
  818. /*
  819. * At this point we have built a mask of cpus representing the
  820. * lowest priority tasks in the system. Now we want to elect
  821. * the best one based on our affinity and topology.
  822. *
  823. * We prioritize the last cpu that the task executed on since
  824. * it is most likely cache-hot in that location.
  825. */
  826. if (cpu_isset(cpu, *lowest_mask))
  827. return cpu;
  828. /*
  829. * Otherwise, we consult the sched_domains span maps to figure
  830. * out which cpu is logically closest to our hot cache data.
  831. */
  832. if (this_cpu == cpu)
  833. this_cpu = -1; /* Skip this_cpu opt if the same */
  834. for_each_domain(cpu, sd) {
  835. if (sd->flags & SD_WAKE_AFFINE) {
  836. cpumask_t domain_mask;
  837. int best_cpu;
  838. cpumask_and(&domain_mask, sched_domain_span(sd),
  839. lowest_mask);
  840. best_cpu = pick_optimal_cpu(this_cpu,
  841. &domain_mask);
  842. if (best_cpu != -1)
  843. return best_cpu;
  844. }
  845. }
  846. /*
  847. * And finally, if there were no matches within the domains
  848. * just give the caller *something* to work with from the compatible
  849. * locations.
  850. */
  851. return pick_optimal_cpu(this_cpu, lowest_mask);
  852. }
  853. /* Will lock the rq it finds */
  854. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  855. {
  856. struct rq *lowest_rq = NULL;
  857. int tries;
  858. int cpu;
  859. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  860. cpu = find_lowest_rq(task);
  861. if ((cpu == -1) || (cpu == rq->cpu))
  862. break;
  863. lowest_rq = cpu_rq(cpu);
  864. /* if the prio of this runqueue changed, try again */
  865. if (double_lock_balance(rq, lowest_rq)) {
  866. /*
  867. * We had to unlock the run queue. In
  868. * the mean time, task could have
  869. * migrated already or had its affinity changed.
  870. * Also make sure that it wasn't scheduled on its rq.
  871. */
  872. if (unlikely(task_rq(task) != rq ||
  873. !cpu_isset(lowest_rq->cpu,
  874. task->cpus_allowed) ||
  875. task_running(rq, task) ||
  876. !task->se.on_rq)) {
  877. spin_unlock(&lowest_rq->lock);
  878. lowest_rq = NULL;
  879. break;
  880. }
  881. }
  882. /* If this rq is still suitable use it. */
  883. if (lowest_rq->rt.highest_prio > task->prio)
  884. break;
  885. /* try again */
  886. double_unlock_balance(rq, lowest_rq);
  887. lowest_rq = NULL;
  888. }
  889. return lowest_rq;
  890. }
  891. /*
  892. * If the current CPU has more than one RT task, see if the non
  893. * running task can migrate over to a CPU that is running a task
  894. * of lesser priority.
  895. */
  896. static int push_rt_task(struct rq *rq)
  897. {
  898. struct task_struct *next_task;
  899. struct rq *lowest_rq;
  900. int ret = 0;
  901. int paranoid = RT_MAX_TRIES;
  902. if (!rq->rt.overloaded)
  903. return 0;
  904. next_task = pick_next_highest_task_rt(rq, -1);
  905. if (!next_task)
  906. return 0;
  907. retry:
  908. if (unlikely(next_task == rq->curr)) {
  909. WARN_ON(1);
  910. return 0;
  911. }
  912. /*
  913. * It's possible that the next_task slipped in of
  914. * higher priority than current. If that's the case
  915. * just reschedule current.
  916. */
  917. if (unlikely(next_task->prio < rq->curr->prio)) {
  918. resched_task(rq->curr);
  919. return 0;
  920. }
  921. /* We might release rq lock */
  922. get_task_struct(next_task);
  923. /* find_lock_lowest_rq locks the rq if found */
  924. lowest_rq = find_lock_lowest_rq(next_task, rq);
  925. if (!lowest_rq) {
  926. struct task_struct *task;
  927. /*
  928. * find lock_lowest_rq releases rq->lock
  929. * so it is possible that next_task has changed.
  930. * If it has, then try again.
  931. */
  932. task = pick_next_highest_task_rt(rq, -1);
  933. if (unlikely(task != next_task) && task && paranoid--) {
  934. put_task_struct(next_task);
  935. next_task = task;
  936. goto retry;
  937. }
  938. goto out;
  939. }
  940. deactivate_task(rq, next_task, 0);
  941. set_task_cpu(next_task, lowest_rq->cpu);
  942. activate_task(lowest_rq, next_task, 0);
  943. resched_task(lowest_rq->curr);
  944. double_unlock_balance(rq, lowest_rq);
  945. ret = 1;
  946. out:
  947. put_task_struct(next_task);
  948. return ret;
  949. }
  950. /*
  951. * TODO: Currently we just use the second highest prio task on
  952. * the queue, and stop when it can't migrate (or there's
  953. * no more RT tasks). There may be a case where a lower
  954. * priority RT task has a different affinity than the
  955. * higher RT task. In this case the lower RT task could
  956. * possibly be able to migrate where as the higher priority
  957. * RT task could not. We currently ignore this issue.
  958. * Enhancements are welcome!
  959. */
  960. static void push_rt_tasks(struct rq *rq)
  961. {
  962. /* push_rt_task will return true if it moved an RT */
  963. while (push_rt_task(rq))
  964. ;
  965. }
  966. static int pull_rt_task(struct rq *this_rq)
  967. {
  968. int this_cpu = this_rq->cpu, ret = 0, cpu;
  969. struct task_struct *p, *next;
  970. struct rq *src_rq;
  971. if (likely(!rt_overloaded(this_rq)))
  972. return 0;
  973. next = pick_next_task_rt(this_rq);
  974. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  975. if (this_cpu == cpu)
  976. continue;
  977. src_rq = cpu_rq(cpu);
  978. /*
  979. * We can potentially drop this_rq's lock in
  980. * double_lock_balance, and another CPU could
  981. * steal our next task - hence we must cause
  982. * the caller to recalculate the next task
  983. * in that case:
  984. */
  985. if (double_lock_balance(this_rq, src_rq)) {
  986. struct task_struct *old_next = next;
  987. next = pick_next_task_rt(this_rq);
  988. if (next != old_next)
  989. ret = 1;
  990. }
  991. /*
  992. * Are there still pullable RT tasks?
  993. */
  994. if (src_rq->rt.rt_nr_running <= 1)
  995. goto skip;
  996. p = pick_next_highest_task_rt(src_rq, this_cpu);
  997. /*
  998. * Do we have an RT task that preempts
  999. * the to-be-scheduled task?
  1000. */
  1001. if (p && (!next || (p->prio < next->prio))) {
  1002. WARN_ON(p == src_rq->curr);
  1003. WARN_ON(!p->se.on_rq);
  1004. /*
  1005. * There's a chance that p is higher in priority
  1006. * than what's currently running on its cpu.
  1007. * This is just that p is wakeing up and hasn't
  1008. * had a chance to schedule. We only pull
  1009. * p if it is lower in priority than the
  1010. * current task on the run queue or
  1011. * this_rq next task is lower in prio than
  1012. * the current task on that rq.
  1013. */
  1014. if (p->prio < src_rq->curr->prio ||
  1015. (next && next->prio < src_rq->curr->prio))
  1016. goto skip;
  1017. ret = 1;
  1018. deactivate_task(src_rq, p, 0);
  1019. set_task_cpu(p, this_cpu);
  1020. activate_task(this_rq, p, 0);
  1021. /*
  1022. * We continue with the search, just in
  1023. * case there's an even higher prio task
  1024. * in another runqueue. (low likelyhood
  1025. * but possible)
  1026. *
  1027. * Update next so that we won't pick a task
  1028. * on another cpu with a priority lower (or equal)
  1029. * than the one we just picked.
  1030. */
  1031. next = p;
  1032. }
  1033. skip:
  1034. double_unlock_balance(this_rq, src_rq);
  1035. }
  1036. return ret;
  1037. }
  1038. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  1039. {
  1040. /* Try to pull RT tasks here if we lower this rq's prio */
  1041. if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
  1042. pull_rt_task(rq);
  1043. }
  1044. static void post_schedule_rt(struct rq *rq)
  1045. {
  1046. /*
  1047. * If we have more than one rt_task queued, then
  1048. * see if we can push the other rt_tasks off to other CPUS.
  1049. * Note we may release the rq lock, and since
  1050. * the lock was owned by prev, we need to release it
  1051. * first via finish_lock_switch and then reaquire it here.
  1052. */
  1053. if (unlikely(rq->rt.overloaded)) {
  1054. spin_lock_irq(&rq->lock);
  1055. push_rt_tasks(rq);
  1056. spin_unlock_irq(&rq->lock);
  1057. }
  1058. }
  1059. /*
  1060. * If we are not running and we are not going to reschedule soon, we should
  1061. * try to push tasks away now
  1062. */
  1063. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  1064. {
  1065. if (!task_running(rq, p) &&
  1066. !test_tsk_need_resched(rq->curr) &&
  1067. rq->rt.overloaded)
  1068. push_rt_tasks(rq);
  1069. }
  1070. static unsigned long
  1071. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1072. unsigned long max_load_move,
  1073. struct sched_domain *sd, enum cpu_idle_type idle,
  1074. int *all_pinned, int *this_best_prio)
  1075. {
  1076. /* don't touch RT tasks */
  1077. return 0;
  1078. }
  1079. static int
  1080. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1081. struct sched_domain *sd, enum cpu_idle_type idle)
  1082. {
  1083. /* don't touch RT tasks */
  1084. return 0;
  1085. }
  1086. static void set_cpus_allowed_rt(struct task_struct *p,
  1087. const cpumask_t *new_mask)
  1088. {
  1089. int weight = cpus_weight(*new_mask);
  1090. BUG_ON(!rt_task(p));
  1091. /*
  1092. * Update the migration status of the RQ if we have an RT task
  1093. * which is running AND changing its weight value.
  1094. */
  1095. if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
  1096. struct rq *rq = task_rq(p);
  1097. if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
  1098. rq->rt.rt_nr_migratory++;
  1099. } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
  1100. BUG_ON(!rq->rt.rt_nr_migratory);
  1101. rq->rt.rt_nr_migratory--;
  1102. }
  1103. update_rt_migration(rq);
  1104. }
  1105. p->cpus_allowed = *new_mask;
  1106. p->rt.nr_cpus_allowed = weight;
  1107. }
  1108. /* Assumes rq->lock is held */
  1109. static void rq_online_rt(struct rq *rq)
  1110. {
  1111. if (rq->rt.overloaded)
  1112. rt_set_overload(rq);
  1113. __enable_runtime(rq);
  1114. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio);
  1115. }
  1116. /* Assumes rq->lock is held */
  1117. static void rq_offline_rt(struct rq *rq)
  1118. {
  1119. if (rq->rt.overloaded)
  1120. rt_clear_overload(rq);
  1121. __disable_runtime(rq);
  1122. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1123. }
  1124. /*
  1125. * When switch from the rt queue, we bring ourselves to a position
  1126. * that we might want to pull RT tasks from other runqueues.
  1127. */
  1128. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  1129. int running)
  1130. {
  1131. /*
  1132. * If there are other RT tasks then we will reschedule
  1133. * and the scheduling of the other RT tasks will handle
  1134. * the balancing. But if we are the last RT task
  1135. * we may need to handle the pulling of RT tasks
  1136. * now.
  1137. */
  1138. if (!rq->rt.rt_nr_running)
  1139. pull_rt_task(rq);
  1140. }
  1141. #endif /* CONFIG_SMP */
  1142. /*
  1143. * When switching a task to RT, we may overload the runqueue
  1144. * with RT tasks. In this case we try to push them off to
  1145. * other runqueues.
  1146. */
  1147. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  1148. int running)
  1149. {
  1150. int check_resched = 1;
  1151. /*
  1152. * If we are already running, then there's nothing
  1153. * that needs to be done. But if we are not running
  1154. * we may need to preempt the current running task.
  1155. * If that current running task is also an RT task
  1156. * then see if we can move to another run queue.
  1157. */
  1158. if (!running) {
  1159. #ifdef CONFIG_SMP
  1160. if (rq->rt.overloaded && push_rt_task(rq) &&
  1161. /* Don't resched if we changed runqueues */
  1162. rq != task_rq(p))
  1163. check_resched = 0;
  1164. #endif /* CONFIG_SMP */
  1165. if (check_resched && p->prio < rq->curr->prio)
  1166. resched_task(rq->curr);
  1167. }
  1168. }
  1169. /*
  1170. * Priority of the task has changed. This may cause
  1171. * us to initiate a push or pull.
  1172. */
  1173. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  1174. int oldprio, int running)
  1175. {
  1176. if (running) {
  1177. #ifdef CONFIG_SMP
  1178. /*
  1179. * If our priority decreases while running, we
  1180. * may need to pull tasks to this runqueue.
  1181. */
  1182. if (oldprio < p->prio)
  1183. pull_rt_task(rq);
  1184. /*
  1185. * If there's a higher priority task waiting to run
  1186. * then reschedule. Note, the above pull_rt_task
  1187. * can release the rq lock and p could migrate.
  1188. * Only reschedule if p is still on the same runqueue.
  1189. */
  1190. if (p->prio > rq->rt.highest_prio && rq->curr == p)
  1191. resched_task(p);
  1192. #else
  1193. /* For UP simply resched on drop of prio */
  1194. if (oldprio < p->prio)
  1195. resched_task(p);
  1196. #endif /* CONFIG_SMP */
  1197. } else {
  1198. /*
  1199. * This task is not running, but if it is
  1200. * greater than the current running task
  1201. * then reschedule.
  1202. */
  1203. if (p->prio < rq->curr->prio)
  1204. resched_task(rq->curr);
  1205. }
  1206. }
  1207. static void watchdog(struct rq *rq, struct task_struct *p)
  1208. {
  1209. unsigned long soft, hard;
  1210. if (!p->signal)
  1211. return;
  1212. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  1213. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  1214. if (soft != RLIM_INFINITY) {
  1215. unsigned long next;
  1216. p->rt.timeout++;
  1217. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1218. if (p->rt.timeout > next)
  1219. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1220. }
  1221. }
  1222. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1223. {
  1224. update_curr_rt(rq);
  1225. watchdog(rq, p);
  1226. /*
  1227. * RR tasks need a special form of timeslice management.
  1228. * FIFO tasks have no timeslices.
  1229. */
  1230. if (p->policy != SCHED_RR)
  1231. return;
  1232. if (--p->rt.time_slice)
  1233. return;
  1234. p->rt.time_slice = DEF_TIMESLICE;
  1235. /*
  1236. * Requeue to the end of queue if we are not the only element
  1237. * on the queue:
  1238. */
  1239. if (p->rt.run_list.prev != p->rt.run_list.next) {
  1240. requeue_task_rt(rq, p, 0);
  1241. set_tsk_need_resched(p);
  1242. }
  1243. }
  1244. static void set_curr_task_rt(struct rq *rq)
  1245. {
  1246. struct task_struct *p = rq->curr;
  1247. p->se.exec_start = rq->clock;
  1248. }
  1249. static const struct sched_class rt_sched_class = {
  1250. .next = &fair_sched_class,
  1251. .enqueue_task = enqueue_task_rt,
  1252. .dequeue_task = dequeue_task_rt,
  1253. .yield_task = yield_task_rt,
  1254. .check_preempt_curr = check_preempt_curr_rt,
  1255. .pick_next_task = pick_next_task_rt,
  1256. .put_prev_task = put_prev_task_rt,
  1257. #ifdef CONFIG_SMP
  1258. .select_task_rq = select_task_rq_rt,
  1259. .load_balance = load_balance_rt,
  1260. .move_one_task = move_one_task_rt,
  1261. .set_cpus_allowed = set_cpus_allowed_rt,
  1262. .rq_online = rq_online_rt,
  1263. .rq_offline = rq_offline_rt,
  1264. .pre_schedule = pre_schedule_rt,
  1265. .post_schedule = post_schedule_rt,
  1266. .task_wake_up = task_wake_up_rt,
  1267. .switched_from = switched_from_rt,
  1268. #endif
  1269. .set_curr_task = set_curr_task_rt,
  1270. .task_tick = task_tick_rt,
  1271. .prio_changed = prio_changed_rt,
  1272. .switched_to = switched_to_rt,
  1273. };
  1274. #ifdef CONFIG_SCHED_DEBUG
  1275. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1276. static void print_rt_stats(struct seq_file *m, int cpu)
  1277. {
  1278. struct rt_rq *rt_rq;
  1279. rcu_read_lock();
  1280. for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu))
  1281. print_rt_rq(m, cpu, rt_rq);
  1282. rcu_read_unlock();
  1283. }
  1284. #endif /* CONFIG_SCHED_DEBUG */