rt.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  1. /*
  2. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3. * policies)
  4. */
  5. #include "sched.h"
  6. #if defined(CONFIG_MT_SCHED_TRACE)
  7. #include <trace/events/sched.h>
  8. #endif
  9. #include <linux/slab.h>
  10. #ifdef CONFIG_MTPROF
  11. #include "mt_sched_mon.h"
  12. #endif
  13. int sched_rr_timeslice = RR_TIMESLICE;
  14. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
  15. struct rt_bandwidth def_rt_bandwidth;
  16. static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
  17. {
  18. struct rt_bandwidth *rt_b =
  19. container_of(timer, struct rt_bandwidth, rt_period_timer);
  20. ktime_t now;
  21. int overrun;
  22. int idle = 0;
  23. for (;;) {
  24. now = hrtimer_cb_get_time(timer);
  25. overrun = hrtimer_forward(timer, now, rt_b->rt_period);
  26. if (!overrun)
  27. break;
  28. #ifdef CONFIG_MTPROF
  29. /* mt throttle monitor */
  30. mt_rt_mon_switch(MON_RESET);
  31. mt_rt_mon_switch(MON_START);
  32. #endif
  33. idle = do_sched_rt_period_timer(rt_b, overrun);
  34. }
  35. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  36. }
  37. void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  38. {
  39. rt_b->rt_period = ns_to_ktime(period);
  40. rt_b->rt_runtime = runtime;
  41. raw_spin_lock_init(&rt_b->rt_runtime_lock);
  42. hrtimer_init(&rt_b->rt_period_timer,
  43. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  44. rt_b->rt_period_timer.function = sched_rt_period_timer;
  45. }
  46. static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
  47. {
  48. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  49. return;
  50. if (hrtimer_active(&rt_b->rt_period_timer))
  51. return;
  52. raw_spin_lock(&rt_b->rt_runtime_lock);
  53. start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
  54. raw_spin_unlock(&rt_b->rt_runtime_lock);
  55. }
  56. #ifdef CONFIG_PROVE_LOCKING
  57. DEFINE_RAW_SPINLOCK(rt_rq_runtime_spinlock);
  58. #define MAX_SPIN_KEY 10
  59. DEFINE_PER_CPU(struct lock_class_key, spin_key[MAX_SPIN_KEY]);
  60. DEFINE_PER_CPU(int, spin_key_idx);
  61. #endif
  62. void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
  63. {
  64. struct rt_prio_array *array;
  65. int i;
  66. #ifdef CONFIG_PROVE_LOCKING
  67. int cpu, idx;
  68. #endif
  69. array = &rt_rq->active;
  70. for (i = 0; i < MAX_RT_PRIO; i++) {
  71. INIT_LIST_HEAD(array->queue + i);
  72. __clear_bit(i, array->bitmap);
  73. }
  74. /* delimiter for bitsearch: */
  75. __set_bit(MAX_RT_PRIO, array->bitmap);
  76. #if defined CONFIG_SMP
  77. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  78. rt_rq->highest_prio.next = MAX_RT_PRIO;
  79. rt_rq->rt_nr_migratory = 0;
  80. rt_rq->overloaded = 0;
  81. plist_head_init(&rt_rq->pushable_tasks);
  82. #endif
  83. /* We start is dequeued state, because no RT tasks are queued */
  84. rt_rq->rt_queued = 0;
  85. rt_rq->rt_time = 0;
  86. rt_rq->rt_throttled = 0;
  87. rt_rq->rt_runtime = 0;
  88. #ifdef CONFIG_PROVE_LOCKING
  89. raw_spin_lock(&rt_rq_runtime_spinlock);
  90. cpu = rq->cpu;
  91. idx = per_cpu(spin_key_idx, cpu);
  92. #endif
  93. raw_spin_lock_init(&rt_rq->rt_runtime_lock);
  94. #ifdef CONFIG_PROVE_LOCKING
  95. lockdep_set_class(&rt_rq->rt_runtime_lock, &per_cpu(spin_key[idx], cpu));
  96. per_cpu(spin_key_idx, cpu)++;
  97. BUG_ON(per_cpu(spin_key_idx, cpu) >= MAX_SPIN_KEY);
  98. raw_spin_unlock(&rt_rq_runtime_spinlock);
  99. #endif
  100. }
  101. #ifdef CONFIG_RT_GROUP_SCHED
  102. static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
  103. {
  104. hrtimer_cancel(&rt_b->rt_period_timer);
  105. }
  106. #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
  107. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  108. {
  109. #ifdef CONFIG_SCHED_DEBUG
  110. WARN_ON_ONCE(!rt_entity_is_task(rt_se));
  111. #endif
  112. return container_of(rt_se, struct task_struct, rt);
  113. }
  114. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  115. {
  116. return rt_rq->rq;
  117. }
  118. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  119. {
  120. return rt_se->rt_rq;
  121. }
  122. static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
  123. {
  124. struct rt_rq *rt_rq = rt_se->rt_rq;
  125. return rt_rq->rq;
  126. }
  127. void free_rt_sched_group(struct task_group *tg)
  128. {
  129. int i;
  130. if (tg->rt_se)
  131. destroy_rt_bandwidth(&tg->rt_bandwidth);
  132. for_each_possible_cpu(i) {
  133. if (tg->rt_rq)
  134. kfree(tg->rt_rq[i]);
  135. if (tg->rt_se)
  136. kfree(tg->rt_se[i]);
  137. }
  138. kfree(tg->rt_rq);
  139. kfree(tg->rt_se);
  140. }
  141. void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  142. struct sched_rt_entity *rt_se, int cpu,
  143. struct sched_rt_entity *parent)
  144. {
  145. struct rq *rq = cpu_rq(cpu);
  146. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  147. rt_rq->rt_nr_boosted = 0;
  148. rt_rq->rq = rq;
  149. rt_rq->tg = tg;
  150. tg->rt_rq[cpu] = rt_rq;
  151. tg->rt_se[cpu] = rt_se;
  152. if (!rt_se)
  153. return;
  154. if (!parent)
  155. rt_se->rt_rq = &rq->rt;
  156. else
  157. rt_se->rt_rq = parent->my_q;
  158. rt_se->my_q = rt_rq;
  159. rt_se->parent = parent;
  160. INIT_LIST_HEAD(&rt_se->run_list);
  161. }
  162. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  163. {
  164. struct rt_rq *rt_rq;
  165. struct sched_rt_entity *rt_se;
  166. int i;
  167. tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
  168. if (!tg->rt_rq)
  169. goto err;
  170. tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
  171. if (!tg->rt_se)
  172. goto err;
  173. init_rt_bandwidth(&tg->rt_bandwidth,
  174. ktime_to_ns(def_rt_bandwidth.rt_period), 0);
  175. for_each_possible_cpu(i) {
  176. rt_rq = kzalloc_node(sizeof(struct rt_rq),
  177. GFP_KERNEL, cpu_to_node(i));
  178. if (!rt_rq)
  179. goto err;
  180. rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
  181. GFP_KERNEL, cpu_to_node(i));
  182. if (!rt_se)
  183. goto err_free_rq;
  184. init_rt_rq(rt_rq, cpu_rq(i));
  185. rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
  186. init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
  187. }
  188. return 1;
  189. err_free_rq:
  190. kfree(rt_rq);
  191. err:
  192. return 0;
  193. }
  194. #else /* CONFIG_RT_GROUP_SCHED */
  195. #define rt_entity_is_task(rt_se) (1)
  196. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  197. {
  198. return container_of(rt_se, struct task_struct, rt);
  199. }
  200. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  201. {
  202. return container_of(rt_rq, struct rq, rt);
  203. }
  204. static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
  205. {
  206. struct task_struct *p = rt_task_of(rt_se);
  207. return task_rq(p);
  208. }
  209. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  210. {
  211. struct rq *rq = rq_of_rt_se(rt_se);
  212. return &rq->rt;
  213. }
  214. void free_rt_sched_group(struct task_group *tg) { }
  215. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  216. {
  217. return 1;
  218. }
  219. #endif /* CONFIG_RT_GROUP_SCHED */
  220. #ifdef CONFIG_SMP
  221. static int pull_rt_task(struct rq *this_rq);
  222. static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
  223. {
  224. /* Try to pull RT tasks here if we lower this rq's prio */
  225. return rq->rt.highest_prio.curr > prev->prio;
  226. }
  227. static inline int rt_overloaded(struct rq *rq)
  228. {
  229. return atomic_read(&rq->rd->rto_count);
  230. }
  231. static inline void rt_set_overload(struct rq *rq)
  232. {
  233. if (!rq->online)
  234. return;
  235. cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
  236. /*
  237. * Make sure the mask is visible before we set
  238. * the overload count. That is checked to determine
  239. * if we should look at the mask. It would be a shame
  240. * if we looked at the mask, but the mask was not
  241. * updated yet.
  242. *
  243. * Matched by the barrier in pull_rt_task().
  244. */
  245. smp_wmb();
  246. atomic_inc(&rq->rd->rto_count);
  247. }
  248. static inline void rt_clear_overload(struct rq *rq)
  249. {
  250. if (!rq->online)
  251. return;
  252. /* the order here really doesn't matter */
  253. atomic_dec(&rq->rd->rto_count);
  254. cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
  255. }
  256. static void update_rt_migration(struct rt_rq *rt_rq)
  257. {
  258. if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
  259. if (!rt_rq->overloaded) {
  260. rt_set_overload(rq_of_rt_rq(rt_rq));
  261. rt_rq->overloaded = 1;
  262. }
  263. } else if (rt_rq->overloaded) {
  264. rt_clear_overload(rq_of_rt_rq(rt_rq));
  265. rt_rq->overloaded = 0;
  266. }
  267. }
  268. static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  269. {
  270. struct task_struct *p;
  271. if (!rt_entity_is_task(rt_se))
  272. return;
  273. p = rt_task_of(rt_se);
  274. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  275. rt_rq->rt_nr_total++;
  276. if (p->nr_cpus_allowed > 1)
  277. rt_rq->rt_nr_migratory++;
  278. update_rt_migration(rt_rq);
  279. }
  280. static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  281. {
  282. struct task_struct *p;
  283. if (!rt_entity_is_task(rt_se))
  284. return;
  285. p = rt_task_of(rt_se);
  286. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  287. rt_rq->rt_nr_total--;
  288. if (p->nr_cpus_allowed > 1)
  289. rt_rq->rt_nr_migratory--;
  290. update_rt_migration(rt_rq);
  291. }
  292. static inline int has_pushable_tasks(struct rq *rq)
  293. {
  294. return !plist_head_empty(&rq->rt.pushable_tasks);
  295. }
  296. static inline void set_post_schedule(struct rq *rq)
  297. {
  298. /*
  299. * We detect this state here so that we can avoid taking the RQ
  300. * lock again later if there is no need to push
  301. */
  302. rq->post_schedule = has_pushable_tasks(rq);
  303. }
  304. static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  305. {
  306. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  307. plist_node_init(&p->pushable_tasks, p->prio);
  308. plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
  309. /* Update the highest prio pushable task */
  310. if (p->prio < rq->rt.highest_prio.next)
  311. rq->rt.highest_prio.next = p->prio;
  312. }
  313. static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  314. {
  315. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  316. /* Update the new highest prio pushable task */
  317. if (has_pushable_tasks(rq)) {
  318. p = plist_first_entry(&rq->rt.pushable_tasks,
  319. struct task_struct, pushable_tasks);
  320. rq->rt.highest_prio.next = p->prio;
  321. } else
  322. rq->rt.highest_prio.next = MAX_RT_PRIO;
  323. }
  324. #else
  325. static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  326. {
  327. }
  328. static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  329. {
  330. }
  331. static inline
  332. void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  333. {
  334. }
  335. static inline
  336. void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  337. {
  338. }
  339. static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
  340. {
  341. return false;
  342. }
  343. static inline int pull_rt_task(struct rq *this_rq)
  344. {
  345. return 0;
  346. }
  347. static inline void set_post_schedule(struct rq *rq)
  348. {
  349. }
  350. #endif /* CONFIG_SMP */
  351. static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
  352. static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
  353. static inline int on_rt_rq(struct sched_rt_entity *rt_se)
  354. {
  355. return !list_empty(&rt_se->run_list);
  356. }
  357. #ifdef CONFIG_RT_GROUP_SCHED
  358. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  359. {
  360. if (!rt_rq->tg)
  361. return RUNTIME_INF;
  362. return rt_rq->rt_runtime;
  363. }
  364. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  365. {
  366. return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
  367. }
  368. typedef struct task_group *rt_rq_iter_t;
  369. static inline struct task_group *next_task_group(struct task_group *tg)
  370. {
  371. do {
  372. tg = list_entry_rcu(tg->list.next,
  373. typeof(struct task_group), list);
  374. } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
  375. if (&tg->list == &task_groups)
  376. tg = NULL;
  377. return tg;
  378. }
  379. #define for_each_rt_rq(rt_rq, iter, rq) \
  380. for (iter = container_of(&task_groups, typeof(*iter), list); \
  381. (iter = next_task_group(iter)) && \
  382. (rt_rq = iter->rt_rq[cpu_of(rq)]);)
  383. #define for_each_sched_rt_entity(rt_se) \
  384. for (; rt_se; rt_se = rt_se->parent)
  385. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  386. {
  387. return rt_se->my_q;
  388. }
  389. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
  390. static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
  391. static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  392. {
  393. struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
  394. struct rq *rq = rq_of_rt_rq(rt_rq);
  395. struct sched_rt_entity *rt_se;
  396. int cpu = cpu_of(rq);
  397. rt_se = rt_rq->tg->rt_se[cpu];
  398. if (rt_rq->rt_nr_running) {
  399. if (!rt_se)
  400. enqueue_top_rt_rq(rt_rq);
  401. else if (!on_rt_rq(rt_se))
  402. enqueue_rt_entity(rt_se, false);
  403. if (rt_rq->highest_prio.curr < curr->prio)
  404. resched_curr(rq);
  405. }
  406. }
  407. static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  408. {
  409. struct sched_rt_entity *rt_se;
  410. int cpu = cpu_of(rq_of_rt_rq(rt_rq));
  411. rt_se = rt_rq->tg->rt_se[cpu];
  412. if (!rt_se)
  413. dequeue_top_rt_rq(rt_rq);
  414. else if (on_rt_rq(rt_se))
  415. dequeue_rt_entity(rt_se);
  416. }
  417. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  418. {
  419. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  420. }
  421. static int rt_se_boosted(struct sched_rt_entity *rt_se)
  422. {
  423. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  424. struct task_struct *p;
  425. if (rt_rq)
  426. return !!rt_rq->rt_nr_boosted;
  427. p = rt_task_of(rt_se);
  428. return p->prio != p->normal_prio;
  429. }
  430. #ifdef CONFIG_SMP
  431. static inline const struct cpumask *sched_rt_period_mask(void)
  432. {
  433. return this_rq()->rd->span;
  434. }
  435. #else
  436. static inline const struct cpumask *sched_rt_period_mask(void)
  437. {
  438. return cpu_online_mask;
  439. }
  440. #endif
  441. static inline
  442. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  443. {
  444. return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
  445. }
  446. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  447. {
  448. return &rt_rq->tg->rt_bandwidth;
  449. }
  450. #else /* !CONFIG_RT_GROUP_SCHED */
  451. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  452. {
  453. return rt_rq->rt_runtime;
  454. }
  455. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  456. {
  457. return ktime_to_ns(def_rt_bandwidth.rt_period);
  458. }
  459. typedef struct rt_rq *rt_rq_iter_t;
  460. #define for_each_rt_rq(rt_rq, iter, rq) \
  461. for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
  462. #define for_each_sched_rt_entity(rt_se) \
  463. for (; rt_se; rt_se = NULL)
  464. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  465. {
  466. return NULL;
  467. }
  468. static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  469. {
  470. struct rq *rq = rq_of_rt_rq(rt_rq);
  471. if (!rt_rq->rt_nr_running)
  472. return;
  473. enqueue_top_rt_rq(rt_rq);
  474. resched_curr(rq);
  475. }
  476. static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  477. {
  478. dequeue_top_rt_rq(rt_rq);
  479. }
  480. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  481. {
  482. return rt_rq->rt_throttled;
  483. }
  484. static inline const struct cpumask *sched_rt_period_mask(void)
  485. {
  486. return cpu_online_mask;
  487. }
  488. static inline
  489. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  490. {
  491. return &cpu_rq(cpu)->rt;
  492. }
  493. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  494. {
  495. return &def_rt_bandwidth;
  496. }
  497. #endif /* CONFIG_RT_GROUP_SCHED */
  498. bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
  499. {
  500. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  501. return (hrtimer_active(&rt_b->rt_period_timer) ||
  502. rt_rq->rt_time < rt_b->rt_runtime);
  503. }
  504. #ifdef CONFIG_SMP
  505. /*
  506. * We ran out of runtime, see if we can borrow some from our neighbours.
  507. */
  508. static int do_balance_runtime(struct rt_rq *rt_rq)
  509. {
  510. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  511. struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
  512. int i, weight, more = 0;
  513. u64 rt_period;
  514. weight = cpumask_weight(rd->span);
  515. raw_spin_lock(&rt_b->rt_runtime_lock);
  516. rt_period = ktime_to_ns(rt_b->rt_period);
  517. #ifdef MTK_DEBUG_CGROUP
  518. pr_warn(" do_balance_runtime curr_cpu=%d, dst_cpu=%d, span=%lu\n",
  519. smp_processor_id(), rt_rq->rq->cpu, rd->span->bits[0]);
  520. #endif
  521. for_each_cpu(i, rd->span) {
  522. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  523. s64 diff;
  524. if (iter == rt_rq)
  525. continue;
  526. /* sched: use try lock to prevent deadlock */
  527. /* raw_spin_lock(&iter->rt_runtime_lock); */
  528. #ifdef MTK_DEBUG_CGROUP
  529. pr_warn(" do_balance_runtime get lock cpu=%d\n", i);
  530. #endif
  531. if (!raw_spin_trylock(&iter->rt_runtime_lock)) {
  532. #ifdef MTK_DEBUG_CGROUP
  533. pr_warn(" do_balance_runtime try lock fail cpu=%d\n", i);
  534. #endif
  535. continue;
  536. }
  537. /*
  538. * Either all rqs have inf runtime and there's nothing to steal
  539. * or __disable_runtime() below sets a specific rq to inf to
  540. * indicate its been disabled and disalow stealing.
  541. */
  542. if (iter->rt_runtime == RUNTIME_INF)
  543. goto next;
  544. /*
  545. * From runqueues with spare time, take 1/n part of their
  546. * spare time, but no more than our period.
  547. */
  548. diff = iter->rt_runtime - iter->rt_time;
  549. #ifdef MTK_DEBUG_CGROUP
  550. pr_warn("borrow, dst_cpu=%d, src_cpu=%d, src_cpu2=%d, src_addr=%x, dst_addr=%x,dst->rt_runtime=%llu, src->rt_runtime=%llu, diff=%lld, span=%lu\n",
  551. rt_rq->rq->cpu, i, iter->rq->cpu, iter,
  552. rt_rq, rt_rq->rt_runtime, iter->rt_runtime, diff, rd->span->bits[0]);
  553. #endif
  554. if (diff > 0) {
  555. diff = div_u64((u64)diff, weight);
  556. if (rt_rq->rt_runtime + diff > rt_period)
  557. diff = rt_period - rt_rq->rt_runtime;
  558. iter->rt_runtime -= diff;
  559. rt_rq->rt_runtime += diff;
  560. more = 1;
  561. #ifdef MTK_DEBUG_CGROUP
  562. pr_warn("borrow successfully, dst_cpu=%d, src_cpu=%d, src_cpu2=%d, src_addr=%x, dst_addr=%x,dst->rt_runtime=%llu, src->rt_runtime=%llu, diff=%lld, span=%lu\n",
  563. rt_rq->rq->cpu, i, iter->rq->cpu, iter,
  564. rt_rq, rt_rq->rt_runtime, iter->rt_runtime, diff, rd->span->bits[0]);
  565. #endif
  566. if (rt_rq->rt_runtime == rt_period) {
  567. raw_spin_unlock(&iter->rt_runtime_lock);
  568. break;
  569. }
  570. }
  571. next:
  572. raw_spin_unlock(&iter->rt_runtime_lock);
  573. }
  574. raw_spin_unlock(&rt_b->rt_runtime_lock);
  575. return more;
  576. }
  577. /*
  578. * Ensure this RQ takes back all the runtime it lend to its neighbours.
  579. */
  580. static void __disable_runtime(struct rq *rq)
  581. {
  582. struct root_domain *rd = rq->rd;
  583. rt_rq_iter_t iter;
  584. struct rt_rq *rt_rq;
  585. if (unlikely(!scheduler_running))
  586. return;
  587. for_each_rt_rq(rt_rq, iter, rq) {
  588. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  589. s64 want;
  590. int i;
  591. raw_spin_lock(&rt_b->rt_runtime_lock);
  592. raw_spin_lock(&rt_rq->rt_runtime_lock);
  593. /*
  594. * Either we're all inf and nobody needs to borrow, or we're
  595. * already disabled and thus have nothing to do, or we have
  596. * exactly the right amount of runtime to take out.
  597. */
  598. #ifdef MTK_DEBUG_CGROUP
  599. pr_warn("0. disable_runtime, cpu=%d, rd->span=%lu, rt_rq_addr=%x, rt_rq->rt_runtime=%llu, rt_b->rt_runtime=%llu\n",
  600. rt_rq->rq->cpu, rd->span->bits[0],
  601. rt_rq, rt_rq->rt_runtime, rt_b->rt_runtime);
  602. #endif
  603. if (rt_rq->rt_runtime == RUNTIME_INF ||
  604. rt_rq->rt_runtime == rt_b->rt_runtime)
  605. goto balanced;
  606. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  607. /*
  608. * Calculate the difference between what we started out with
  609. * and what we current have, that's the amount of runtime
  610. * we lend and now have to reclaim.
  611. */
  612. want = rt_b->rt_runtime - rt_rq->rt_runtime;
  613. /*
  614. * Greedy reclaim, take back as much as we can.
  615. */
  616. for_each_cpu(i, rd->span) {
  617. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  618. s64 diff;
  619. #ifdef MTK_DEBUG_CGROUP
  620. pr_warn("0. disable_runtime, cpu=%d,rt_b->rt_runtime=%llu, rt_rq->rt_runtime=%llu, want=%lld, rd->span=%lu\n",
  621. rt_rq->rq->cpu, rt_b->rt_runtime, rt_rq->rt_runtime, want, rd->span->bits[0]);
  622. #endif
  623. /*
  624. * Can't reclaim from ourselves or disabled runqueues.
  625. */
  626. if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) {
  627. #ifdef MTK_DEBUG_CGROUP
  628. pr_warn("1. disable_runtime, cpu=%d, %llu\n",
  629. i, iter->rt_runtime);
  630. #endif
  631. continue;
  632. }
  633. raw_spin_lock(&iter->rt_runtime_lock);
  634. #ifdef MTK_DEBUG_CGROUP
  635. pr_warn("2-1. disable_runtime cpu=%d, want=%lld, iter->rt_runtime=%llu\n",
  636. i, want, iter->rt_runtime);
  637. #endif
  638. if (want > 0) {
  639. diff = min_t(s64, iter->rt_runtime, want);
  640. iter->rt_runtime -= diff;
  641. want -= diff;
  642. #ifdef MTK_DEBUG_CGROUP
  643. pr_warn("2. disable_runtime, rt_runtime=%llu, diff=%lld, want=%lld\n",
  644. iter->rt_runtime, diff, want);
  645. #endif
  646. } else {
  647. iter->rt_runtime -= want;
  648. want -= want;
  649. #ifdef MTK_DEBUG_CGROUP
  650. pr_warn("3. disable_runtime, rt_runtime=%llu, want=%lld\n", iter->rt_runtime, want);
  651. #endif
  652. }
  653. raw_spin_unlock(&iter->rt_runtime_lock);
  654. if (!want)
  655. break;
  656. }
  657. raw_spin_lock(&rt_rq->rt_runtime_lock);
  658. /*
  659. * We cannot be left wanting - that would mean some runtime
  660. * leaked out of the system.
  661. */
  662. if (want) {
  663. #ifdef MTK_DEBUG_CGROUP
  664. pr_warn("4. disable_runtime, want=%lld, rt_rq->rt_runtime=%llu\n",
  665. want, rt_rq->rt_runtime);
  666. {
  667. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, 0);
  668. pr_warn("4-0. disable_runtime %llu\n", iter->rt_runtime);
  669. iter = sched_rt_period_rt_rq(rt_b, 1);
  670. pr_warn("4-1. disable_runtime %llu\n", iter->rt_runtime);
  671. iter = sched_rt_period_rt_rq(rt_b, 2);
  672. pr_warn("4-2. disable_runtime %llu\n", iter->rt_runtime);
  673. iter = sched_rt_period_rt_rq(rt_b, 3);
  674. pr_warn("4-3. disable_runtime %llu\n", iter->rt_runtime);
  675. }
  676. #endif
  677. BUG_ON(want);
  678. }
  679. balanced:
  680. /*
  681. * Disable all the borrow logic by pretending we have inf
  682. * runtime - in which case borrowing doesn't make sense.
  683. */
  684. /* sched: prevent normal task could run anymore, use rt_disable_borrow */
  685. /* rt_rq->rt_runtime = RUNTIME_INF; */
  686. rt_rq->rt_runtime = rt_b->rt_runtime;
  687. /* sched: set rt_runtime =0 and print __disable_runtime rt_throttled*/
  688. if (1 == rt_rq->rt_throttled) {
  689. u64 rt_time_pre;
  690. rt_time_pre = rt_rq->rt_time;
  691. rt_rq->rt_throttled = 0;
  692. printk_deferred("sched: disable_runtime: RT throttling inactivated, cpu=%d\n",
  693. rq->cpu);
  694. printk_deferred("sched: cpu=%d, rt_time[%llu -> %llu], rt_throttled = %d\n",
  695. rq->cpu, rt_time_pre, rt_rq->rt_time, rt_rq->rt_throttled);
  696. printk_deferred("sched: rt_runtime=[%llu]\n",
  697. rt_rq->rt_runtime);
  698. }
  699. rt_rq->rt_throttled = 0;
  700. #ifdef MTK_DEBUG_CGROUP
  701. {
  702. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, 0);
  703. pr_warn("5-0. disable_runtime %llu\n", iter->rt_runtime);
  704. iter = sched_rt_period_rt_rq(rt_b, 1);
  705. pr_warn("5-1. disable_runtime %llu\n", iter->rt_runtime);
  706. iter = sched_rt_period_rt_rq(rt_b, 2);
  707. pr_warn("5-2. disable_runtime %llu\n", iter->rt_runtime);
  708. iter = sched_rt_period_rt_rq(rt_b, 3);
  709. pr_warn("5-3. disable_runtime %llu\n", iter->rt_runtime);
  710. }
  711. #endif
  712. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  713. raw_spin_unlock(&rt_b->rt_runtime_lock);
  714. #ifdef MTK_DEBUG_CGROUP
  715. pr_warn("disable_runtime after: rt_rq->rt_runtime=%llu rq_rt->rt_throttled=%d\n",
  716. rt_rq->rt_runtime, rt_rq->rt_throttled);
  717. #endif
  718. /* Make rt_rq available for pick_next_task() */
  719. sched_rt_rq_enqueue(rt_rq);
  720. }
  721. /* sched:add trace_sched*/
  722. mt_sched_printf(sched_rt_info, "cpu=%d rt_throttled=%d", rq->cpu, rq->rt.rt_throttled);
  723. }
  724. static void __enable_runtime(struct rq *rq)
  725. {
  726. rt_rq_iter_t iter;
  727. struct rt_rq *rt_rq;
  728. if (unlikely(!scheduler_running))
  729. return;
  730. /*
  731. * Reset each runqueue's bandwidth settings
  732. */
  733. for_each_rt_rq(rt_rq, iter, rq) {
  734. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  735. raw_spin_lock(&rt_b->rt_runtime_lock);
  736. raw_spin_lock(&rt_rq->rt_runtime_lock);
  737. #ifdef MTK_DEBUG_CGROUP
  738. pr_warn("enable_runtime %d\n", rq->cpu);
  739. #endif
  740. rt_rq->rt_runtime = rt_b->rt_runtime;
  741. rt_rq->rt_time = 0;
  742. rt_rq->rt_throttled = 0;
  743. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  744. raw_spin_unlock(&rt_b->rt_runtime_lock);
  745. }
  746. /* sched:add trace_sched*/
  747. mt_sched_printf(sched_rt_info, "cpu=%d rt_throttled=%d", rq->cpu, rq->rt.rt_throttled);
  748. }
  749. static int balance_runtime(struct rt_rq *rt_rq)
  750. {
  751. int more = 0;
  752. if (!sched_feat(RT_RUNTIME_SHARE))
  753. return more;
  754. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  755. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  756. more = do_balance_runtime(rt_rq);
  757. raw_spin_lock(&rt_rq->rt_runtime_lock);
  758. }
  759. return more;
  760. }
  761. #else /* !CONFIG_SMP */
  762. static inline int balance_runtime(struct rt_rq *rt_rq)
  763. {
  764. return 0;
  765. }
  766. #endif /* CONFIG_SMP */
  767. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  768. {
  769. int i, idle = 1, throttled = 0;
  770. const struct cpumask *span;
  771. span = sched_rt_period_mask();
  772. #ifdef CONFIG_RT_GROUP_SCHED
  773. /*
  774. * FIXME: isolated CPUs should really leave the root task group,
  775. * whether they are isolcpus or were isolated via cpusets, lest
  776. * the timer run on a CPU which does not service all runqueues,
  777. * potentially leaving other CPUs indefinitely throttled. If
  778. * isolation is really required, the user will turn the throttle
  779. * off to kill the perturbations it causes anyway. Meanwhile,
  780. * this maintains functionality for boot and/or troubleshooting.
  781. */
  782. if (rt_b == &root_task_group.rt_bandwidth)
  783. span = cpu_online_mask;
  784. #endif
  785. #ifdef MTK_DEBUG_CGROUP
  786. pr_warn("do_sched_rt_period_timer curr_cpu=%d\n", smp_processor_id());
  787. #endif
  788. for_each_cpu(i, span) {
  789. int enqueue = 0;
  790. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  791. struct rq *rq = rq_of_rt_rq(rt_rq);
  792. raw_spin_lock(&rq->lock);
  793. if (rt_rq->rt_time) {
  794. u64 runtime;
  795. /* sched:get runtime*/
  796. u64 runtime_pre, rt_time_pre;
  797. raw_spin_lock(&rt_rq->rt_runtime_lock);
  798. if (rt_rq->rt_throttled) {
  799. runtime_pre = rt_rq->rt_runtime;
  800. balance_runtime(rt_rq);
  801. rt_time_pre = rt_rq->rt_time;
  802. }
  803. runtime = rt_rq->rt_runtime;
  804. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  805. /* sched:print throttle*/
  806. if (rt_rq->rt_throttled) {
  807. printk_deferred("sched: cpu=%d, [%llu -> %llu]",
  808. i, rt_time_pre, rt_rq->rt_time);
  809. printk_deferred(" -= min(%llu, %d*[%llu -> %llu])\n",
  810. rt_time_pre, overrun, runtime_pre, runtime);
  811. }
  812. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  813. /* sched:print throttle*/
  814. printk_deferred("sched: RT throttling inactivated cpu=%d\n", i);
  815. rt_rq->rt_throttled = 0;
  816. mt_sched_printf(sched_rt_info, "cpu=%d rt_throttled=%d",
  817. rq_cpu(rq), rq->rt.rt_throttled);
  818. enqueue = 1;
  819. /*
  820. * Force a clock update if the CPU was idle,
  821. * lest wakeup -> unthrottle time accumulate.
  822. */
  823. if (rt_rq->rt_nr_running && rq->curr == rq->idle)
  824. rq->skip_clock_update = -1;
  825. }
  826. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  827. idle = 0;
  828. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  829. } else if (rt_rq->rt_nr_running) {
  830. idle = 0;
  831. if (!rt_rq_throttled(rt_rq))
  832. enqueue = 1;
  833. }
  834. if (rt_rq->rt_throttled)
  835. throttled = 1;
  836. if (enqueue)
  837. sched_rt_rq_enqueue(rt_rq);
  838. raw_spin_unlock(&rq->lock);
  839. }
  840. if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
  841. return 1;
  842. return idle;
  843. }
  844. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  845. {
  846. #ifdef CONFIG_RT_GROUP_SCHED
  847. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  848. if (rt_rq)
  849. return rt_rq->highest_prio.curr;
  850. #endif
  851. return rt_task_of(rt_se)->prio;
  852. }
  853. /* sched:add rt exec info*/
  854. DEFINE_PER_CPU(u64, rt_throttling_start);
  855. DEFINE_PER_CPU(u64, exec_delta_time);
  856. DEFINE_PER_CPU(u64, clock_task);
  857. DEFINE_PER_CPU(u64, exec_start);
  858. DEFINE_PER_CPU(struct task_struct, exec_task);
  859. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  860. {
  861. u64 runtime = sched_rt_runtime(rt_rq);
  862. u64 runtime_pre;
  863. if (rt_rq->rt_throttled)
  864. return rt_rq_throttled(rt_rq);
  865. if (runtime >= sched_rt_period(rt_rq))
  866. return 0;
  867. /* sched:get runtime*/
  868. runtime_pre = runtime;
  869. balance_runtime(rt_rq);
  870. runtime = sched_rt_runtime(rt_rq);
  871. if (runtime == RUNTIME_INF)
  872. return 0;
  873. if (rt_rq->rt_time > runtime) {
  874. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  875. #ifdef CONFIG_RT_GROUP_SCHED
  876. int cpu = rq_cpu(rt_rq->rq);
  877. /* sched:print throttle*/
  878. printk_deferred("sched: cpu=%d rt_time %llu <-> runtime",
  879. cpu, rt_rq->rt_time);
  880. printk_deferred(" [%llu -> %llu], exec_task[%d:%s], prio=%d, exec_delta_time[%llu]",
  881. runtime_pre, runtime,
  882. per_cpu(exec_task, cpu).pid,
  883. per_cpu(exec_task, cpu).comm,
  884. per_cpu(exec_task, cpu).prio,
  885. per_cpu(exec_delta_time, cpu));
  886. printk_deferred(", clock_task[%llu], exec_start[%llu]\n",
  887. per_cpu(clock_task, cpu), per_cpu(exec_start, cpu));
  888. #endif
  889. /*
  890. * Don't actually throttle groups that have no runtime assigned
  891. * but accrue some time due to boosting.
  892. */
  893. if (likely(rt_b->rt_runtime)) {
  894. rt_rq->rt_throttled = 1;
  895. /* sched:print throttle every time*/
  896. printk_deferred("sched: RT throttling activated\n");
  897. #ifdef CONFIG_RT_GROUP_SCHED
  898. mt_sched_printf(sched_rt_info, "cpu=%d rt_throttled=%d",
  899. cpu, rt_rq->rt_throttled);
  900. per_cpu(rt_throttling_start, cpu) = rq_clock_task(rt_rq->rq);
  901. #endif
  902. #ifdef CONFIG_MTPROF
  903. /* sched:rt throttle monitor */
  904. mt_rt_mon_switch(MON_STOP);
  905. mt_rt_mon_print_task();
  906. #endif
  907. } else {
  908. /*
  909. * In case we did anyway, make it go away,
  910. * replenishment is a joke, since it will replenish us
  911. * with exactly 0 ns.
  912. */
  913. rt_rq->rt_time = 0;
  914. }
  915. if (rt_rq_throttled(rt_rq)) {
  916. sched_rt_rq_dequeue(rt_rq);
  917. return 1;
  918. }
  919. }
  920. return 0;
  921. }
  922. /*
  923. * Update the current task's runtime statistics. Skip current tasks that
  924. * are not in our scheduling class.
  925. */
  926. static void update_curr_rt(struct rq *rq)
  927. {
  928. struct task_struct *curr = rq->curr;
  929. struct sched_rt_entity *rt_se = &curr->rt;
  930. u64 delta_exec;
  931. int cpu = rq_cpu(rq);
  932. if (curr->sched_class != &rt_sched_class)
  933. return;
  934. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  935. if (unlikely((s64)delta_exec <= 0))
  936. return;
  937. schedstat_set(curr->se.statistics.exec_max,
  938. max(curr->se.statistics.exec_max, delta_exec));
  939. /* sched:update rt exec info*/
  940. per_cpu(exec_task, cpu).pid = curr->pid;
  941. per_cpu(exec_task, cpu).prio = curr->prio;
  942. strcpy(per_cpu(exec_task, cpu).comm, curr->comm);
  943. per_cpu(exec_delta_time, cpu) = delta_exec;
  944. per_cpu(clock_task, cpu) = rq->clock_task;
  945. per_cpu(exec_start, cpu) = curr->se.exec_start;
  946. curr->se.sum_exec_runtime += delta_exec;
  947. account_group_exec_runtime(curr, delta_exec);
  948. curr->se.exec_start = rq_clock_task(rq);
  949. cpuacct_charge(curr, delta_exec);
  950. sched_rt_avg_update(rq, delta_exec);
  951. if (!rt_bandwidth_enabled())
  952. return;
  953. for_each_sched_rt_entity(rt_se) {
  954. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  955. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  956. raw_spin_lock(&rt_rq->rt_runtime_lock);
  957. rt_rq->rt_time += delta_exec;
  958. if (sched_rt_runtime_exceeded(rt_rq))
  959. resched_curr(rq);
  960. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  961. }
  962. }
  963. }
  964. static void
  965. dequeue_top_rt_rq(struct rt_rq *rt_rq)
  966. {
  967. struct rq *rq = rq_of_rt_rq(rt_rq);
  968. BUG_ON(&rq->rt != rt_rq);
  969. if (!rt_rq->rt_queued)
  970. return;
  971. BUG_ON(!rq->nr_running);
  972. sub_nr_running(rq, rt_rq->rt_nr_running);
  973. rt_rq->rt_queued = 0;
  974. }
  975. static void
  976. enqueue_top_rt_rq(struct rt_rq *rt_rq)
  977. {
  978. struct rq *rq = rq_of_rt_rq(rt_rq);
  979. BUG_ON(&rq->rt != rt_rq);
  980. if (rt_rq->rt_queued)
  981. return;
  982. if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
  983. return;
  984. add_nr_running(rq, rt_rq->rt_nr_running);
  985. rt_rq->rt_queued = 1;
  986. }
  987. #if defined CONFIG_SMP
  988. static void
  989. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  990. {
  991. struct rq *rq = rq_of_rt_rq(rt_rq);
  992. #ifdef CONFIG_RT_GROUP_SCHED
  993. /*
  994. * Change rq's cpupri only if rt_rq is the top queue.
  995. */
  996. if (&rq->rt != rt_rq)
  997. return;
  998. #endif
  999. if (rq->online && prio < prev_prio)
  1000. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  1001. }
  1002. static void
  1003. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  1004. {
  1005. struct rq *rq = rq_of_rt_rq(rt_rq);
  1006. #ifdef CONFIG_RT_GROUP_SCHED
  1007. /*
  1008. * Change rq's cpupri only if rt_rq is the top queue.
  1009. */
  1010. if (&rq->rt != rt_rq)
  1011. return;
  1012. #endif
  1013. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  1014. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  1015. }
  1016. #else /* CONFIG_SMP */
  1017. static inline
  1018. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  1019. static inline
  1020. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  1021. #endif /* CONFIG_SMP */
  1022. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  1023. static void
  1024. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  1025. {
  1026. int prev_prio = rt_rq->highest_prio.curr;
  1027. if (prio < prev_prio)
  1028. rt_rq->highest_prio.curr = prio;
  1029. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  1030. }
  1031. static void
  1032. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  1033. {
  1034. int prev_prio = rt_rq->highest_prio.curr;
  1035. if (rt_rq->rt_nr_running) {
  1036. WARN_ON(prio < prev_prio);
  1037. /*
  1038. * This may have been our highest task, and therefore
  1039. * we may have some recomputation to do
  1040. */
  1041. if (prio == prev_prio) {
  1042. struct rt_prio_array *array = &rt_rq->active;
  1043. rt_rq->highest_prio.curr =
  1044. sched_find_first_bit(array->bitmap);
  1045. }
  1046. } else
  1047. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  1048. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  1049. }
  1050. #else
  1051. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  1052. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  1053. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  1054. #ifdef CONFIG_RT_GROUP_SCHED
  1055. static void
  1056. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  1057. {
  1058. if (rt_se_boosted(rt_se))
  1059. rt_rq->rt_nr_boosted++;
  1060. if (rt_rq->tg)
  1061. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  1062. }
  1063. static void
  1064. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  1065. {
  1066. if (rt_se_boosted(rt_se))
  1067. rt_rq->rt_nr_boosted--;
  1068. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  1069. }
  1070. #else /* CONFIG_RT_GROUP_SCHED */
  1071. static void
  1072. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  1073. {
  1074. start_rt_bandwidth(&def_rt_bandwidth);
  1075. }
  1076. static inline
  1077. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  1078. #endif /* CONFIG_RT_GROUP_SCHED */
  1079. static inline
  1080. unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
  1081. {
  1082. struct rt_rq *group_rq = group_rt_rq(rt_se);
  1083. if (group_rq)
  1084. return group_rq->rt_nr_running;
  1085. else
  1086. return 1;
  1087. }
  1088. static inline
  1089. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  1090. {
  1091. int prio = rt_se_prio(rt_se);
  1092. WARN_ON(!rt_prio(prio));
  1093. rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
  1094. inc_rt_prio(rt_rq, prio);
  1095. inc_rt_migration(rt_se, rt_rq);
  1096. inc_rt_group(rt_se, rt_rq);
  1097. }
  1098. static inline
  1099. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  1100. {
  1101. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  1102. WARN_ON(!rt_rq->rt_nr_running);
  1103. rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
  1104. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  1105. dec_rt_migration(rt_se, rt_rq);
  1106. dec_rt_group(rt_se, rt_rq);
  1107. }
  1108. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
  1109. {
  1110. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  1111. struct rt_prio_array *array = &rt_rq->active;
  1112. struct rt_rq *group_rq = group_rt_rq(rt_se);
  1113. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  1114. /*
  1115. * Don't enqueue the group if its throttled, or when empty.
  1116. * The latter is a consequence of the former when a child group
  1117. * get throttled and the current group doesn't have any other
  1118. * active members.
  1119. */
  1120. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  1121. return;
  1122. if (head)
  1123. list_add(&rt_se->run_list, queue);
  1124. else
  1125. list_add_tail(&rt_se->run_list, queue);
  1126. __set_bit(rt_se_prio(rt_se), array->bitmap);
  1127. inc_rt_tasks(rt_se, rt_rq);
  1128. }
  1129. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  1130. {
  1131. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  1132. struct rt_prio_array *array = &rt_rq->active;
  1133. list_del_init(&rt_se->run_list);
  1134. if (list_empty(array->queue + rt_se_prio(rt_se)))
  1135. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  1136. dec_rt_tasks(rt_se, rt_rq);
  1137. }
  1138. /*
  1139. * Because the prio of an upper entry depends on the lower
  1140. * entries, we must remove entries top - down.
  1141. */
  1142. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  1143. {
  1144. struct sched_rt_entity *back = NULL;
  1145. for_each_sched_rt_entity(rt_se) {
  1146. rt_se->back = back;
  1147. back = rt_se;
  1148. }
  1149. dequeue_top_rt_rq(rt_rq_of_se(back));
  1150. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  1151. if (on_rt_rq(rt_se))
  1152. __dequeue_rt_entity(rt_se);
  1153. }
  1154. }
  1155. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
  1156. {
  1157. struct rq *rq = rq_of_rt_se(rt_se);
  1158. dequeue_rt_stack(rt_se);
  1159. for_each_sched_rt_entity(rt_se)
  1160. __enqueue_rt_entity(rt_se, head);
  1161. enqueue_top_rt_rq(&rq->rt);
  1162. }
  1163. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  1164. {
  1165. struct rq *rq = rq_of_rt_se(rt_se);
  1166. dequeue_rt_stack(rt_se);
  1167. for_each_sched_rt_entity(rt_se) {
  1168. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  1169. if (rt_rq && rt_rq->rt_nr_running)
  1170. __enqueue_rt_entity(rt_se, false);
  1171. }
  1172. enqueue_top_rt_rq(&rq->rt);
  1173. }
  1174. /*
  1175. * Adding/removing a task to/from a priority array:
  1176. */
  1177. static void
  1178. enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1179. {
  1180. struct sched_rt_entity *rt_se = &p->rt;
  1181. if (flags & ENQUEUE_WAKEUP)
  1182. rt_se->timeout = 0;
  1183. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  1184. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  1185. enqueue_pushable_task(rq, p);
  1186. }
  1187. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1188. {
  1189. struct sched_rt_entity *rt_se = &p->rt;
  1190. update_curr_rt(rq);
  1191. dequeue_rt_entity(rt_se);
  1192. dequeue_pushable_task(rq, p);
  1193. }
  1194. /*
  1195. * Put task to the head or the end of the run list without the overhead of
  1196. * dequeue followed by enqueue.
  1197. */
  1198. static void
  1199. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  1200. {
  1201. if (on_rt_rq(rt_se)) {
  1202. struct rt_prio_array *array = &rt_rq->active;
  1203. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  1204. if (head)
  1205. list_move(&rt_se->run_list, queue);
  1206. else
  1207. list_move_tail(&rt_se->run_list, queue);
  1208. }
  1209. }
  1210. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  1211. {
  1212. struct sched_rt_entity *rt_se = &p->rt;
  1213. struct rt_rq *rt_rq;
  1214. for_each_sched_rt_entity(rt_se) {
  1215. rt_rq = rt_rq_of_se(rt_se);
  1216. requeue_rt_entity(rt_rq, rt_se, head);
  1217. }
  1218. }
  1219. static void yield_task_rt(struct rq *rq)
  1220. {
  1221. requeue_task_rt(rq, rq->curr, 0);
  1222. }
  1223. #ifdef CONFIG_SMP
  1224. static int find_lowest_rq(struct task_struct *task);
  1225. static int
  1226. select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
  1227. {
  1228. struct task_struct *curr;
  1229. struct rq *rq;
  1230. if (p->nr_cpus_allowed == 1)
  1231. goto out;
  1232. /* For anything but wake ups, just return the task_cpu */
  1233. if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
  1234. goto out;
  1235. rq = cpu_rq(cpu);
  1236. rcu_read_lock();
  1237. curr = ACCESS_ONCE(rq->curr); /* unlocked access */
  1238. /*
  1239. * If the current task on @p's runqueue is an RT task, then
  1240. * try to see if we can wake this RT task up on another
  1241. * runqueue. Otherwise simply start this RT task
  1242. * on its current runqueue.
  1243. *
  1244. * We want to avoid overloading runqueues. If the woken
  1245. * task is a higher priority, then it will stay on this CPU
  1246. * and the lower prio task should be moved to another CPU.
  1247. * Even though this will probably make the lower prio task
  1248. * lose its cache, we do not want to bounce a higher task
  1249. * around just because it gave up its CPU, perhaps for a
  1250. * lock?
  1251. *
  1252. * For equal prio tasks, we just let the scheduler sort it out.
  1253. *
  1254. * Otherwise, just let it ride on the affined RQ and the
  1255. * post-schedule router will push the preempted task away
  1256. *
  1257. * This test is optimistic, if we get it wrong the load-balancer
  1258. * will have to sort it out.
  1259. */
  1260. #if defined(CONFIG_MT_SCHED_TRACE)
  1261. if (curr) {
  1262. mt_sched_printf(sched_rt_info,
  1263. "1 select_task_rq_rt cpu=%d p=%d:%s:prio=%d:0x%x curr=%d:%s:prio=%d:0x%x",
  1264. cpu, p->pid, p->comm, p->prio, p->nr_cpus_allowed, curr->pid,
  1265. curr->comm, curr->prio, curr->nr_cpus_allowed);
  1266. } else {
  1267. mt_sched_printf(sched_rt_info, "1 select_task_rq_rt cpu=%d p=%d:%s:prio=%d:0x%x",
  1268. cpu, p->pid, p->comm, p->prio, p->nr_cpus_allowed);
  1269. }
  1270. #endif
  1271. #if defined(CONFIG_MT_SCHED_INTEROP)
  1272. /* if the task is allowed to put more than one CPU. */
  1273. if ((p->nr_cpus_allowed > 1)) {
  1274. #else
  1275. if (curr && unlikely(rt_task(curr)) &&
  1276. (curr->nr_cpus_allowed < 2 ||
  1277. curr->prio <= p->prio)) {
  1278. #endif
  1279. int target = find_lowest_rq(p);
  1280. if (target != -1)
  1281. cpu = target;
  1282. mt_sched_printf(sched_rt_info, "2. select_task_rq_rt %d:%s to cpu=%d", p->pid, p->comm, cpu);
  1283. }
  1284. rcu_read_unlock();
  1285. out:
  1286. return cpu;
  1287. }
  1288. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  1289. {
  1290. if (rq->curr->nr_cpus_allowed == 1)
  1291. return;
  1292. if (p->nr_cpus_allowed != 1
  1293. && cpupri_find(&rq->rd->cpupri, p, NULL))
  1294. return;
  1295. if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  1296. return;
  1297. /*
  1298. * There appears to be other cpus that can accept
  1299. * current and none to run 'p', so lets reschedule
  1300. * to try and push current away:
  1301. */
  1302. requeue_task_rt(rq, p, 1);
  1303. resched_curr(rq);
  1304. }
  1305. #endif /* CONFIG_SMP */
  1306. /*
  1307. * Preempt the current task with a newly woken task if needed:
  1308. */
  1309. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
  1310. {
  1311. mt_sched_printf(sched_rt_info, "check_preempt_curr_rt %d:%d:%s", p->prio, rq->curr->prio, p->comm);
  1312. if (p->prio < rq->curr->prio) {
  1313. resched_curr(rq);
  1314. return;
  1315. }
  1316. #ifdef CONFIG_SMP
  1317. /*
  1318. * If:
  1319. *
  1320. * - the newly woken task is of equal priority to the current task
  1321. * - the newly woken task is non-migratable while current is migratable
  1322. * - current will be preempted on the next reschedule
  1323. *
  1324. * we should check to see if current can readily move to a different
  1325. * cpu. If so, we will reschedule to allow the push logic to try
  1326. * to move current somewhere else, making room for our non-migratable
  1327. * task.
  1328. */
  1329. if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
  1330. check_preempt_equal_prio(rq, p);
  1331. #endif
  1332. }
  1333. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  1334. struct rt_rq *rt_rq)
  1335. {
  1336. struct rt_prio_array *array = &rt_rq->active;
  1337. struct sched_rt_entity *next = NULL;
  1338. struct list_head *queue;
  1339. int idx;
  1340. idx = sched_find_first_bit(array->bitmap);
  1341. BUG_ON(idx >= MAX_RT_PRIO);
  1342. queue = array->queue + idx;
  1343. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  1344. return next;
  1345. }
  1346. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  1347. {
  1348. struct sched_rt_entity *rt_se;
  1349. struct task_struct *p;
  1350. struct rt_rq *rt_rq = &rq->rt;
  1351. do {
  1352. rt_se = pick_next_rt_entity(rq, rt_rq);
  1353. BUG_ON(!rt_se);
  1354. rt_rq = group_rt_rq(rt_se);
  1355. } while (rt_rq);
  1356. p = rt_task_of(rt_se);
  1357. p->se.exec_start = rq_clock_task(rq);
  1358. return p;
  1359. }
  1360. static struct task_struct *
  1361. pick_next_task_rt(struct rq *rq, struct task_struct *prev)
  1362. {
  1363. struct task_struct *p = NULL;
  1364. struct rt_rq *rt_rq = &rq->rt;
  1365. if (need_pull_rt_task(rq, prev)) {
  1366. pull_rt_task(rq);
  1367. /*
  1368. * pull_rt_task() can drop (and re-acquire) rq->lock; this
  1369. * means a dl or stop task can slip in, in which case we need
  1370. * to re-start task selection.
  1371. */
  1372. if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
  1373. rq->dl.dl_nr_running))
  1374. return RETRY_TASK;
  1375. }
  1376. /*
  1377. * We may dequeue prev's rt_rq in put_prev_task().
  1378. * So, we update time before rt_nr_running check.
  1379. */
  1380. if (prev->sched_class == &rt_sched_class)
  1381. update_curr_rt(rq);
  1382. if (!rt_rq->rt_queued) {
  1383. /*sched: prevent wdt from RT throttle */
  1384. struct rt_prio_array *array = &rt_rq->active;
  1385. struct sched_rt_entity *rt_se;
  1386. int idx = 0, prio = MAX_RT_PRIO - 1 - idx; /* WDT priority */
  1387. if (test_bit(idx, array->bitmap)) {
  1388. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  1389. p = rt_task_of(rt_se);
  1390. if ((p->rt_priority == prio) && (0 == strncmp(p->comm, "wdtk", 4))) {
  1391. p->se.exec_start = rq->clock_task;
  1392. if (prev != p) {
  1393. printk_deferred("sched: unthrottle %d:%s state=%lu\n",
  1394. p->pid, p->comm, p->state);
  1395. }
  1396. goto found;
  1397. }
  1398. }
  1399. }
  1400. return NULL;
  1401. }
  1402. found:
  1403. put_prev_task(rq, prev);
  1404. if (NULL == p)
  1405. p = _pick_next_task_rt(rq);
  1406. /* The running task is never eligible for pushing */
  1407. dequeue_pushable_task(rq, p);
  1408. set_post_schedule(rq);
  1409. return p;
  1410. }
  1411. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  1412. {
  1413. update_curr_rt(rq);
  1414. /*
  1415. * The previous task needs to be made eligible for pushing
  1416. * if it is still active
  1417. */
  1418. if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  1419. enqueue_pushable_task(rq, p);
  1420. }
  1421. #ifdef CONFIG_SMP
  1422. /* Only try algorithms three times */
  1423. #define RT_MAX_TRIES 3
  1424. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  1425. {
  1426. if (!task_running(rq, p) &&
  1427. cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  1428. return 1;
  1429. return 0;
  1430. }
  1431. /*
  1432. * Return the highest pushable rq's task, which is suitable to be executed
  1433. * on the cpu, NULL otherwise
  1434. */
  1435. static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
  1436. {
  1437. struct plist_head *head = &rq->rt.pushable_tasks;
  1438. struct task_struct *p;
  1439. if (!has_pushable_tasks(rq))
  1440. return NULL;
  1441. plist_for_each_entry(p, head, pushable_tasks) {
  1442. if (pick_rt_task(rq, p, cpu))
  1443. return p;
  1444. }
  1445. return NULL;
  1446. }
  1447. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  1448. #ifdef CONFIG_MT_SCHED_INTEROP
  1449. static int mt_sched_interop_rt(int cpu, struct cpumask *lowest_mask)
  1450. {
  1451. int lowest_cpu = -1, lowest_prio = 0;
  1452. mt_sched_printf(sched_interop, "current cpu=%d, find idle cpu from cpumask 0x%lx",
  1453. cpu, lowest_mask->bits[0]);
  1454. if (cpumask_test_cpu(cpu, lowest_mask) && idle_cpu(cpu))
  1455. return cpu;
  1456. for_each_cpu(cpu, lowest_mask) {
  1457. struct rq *rq;
  1458. struct task_struct *curr;
  1459. if (idle_cpu(cpu))
  1460. return cpu;
  1461. rq = cpu_rq(cpu);
  1462. curr = rq->curr;
  1463. if ((curr->sched_class == &fair_sched_class) && (curr->prio > lowest_prio)) {
  1464. lowest_prio = curr->prio;
  1465. lowest_cpu = cpu;
  1466. mt_sched_printf(sched_interop, "lowest_cpu=%d, lowest_prio=%d",
  1467. lowest_cpu, lowest_prio);
  1468. }
  1469. }
  1470. if (-1 != lowest_cpu)
  1471. return lowest_cpu;
  1472. return -1;
  1473. }
  1474. #endif
  1475. static int find_lowest_rq(struct task_struct *task)
  1476. {
  1477. struct sched_domain *sd;
  1478. struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
  1479. int this_cpu = smp_processor_id();
  1480. int cpu = task_cpu(task);
  1481. #ifdef CONFIG_MT_SCHED_INTEROP
  1482. int interop_cpu;
  1483. #endif
  1484. mt_sched_printf(sched_rt_info,
  1485. "1 find_lowest_rq lowest_mask=0x%lx, task->cpus_allowed=0x%lx",
  1486. lowest_mask->bits[0], task->cpus_allowed.bits[0]);
  1487. /* Make sure the mask is initialized first */
  1488. if (unlikely(!lowest_mask))
  1489. return -1;
  1490. if (task->nr_cpus_allowed == 1)
  1491. return -1; /* No other targets possible */
  1492. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  1493. return -1; /* No targets found */
  1494. #ifdef CONFIG_MT_SCHED_INTEROP
  1495. interop_cpu = mt_sched_interop_rt(cpu, lowest_mask);
  1496. if (interop_cpu != -1) {
  1497. mt_sched_printf(sched_interop, "find idle cpu=%d", interop_cpu);
  1498. return interop_cpu;
  1499. }
  1500. #endif
  1501. /*
  1502. * At this point we have built a mask of cpus representing the
  1503. * lowest priority tasks in the system. Now we want to elect
  1504. * the best one based on our affinity and topology.
  1505. *
  1506. * We prioritize the last cpu that the task executed on since
  1507. * it is most likely cache-hot in that location.
  1508. */
  1509. if (cpumask_test_cpu(cpu, lowest_mask))
  1510. return cpu;
  1511. /*
  1512. * Otherwise, we consult the sched_domains span maps to figure
  1513. * out which cpu is logically closest to our hot cache data.
  1514. */
  1515. if (!cpumask_test_cpu(this_cpu, lowest_mask))
  1516. this_cpu = -1; /* Skip this_cpu opt if not among lowest */
  1517. rcu_read_lock();
  1518. for_each_domain(cpu, sd) {
  1519. if (sd->flags & SD_WAKE_AFFINE) {
  1520. int best_cpu;
  1521. /*
  1522. * "this_cpu" is cheaper to preempt than a
  1523. * remote processor.
  1524. */
  1525. if (this_cpu != -1 &&
  1526. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1527. rcu_read_unlock();
  1528. return this_cpu;
  1529. }
  1530. best_cpu = cpumask_first_and(lowest_mask,
  1531. sched_domain_span(sd));
  1532. if (best_cpu < nr_cpu_ids) {
  1533. rcu_read_unlock();
  1534. return best_cpu;
  1535. }
  1536. }
  1537. }
  1538. rcu_read_unlock();
  1539. /*
  1540. * And finally, if there were no matches within the domains
  1541. * just give the caller *something* to work with from the compatible
  1542. * locations.
  1543. */
  1544. if (this_cpu != -1)
  1545. return this_cpu;
  1546. cpu = cpumask_any(lowest_mask);
  1547. if (cpu < nr_cpu_ids)
  1548. return cpu;
  1549. return -1;
  1550. }
  1551. /* Will lock the rq it finds */
  1552. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  1553. {
  1554. struct rq *lowest_rq = NULL;
  1555. int tries;
  1556. int cpu;
  1557. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  1558. cpu = find_lowest_rq(task);
  1559. if ((cpu == -1) || (cpu == rq->cpu))
  1560. break;
  1561. lowest_rq = cpu_rq(cpu);
  1562. /* if the prio of this runqueue changed, try again */
  1563. if (double_lock_balance(rq, lowest_rq)) {
  1564. /*
  1565. * We had to unlock the run queue. In
  1566. * the mean time, task could have
  1567. * migrated already or had its affinity changed.
  1568. * Also make sure that it wasn't scheduled on its rq.
  1569. */
  1570. mt_sched_printf(sched_rt_info, "1. find_lock_lowest_rq %d %d %d %s",
  1571. lowest_rq->cpu, rq->cpu, task->pid, task->comm);
  1572. if (unlikely(task_rq(task) != rq ||
  1573. !cpumask_test_cpu(lowest_rq->cpu,
  1574. tsk_cpus_allowed(task)) ||
  1575. task_running(rq, task) ||
  1576. !task_on_rq_queued(task))) {
  1577. double_unlock_balance(rq, lowest_rq);
  1578. lowest_rq = NULL;
  1579. break;
  1580. }
  1581. }
  1582. /* If this rq is still suitable use it. */
  1583. if (lowest_rq->rt.highest_prio.curr > task->prio)
  1584. break;
  1585. /* try again */
  1586. double_unlock_balance(rq, lowest_rq);
  1587. lowest_rq = NULL;
  1588. }
  1589. return lowest_rq;
  1590. }
  1591. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1592. {
  1593. struct task_struct *p;
  1594. if (!has_pushable_tasks(rq))
  1595. return NULL;
  1596. p = plist_first_entry(&rq->rt.pushable_tasks,
  1597. struct task_struct, pushable_tasks);
  1598. BUG_ON(rq->cpu != task_cpu(p));
  1599. BUG_ON(task_current(rq, p));
  1600. BUG_ON(p->nr_cpus_allowed <= 1);
  1601. BUG_ON(!task_on_rq_queued(p));
  1602. BUG_ON(!rt_task(p));
  1603. return p;
  1604. }
  1605. /*
  1606. * If the current CPU has more than one RT task, see if the non
  1607. * running task can migrate over to a CPU that is running a task
  1608. * of lesser priority.
  1609. */
  1610. static int push_rt_task(struct rq *rq)
  1611. {
  1612. struct task_struct *next_task;
  1613. struct rq *lowest_rq;
  1614. int ret = 0;
  1615. if (!rq->rt.overloaded)
  1616. return 0;
  1617. next_task = pick_next_pushable_task(rq);
  1618. if (!next_task)
  1619. return 0;
  1620. retry:
  1621. if (unlikely(next_task == rq->curr)) {
  1622. WARN_ON(1);
  1623. return 0;
  1624. }
  1625. /*
  1626. * It's possible that the next_task slipped in of
  1627. * higher priority than current. If that's the case
  1628. * just reschedule current.
  1629. */
  1630. if (unlikely(next_task->prio < rq->curr->prio)) {
  1631. resched_curr(rq);
  1632. return 0;
  1633. }
  1634. /* We might release rq lock */
  1635. get_task_struct(next_task);
  1636. /* find_lock_lowest_rq locks the rq if found */
  1637. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1638. if (!lowest_rq) {
  1639. struct task_struct *task;
  1640. /*
  1641. * find_lock_lowest_rq releases rq->lock
  1642. * so it is possible that next_task has migrated.
  1643. *
  1644. * We need to make sure that the task is still on the same
  1645. * run-queue and is also still the next task eligible for
  1646. * pushing.
  1647. */
  1648. task = pick_next_pushable_task(rq);
  1649. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1650. /*
  1651. * The task hasn't migrated, and is still the next
  1652. * eligible task, but we failed to find a run-queue
  1653. * to push it to. Do not retry in this case, since
  1654. * other cpus will pull from us when ready.
  1655. */
  1656. goto out;
  1657. }
  1658. if (!task)
  1659. /* No more tasks, just exit */
  1660. goto out;
  1661. /*
  1662. * Something has shifted, try again.
  1663. */
  1664. put_task_struct(next_task);
  1665. next_task = task;
  1666. goto retry;
  1667. }
  1668. deactivate_task(rq, next_task, 0);
  1669. set_task_cpu(next_task, lowest_rq->cpu);
  1670. activate_task(lowest_rq, next_task, 0);
  1671. ret = 1;
  1672. resched_curr(lowest_rq);
  1673. double_unlock_balance(rq, lowest_rq);
  1674. out:
  1675. put_task_struct(next_task);
  1676. return ret;
  1677. }
  1678. static void push_rt_tasks(struct rq *rq)
  1679. {
  1680. /* push_rt_task will return true if it moved an RT */
  1681. while (push_rt_task(rq))
  1682. ;
  1683. }
  1684. static int pull_rt_task(struct rq *this_rq)
  1685. {
  1686. int this_cpu = this_rq->cpu, ret = 0, cpu;
  1687. struct task_struct *p;
  1688. struct rq *src_rq;
  1689. if (likely(!rt_overloaded(this_rq)))
  1690. return 0;
  1691. /*
  1692. * Match the barrier from rt_set_overloaded; this guarantees that if we
  1693. * see overloaded we must also see the rto_mask bit.
  1694. */
  1695. smp_rmb();
  1696. /* sched:add trace_sched*/
  1697. mt_sched_printf(sched_rt_info, "1. pull_rt_task %lu ", this_rq->rd->rto_mask->bits[0]);
  1698. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1699. if (this_cpu == cpu)
  1700. continue;
  1701. src_rq = cpu_rq(cpu);
  1702. /*
  1703. * Don't bother taking the src_rq->lock if the next highest
  1704. * task is known to be lower-priority than our current task.
  1705. * This may look racy, but if this value is about to go
  1706. * logically higher, the src_rq will push this task away.
  1707. * And if its going logically lower, we do not care
  1708. */
  1709. /* sched:add trace_sched*/
  1710. mt_sched_printf(sched_rt_info, "2. pull_rt_task %d %d ",
  1711. src_rq->rt.highest_prio.next, this_rq->rt.highest_prio.curr);
  1712. if (src_rq->rt.highest_prio.next >=
  1713. this_rq->rt.highest_prio.curr)
  1714. continue;
  1715. /*
  1716. * We can potentially drop this_rq's lock in
  1717. * double_lock_balance, and another CPU could
  1718. * alter this_rq
  1719. */
  1720. double_lock_balance(this_rq, src_rq);
  1721. /*
  1722. * We can pull only a task, which is pushable
  1723. * on its rq, and no others.
  1724. */
  1725. p = pick_highest_pushable_task(src_rq, this_cpu);
  1726. /*
  1727. * Do we have an RT task that preempts
  1728. * the to-be-scheduled task?
  1729. */
  1730. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1731. WARN_ON(p == src_rq->curr);
  1732. WARN_ON(!task_on_rq_queued(p));
  1733. /*
  1734. * There's a chance that p is higher in priority
  1735. * than what's currently running on its cpu.
  1736. * This is just that p is wakeing up and hasn't
  1737. * had a chance to schedule. We only pull
  1738. * p if it is lower in priority than the
  1739. * current task on the run queue
  1740. */
  1741. if (p->prio < src_rq->curr->prio)
  1742. goto skip;
  1743. ret = 1;
  1744. deactivate_task(src_rq, p, 0);
  1745. set_task_cpu(p, this_cpu);
  1746. activate_task(this_rq, p, 0);
  1747. /*
  1748. * We continue with the search, just in
  1749. * case there's an even higher prio task
  1750. * in another runqueue. (low likelihood
  1751. * but possible)
  1752. */
  1753. }
  1754. skip:
  1755. double_unlock_balance(this_rq, src_rq);
  1756. }
  1757. return ret;
  1758. }
  1759. static void post_schedule_rt(struct rq *rq)
  1760. {
  1761. push_rt_tasks(rq);
  1762. }
  1763. /*
  1764. * If we are not running and we are not going to reschedule soon, we should
  1765. * try to push tasks away now
  1766. */
  1767. static void task_woken_rt(struct rq *rq, struct task_struct *p)
  1768. {
  1769. if (!task_running(rq, p) &&
  1770. !test_tsk_need_resched(rq->curr) &&
  1771. has_pushable_tasks(rq) &&
  1772. p->nr_cpus_allowed > 1 &&
  1773. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  1774. (rq->curr->nr_cpus_allowed < 2 ||
  1775. rq->curr->prio <= p->prio))
  1776. push_rt_tasks(rq);
  1777. }
  1778. static void set_cpus_allowed_rt(struct task_struct *p,
  1779. const struct cpumask *new_mask)
  1780. {
  1781. struct rq *rq;
  1782. int weight;
  1783. BUG_ON(!rt_task(p));
  1784. if (!task_on_rq_queued(p))
  1785. return;
  1786. weight = cpumask_weight(new_mask);
  1787. /*
  1788. * Only update if the process changes its state from whether it
  1789. * can migrate or not.
  1790. */
  1791. if ((p->nr_cpus_allowed > 1) == (weight > 1))
  1792. return;
  1793. rq = task_rq(p);
  1794. /*
  1795. * The process used to be able to migrate OR it can now migrate
  1796. */
  1797. if (weight <= 1) {
  1798. if (!task_current(rq, p))
  1799. dequeue_pushable_task(rq, p);
  1800. BUG_ON(!rq->rt.rt_nr_migratory);
  1801. rq->rt.rt_nr_migratory--;
  1802. } else {
  1803. if (!task_current(rq, p))
  1804. enqueue_pushable_task(rq, p);
  1805. rq->rt.rt_nr_migratory++;
  1806. }
  1807. update_rt_migration(&rq->rt);
  1808. }
  1809. /* Assumes rq->lock is held */
  1810. static void rq_online_rt(struct rq *rq)
  1811. {
  1812. if (rq->rt.overloaded)
  1813. rt_set_overload(rq);
  1814. __enable_runtime(rq);
  1815. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1816. }
  1817. /* Assumes rq->lock is held */
  1818. static void rq_offline_rt(struct rq *rq)
  1819. {
  1820. if (rq->rt.overloaded)
  1821. rt_clear_overload(rq);
  1822. __disable_runtime(rq);
  1823. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1824. }
  1825. void unthrottle_offline_rt_rqs(struct rq *rq)
  1826. {
  1827. rt_rq_iter_t iter;
  1828. struct rt_rq *rt_rq;
  1829. for_each_rt_rq(rt_rq, iter, rq) {
  1830. if (rt_rq_throttled(rt_rq)) {
  1831. rt_rq->rt_throttled = 0;
  1832. printk_deferred("sched: migrate_tasks: RT throttling inactivated\n");
  1833. }
  1834. sched_rt_rq_enqueue(rt_rq);
  1835. }
  1836. }
  1837. /*
  1838. * When switch from the rt queue, we bring ourselves to a position
  1839. * that we might want to pull RT tasks from other runqueues.
  1840. */
  1841. static void switched_from_rt(struct rq *rq, struct task_struct *p)
  1842. {
  1843. /*
  1844. * If there are other RT tasks then we will reschedule
  1845. * and the scheduling of the other RT tasks will handle
  1846. * the balancing. But if we are the last RT task
  1847. * we may need to handle the pulling of RT tasks
  1848. * now.
  1849. */
  1850. if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
  1851. return;
  1852. if (pull_rt_task(rq))
  1853. resched_curr(rq);
  1854. }
  1855. void __init init_sched_rt_class(void)
  1856. {
  1857. unsigned int i;
  1858. for_each_possible_cpu(i) {
  1859. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  1860. GFP_KERNEL, cpu_to_node(i));
  1861. }
  1862. }
  1863. #endif /* CONFIG_SMP */
  1864. /*
  1865. * When switching a task to RT, we may overload the runqueue
  1866. * with RT tasks. In this case we try to push them off to
  1867. * other runqueues.
  1868. */
  1869. static void switched_to_rt(struct rq *rq, struct task_struct *p)
  1870. {
  1871. int check_resched = 1;
  1872. /*
  1873. * If we are already running, then there's nothing
  1874. * that needs to be done. But if we are not running
  1875. * we may need to preempt the current running task.
  1876. * If that current running task is also an RT task
  1877. * then see if we can move to another run queue.
  1878. */
  1879. if (task_on_rq_queued(p) && rq->curr != p) {
  1880. #ifdef CONFIG_SMP
  1881. if (p->nr_cpus_allowed > 1 && rq->rt.overloaded &&
  1882. /* Don't resched if we changed runqueues */
  1883. push_rt_task(rq) && rq != task_rq(p))
  1884. check_resched = 0;
  1885. #endif /* CONFIG_SMP */
  1886. if (check_resched && p->prio < rq->curr->prio)
  1887. resched_curr(rq);
  1888. }
  1889. }
  1890. /*
  1891. * Priority of the task has changed. This may cause
  1892. * us to initiate a push or pull.
  1893. */
  1894. static void
  1895. prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
  1896. {
  1897. if (!task_on_rq_queued(p))
  1898. return;
  1899. if (rq->curr == p) {
  1900. #ifdef CONFIG_SMP
  1901. /*
  1902. * If our priority decreases while running, we
  1903. * may need to pull tasks to this runqueue.
  1904. */
  1905. if (oldprio < p->prio)
  1906. pull_rt_task(rq);
  1907. /*
  1908. * If there's a higher priority task waiting to run
  1909. * then reschedule. Note, the above pull_rt_task
  1910. * can release the rq lock and p could migrate.
  1911. * Only reschedule if p is still on the same runqueue.
  1912. */
  1913. if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
  1914. resched_curr(rq);
  1915. #else
  1916. /* For UP simply resched on drop of prio */
  1917. if (oldprio < p->prio)
  1918. resched_curr(rq);
  1919. #endif /* CONFIG_SMP */
  1920. } else {
  1921. /*
  1922. * This task is not running, but if it is
  1923. * greater than the current running task
  1924. * then reschedule.
  1925. */
  1926. if (p->prio < rq->curr->prio)
  1927. resched_curr(rq);
  1928. }
  1929. }
  1930. static void watchdog(struct rq *rq, struct task_struct *p)
  1931. {
  1932. unsigned long soft, hard;
  1933. /* max may change after cur was read, this will be fixed next tick */
  1934. soft = task_rlimit(p, RLIMIT_RTTIME);
  1935. hard = task_rlimit_max(p, RLIMIT_RTTIME);
  1936. if (soft != RLIM_INFINITY) {
  1937. unsigned long next;
  1938. if (p->rt.watchdog_stamp != jiffies) {
  1939. p->rt.timeout++;
  1940. p->rt.watchdog_stamp = jiffies;
  1941. }
  1942. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1943. if (p->rt.timeout > next)
  1944. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1945. }
  1946. }
  1947. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1948. {
  1949. struct sched_rt_entity *rt_se = &p->rt;
  1950. update_curr_rt(rq);
  1951. watchdog(rq, p);
  1952. /*
  1953. * RR tasks need a special form of timeslice management.
  1954. * FIFO tasks have no timeslices.
  1955. */
  1956. if (p->policy != SCHED_RR)
  1957. return;
  1958. if (--p->rt.time_slice)
  1959. return;
  1960. p->rt.time_slice = sched_rr_timeslice;
  1961. /*
  1962. * Requeue to the end of queue if we (and all of our ancestors) are not
  1963. * the only element on the queue
  1964. */
  1965. for_each_sched_rt_entity(rt_se) {
  1966. if (rt_se->run_list.prev != rt_se->run_list.next) {
  1967. requeue_task_rt(rq, p, 0);
  1968. resched_curr(rq);
  1969. return;
  1970. }
  1971. }
  1972. }
  1973. static void set_curr_task_rt(struct rq *rq)
  1974. {
  1975. struct task_struct *p = rq->curr;
  1976. p->se.exec_start = rq_clock_task(rq);
  1977. /* The running task is never eligible for pushing */
  1978. dequeue_pushable_task(rq, p);
  1979. }
  1980. static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
  1981. {
  1982. /*
  1983. * Time slice is 0 for SCHED_FIFO tasks
  1984. */
  1985. if (task->policy == SCHED_RR)
  1986. return sched_rr_timeslice;
  1987. else
  1988. return 0;
  1989. }
  1990. const struct sched_class rt_sched_class = {
  1991. .next = &fair_sched_class,
  1992. .enqueue_task = enqueue_task_rt,
  1993. .dequeue_task = dequeue_task_rt,
  1994. .yield_task = yield_task_rt,
  1995. .check_preempt_curr = check_preempt_curr_rt,
  1996. .pick_next_task = pick_next_task_rt,
  1997. .put_prev_task = put_prev_task_rt,
  1998. #ifdef CONFIG_SMP
  1999. .select_task_rq = select_task_rq_rt,
  2000. .set_cpus_allowed = set_cpus_allowed_rt,
  2001. .rq_online = rq_online_rt,
  2002. .rq_offline = rq_offline_rt,
  2003. .post_schedule = post_schedule_rt,
  2004. .task_woken = task_woken_rt,
  2005. .switched_from = switched_from_rt,
  2006. #endif
  2007. .set_curr_task = set_curr_task_rt,
  2008. .task_tick = task_tick_rt,
  2009. .get_rr_interval = get_rr_interval_rt,
  2010. .prio_changed = prio_changed_rt,
  2011. .switched_to = switched_to_rt,
  2012. .update_curr = update_curr_rt,
  2013. };
  2014. #ifdef CONFIG_SCHED_DEBUG
  2015. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  2016. void print_rt_stats(struct seq_file *m, int cpu)
  2017. {
  2018. rt_rq_iter_t iter;
  2019. struct rt_rq *rt_rq;
  2020. rcu_read_lock();
  2021. for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
  2022. print_rt_rq(m, cpu, rt_rq);
  2023. rcu_read_unlock();
  2024. }
  2025. #endif /* CONFIG_SCHED_DEBUG */