sched_rt.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. cpu_set(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. cpu_clear(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 cpumask_t sched_rt_period_mask(void)
  115. {
  116. return cpu_rq(smp_processor_id())->rd->span;
  117. }
  118. #else
  119. static inline cpumask_t sched_rt_period_mask(void)
  120. {
  121. return cpu_online_map;
  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 cpumask_t sched_rt_period_mask(void)
  173. {
  174. return cpu_online_map;
  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 = cpus_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_mask_nr(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_mask(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. cpumask_t 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_mask(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. curr->se.exec_start = rq->clock;
  435. cpuacct_charge(curr, delta_exec);
  436. if (!rt_bandwidth_enabled())
  437. return;
  438. for_each_sched_rt_entity(rt_se) {
  439. rt_rq = rt_rq_of_se(rt_se);
  440. spin_lock(&rt_rq->rt_runtime_lock);
  441. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  442. rt_rq->rt_time += delta_exec;
  443. if (sched_rt_runtime_exceeded(rt_rq))
  444. resched_task(curr);
  445. }
  446. spin_unlock(&rt_rq->rt_runtime_lock);
  447. }
  448. }
  449. static inline
  450. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  451. {
  452. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  453. rt_rq->rt_nr_running++;
  454. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  455. if (rt_se_prio(rt_se) < rt_rq->highest_prio) {
  456. #ifdef CONFIG_SMP
  457. struct rq *rq = rq_of_rt_rq(rt_rq);
  458. #endif
  459. rt_rq->highest_prio = rt_se_prio(rt_se);
  460. #ifdef CONFIG_SMP
  461. if (rq->online)
  462. cpupri_set(&rq->rd->cpupri, rq->cpu,
  463. rt_se_prio(rt_se));
  464. #endif
  465. }
  466. #endif
  467. #ifdef CONFIG_SMP
  468. if (rt_se->nr_cpus_allowed > 1) {
  469. struct rq *rq = rq_of_rt_rq(rt_rq);
  470. rq->rt.rt_nr_migratory++;
  471. }
  472. update_rt_migration(rq_of_rt_rq(rt_rq));
  473. #endif
  474. #ifdef CONFIG_RT_GROUP_SCHED
  475. if (rt_se_boosted(rt_se))
  476. rt_rq->rt_nr_boosted++;
  477. if (rt_rq->tg)
  478. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  479. #else
  480. start_rt_bandwidth(&def_rt_bandwidth);
  481. #endif
  482. }
  483. static inline
  484. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  485. {
  486. #ifdef CONFIG_SMP
  487. int highest_prio = rt_rq->highest_prio;
  488. #endif
  489. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  490. WARN_ON(!rt_rq->rt_nr_running);
  491. rt_rq->rt_nr_running--;
  492. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  493. if (rt_rq->rt_nr_running) {
  494. struct rt_prio_array *array;
  495. WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
  496. if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
  497. /* recalculate */
  498. array = &rt_rq->active;
  499. rt_rq->highest_prio =
  500. sched_find_first_bit(array->bitmap);
  501. } /* otherwise leave rq->highest prio alone */
  502. } else
  503. rt_rq->highest_prio = MAX_RT_PRIO;
  504. #endif
  505. #ifdef CONFIG_SMP
  506. if (rt_se->nr_cpus_allowed > 1) {
  507. struct rq *rq = rq_of_rt_rq(rt_rq);
  508. rq->rt.rt_nr_migratory--;
  509. }
  510. if (rt_rq->highest_prio != highest_prio) {
  511. struct rq *rq = rq_of_rt_rq(rt_rq);
  512. if (rq->online)
  513. cpupri_set(&rq->rd->cpupri, rq->cpu,
  514. rt_rq->highest_prio);
  515. }
  516. update_rt_migration(rq_of_rt_rq(rt_rq));
  517. #endif /* CONFIG_SMP */
  518. #ifdef CONFIG_RT_GROUP_SCHED
  519. if (rt_se_boosted(rt_se))
  520. rt_rq->rt_nr_boosted--;
  521. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  522. #endif
  523. }
  524. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
  525. {
  526. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  527. struct rt_prio_array *array = &rt_rq->active;
  528. struct rt_rq *group_rq = group_rt_rq(rt_se);
  529. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  530. /*
  531. * Don't enqueue the group if its throttled, or when empty.
  532. * The latter is a consequence of the former when a child group
  533. * get throttled and the current group doesn't have any other
  534. * active members.
  535. */
  536. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  537. return;
  538. list_add_tail(&rt_se->run_list, queue);
  539. __set_bit(rt_se_prio(rt_se), array->bitmap);
  540. inc_rt_tasks(rt_se, rt_rq);
  541. }
  542. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  543. {
  544. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  545. struct rt_prio_array *array = &rt_rq->active;
  546. list_del_init(&rt_se->run_list);
  547. if (list_empty(array->queue + rt_se_prio(rt_se)))
  548. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  549. dec_rt_tasks(rt_se, rt_rq);
  550. }
  551. /*
  552. * Because the prio of an upper entry depends on the lower
  553. * entries, we must remove entries top - down.
  554. */
  555. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  556. {
  557. struct sched_rt_entity *back = NULL;
  558. for_each_sched_rt_entity(rt_se) {
  559. rt_se->back = back;
  560. back = rt_se;
  561. }
  562. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  563. if (on_rt_rq(rt_se))
  564. __dequeue_rt_entity(rt_se);
  565. }
  566. }
  567. static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
  568. {
  569. dequeue_rt_stack(rt_se);
  570. for_each_sched_rt_entity(rt_se)
  571. __enqueue_rt_entity(rt_se);
  572. }
  573. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  574. {
  575. dequeue_rt_stack(rt_se);
  576. for_each_sched_rt_entity(rt_se) {
  577. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  578. if (rt_rq && rt_rq->rt_nr_running)
  579. __enqueue_rt_entity(rt_se);
  580. }
  581. }
  582. /*
  583. * Adding/removing a task to/from a priority array:
  584. */
  585. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  586. {
  587. struct sched_rt_entity *rt_se = &p->rt;
  588. if (wakeup)
  589. rt_se->timeout = 0;
  590. enqueue_rt_entity(rt_se);
  591. inc_cpu_load(rq, p->se.load.weight);
  592. }
  593. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  594. {
  595. struct sched_rt_entity *rt_se = &p->rt;
  596. update_curr_rt(rq);
  597. dequeue_rt_entity(rt_se);
  598. dec_cpu_load(rq, p->se.load.weight);
  599. }
  600. /*
  601. * Put task to the end of the run list without the overhead of dequeue
  602. * followed by enqueue.
  603. */
  604. static void
  605. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  606. {
  607. if (on_rt_rq(rt_se)) {
  608. struct rt_prio_array *array = &rt_rq->active;
  609. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  610. if (head)
  611. list_move(&rt_se->run_list, queue);
  612. else
  613. list_move_tail(&rt_se->run_list, queue);
  614. }
  615. }
  616. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  617. {
  618. struct sched_rt_entity *rt_se = &p->rt;
  619. struct rt_rq *rt_rq;
  620. for_each_sched_rt_entity(rt_se) {
  621. rt_rq = rt_rq_of_se(rt_se);
  622. requeue_rt_entity(rt_rq, rt_se, head);
  623. }
  624. }
  625. static void yield_task_rt(struct rq *rq)
  626. {
  627. requeue_task_rt(rq, rq->curr, 0);
  628. }
  629. #ifdef CONFIG_SMP
  630. static int find_lowest_rq(struct task_struct *task);
  631. static int select_task_rq_rt(struct task_struct *p, int sync)
  632. {
  633. struct rq *rq = task_rq(p);
  634. /*
  635. * If the current task is an RT task, then
  636. * try to see if we can wake this RT task up on another
  637. * runqueue. Otherwise simply start this RT task
  638. * on its current runqueue.
  639. *
  640. * We want to avoid overloading runqueues. Even if
  641. * the RT task is of higher priority than the current RT task.
  642. * RT tasks behave differently than other tasks. If
  643. * one gets preempted, we try to push it off to another queue.
  644. * So trying to keep a preempting RT task on the same
  645. * cache hot CPU will force the running RT task to
  646. * a cold CPU. So we waste all the cache for the lower
  647. * RT task in hopes of saving some of a RT task
  648. * that is just being woken and probably will have
  649. * cold cache anyway.
  650. */
  651. if (unlikely(rt_task(rq->curr)) &&
  652. (p->rt.nr_cpus_allowed > 1)) {
  653. int cpu = find_lowest_rq(p);
  654. return (cpu == -1) ? task_cpu(p) : cpu;
  655. }
  656. /*
  657. * Otherwise, just let it ride on the affined RQ and the
  658. * post-schedule router will push the preempted task away
  659. */
  660. return task_cpu(p);
  661. }
  662. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  663. {
  664. cpumask_t mask;
  665. if (rq->curr->rt.nr_cpus_allowed == 1)
  666. return;
  667. if (p->rt.nr_cpus_allowed != 1
  668. && cpupri_find(&rq->rd->cpupri, p, &mask))
  669. return;
  670. if (!cpupri_find(&rq->rd->cpupri, rq->curr, &mask))
  671. return;
  672. /*
  673. * There appears to be other cpus that can accept
  674. * current and none to run 'p', so lets reschedule
  675. * to try and push current away:
  676. */
  677. requeue_task_rt(rq, p, 1);
  678. resched_task(rq->curr);
  679. }
  680. #endif /* CONFIG_SMP */
  681. /*
  682. * Preempt the current task with a newly woken task if needed:
  683. */
  684. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync)
  685. {
  686. if (p->prio < rq->curr->prio) {
  687. resched_task(rq->curr);
  688. return;
  689. }
  690. #ifdef CONFIG_SMP
  691. /*
  692. * If:
  693. *
  694. * - the newly woken task is of equal priority to the current task
  695. * - the newly woken task is non-migratable while current is migratable
  696. * - current will be preempted on the next reschedule
  697. *
  698. * we should check to see if current can readily move to a different
  699. * cpu. If so, we will reschedule to allow the push logic to try
  700. * to move current somewhere else, making room for our non-migratable
  701. * task.
  702. */
  703. if (p->prio == rq->curr->prio && !need_resched())
  704. check_preempt_equal_prio(rq, p);
  705. #endif
  706. }
  707. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  708. struct rt_rq *rt_rq)
  709. {
  710. struct rt_prio_array *array = &rt_rq->active;
  711. struct sched_rt_entity *next = NULL;
  712. struct list_head *queue;
  713. int idx;
  714. idx = sched_find_first_bit(array->bitmap);
  715. BUG_ON(idx >= MAX_RT_PRIO);
  716. queue = array->queue + idx;
  717. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  718. return next;
  719. }
  720. static struct task_struct *pick_next_task_rt(struct rq *rq)
  721. {
  722. struct sched_rt_entity *rt_se;
  723. struct task_struct *p;
  724. struct rt_rq *rt_rq;
  725. rt_rq = &rq->rt;
  726. if (unlikely(!rt_rq->rt_nr_running))
  727. return NULL;
  728. if (rt_rq_throttled(rt_rq))
  729. return NULL;
  730. do {
  731. rt_se = pick_next_rt_entity(rq, rt_rq);
  732. BUG_ON(!rt_se);
  733. rt_rq = group_rt_rq(rt_se);
  734. } while (rt_rq);
  735. p = rt_task_of(rt_se);
  736. p->se.exec_start = rq->clock;
  737. return p;
  738. }
  739. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  740. {
  741. update_curr_rt(rq);
  742. p->se.exec_start = 0;
  743. }
  744. #ifdef CONFIG_SMP
  745. /* Only try algorithms three times */
  746. #define RT_MAX_TRIES 3
  747. static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
  748. static void double_unlock_balance(struct rq *this_rq, struct rq *busiest);
  749. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  750. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  751. {
  752. if (!task_running(rq, p) &&
  753. (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
  754. (p->rt.nr_cpus_allowed > 1))
  755. return 1;
  756. return 0;
  757. }
  758. /* Return the second highest RT task, NULL otherwise */
  759. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  760. {
  761. struct task_struct *next = NULL;
  762. struct sched_rt_entity *rt_se;
  763. struct rt_prio_array *array;
  764. struct rt_rq *rt_rq;
  765. int idx;
  766. for_each_leaf_rt_rq(rt_rq, rq) {
  767. array = &rt_rq->active;
  768. idx = sched_find_first_bit(array->bitmap);
  769. next_idx:
  770. if (idx >= MAX_RT_PRIO)
  771. continue;
  772. if (next && next->prio < idx)
  773. continue;
  774. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  775. struct task_struct *p = rt_task_of(rt_se);
  776. if (pick_rt_task(rq, p, cpu)) {
  777. next = p;
  778. break;
  779. }
  780. }
  781. if (!next) {
  782. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  783. goto next_idx;
  784. }
  785. }
  786. return next;
  787. }
  788. static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
  789. static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
  790. {
  791. int first;
  792. /* "this_cpu" is cheaper to preempt than a remote processor */
  793. if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
  794. return this_cpu;
  795. first = first_cpu(*mask);
  796. if (first != NR_CPUS)
  797. return first;
  798. return -1;
  799. }
  800. static int find_lowest_rq(struct task_struct *task)
  801. {
  802. struct sched_domain *sd;
  803. cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
  804. int this_cpu = smp_processor_id();
  805. int cpu = task_cpu(task);
  806. if (task->rt.nr_cpus_allowed == 1)
  807. return -1; /* No other targets possible */
  808. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  809. return -1; /* No targets found */
  810. /*
  811. * Only consider CPUs that are usable for migration.
  812. * I guess we might want to change cpupri_find() to ignore those
  813. * in the first place.
  814. */
  815. cpus_and(*lowest_mask, *lowest_mask, cpu_active_map);
  816. /*
  817. * At this point we have built a mask of cpus representing the
  818. * lowest priority tasks in the system. Now we want to elect
  819. * the best one based on our affinity and topology.
  820. *
  821. * We prioritize the last cpu that the task executed on since
  822. * it is most likely cache-hot in that location.
  823. */
  824. if (cpu_isset(cpu, *lowest_mask))
  825. return cpu;
  826. /*
  827. * Otherwise, we consult the sched_domains span maps to figure
  828. * out which cpu is logically closest to our hot cache data.
  829. */
  830. if (this_cpu == cpu)
  831. this_cpu = -1; /* Skip this_cpu opt if the same */
  832. for_each_domain(cpu, sd) {
  833. if (sd->flags & SD_WAKE_AFFINE) {
  834. cpumask_t domain_mask;
  835. int best_cpu;
  836. cpus_and(domain_mask, sd->span, *lowest_mask);
  837. best_cpu = pick_optimal_cpu(this_cpu,
  838. &domain_mask);
  839. if (best_cpu != -1)
  840. return best_cpu;
  841. }
  842. }
  843. /*
  844. * And finally, if there were no matches within the domains
  845. * just give the caller *something* to work with from the compatible
  846. * locations.
  847. */
  848. return pick_optimal_cpu(this_cpu, lowest_mask);
  849. }
  850. /* Will lock the rq it finds */
  851. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  852. {
  853. struct rq *lowest_rq = NULL;
  854. int tries;
  855. int cpu;
  856. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  857. cpu = find_lowest_rq(task);
  858. if ((cpu == -1) || (cpu == rq->cpu))
  859. break;
  860. lowest_rq = cpu_rq(cpu);
  861. /* if the prio of this runqueue changed, try again */
  862. if (double_lock_balance(rq, lowest_rq)) {
  863. /*
  864. * We had to unlock the run queue. In
  865. * the mean time, task could have
  866. * migrated already or had its affinity changed.
  867. * Also make sure that it wasn't scheduled on its rq.
  868. */
  869. if (unlikely(task_rq(task) != rq ||
  870. !cpu_isset(lowest_rq->cpu,
  871. task->cpus_allowed) ||
  872. task_running(rq, task) ||
  873. !task->se.on_rq)) {
  874. spin_unlock(&lowest_rq->lock);
  875. lowest_rq = NULL;
  876. break;
  877. }
  878. }
  879. /* If this rq is still suitable use it. */
  880. if (lowest_rq->rt.highest_prio > task->prio)
  881. break;
  882. /* try again */
  883. double_unlock_balance(rq, lowest_rq);
  884. lowest_rq = NULL;
  885. }
  886. return lowest_rq;
  887. }
  888. /*
  889. * If the current CPU has more than one RT task, see if the non
  890. * running task can migrate over to a CPU that is running a task
  891. * of lesser priority.
  892. */
  893. static int push_rt_task(struct rq *rq)
  894. {
  895. struct task_struct *next_task;
  896. struct rq *lowest_rq;
  897. int ret = 0;
  898. int paranoid = RT_MAX_TRIES;
  899. if (!rq->rt.overloaded)
  900. return 0;
  901. next_task = pick_next_highest_task_rt(rq, -1);
  902. if (!next_task)
  903. return 0;
  904. retry:
  905. if (unlikely(next_task == rq->curr)) {
  906. WARN_ON(1);
  907. return 0;
  908. }
  909. /*
  910. * It's possible that the next_task slipped in of
  911. * higher priority than current. If that's the case
  912. * just reschedule current.
  913. */
  914. if (unlikely(next_task->prio < rq->curr->prio)) {
  915. resched_task(rq->curr);
  916. return 0;
  917. }
  918. /* We might release rq lock */
  919. get_task_struct(next_task);
  920. /* find_lock_lowest_rq locks the rq if found */
  921. lowest_rq = find_lock_lowest_rq(next_task, rq);
  922. if (!lowest_rq) {
  923. struct task_struct *task;
  924. /*
  925. * find lock_lowest_rq releases rq->lock
  926. * so it is possible that next_task has changed.
  927. * If it has, then try again.
  928. */
  929. task = pick_next_highest_task_rt(rq, -1);
  930. if (unlikely(task != next_task) && task && paranoid--) {
  931. put_task_struct(next_task);
  932. next_task = task;
  933. goto retry;
  934. }
  935. goto out;
  936. }
  937. deactivate_task(rq, next_task, 0);
  938. set_task_cpu(next_task, lowest_rq->cpu);
  939. activate_task(lowest_rq, next_task, 0);
  940. resched_task(lowest_rq->curr);
  941. double_unlock_balance(rq, lowest_rq);
  942. ret = 1;
  943. out:
  944. put_task_struct(next_task);
  945. return ret;
  946. }
  947. /*
  948. * TODO: Currently we just use the second highest prio task on
  949. * the queue, and stop when it can't migrate (or there's
  950. * no more RT tasks). There may be a case where a lower
  951. * priority RT task has a different affinity than the
  952. * higher RT task. In this case the lower RT task could
  953. * possibly be able to migrate where as the higher priority
  954. * RT task could not. We currently ignore this issue.
  955. * Enhancements are welcome!
  956. */
  957. static void push_rt_tasks(struct rq *rq)
  958. {
  959. /* push_rt_task will return true if it moved an RT */
  960. while (push_rt_task(rq))
  961. ;
  962. }
  963. static int pull_rt_task(struct rq *this_rq)
  964. {
  965. int this_cpu = this_rq->cpu, ret = 0, cpu;
  966. struct task_struct *p, *next;
  967. struct rq *src_rq;
  968. if (likely(!rt_overloaded(this_rq)))
  969. return 0;
  970. next = pick_next_task_rt(this_rq);
  971. for_each_cpu_mask_nr(cpu, this_rq->rd->rto_mask) {
  972. if (this_cpu == cpu)
  973. continue;
  974. src_rq = cpu_rq(cpu);
  975. /*
  976. * We can potentially drop this_rq's lock in
  977. * double_lock_balance, and another CPU could
  978. * steal our next task - hence we must cause
  979. * the caller to recalculate the next task
  980. * in that case:
  981. */
  982. if (double_lock_balance(this_rq, src_rq)) {
  983. struct task_struct *old_next = next;
  984. next = pick_next_task_rt(this_rq);
  985. if (next != old_next)
  986. ret = 1;
  987. }
  988. /*
  989. * Are there still pullable RT tasks?
  990. */
  991. if (src_rq->rt.rt_nr_running <= 1)
  992. goto skip;
  993. p = pick_next_highest_task_rt(src_rq, this_cpu);
  994. /*
  995. * Do we have an RT task that preempts
  996. * the to-be-scheduled task?
  997. */
  998. if (p && (!next || (p->prio < next->prio))) {
  999. WARN_ON(p == src_rq->curr);
  1000. WARN_ON(!p->se.on_rq);
  1001. /*
  1002. * There's a chance that p is higher in priority
  1003. * than what's currently running on its cpu.
  1004. * This is just that p is wakeing up and hasn't
  1005. * had a chance to schedule. We only pull
  1006. * p if it is lower in priority than the
  1007. * current task on the run queue or
  1008. * this_rq next task is lower in prio than
  1009. * the current task on that rq.
  1010. */
  1011. if (p->prio < src_rq->curr->prio ||
  1012. (next && next->prio < src_rq->curr->prio))
  1013. goto skip;
  1014. ret = 1;
  1015. deactivate_task(src_rq, p, 0);
  1016. set_task_cpu(p, this_cpu);
  1017. activate_task(this_rq, p, 0);
  1018. /*
  1019. * We continue with the search, just in
  1020. * case there's an even higher prio task
  1021. * in another runqueue. (low likelyhood
  1022. * but possible)
  1023. *
  1024. * Update next so that we won't pick a task
  1025. * on another cpu with a priority lower (or equal)
  1026. * than the one we just picked.
  1027. */
  1028. next = p;
  1029. }
  1030. skip:
  1031. double_unlock_balance(this_rq, src_rq);
  1032. }
  1033. return ret;
  1034. }
  1035. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  1036. {
  1037. /* Try to pull RT tasks here if we lower this rq's prio */
  1038. if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
  1039. pull_rt_task(rq);
  1040. }
  1041. static void post_schedule_rt(struct rq *rq)
  1042. {
  1043. /*
  1044. * If we have more than one rt_task queued, then
  1045. * see if we can push the other rt_tasks off to other CPUS.
  1046. * Note we may release the rq lock, and since
  1047. * the lock was owned by prev, we need to release it
  1048. * first via finish_lock_switch and then reaquire it here.
  1049. */
  1050. if (unlikely(rq->rt.overloaded)) {
  1051. spin_lock_irq(&rq->lock);
  1052. push_rt_tasks(rq);
  1053. spin_unlock_irq(&rq->lock);
  1054. }
  1055. }
  1056. /*
  1057. * If we are not running and we are not going to reschedule soon, we should
  1058. * try to push tasks away now
  1059. */
  1060. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  1061. {
  1062. if (!task_running(rq, p) &&
  1063. !test_tsk_need_resched(rq->curr) &&
  1064. rq->rt.overloaded)
  1065. push_rt_tasks(rq);
  1066. }
  1067. static unsigned long
  1068. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1069. unsigned long max_load_move,
  1070. struct sched_domain *sd, enum cpu_idle_type idle,
  1071. int *all_pinned, int *this_best_prio)
  1072. {
  1073. /* don't touch RT tasks */
  1074. return 0;
  1075. }
  1076. static int
  1077. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1078. struct sched_domain *sd, enum cpu_idle_type idle)
  1079. {
  1080. /* don't touch RT tasks */
  1081. return 0;
  1082. }
  1083. static void set_cpus_allowed_rt(struct task_struct *p,
  1084. const cpumask_t *new_mask)
  1085. {
  1086. int weight = cpus_weight(*new_mask);
  1087. BUG_ON(!rt_task(p));
  1088. /*
  1089. * Update the migration status of the RQ if we have an RT task
  1090. * which is running AND changing its weight value.
  1091. */
  1092. if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
  1093. struct rq *rq = task_rq(p);
  1094. if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
  1095. rq->rt.rt_nr_migratory++;
  1096. } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
  1097. BUG_ON(!rq->rt.rt_nr_migratory);
  1098. rq->rt.rt_nr_migratory--;
  1099. }
  1100. update_rt_migration(rq);
  1101. }
  1102. p->cpus_allowed = *new_mask;
  1103. p->rt.nr_cpus_allowed = weight;
  1104. }
  1105. /* Assumes rq->lock is held */
  1106. static void rq_online_rt(struct rq *rq)
  1107. {
  1108. if (rq->rt.overloaded)
  1109. rt_set_overload(rq);
  1110. __enable_runtime(rq);
  1111. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio);
  1112. }
  1113. /* Assumes rq->lock is held */
  1114. static void rq_offline_rt(struct rq *rq)
  1115. {
  1116. if (rq->rt.overloaded)
  1117. rt_clear_overload(rq);
  1118. __disable_runtime(rq);
  1119. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1120. }
  1121. /*
  1122. * When switch from the rt queue, we bring ourselves to a position
  1123. * that we might want to pull RT tasks from other runqueues.
  1124. */
  1125. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  1126. int running)
  1127. {
  1128. /*
  1129. * If there are other RT tasks then we will reschedule
  1130. * and the scheduling of the other RT tasks will handle
  1131. * the balancing. But if we are the last RT task
  1132. * we may need to handle the pulling of RT tasks
  1133. * now.
  1134. */
  1135. if (!rq->rt.rt_nr_running)
  1136. pull_rt_task(rq);
  1137. }
  1138. #endif /* CONFIG_SMP */
  1139. /*
  1140. * When switching a task to RT, we may overload the runqueue
  1141. * with RT tasks. In this case we try to push them off to
  1142. * other runqueues.
  1143. */
  1144. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  1145. int running)
  1146. {
  1147. int check_resched = 1;
  1148. /*
  1149. * If we are already running, then there's nothing
  1150. * that needs to be done. But if we are not running
  1151. * we may need to preempt the current running task.
  1152. * If that current running task is also an RT task
  1153. * then see if we can move to another run queue.
  1154. */
  1155. if (!running) {
  1156. #ifdef CONFIG_SMP
  1157. if (rq->rt.overloaded && push_rt_task(rq) &&
  1158. /* Don't resched if we changed runqueues */
  1159. rq != task_rq(p))
  1160. check_resched = 0;
  1161. #endif /* CONFIG_SMP */
  1162. if (check_resched && p->prio < rq->curr->prio)
  1163. resched_task(rq->curr);
  1164. }
  1165. }
  1166. /*
  1167. * Priority of the task has changed. This may cause
  1168. * us to initiate a push or pull.
  1169. */
  1170. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  1171. int oldprio, int running)
  1172. {
  1173. if (running) {
  1174. #ifdef CONFIG_SMP
  1175. /*
  1176. * If our priority decreases while running, we
  1177. * may need to pull tasks to this runqueue.
  1178. */
  1179. if (oldprio < p->prio)
  1180. pull_rt_task(rq);
  1181. /*
  1182. * If there's a higher priority task waiting to run
  1183. * then reschedule. Note, the above pull_rt_task
  1184. * can release the rq lock and p could migrate.
  1185. * Only reschedule if p is still on the same runqueue.
  1186. */
  1187. if (p->prio > rq->rt.highest_prio && rq->curr == p)
  1188. resched_task(p);
  1189. #else
  1190. /* For UP simply resched on drop of prio */
  1191. if (oldprio < p->prio)
  1192. resched_task(p);
  1193. #endif /* CONFIG_SMP */
  1194. } else {
  1195. /*
  1196. * This task is not running, but if it is
  1197. * greater than the current running task
  1198. * then reschedule.
  1199. */
  1200. if (p->prio < rq->curr->prio)
  1201. resched_task(rq->curr);
  1202. }
  1203. }
  1204. static void watchdog(struct rq *rq, struct task_struct *p)
  1205. {
  1206. unsigned long soft, hard;
  1207. if (!p->signal)
  1208. return;
  1209. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  1210. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  1211. if (soft != RLIM_INFINITY) {
  1212. unsigned long next;
  1213. p->rt.timeout++;
  1214. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1215. if (p->rt.timeout > next)
  1216. p->it_sched_expires = p->se.sum_exec_runtime;
  1217. }
  1218. }
  1219. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1220. {
  1221. update_curr_rt(rq);
  1222. watchdog(rq, p);
  1223. /*
  1224. * RR tasks need a special form of timeslice management.
  1225. * FIFO tasks have no timeslices.
  1226. */
  1227. if (p->policy != SCHED_RR)
  1228. return;
  1229. if (--p->rt.time_slice)
  1230. return;
  1231. p->rt.time_slice = DEF_TIMESLICE;
  1232. /*
  1233. * Requeue to the end of queue if we are not the only element
  1234. * on the queue:
  1235. */
  1236. if (p->rt.run_list.prev != p->rt.run_list.next) {
  1237. requeue_task_rt(rq, p, 0);
  1238. set_tsk_need_resched(p);
  1239. }
  1240. }
  1241. static void set_curr_task_rt(struct rq *rq)
  1242. {
  1243. struct task_struct *p = rq->curr;
  1244. p->se.exec_start = rq->clock;
  1245. }
  1246. static const struct sched_class rt_sched_class = {
  1247. .next = &fair_sched_class,
  1248. .enqueue_task = enqueue_task_rt,
  1249. .dequeue_task = dequeue_task_rt,
  1250. .yield_task = yield_task_rt,
  1251. #ifdef CONFIG_SMP
  1252. .select_task_rq = select_task_rq_rt,
  1253. #endif /* CONFIG_SMP */
  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. .load_balance = load_balance_rt,
  1259. .move_one_task = move_one_task_rt,
  1260. .set_cpus_allowed = set_cpus_allowed_rt,
  1261. .rq_online = rq_online_rt,
  1262. .rq_offline = rq_offline_rt,
  1263. .pre_schedule = pre_schedule_rt,
  1264. .post_schedule = post_schedule_rt,
  1265. .task_wake_up = task_wake_up_rt,
  1266. .switched_from = switched_from_rt,
  1267. #endif
  1268. .set_curr_task = set_curr_task_rt,
  1269. .task_tick = task_tick_rt,
  1270. .prio_changed = prio_changed_rt,
  1271. .switched_to = switched_to_rt,
  1272. };
  1273. #ifdef CONFIG_SCHED_DEBUG
  1274. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1275. static void print_rt_stats(struct seq_file *m, int cpu)
  1276. {
  1277. struct rt_rq *rt_rq;
  1278. rcu_read_lock();
  1279. for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu))
  1280. print_rt_rq(m, cpu, rt_rq);
  1281. rcu_read_unlock();
  1282. }
  1283. #endif /* CONFIG_SCHED_DEBUG */