sched_rt.c 42 KB

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