sched_rt.c 31 KB

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