sched_rt.c 34 KB

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