rt.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  4. * policies)
  5. */
  6. int sched_rr_timeslice = RR_TIMESLICE;
  7. /* More than 4 hours if BW_SHIFT equals 20. */
  8. static const u64 max_rt_runtime = MAX_BW;
  9. /*
  10. * period over which we measure -rt task CPU usage in us.
  11. * default: 1s
  12. */
  13. int sysctl_sched_rt_period = 1000000;
  14. /*
  15. * part of the period that we allow rt tasks to run in us.
  16. * default: 0.95s
  17. */
  18. int sysctl_sched_rt_runtime = 950000;
  19. #ifdef CONFIG_SYSCTL
  20. static int sysctl_sched_rr_timeslice = (MSEC_PER_SEC * RR_TIMESLICE) / HZ;
  21. static int sched_rt_handler(const struct ctl_table *table, int write, void *buffer,
  22. size_t *lenp, loff_t *ppos);
  23. static int sched_rr_handler(const struct ctl_table *table, int write, void *buffer,
  24. size_t *lenp, loff_t *ppos);
  25. static struct ctl_table sched_rt_sysctls[] = {
  26. {
  27. .procname = "sched_rt_period_us",
  28. .data = &sysctl_sched_rt_period,
  29. .maxlen = sizeof(int),
  30. .mode = 0644,
  31. .proc_handler = sched_rt_handler,
  32. .extra1 = SYSCTL_ONE,
  33. .extra2 = SYSCTL_INT_MAX,
  34. },
  35. {
  36. .procname = "sched_rt_runtime_us",
  37. .data = &sysctl_sched_rt_runtime,
  38. .maxlen = sizeof(int),
  39. .mode = 0644,
  40. .proc_handler = sched_rt_handler,
  41. .extra1 = SYSCTL_NEG_ONE,
  42. .extra2 = (void *)&sysctl_sched_rt_period,
  43. },
  44. {
  45. .procname = "sched_rr_timeslice_ms",
  46. .data = &sysctl_sched_rr_timeslice,
  47. .maxlen = sizeof(int),
  48. .mode = 0644,
  49. .proc_handler = sched_rr_handler,
  50. },
  51. };
  52. static int __init sched_rt_sysctl_init(void)
  53. {
  54. register_sysctl_init("kernel", sched_rt_sysctls);
  55. return 0;
  56. }
  57. late_initcall(sched_rt_sysctl_init);
  58. #endif
  59. void init_rt_rq(struct rt_rq *rt_rq)
  60. {
  61. struct rt_prio_array *array;
  62. int i;
  63. array = &rt_rq->active;
  64. for (i = 0; i < MAX_RT_PRIO; i++) {
  65. INIT_LIST_HEAD(array->queue + i);
  66. __clear_bit(i, array->bitmap);
  67. }
  68. /* delimiter for bitsearch: */
  69. __set_bit(MAX_RT_PRIO, array->bitmap);
  70. #if defined CONFIG_SMP
  71. rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
  72. rt_rq->highest_prio.next = MAX_RT_PRIO-1;
  73. rt_rq->overloaded = 0;
  74. plist_head_init(&rt_rq->pushable_tasks);
  75. #endif /* CONFIG_SMP */
  76. /* We start is dequeued state, because no RT tasks are queued */
  77. rt_rq->rt_queued = 0;
  78. #ifdef CONFIG_RT_GROUP_SCHED
  79. rt_rq->rt_time = 0;
  80. rt_rq->rt_throttled = 0;
  81. rt_rq->rt_runtime = 0;
  82. raw_spin_lock_init(&rt_rq->rt_runtime_lock);
  83. #endif
  84. }
  85. #ifdef CONFIG_RT_GROUP_SCHED
  86. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
  87. static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
  88. {
  89. struct rt_bandwidth *rt_b =
  90. container_of(timer, struct rt_bandwidth, rt_period_timer);
  91. int idle = 0;
  92. int overrun;
  93. raw_spin_lock(&rt_b->rt_runtime_lock);
  94. for (;;) {
  95. overrun = hrtimer_forward_now(timer, rt_b->rt_period);
  96. if (!overrun)
  97. break;
  98. raw_spin_unlock(&rt_b->rt_runtime_lock);
  99. idle = do_sched_rt_period_timer(rt_b, overrun);
  100. raw_spin_lock(&rt_b->rt_runtime_lock);
  101. }
  102. if (idle)
  103. rt_b->rt_period_active = 0;
  104. raw_spin_unlock(&rt_b->rt_runtime_lock);
  105. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  106. }
  107. void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  108. {
  109. rt_b->rt_period = ns_to_ktime(period);
  110. rt_b->rt_runtime = runtime;
  111. raw_spin_lock_init(&rt_b->rt_runtime_lock);
  112. hrtimer_init(&rt_b->rt_period_timer, CLOCK_MONOTONIC,
  113. HRTIMER_MODE_REL_HARD);
  114. rt_b->rt_period_timer.function = sched_rt_period_timer;
  115. }
  116. static inline void do_start_rt_bandwidth(struct rt_bandwidth *rt_b)
  117. {
  118. raw_spin_lock(&rt_b->rt_runtime_lock);
  119. if (!rt_b->rt_period_active) {
  120. rt_b->rt_period_active = 1;
  121. /*
  122. * SCHED_DEADLINE updates the bandwidth, as a run away
  123. * RT task with a DL task could hog a CPU. But DL does
  124. * not reset the period. If a deadline task was running
  125. * without an RT task running, it can cause RT tasks to
  126. * throttle when they start up. Kick the timer right away
  127. * to update the period.
  128. */
  129. hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
  130. hrtimer_start_expires(&rt_b->rt_period_timer,
  131. HRTIMER_MODE_ABS_PINNED_HARD);
  132. }
  133. raw_spin_unlock(&rt_b->rt_runtime_lock);
  134. }
  135. static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
  136. {
  137. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  138. return;
  139. do_start_rt_bandwidth(rt_b);
  140. }
  141. static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
  142. {
  143. hrtimer_cancel(&rt_b->rt_period_timer);
  144. }
  145. #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
  146. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  147. {
  148. #ifdef CONFIG_SCHED_DEBUG
  149. WARN_ON_ONCE(!rt_entity_is_task(rt_se));
  150. #endif
  151. return container_of(rt_se, struct task_struct, rt);
  152. }
  153. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  154. {
  155. return rt_rq->rq;
  156. }
  157. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  158. {
  159. return rt_se->rt_rq;
  160. }
  161. static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
  162. {
  163. struct rt_rq *rt_rq = rt_se->rt_rq;
  164. return rt_rq->rq;
  165. }
  166. void unregister_rt_sched_group(struct task_group *tg)
  167. {
  168. if (tg->rt_se)
  169. destroy_rt_bandwidth(&tg->rt_bandwidth);
  170. }
  171. void free_rt_sched_group(struct task_group *tg)
  172. {
  173. int i;
  174. for_each_possible_cpu(i) {
  175. if (tg->rt_rq)
  176. kfree(tg->rt_rq[i]);
  177. if (tg->rt_se)
  178. kfree(tg->rt_se[i]);
  179. }
  180. kfree(tg->rt_rq);
  181. kfree(tg->rt_se);
  182. }
  183. void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  184. struct sched_rt_entity *rt_se, int cpu,
  185. struct sched_rt_entity *parent)
  186. {
  187. struct rq *rq = cpu_rq(cpu);
  188. rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
  189. rt_rq->rt_nr_boosted = 0;
  190. rt_rq->rq = rq;
  191. rt_rq->tg = tg;
  192. tg->rt_rq[cpu] = rt_rq;
  193. tg->rt_se[cpu] = rt_se;
  194. if (!rt_se)
  195. return;
  196. if (!parent)
  197. rt_se->rt_rq = &rq->rt;
  198. else
  199. rt_se->rt_rq = parent->my_q;
  200. rt_se->my_q = rt_rq;
  201. rt_se->parent = parent;
  202. INIT_LIST_HEAD(&rt_se->run_list);
  203. }
  204. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  205. {
  206. struct rt_rq *rt_rq;
  207. struct sched_rt_entity *rt_se;
  208. int i;
  209. tg->rt_rq = kcalloc(nr_cpu_ids, sizeof(rt_rq), GFP_KERNEL);
  210. if (!tg->rt_rq)
  211. goto err;
  212. tg->rt_se = kcalloc(nr_cpu_ids, sizeof(rt_se), GFP_KERNEL);
  213. if (!tg->rt_se)
  214. goto err;
  215. init_rt_bandwidth(&tg->rt_bandwidth, ktime_to_ns(global_rt_period()), 0);
  216. for_each_possible_cpu(i) {
  217. rt_rq = kzalloc_node(sizeof(struct rt_rq),
  218. GFP_KERNEL, cpu_to_node(i));
  219. if (!rt_rq)
  220. goto err;
  221. rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
  222. GFP_KERNEL, cpu_to_node(i));
  223. if (!rt_se)
  224. goto err_free_rq;
  225. init_rt_rq(rt_rq);
  226. rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
  227. init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
  228. }
  229. return 1;
  230. err_free_rq:
  231. kfree(rt_rq);
  232. err:
  233. return 0;
  234. }
  235. #else /* CONFIG_RT_GROUP_SCHED */
  236. #define rt_entity_is_task(rt_se) (1)
  237. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  238. {
  239. return container_of(rt_se, struct task_struct, rt);
  240. }
  241. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  242. {
  243. return container_of(rt_rq, struct rq, rt);
  244. }
  245. static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
  246. {
  247. struct task_struct *p = rt_task_of(rt_se);
  248. return task_rq(p);
  249. }
  250. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  251. {
  252. struct rq *rq = rq_of_rt_se(rt_se);
  253. return &rq->rt;
  254. }
  255. void unregister_rt_sched_group(struct task_group *tg) { }
  256. void free_rt_sched_group(struct task_group *tg) { }
  257. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  258. {
  259. return 1;
  260. }
  261. #endif /* CONFIG_RT_GROUP_SCHED */
  262. #ifdef CONFIG_SMP
  263. static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
  264. {
  265. /* Try to pull RT tasks here if we lower this rq's prio */
  266. return rq->online && rq->rt.highest_prio.curr > prev->prio;
  267. }
  268. static inline int rt_overloaded(struct rq *rq)
  269. {
  270. return atomic_read(&rq->rd->rto_count);
  271. }
  272. static inline void rt_set_overload(struct rq *rq)
  273. {
  274. if (!rq->online)
  275. return;
  276. cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
  277. /*
  278. * Make sure the mask is visible before we set
  279. * the overload count. That is checked to determine
  280. * if we should look at the mask. It would be a shame
  281. * if we looked at the mask, but the mask was not
  282. * updated yet.
  283. *
  284. * Matched by the barrier in pull_rt_task().
  285. */
  286. smp_wmb();
  287. atomic_inc(&rq->rd->rto_count);
  288. }
  289. static inline void rt_clear_overload(struct rq *rq)
  290. {
  291. if (!rq->online)
  292. return;
  293. /* the order here really doesn't matter */
  294. atomic_dec(&rq->rd->rto_count);
  295. cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
  296. }
  297. static inline int has_pushable_tasks(struct rq *rq)
  298. {
  299. return !plist_head_empty(&rq->rt.pushable_tasks);
  300. }
  301. static DEFINE_PER_CPU(struct balance_callback, rt_push_head);
  302. static DEFINE_PER_CPU(struct balance_callback, rt_pull_head);
  303. static void push_rt_tasks(struct rq *);
  304. static void pull_rt_task(struct rq *);
  305. static inline void rt_queue_push_tasks(struct rq *rq)
  306. {
  307. if (!has_pushable_tasks(rq))
  308. return;
  309. queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
  310. }
  311. static inline void rt_queue_pull_task(struct rq *rq)
  312. {
  313. queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
  314. }
  315. static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  316. {
  317. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  318. plist_node_init(&p->pushable_tasks, p->prio);
  319. plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
  320. /* Update the highest prio pushable task */
  321. if (p->prio < rq->rt.highest_prio.next)
  322. rq->rt.highest_prio.next = p->prio;
  323. if (!rq->rt.overloaded) {
  324. rt_set_overload(rq);
  325. rq->rt.overloaded = 1;
  326. }
  327. }
  328. static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  329. {
  330. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  331. /* Update the new highest prio pushable task */
  332. if (has_pushable_tasks(rq)) {
  333. p = plist_first_entry(&rq->rt.pushable_tasks,
  334. struct task_struct, pushable_tasks);
  335. rq->rt.highest_prio.next = p->prio;
  336. } else {
  337. rq->rt.highest_prio.next = MAX_RT_PRIO-1;
  338. if (rq->rt.overloaded) {
  339. rt_clear_overload(rq);
  340. rq->rt.overloaded = 0;
  341. }
  342. }
  343. }
  344. #else
  345. static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  346. {
  347. }
  348. static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  349. {
  350. }
  351. static inline void rt_queue_push_tasks(struct rq *rq)
  352. {
  353. }
  354. #endif /* CONFIG_SMP */
  355. static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
  356. static void dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count);
  357. static inline int on_rt_rq(struct sched_rt_entity *rt_se)
  358. {
  359. return rt_se->on_rq;
  360. }
  361. #ifdef CONFIG_UCLAMP_TASK
  362. /*
  363. * Verify the fitness of task @p to run on @cpu taking into account the uclamp
  364. * settings.
  365. *
  366. * This check is only important for heterogeneous systems where uclamp_min value
  367. * is higher than the capacity of a @cpu. For non-heterogeneous system this
  368. * function will always return true.
  369. *
  370. * The function will return true if the capacity of the @cpu is >= the
  371. * uclamp_min and false otherwise.
  372. *
  373. * Note that uclamp_min will be clamped to uclamp_max if uclamp_min
  374. * > uclamp_max.
  375. */
  376. static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
  377. {
  378. unsigned int min_cap;
  379. unsigned int max_cap;
  380. unsigned int cpu_cap;
  381. /* Only heterogeneous systems can benefit from this check */
  382. if (!sched_asym_cpucap_active())
  383. return true;
  384. min_cap = uclamp_eff_value(p, UCLAMP_MIN);
  385. max_cap = uclamp_eff_value(p, UCLAMP_MAX);
  386. cpu_cap = arch_scale_cpu_capacity(cpu);
  387. return cpu_cap >= min(min_cap, max_cap);
  388. }
  389. #else
  390. static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
  391. {
  392. return true;
  393. }
  394. #endif
  395. #ifdef CONFIG_RT_GROUP_SCHED
  396. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  397. {
  398. if (!rt_rq->tg)
  399. return RUNTIME_INF;
  400. return rt_rq->rt_runtime;
  401. }
  402. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  403. {
  404. return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
  405. }
  406. typedef struct task_group *rt_rq_iter_t;
  407. static inline struct task_group *next_task_group(struct task_group *tg)
  408. {
  409. do {
  410. tg = list_entry_rcu(tg->list.next,
  411. typeof(struct task_group), list);
  412. } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
  413. if (&tg->list == &task_groups)
  414. tg = NULL;
  415. return tg;
  416. }
  417. #define for_each_rt_rq(rt_rq, iter, rq) \
  418. for (iter = container_of(&task_groups, typeof(*iter), list); \
  419. (iter = next_task_group(iter)) && \
  420. (rt_rq = iter->rt_rq[cpu_of(rq)]);)
  421. #define for_each_sched_rt_entity(rt_se) \
  422. for (; rt_se; rt_se = rt_se->parent)
  423. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  424. {
  425. return rt_se->my_q;
  426. }
  427. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
  428. static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
  429. static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  430. {
  431. struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
  432. struct rq *rq = rq_of_rt_rq(rt_rq);
  433. struct sched_rt_entity *rt_se;
  434. int cpu = cpu_of(rq);
  435. rt_se = rt_rq->tg->rt_se[cpu];
  436. if (rt_rq->rt_nr_running) {
  437. if (!rt_se)
  438. enqueue_top_rt_rq(rt_rq);
  439. else if (!on_rt_rq(rt_se))
  440. enqueue_rt_entity(rt_se, 0);
  441. if (rt_rq->highest_prio.curr < curr->prio)
  442. resched_curr(rq);
  443. }
  444. }
  445. static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  446. {
  447. struct sched_rt_entity *rt_se;
  448. int cpu = cpu_of(rq_of_rt_rq(rt_rq));
  449. rt_se = rt_rq->tg->rt_se[cpu];
  450. if (!rt_se) {
  451. dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
  452. /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
  453. cpufreq_update_util(rq_of_rt_rq(rt_rq), 0);
  454. }
  455. else if (on_rt_rq(rt_se))
  456. dequeue_rt_entity(rt_se, 0);
  457. }
  458. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  459. {
  460. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  461. }
  462. static int rt_se_boosted(struct sched_rt_entity *rt_se)
  463. {
  464. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  465. struct task_struct *p;
  466. if (rt_rq)
  467. return !!rt_rq->rt_nr_boosted;
  468. p = rt_task_of(rt_se);
  469. return p->prio != p->normal_prio;
  470. }
  471. #ifdef CONFIG_SMP
  472. static inline const struct cpumask *sched_rt_period_mask(void)
  473. {
  474. return this_rq()->rd->span;
  475. }
  476. #else
  477. static inline const struct cpumask *sched_rt_period_mask(void)
  478. {
  479. return cpu_online_mask;
  480. }
  481. #endif
  482. static inline
  483. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  484. {
  485. return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
  486. }
  487. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  488. {
  489. return &rt_rq->tg->rt_bandwidth;
  490. }
  491. bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
  492. {
  493. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  494. return (hrtimer_active(&rt_b->rt_period_timer) ||
  495. rt_rq->rt_time < rt_b->rt_runtime);
  496. }
  497. #ifdef CONFIG_SMP
  498. /*
  499. * We ran out of runtime, see if we can borrow some from our neighbours.
  500. */
  501. static void do_balance_runtime(struct rt_rq *rt_rq)
  502. {
  503. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  504. struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
  505. int i, weight;
  506. u64 rt_period;
  507. weight = cpumask_weight(rd->span);
  508. raw_spin_lock(&rt_b->rt_runtime_lock);
  509. rt_period = ktime_to_ns(rt_b->rt_period);
  510. for_each_cpu(i, rd->span) {
  511. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  512. s64 diff;
  513. if (iter == rt_rq)
  514. continue;
  515. raw_spin_lock(&iter->rt_runtime_lock);
  516. /*
  517. * Either all rqs have inf runtime and there's nothing to steal
  518. * or __disable_runtime() below sets a specific rq to inf to
  519. * indicate its been disabled and disallow stealing.
  520. */
  521. if (iter->rt_runtime == RUNTIME_INF)
  522. goto next;
  523. /*
  524. * From runqueues with spare time, take 1/n part of their
  525. * spare time, but no more than our period.
  526. */
  527. diff = iter->rt_runtime - iter->rt_time;
  528. if (diff > 0) {
  529. diff = div_u64((u64)diff, weight);
  530. if (rt_rq->rt_runtime + diff > rt_period)
  531. diff = rt_period - rt_rq->rt_runtime;
  532. iter->rt_runtime -= diff;
  533. rt_rq->rt_runtime += diff;
  534. if (rt_rq->rt_runtime == rt_period) {
  535. raw_spin_unlock(&iter->rt_runtime_lock);
  536. break;
  537. }
  538. }
  539. next:
  540. raw_spin_unlock(&iter->rt_runtime_lock);
  541. }
  542. raw_spin_unlock(&rt_b->rt_runtime_lock);
  543. }
  544. /*
  545. * Ensure this RQ takes back all the runtime it lend to its neighbours.
  546. */
  547. static void __disable_runtime(struct rq *rq)
  548. {
  549. struct root_domain *rd = rq->rd;
  550. rt_rq_iter_t iter;
  551. struct rt_rq *rt_rq;
  552. if (unlikely(!scheduler_running))
  553. return;
  554. for_each_rt_rq(rt_rq, iter, rq) {
  555. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  556. s64 want;
  557. int i;
  558. raw_spin_lock(&rt_b->rt_runtime_lock);
  559. raw_spin_lock(&rt_rq->rt_runtime_lock);
  560. /*
  561. * Either we're all inf and nobody needs to borrow, or we're
  562. * already disabled and thus have nothing to do, or we have
  563. * exactly the right amount of runtime to take out.
  564. */
  565. if (rt_rq->rt_runtime == RUNTIME_INF ||
  566. rt_rq->rt_runtime == rt_b->rt_runtime)
  567. goto balanced;
  568. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  569. /*
  570. * Calculate the difference between what we started out with
  571. * and what we current have, that's the amount of runtime
  572. * we lend and now have to reclaim.
  573. */
  574. want = rt_b->rt_runtime - rt_rq->rt_runtime;
  575. /*
  576. * Greedy reclaim, take back as much as we can.
  577. */
  578. for_each_cpu(i, rd->span) {
  579. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  580. s64 diff;
  581. /*
  582. * Can't reclaim from ourselves or disabled runqueues.
  583. */
  584. if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
  585. continue;
  586. raw_spin_lock(&iter->rt_runtime_lock);
  587. if (want > 0) {
  588. diff = min_t(s64, iter->rt_runtime, want);
  589. iter->rt_runtime -= diff;
  590. want -= diff;
  591. } else {
  592. iter->rt_runtime -= want;
  593. want -= want;
  594. }
  595. raw_spin_unlock(&iter->rt_runtime_lock);
  596. if (!want)
  597. break;
  598. }
  599. raw_spin_lock(&rt_rq->rt_runtime_lock);
  600. /*
  601. * We cannot be left wanting - that would mean some runtime
  602. * leaked out of the system.
  603. */
  604. WARN_ON_ONCE(want);
  605. balanced:
  606. /*
  607. * Disable all the borrow logic by pretending we have inf
  608. * runtime - in which case borrowing doesn't make sense.
  609. */
  610. rt_rq->rt_runtime = RUNTIME_INF;
  611. rt_rq->rt_throttled = 0;
  612. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  613. raw_spin_unlock(&rt_b->rt_runtime_lock);
  614. /* Make rt_rq available for pick_next_task() */
  615. sched_rt_rq_enqueue(rt_rq);
  616. }
  617. }
  618. static void __enable_runtime(struct rq *rq)
  619. {
  620. rt_rq_iter_t iter;
  621. struct rt_rq *rt_rq;
  622. if (unlikely(!scheduler_running))
  623. return;
  624. /*
  625. * Reset each runqueue's bandwidth settings
  626. */
  627. for_each_rt_rq(rt_rq, iter, rq) {
  628. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  629. raw_spin_lock(&rt_b->rt_runtime_lock);
  630. raw_spin_lock(&rt_rq->rt_runtime_lock);
  631. rt_rq->rt_runtime = rt_b->rt_runtime;
  632. rt_rq->rt_time = 0;
  633. rt_rq->rt_throttled = 0;
  634. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  635. raw_spin_unlock(&rt_b->rt_runtime_lock);
  636. }
  637. }
  638. static void balance_runtime(struct rt_rq *rt_rq)
  639. {
  640. if (!sched_feat(RT_RUNTIME_SHARE))
  641. return;
  642. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  643. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  644. do_balance_runtime(rt_rq);
  645. raw_spin_lock(&rt_rq->rt_runtime_lock);
  646. }
  647. }
  648. #else /* !CONFIG_SMP */
  649. static inline void balance_runtime(struct rt_rq *rt_rq) {}
  650. #endif /* CONFIG_SMP */
  651. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  652. {
  653. int i, idle = 1, throttled = 0;
  654. const struct cpumask *span;
  655. span = sched_rt_period_mask();
  656. /*
  657. * FIXME: isolated CPUs should really leave the root task group,
  658. * whether they are isolcpus or were isolated via cpusets, lest
  659. * the timer run on a CPU which does not service all runqueues,
  660. * potentially leaving other CPUs indefinitely throttled. If
  661. * isolation is really required, the user will turn the throttle
  662. * off to kill the perturbations it causes anyway. Meanwhile,
  663. * this maintains functionality for boot and/or troubleshooting.
  664. */
  665. if (rt_b == &root_task_group.rt_bandwidth)
  666. span = cpu_online_mask;
  667. for_each_cpu(i, span) {
  668. int enqueue = 0;
  669. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  670. struct rq *rq = rq_of_rt_rq(rt_rq);
  671. struct rq_flags rf;
  672. int skip;
  673. /*
  674. * When span == cpu_online_mask, taking each rq->lock
  675. * can be time-consuming. Try to avoid it when possible.
  676. */
  677. raw_spin_lock(&rt_rq->rt_runtime_lock);
  678. if (!sched_feat(RT_RUNTIME_SHARE) && rt_rq->rt_runtime != RUNTIME_INF)
  679. rt_rq->rt_runtime = rt_b->rt_runtime;
  680. skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
  681. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  682. if (skip)
  683. continue;
  684. rq_lock(rq, &rf);
  685. update_rq_clock(rq);
  686. if (rt_rq->rt_time) {
  687. u64 runtime;
  688. raw_spin_lock(&rt_rq->rt_runtime_lock);
  689. if (rt_rq->rt_throttled)
  690. balance_runtime(rt_rq);
  691. runtime = rt_rq->rt_runtime;
  692. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  693. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  694. rt_rq->rt_throttled = 0;
  695. enqueue = 1;
  696. /*
  697. * When we're idle and a woken (rt) task is
  698. * throttled wakeup_preempt() will set
  699. * skip_update and the time between the wakeup
  700. * and this unthrottle will get accounted as
  701. * 'runtime'.
  702. */
  703. if (rt_rq->rt_nr_running && rq->curr == rq->idle)
  704. rq_clock_cancel_skipupdate(rq);
  705. }
  706. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  707. idle = 0;
  708. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  709. } else if (rt_rq->rt_nr_running) {
  710. idle = 0;
  711. if (!rt_rq_throttled(rt_rq))
  712. enqueue = 1;
  713. }
  714. if (rt_rq->rt_throttled)
  715. throttled = 1;
  716. if (enqueue)
  717. sched_rt_rq_enqueue(rt_rq);
  718. rq_unlock(rq, &rf);
  719. }
  720. if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
  721. return 1;
  722. return idle;
  723. }
  724. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  725. {
  726. u64 runtime = sched_rt_runtime(rt_rq);
  727. if (rt_rq->rt_throttled)
  728. return rt_rq_throttled(rt_rq);
  729. if (runtime >= sched_rt_period(rt_rq))
  730. return 0;
  731. balance_runtime(rt_rq);
  732. runtime = sched_rt_runtime(rt_rq);
  733. if (runtime == RUNTIME_INF)
  734. return 0;
  735. if (rt_rq->rt_time > runtime) {
  736. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  737. /*
  738. * Don't actually throttle groups that have no runtime assigned
  739. * but accrue some time due to boosting.
  740. */
  741. if (likely(rt_b->rt_runtime)) {
  742. rt_rq->rt_throttled = 1;
  743. printk_deferred_once("sched: RT throttling activated\n");
  744. } else {
  745. /*
  746. * In case we did anyway, make it go away,
  747. * replenishment is a joke, since it will replenish us
  748. * with exactly 0 ns.
  749. */
  750. rt_rq->rt_time = 0;
  751. }
  752. if (rt_rq_throttled(rt_rq)) {
  753. sched_rt_rq_dequeue(rt_rq);
  754. return 1;
  755. }
  756. }
  757. return 0;
  758. }
  759. #else /* !CONFIG_RT_GROUP_SCHED */
  760. typedef struct rt_rq *rt_rq_iter_t;
  761. #define for_each_rt_rq(rt_rq, iter, rq) \
  762. for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
  763. #define for_each_sched_rt_entity(rt_se) \
  764. for (; rt_se; rt_se = NULL)
  765. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  766. {
  767. return NULL;
  768. }
  769. static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  770. {
  771. struct rq *rq = rq_of_rt_rq(rt_rq);
  772. if (!rt_rq->rt_nr_running)
  773. return;
  774. enqueue_top_rt_rq(rt_rq);
  775. resched_curr(rq);
  776. }
  777. static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  778. {
  779. dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
  780. }
  781. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  782. {
  783. return false;
  784. }
  785. static inline const struct cpumask *sched_rt_period_mask(void)
  786. {
  787. return cpu_online_mask;
  788. }
  789. static inline
  790. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  791. {
  792. return &cpu_rq(cpu)->rt;
  793. }
  794. #ifdef CONFIG_SMP
  795. static void __enable_runtime(struct rq *rq) { }
  796. static void __disable_runtime(struct rq *rq) { }
  797. #endif
  798. #endif /* CONFIG_RT_GROUP_SCHED */
  799. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  800. {
  801. #ifdef CONFIG_RT_GROUP_SCHED
  802. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  803. if (rt_rq)
  804. return rt_rq->highest_prio.curr;
  805. #endif
  806. return rt_task_of(rt_se)->prio;
  807. }
  808. /*
  809. * Update the current task's runtime statistics. Skip current tasks that
  810. * are not in our scheduling class.
  811. */
  812. static void update_curr_rt(struct rq *rq)
  813. {
  814. struct task_struct *curr = rq->curr;
  815. s64 delta_exec;
  816. if (curr->sched_class != &rt_sched_class)
  817. return;
  818. delta_exec = update_curr_common(rq);
  819. if (unlikely(delta_exec <= 0))
  820. return;
  821. #ifdef CONFIG_RT_GROUP_SCHED
  822. struct sched_rt_entity *rt_se = &curr->rt;
  823. if (!rt_bandwidth_enabled())
  824. return;
  825. for_each_sched_rt_entity(rt_se) {
  826. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  827. int exceeded;
  828. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  829. raw_spin_lock(&rt_rq->rt_runtime_lock);
  830. rt_rq->rt_time += delta_exec;
  831. exceeded = sched_rt_runtime_exceeded(rt_rq);
  832. if (exceeded)
  833. resched_curr(rq);
  834. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  835. if (exceeded)
  836. do_start_rt_bandwidth(sched_rt_bandwidth(rt_rq));
  837. }
  838. }
  839. #endif
  840. }
  841. static void
  842. dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count)
  843. {
  844. struct rq *rq = rq_of_rt_rq(rt_rq);
  845. BUG_ON(&rq->rt != rt_rq);
  846. if (!rt_rq->rt_queued)
  847. return;
  848. BUG_ON(!rq->nr_running);
  849. sub_nr_running(rq, count);
  850. rt_rq->rt_queued = 0;
  851. }
  852. static void
  853. enqueue_top_rt_rq(struct rt_rq *rt_rq)
  854. {
  855. struct rq *rq = rq_of_rt_rq(rt_rq);
  856. BUG_ON(&rq->rt != rt_rq);
  857. if (rt_rq->rt_queued)
  858. return;
  859. if (rt_rq_throttled(rt_rq))
  860. return;
  861. if (rt_rq->rt_nr_running) {
  862. add_nr_running(rq, rt_rq->rt_nr_running);
  863. rt_rq->rt_queued = 1;
  864. }
  865. /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
  866. cpufreq_update_util(rq, 0);
  867. }
  868. #if defined CONFIG_SMP
  869. static void
  870. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  871. {
  872. struct rq *rq = rq_of_rt_rq(rt_rq);
  873. #ifdef CONFIG_RT_GROUP_SCHED
  874. /*
  875. * Change rq's cpupri only if rt_rq is the top queue.
  876. */
  877. if (&rq->rt != rt_rq)
  878. return;
  879. #endif
  880. if (rq->online && prio < prev_prio)
  881. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  882. }
  883. static void
  884. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  885. {
  886. struct rq *rq = rq_of_rt_rq(rt_rq);
  887. #ifdef CONFIG_RT_GROUP_SCHED
  888. /*
  889. * Change rq's cpupri only if rt_rq is the top queue.
  890. */
  891. if (&rq->rt != rt_rq)
  892. return;
  893. #endif
  894. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  895. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  896. }
  897. #else /* CONFIG_SMP */
  898. static inline
  899. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  900. static inline
  901. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  902. #endif /* CONFIG_SMP */
  903. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  904. static void
  905. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  906. {
  907. int prev_prio = rt_rq->highest_prio.curr;
  908. if (prio < prev_prio)
  909. rt_rq->highest_prio.curr = prio;
  910. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  911. }
  912. static void
  913. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  914. {
  915. int prev_prio = rt_rq->highest_prio.curr;
  916. if (rt_rq->rt_nr_running) {
  917. WARN_ON(prio < prev_prio);
  918. /*
  919. * This may have been our highest task, and therefore
  920. * we may have some re-computation to do
  921. */
  922. if (prio == prev_prio) {
  923. struct rt_prio_array *array = &rt_rq->active;
  924. rt_rq->highest_prio.curr =
  925. sched_find_first_bit(array->bitmap);
  926. }
  927. } else {
  928. rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
  929. }
  930. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  931. }
  932. #else
  933. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  934. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  935. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  936. #ifdef CONFIG_RT_GROUP_SCHED
  937. static void
  938. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  939. {
  940. if (rt_se_boosted(rt_se))
  941. rt_rq->rt_nr_boosted++;
  942. if (rt_rq->tg)
  943. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  944. }
  945. static void
  946. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  947. {
  948. if (rt_se_boosted(rt_se))
  949. rt_rq->rt_nr_boosted--;
  950. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  951. }
  952. #else /* CONFIG_RT_GROUP_SCHED */
  953. static void
  954. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  955. {
  956. }
  957. static inline
  958. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  959. #endif /* CONFIG_RT_GROUP_SCHED */
  960. static inline
  961. unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
  962. {
  963. struct rt_rq *group_rq = group_rt_rq(rt_se);
  964. if (group_rq)
  965. return group_rq->rt_nr_running;
  966. else
  967. return 1;
  968. }
  969. static inline
  970. unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
  971. {
  972. struct rt_rq *group_rq = group_rt_rq(rt_se);
  973. struct task_struct *tsk;
  974. if (group_rq)
  975. return group_rq->rr_nr_running;
  976. tsk = rt_task_of(rt_se);
  977. return (tsk->policy == SCHED_RR) ? 1 : 0;
  978. }
  979. static inline
  980. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  981. {
  982. int prio = rt_se_prio(rt_se);
  983. WARN_ON(!rt_prio(prio));
  984. rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
  985. rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
  986. inc_rt_prio(rt_rq, prio);
  987. inc_rt_group(rt_se, rt_rq);
  988. }
  989. static inline
  990. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  991. {
  992. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  993. WARN_ON(!rt_rq->rt_nr_running);
  994. rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
  995. rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
  996. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  997. dec_rt_group(rt_se, rt_rq);
  998. }
  999. /*
  1000. * Change rt_se->run_list location unless SAVE && !MOVE
  1001. *
  1002. * assumes ENQUEUE/DEQUEUE flags match
  1003. */
  1004. static inline bool move_entity(unsigned int flags)
  1005. {
  1006. if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
  1007. return false;
  1008. return true;
  1009. }
  1010. static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
  1011. {
  1012. list_del_init(&rt_se->run_list);
  1013. if (list_empty(array->queue + rt_se_prio(rt_se)))
  1014. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  1015. rt_se->on_list = 0;
  1016. }
  1017. static inline struct sched_statistics *
  1018. __schedstats_from_rt_se(struct sched_rt_entity *rt_se)
  1019. {
  1020. #ifdef CONFIG_RT_GROUP_SCHED
  1021. /* schedstats is not supported for rt group. */
  1022. if (!rt_entity_is_task(rt_se))
  1023. return NULL;
  1024. #endif
  1025. return &rt_task_of(rt_se)->stats;
  1026. }
  1027. static inline void
  1028. update_stats_wait_start_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
  1029. {
  1030. struct sched_statistics *stats;
  1031. struct task_struct *p = NULL;
  1032. if (!schedstat_enabled())
  1033. return;
  1034. if (rt_entity_is_task(rt_se))
  1035. p = rt_task_of(rt_se);
  1036. stats = __schedstats_from_rt_se(rt_se);
  1037. if (!stats)
  1038. return;
  1039. __update_stats_wait_start(rq_of_rt_rq(rt_rq), p, stats);
  1040. }
  1041. static inline void
  1042. update_stats_enqueue_sleeper_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
  1043. {
  1044. struct sched_statistics *stats;
  1045. struct task_struct *p = NULL;
  1046. if (!schedstat_enabled())
  1047. return;
  1048. if (rt_entity_is_task(rt_se))
  1049. p = rt_task_of(rt_se);
  1050. stats = __schedstats_from_rt_se(rt_se);
  1051. if (!stats)
  1052. return;
  1053. __update_stats_enqueue_sleeper(rq_of_rt_rq(rt_rq), p, stats);
  1054. }
  1055. static inline void
  1056. update_stats_enqueue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
  1057. int flags)
  1058. {
  1059. if (!schedstat_enabled())
  1060. return;
  1061. if (flags & ENQUEUE_WAKEUP)
  1062. update_stats_enqueue_sleeper_rt(rt_rq, rt_se);
  1063. }
  1064. static inline void
  1065. update_stats_wait_end_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
  1066. {
  1067. struct sched_statistics *stats;
  1068. struct task_struct *p = NULL;
  1069. if (!schedstat_enabled())
  1070. return;
  1071. if (rt_entity_is_task(rt_se))
  1072. p = rt_task_of(rt_se);
  1073. stats = __schedstats_from_rt_se(rt_se);
  1074. if (!stats)
  1075. return;
  1076. __update_stats_wait_end(rq_of_rt_rq(rt_rq), p, stats);
  1077. }
  1078. static inline void
  1079. update_stats_dequeue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
  1080. int flags)
  1081. {
  1082. struct task_struct *p = NULL;
  1083. if (!schedstat_enabled())
  1084. return;
  1085. if (rt_entity_is_task(rt_se))
  1086. p = rt_task_of(rt_se);
  1087. if ((flags & DEQUEUE_SLEEP) && p) {
  1088. unsigned int state;
  1089. state = READ_ONCE(p->__state);
  1090. if (state & TASK_INTERRUPTIBLE)
  1091. __schedstat_set(p->stats.sleep_start,
  1092. rq_clock(rq_of_rt_rq(rt_rq)));
  1093. if (state & TASK_UNINTERRUPTIBLE)
  1094. __schedstat_set(p->stats.block_start,
  1095. rq_clock(rq_of_rt_rq(rt_rq)));
  1096. }
  1097. }
  1098. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1099. {
  1100. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  1101. struct rt_prio_array *array = &rt_rq->active;
  1102. struct rt_rq *group_rq = group_rt_rq(rt_se);
  1103. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  1104. /*
  1105. * Don't enqueue the group if its throttled, or when empty.
  1106. * The latter is a consequence of the former when a child group
  1107. * get throttled and the current group doesn't have any other
  1108. * active members.
  1109. */
  1110. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
  1111. if (rt_se->on_list)
  1112. __delist_rt_entity(rt_se, array);
  1113. return;
  1114. }
  1115. if (move_entity(flags)) {
  1116. WARN_ON_ONCE(rt_se->on_list);
  1117. if (flags & ENQUEUE_HEAD)
  1118. list_add(&rt_se->run_list, queue);
  1119. else
  1120. list_add_tail(&rt_se->run_list, queue);
  1121. __set_bit(rt_se_prio(rt_se), array->bitmap);
  1122. rt_se->on_list = 1;
  1123. }
  1124. rt_se->on_rq = 1;
  1125. inc_rt_tasks(rt_se, rt_rq);
  1126. }
  1127. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1128. {
  1129. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  1130. struct rt_prio_array *array = &rt_rq->active;
  1131. if (move_entity(flags)) {
  1132. WARN_ON_ONCE(!rt_se->on_list);
  1133. __delist_rt_entity(rt_se, array);
  1134. }
  1135. rt_se->on_rq = 0;
  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, unsigned int flags)
  1143. {
  1144. struct sched_rt_entity *back = NULL;
  1145. unsigned int rt_nr_running;
  1146. for_each_sched_rt_entity(rt_se) {
  1147. rt_se->back = back;
  1148. back = rt_se;
  1149. }
  1150. rt_nr_running = rt_rq_of_se(back)->rt_nr_running;
  1151. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  1152. if (on_rt_rq(rt_se))
  1153. __dequeue_rt_entity(rt_se, flags);
  1154. }
  1155. dequeue_top_rt_rq(rt_rq_of_se(back), rt_nr_running);
  1156. }
  1157. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1158. {
  1159. struct rq *rq = rq_of_rt_se(rt_se);
  1160. update_stats_enqueue_rt(rt_rq_of_se(rt_se), rt_se, flags);
  1161. dequeue_rt_stack(rt_se, flags);
  1162. for_each_sched_rt_entity(rt_se)
  1163. __enqueue_rt_entity(rt_se, flags);
  1164. enqueue_top_rt_rq(&rq->rt);
  1165. }
  1166. static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1167. {
  1168. struct rq *rq = rq_of_rt_se(rt_se);
  1169. update_stats_dequeue_rt(rt_rq_of_se(rt_se), rt_se, flags);
  1170. dequeue_rt_stack(rt_se, flags);
  1171. for_each_sched_rt_entity(rt_se) {
  1172. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  1173. if (rt_rq && rt_rq->rt_nr_running)
  1174. __enqueue_rt_entity(rt_se, flags);
  1175. }
  1176. enqueue_top_rt_rq(&rq->rt);
  1177. }
  1178. /*
  1179. * Adding/removing a task to/from a priority array:
  1180. */
  1181. static void
  1182. enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1183. {
  1184. struct sched_rt_entity *rt_se = &p->rt;
  1185. if (flags & ENQUEUE_WAKEUP)
  1186. rt_se->timeout = 0;
  1187. check_schedstat_required();
  1188. update_stats_wait_start_rt(rt_rq_of_se(rt_se), rt_se);
  1189. enqueue_rt_entity(rt_se, flags);
  1190. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  1191. enqueue_pushable_task(rq, p);
  1192. }
  1193. static bool dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1194. {
  1195. struct sched_rt_entity *rt_se = &p->rt;
  1196. update_curr_rt(rq);
  1197. dequeue_rt_entity(rt_se, flags);
  1198. dequeue_pushable_task(rq, p);
  1199. return true;
  1200. }
  1201. /*
  1202. * Put task to the head or the end of the run list without the overhead of
  1203. * dequeue followed by enqueue.
  1204. */
  1205. static void
  1206. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  1207. {
  1208. if (on_rt_rq(rt_se)) {
  1209. struct rt_prio_array *array = &rt_rq->active;
  1210. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  1211. if (head)
  1212. list_move(&rt_se->run_list, queue);
  1213. else
  1214. list_move_tail(&rt_se->run_list, queue);
  1215. }
  1216. }
  1217. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  1218. {
  1219. struct sched_rt_entity *rt_se = &p->rt;
  1220. struct rt_rq *rt_rq;
  1221. for_each_sched_rt_entity(rt_se) {
  1222. rt_rq = rt_rq_of_se(rt_se);
  1223. requeue_rt_entity(rt_rq, rt_se, head);
  1224. }
  1225. }
  1226. static void yield_task_rt(struct rq *rq)
  1227. {
  1228. requeue_task_rt(rq, rq->curr, 0);
  1229. }
  1230. #ifdef CONFIG_SMP
  1231. static int find_lowest_rq(struct task_struct *task);
  1232. static int
  1233. select_task_rq_rt(struct task_struct *p, int cpu, int flags)
  1234. {
  1235. struct task_struct *curr;
  1236. struct rq *rq;
  1237. bool test;
  1238. /* For anything but wake ups, just return the task_cpu */
  1239. if (!(flags & (WF_TTWU | WF_FORK)))
  1240. goto out;
  1241. rq = cpu_rq(cpu);
  1242. rcu_read_lock();
  1243. curr = READ_ONCE(rq->curr); /* unlocked access */
  1244. /*
  1245. * If the current task on @p's runqueue is an RT task, then
  1246. * try to see if we can wake this RT task up on another
  1247. * runqueue. Otherwise simply start this RT task
  1248. * on its current runqueue.
  1249. *
  1250. * We want to avoid overloading runqueues. If the woken
  1251. * task is a higher priority, then it will stay on this CPU
  1252. * and the lower prio task should be moved to another CPU.
  1253. * Even though this will probably make the lower prio task
  1254. * lose its cache, we do not want to bounce a higher task
  1255. * around just because it gave up its CPU, perhaps for a
  1256. * lock?
  1257. *
  1258. * For equal prio tasks, we just let the scheduler sort it out.
  1259. *
  1260. * Otherwise, just let it ride on the affine RQ and the
  1261. * post-schedule router will push the preempted task away
  1262. *
  1263. * This test is optimistic, if we get it wrong the load-balancer
  1264. * will have to sort it out.
  1265. *
  1266. * We take into account the capacity of the CPU to ensure it fits the
  1267. * requirement of the task - which is only important on heterogeneous
  1268. * systems like big.LITTLE.
  1269. */
  1270. test = curr &&
  1271. unlikely(rt_task(curr)) &&
  1272. (curr->nr_cpus_allowed < 2 || curr->prio <= p->prio);
  1273. if (test || !rt_task_fits_capacity(p, cpu)) {
  1274. int target = find_lowest_rq(p);
  1275. /*
  1276. * Bail out if we were forcing a migration to find a better
  1277. * fitting CPU but our search failed.
  1278. */
  1279. if (!test && target != -1 && !rt_task_fits_capacity(p, target))
  1280. goto out_unlock;
  1281. /*
  1282. * Don't bother moving it if the destination CPU is
  1283. * not running a lower priority task.
  1284. */
  1285. if (target != -1 &&
  1286. p->prio < cpu_rq(target)->rt.highest_prio.curr)
  1287. cpu = target;
  1288. }
  1289. out_unlock:
  1290. rcu_read_unlock();
  1291. out:
  1292. return cpu;
  1293. }
  1294. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  1295. {
  1296. /*
  1297. * Current can't be migrated, useless to reschedule,
  1298. * let's hope p can move out.
  1299. */
  1300. if (rq->curr->nr_cpus_allowed == 1 ||
  1301. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  1302. return;
  1303. /*
  1304. * p is migratable, so let's not schedule it and
  1305. * see if it is pushed or pulled somewhere else.
  1306. */
  1307. if (p->nr_cpus_allowed != 1 &&
  1308. cpupri_find(&rq->rd->cpupri, p, NULL))
  1309. return;
  1310. /*
  1311. * There appear to be other CPUs that can accept
  1312. * the current task but none can run 'p', so lets reschedule
  1313. * to try and push the current task away:
  1314. */
  1315. requeue_task_rt(rq, p, 1);
  1316. resched_curr(rq);
  1317. }
  1318. static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
  1319. {
  1320. if (!on_rt_rq(&p->rt) && need_pull_rt_task(rq, p)) {
  1321. /*
  1322. * This is OK, because current is on_cpu, which avoids it being
  1323. * picked for load-balance and preemption/IRQs are still
  1324. * disabled avoiding further scheduler activity on it and we've
  1325. * not yet started the picking loop.
  1326. */
  1327. rq_unpin_lock(rq, rf);
  1328. pull_rt_task(rq);
  1329. rq_repin_lock(rq, rf);
  1330. }
  1331. return sched_stop_runnable(rq) || sched_dl_runnable(rq) || sched_rt_runnable(rq);
  1332. }
  1333. #endif /* CONFIG_SMP */
  1334. /*
  1335. * Preempt the current task with a newly woken task if needed:
  1336. */
  1337. static void wakeup_preempt_rt(struct rq *rq, struct task_struct *p, int flags)
  1338. {
  1339. if (p->prio < rq->curr->prio) {
  1340. resched_curr(rq);
  1341. return;
  1342. }
  1343. #ifdef CONFIG_SMP
  1344. /*
  1345. * If:
  1346. *
  1347. * - the newly woken task is of equal priority to the current task
  1348. * - the newly woken task is non-migratable while current is migratable
  1349. * - current will be preempted on the next reschedule
  1350. *
  1351. * we should check to see if current can readily move to a different
  1352. * cpu. If so, we will reschedule to allow the push logic to try
  1353. * to move current somewhere else, making room for our non-migratable
  1354. * task.
  1355. */
  1356. if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
  1357. check_preempt_equal_prio(rq, p);
  1358. #endif
  1359. }
  1360. static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool first)
  1361. {
  1362. struct sched_rt_entity *rt_se = &p->rt;
  1363. struct rt_rq *rt_rq = &rq->rt;
  1364. p->se.exec_start = rq_clock_task(rq);
  1365. if (on_rt_rq(&p->rt))
  1366. update_stats_wait_end_rt(rt_rq, rt_se);
  1367. /* The running task is never eligible for pushing */
  1368. dequeue_pushable_task(rq, p);
  1369. if (!first)
  1370. return;
  1371. /*
  1372. * If prev task was rt, put_prev_task() has already updated the
  1373. * utilization. We only care of the case where we start to schedule a
  1374. * rt task
  1375. */
  1376. if (rq->curr->sched_class != &rt_sched_class)
  1377. update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
  1378. rt_queue_push_tasks(rq);
  1379. }
  1380. static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
  1381. {
  1382. struct rt_prio_array *array = &rt_rq->active;
  1383. struct sched_rt_entity *next = NULL;
  1384. struct list_head *queue;
  1385. int idx;
  1386. idx = sched_find_first_bit(array->bitmap);
  1387. BUG_ON(idx >= MAX_RT_PRIO);
  1388. queue = array->queue + idx;
  1389. if (SCHED_WARN_ON(list_empty(queue)))
  1390. return NULL;
  1391. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  1392. return next;
  1393. }
  1394. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  1395. {
  1396. struct sched_rt_entity *rt_se;
  1397. struct rt_rq *rt_rq = &rq->rt;
  1398. do {
  1399. rt_se = pick_next_rt_entity(rt_rq);
  1400. if (unlikely(!rt_se))
  1401. return NULL;
  1402. rt_rq = group_rt_rq(rt_se);
  1403. } while (rt_rq);
  1404. return rt_task_of(rt_se);
  1405. }
  1406. static struct task_struct *pick_task_rt(struct rq *rq)
  1407. {
  1408. struct task_struct *p;
  1409. if (!sched_rt_runnable(rq))
  1410. return NULL;
  1411. p = _pick_next_task_rt(rq);
  1412. return p;
  1413. }
  1414. static void put_prev_task_rt(struct rq *rq, struct task_struct *p, struct task_struct *next)
  1415. {
  1416. struct sched_rt_entity *rt_se = &p->rt;
  1417. struct rt_rq *rt_rq = &rq->rt;
  1418. if (on_rt_rq(&p->rt))
  1419. update_stats_wait_start_rt(rt_rq, rt_se);
  1420. update_curr_rt(rq);
  1421. update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
  1422. /*
  1423. * The previous task needs to be made eligible for pushing
  1424. * if it is still active
  1425. */
  1426. if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  1427. enqueue_pushable_task(rq, p);
  1428. }
  1429. #ifdef CONFIG_SMP
  1430. /* Only try algorithms three times */
  1431. #define RT_MAX_TRIES 3
  1432. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  1433. {
  1434. if (!task_on_cpu(rq, p) &&
  1435. cpumask_test_cpu(cpu, &p->cpus_mask))
  1436. return 1;
  1437. return 0;
  1438. }
  1439. /*
  1440. * Return the highest pushable rq's task, which is suitable to be executed
  1441. * on the CPU, NULL otherwise
  1442. */
  1443. static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
  1444. {
  1445. struct plist_head *head = &rq->rt.pushable_tasks;
  1446. struct task_struct *p;
  1447. if (!has_pushable_tasks(rq))
  1448. return NULL;
  1449. plist_for_each_entry(p, head, pushable_tasks) {
  1450. if (pick_rt_task(rq, p, cpu))
  1451. return p;
  1452. }
  1453. return NULL;
  1454. }
  1455. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  1456. static int find_lowest_rq(struct task_struct *task)
  1457. {
  1458. struct sched_domain *sd;
  1459. struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
  1460. int this_cpu = smp_processor_id();
  1461. int cpu = task_cpu(task);
  1462. int ret;
  1463. /* Make sure the mask is initialized first */
  1464. if (unlikely(!lowest_mask))
  1465. return -1;
  1466. if (task->nr_cpus_allowed == 1)
  1467. return -1; /* No other targets possible */
  1468. /*
  1469. * If we're on asym system ensure we consider the different capacities
  1470. * of the CPUs when searching for the lowest_mask.
  1471. */
  1472. if (sched_asym_cpucap_active()) {
  1473. ret = cpupri_find_fitness(&task_rq(task)->rd->cpupri,
  1474. task, lowest_mask,
  1475. rt_task_fits_capacity);
  1476. } else {
  1477. ret = cpupri_find(&task_rq(task)->rd->cpupri,
  1478. task, lowest_mask);
  1479. }
  1480. if (!ret)
  1481. return -1; /* No targets found */
  1482. /*
  1483. * At this point we have built a mask of CPUs representing the
  1484. * lowest priority tasks in the system. Now we want to elect
  1485. * the best one based on our affinity and topology.
  1486. *
  1487. * We prioritize the last CPU that the task executed on since
  1488. * it is most likely cache-hot in that location.
  1489. */
  1490. if (cpumask_test_cpu(cpu, lowest_mask))
  1491. return cpu;
  1492. /*
  1493. * Otherwise, we consult the sched_domains span maps to figure
  1494. * out which CPU is logically closest to our hot cache data.
  1495. */
  1496. if (!cpumask_test_cpu(this_cpu, lowest_mask))
  1497. this_cpu = -1; /* Skip this_cpu opt if not among lowest */
  1498. rcu_read_lock();
  1499. for_each_domain(cpu, sd) {
  1500. if (sd->flags & SD_WAKE_AFFINE) {
  1501. int best_cpu;
  1502. /*
  1503. * "this_cpu" is cheaper to preempt than a
  1504. * remote processor.
  1505. */
  1506. if (this_cpu != -1 &&
  1507. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1508. rcu_read_unlock();
  1509. return this_cpu;
  1510. }
  1511. best_cpu = cpumask_any_and_distribute(lowest_mask,
  1512. sched_domain_span(sd));
  1513. if (best_cpu < nr_cpu_ids) {
  1514. rcu_read_unlock();
  1515. return best_cpu;
  1516. }
  1517. }
  1518. }
  1519. rcu_read_unlock();
  1520. /*
  1521. * And finally, if there were no matches within the domains
  1522. * just give the caller *something* to work with from the compatible
  1523. * locations.
  1524. */
  1525. if (this_cpu != -1)
  1526. return this_cpu;
  1527. cpu = cpumask_any_distribute(lowest_mask);
  1528. if (cpu < nr_cpu_ids)
  1529. return cpu;
  1530. return -1;
  1531. }
  1532. /* Will lock the rq it finds */
  1533. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  1534. {
  1535. struct rq *lowest_rq = NULL;
  1536. int tries;
  1537. int cpu;
  1538. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  1539. cpu = find_lowest_rq(task);
  1540. if ((cpu == -1) || (cpu == rq->cpu))
  1541. break;
  1542. lowest_rq = cpu_rq(cpu);
  1543. if (lowest_rq->rt.highest_prio.curr <= task->prio) {
  1544. /*
  1545. * Target rq has tasks of equal or higher priority,
  1546. * retrying does not release any lock and is unlikely
  1547. * to yield a different result.
  1548. */
  1549. lowest_rq = NULL;
  1550. break;
  1551. }
  1552. /* if the prio of this runqueue changed, try again */
  1553. if (double_lock_balance(rq, lowest_rq)) {
  1554. /*
  1555. * We had to unlock the run queue. In
  1556. * the mean time, task could have
  1557. * migrated already or had its affinity changed.
  1558. * Also make sure that it wasn't scheduled on its rq.
  1559. * It is possible the task was scheduled, set
  1560. * "migrate_disabled" and then got preempted, so we must
  1561. * check the task migration disable flag here too.
  1562. */
  1563. if (unlikely(task_rq(task) != rq ||
  1564. !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_mask) ||
  1565. task_on_cpu(rq, task) ||
  1566. !rt_task(task) ||
  1567. is_migration_disabled(task) ||
  1568. !task_on_rq_queued(task))) {
  1569. double_unlock_balance(rq, lowest_rq);
  1570. lowest_rq = NULL;
  1571. break;
  1572. }
  1573. }
  1574. /* If this rq is still suitable use it. */
  1575. if (lowest_rq->rt.highest_prio.curr > task->prio)
  1576. break;
  1577. /* try again */
  1578. double_unlock_balance(rq, lowest_rq);
  1579. lowest_rq = NULL;
  1580. }
  1581. return lowest_rq;
  1582. }
  1583. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1584. {
  1585. struct task_struct *p;
  1586. if (!has_pushable_tasks(rq))
  1587. return NULL;
  1588. p = plist_first_entry(&rq->rt.pushable_tasks,
  1589. struct task_struct, pushable_tasks);
  1590. BUG_ON(rq->cpu != task_cpu(p));
  1591. BUG_ON(task_current(rq, p));
  1592. BUG_ON(p->nr_cpus_allowed <= 1);
  1593. BUG_ON(!task_on_rq_queued(p));
  1594. BUG_ON(!rt_task(p));
  1595. return p;
  1596. }
  1597. /*
  1598. * If the current CPU has more than one RT task, see if the non
  1599. * running task can migrate over to a CPU that is running a task
  1600. * of lesser priority.
  1601. */
  1602. static int push_rt_task(struct rq *rq, bool pull)
  1603. {
  1604. struct task_struct *next_task;
  1605. struct rq *lowest_rq;
  1606. int ret = 0;
  1607. if (!rq->rt.overloaded)
  1608. return 0;
  1609. next_task = pick_next_pushable_task(rq);
  1610. if (!next_task)
  1611. return 0;
  1612. retry:
  1613. /*
  1614. * It's possible that the next_task slipped in of
  1615. * higher priority than current. If that's the case
  1616. * just reschedule current.
  1617. */
  1618. if (unlikely(next_task->prio < rq->curr->prio)) {
  1619. resched_curr(rq);
  1620. return 0;
  1621. }
  1622. if (is_migration_disabled(next_task)) {
  1623. struct task_struct *push_task = NULL;
  1624. int cpu;
  1625. if (!pull || rq->push_busy)
  1626. return 0;
  1627. /*
  1628. * Invoking find_lowest_rq() on anything but an RT task doesn't
  1629. * make sense. Per the above priority check, curr has to
  1630. * be of higher priority than next_task, so no need to
  1631. * reschedule when bailing out.
  1632. *
  1633. * Note that the stoppers are masqueraded as SCHED_FIFO
  1634. * (cf. sched_set_stop_task()), so we can't rely on rt_task().
  1635. */
  1636. if (rq->curr->sched_class != &rt_sched_class)
  1637. return 0;
  1638. cpu = find_lowest_rq(rq->curr);
  1639. if (cpu == -1 || cpu == rq->cpu)
  1640. return 0;
  1641. /*
  1642. * Given we found a CPU with lower priority than @next_task,
  1643. * therefore it should be running. However we cannot migrate it
  1644. * to this other CPU, instead attempt to push the current
  1645. * running task on this CPU away.
  1646. */
  1647. push_task = get_push_task(rq);
  1648. if (push_task) {
  1649. preempt_disable();
  1650. raw_spin_rq_unlock(rq);
  1651. stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
  1652. push_task, &rq->push_work);
  1653. preempt_enable();
  1654. raw_spin_rq_lock(rq);
  1655. }
  1656. return 0;
  1657. }
  1658. if (WARN_ON(next_task == rq->curr))
  1659. return 0;
  1660. /* We might release rq lock */
  1661. get_task_struct(next_task);
  1662. /* find_lock_lowest_rq locks the rq if found */
  1663. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1664. if (!lowest_rq) {
  1665. struct task_struct *task;
  1666. /*
  1667. * find_lock_lowest_rq releases rq->lock
  1668. * so it is possible that next_task has migrated.
  1669. *
  1670. * We need to make sure that the task is still on the same
  1671. * run-queue and is also still the next task eligible for
  1672. * pushing.
  1673. */
  1674. task = pick_next_pushable_task(rq);
  1675. if (task == next_task) {
  1676. /*
  1677. * The task hasn't migrated, and is still the next
  1678. * eligible task, but we failed to find a run-queue
  1679. * to push it to. Do not retry in this case, since
  1680. * other CPUs will pull from us when ready.
  1681. */
  1682. goto out;
  1683. }
  1684. if (!task)
  1685. /* No more tasks, just exit */
  1686. goto out;
  1687. /*
  1688. * Something has shifted, try again.
  1689. */
  1690. put_task_struct(next_task);
  1691. next_task = task;
  1692. goto retry;
  1693. }
  1694. deactivate_task(rq, next_task, 0);
  1695. set_task_cpu(next_task, lowest_rq->cpu);
  1696. activate_task(lowest_rq, next_task, 0);
  1697. resched_curr(lowest_rq);
  1698. ret = 1;
  1699. double_unlock_balance(rq, lowest_rq);
  1700. out:
  1701. put_task_struct(next_task);
  1702. return ret;
  1703. }
  1704. static void push_rt_tasks(struct rq *rq)
  1705. {
  1706. /* push_rt_task will return true if it moved an RT */
  1707. while (push_rt_task(rq, false))
  1708. ;
  1709. }
  1710. #ifdef HAVE_RT_PUSH_IPI
  1711. /*
  1712. * When a high priority task schedules out from a CPU and a lower priority
  1713. * task is scheduled in, a check is made to see if there's any RT tasks
  1714. * on other CPUs that are waiting to run because a higher priority RT task
  1715. * is currently running on its CPU. In this case, the CPU with multiple RT
  1716. * tasks queued on it (overloaded) needs to be notified that a CPU has opened
  1717. * up that may be able to run one of its non-running queued RT tasks.
  1718. *
  1719. * All CPUs with overloaded RT tasks need to be notified as there is currently
  1720. * no way to know which of these CPUs have the highest priority task waiting
  1721. * to run. Instead of trying to take a spinlock on each of these CPUs,
  1722. * which has shown to cause large latency when done on machines with many
  1723. * CPUs, sending an IPI to the CPUs to have them push off the overloaded
  1724. * RT tasks waiting to run.
  1725. *
  1726. * Just sending an IPI to each of the CPUs is also an issue, as on large
  1727. * count CPU machines, this can cause an IPI storm on a CPU, especially
  1728. * if its the only CPU with multiple RT tasks queued, and a large number
  1729. * of CPUs scheduling a lower priority task at the same time.
  1730. *
  1731. * Each root domain has its own IRQ work function that can iterate over
  1732. * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
  1733. * task must be checked if there's one or many CPUs that are lowering
  1734. * their priority, there's a single IRQ work iterator that will try to
  1735. * push off RT tasks that are waiting to run.
  1736. *
  1737. * When a CPU schedules a lower priority task, it will kick off the
  1738. * IRQ work iterator that will jump to each CPU with overloaded RT tasks.
  1739. * As it only takes the first CPU that schedules a lower priority task
  1740. * to start the process, the rto_start variable is incremented and if
  1741. * the atomic result is one, then that CPU will try to take the rto_lock.
  1742. * This prevents high contention on the lock as the process handles all
  1743. * CPUs scheduling lower priority tasks.
  1744. *
  1745. * All CPUs that are scheduling a lower priority task will increment the
  1746. * rt_loop_next variable. This will make sure that the IRQ work iterator
  1747. * checks all RT overloaded CPUs whenever a CPU schedules a new lower
  1748. * priority task, even if the iterator is in the middle of a scan. Incrementing
  1749. * the rt_loop_next will cause the iterator to perform another scan.
  1750. *
  1751. */
  1752. static int rto_next_cpu(struct root_domain *rd)
  1753. {
  1754. int next;
  1755. int cpu;
  1756. /*
  1757. * When starting the IPI RT pushing, the rto_cpu is set to -1,
  1758. * rt_next_cpu() will simply return the first CPU found in
  1759. * the rto_mask.
  1760. *
  1761. * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
  1762. * will return the next CPU found in the rto_mask.
  1763. *
  1764. * If there are no more CPUs left in the rto_mask, then a check is made
  1765. * against rto_loop and rto_loop_next. rto_loop is only updated with
  1766. * the rto_lock held, but any CPU may increment the rto_loop_next
  1767. * without any locking.
  1768. */
  1769. for (;;) {
  1770. /* When rto_cpu is -1 this acts like cpumask_first() */
  1771. cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
  1772. rd->rto_cpu = cpu;
  1773. if (cpu < nr_cpu_ids)
  1774. return cpu;
  1775. rd->rto_cpu = -1;
  1776. /*
  1777. * ACQUIRE ensures we see the @rto_mask changes
  1778. * made prior to the @next value observed.
  1779. *
  1780. * Matches WMB in rt_set_overload().
  1781. */
  1782. next = atomic_read_acquire(&rd->rto_loop_next);
  1783. if (rd->rto_loop == next)
  1784. break;
  1785. rd->rto_loop = next;
  1786. }
  1787. return -1;
  1788. }
  1789. static inline bool rto_start_trylock(atomic_t *v)
  1790. {
  1791. return !atomic_cmpxchg_acquire(v, 0, 1);
  1792. }
  1793. static inline void rto_start_unlock(atomic_t *v)
  1794. {
  1795. atomic_set_release(v, 0);
  1796. }
  1797. static void tell_cpu_to_push(struct rq *rq)
  1798. {
  1799. int cpu = -1;
  1800. /* Keep the loop going if the IPI is currently active */
  1801. atomic_inc(&rq->rd->rto_loop_next);
  1802. /* Only one CPU can initiate a loop at a time */
  1803. if (!rto_start_trylock(&rq->rd->rto_loop_start))
  1804. return;
  1805. raw_spin_lock(&rq->rd->rto_lock);
  1806. /*
  1807. * The rto_cpu is updated under the lock, if it has a valid CPU
  1808. * then the IPI is still running and will continue due to the
  1809. * update to loop_next, and nothing needs to be done here.
  1810. * Otherwise it is finishing up and an IPI needs to be sent.
  1811. */
  1812. if (rq->rd->rto_cpu < 0)
  1813. cpu = rto_next_cpu(rq->rd);
  1814. raw_spin_unlock(&rq->rd->rto_lock);
  1815. rto_start_unlock(&rq->rd->rto_loop_start);
  1816. if (cpu >= 0) {
  1817. /* Make sure the rd does not get freed while pushing */
  1818. sched_get_rd(rq->rd);
  1819. irq_work_queue_on(&rq->rd->rto_push_work, cpu);
  1820. }
  1821. }
  1822. /* Called from hardirq context */
  1823. void rto_push_irq_work_func(struct irq_work *work)
  1824. {
  1825. struct root_domain *rd =
  1826. container_of(work, struct root_domain, rto_push_work);
  1827. struct rq *rq;
  1828. int cpu;
  1829. rq = this_rq();
  1830. /*
  1831. * We do not need to grab the lock to check for has_pushable_tasks.
  1832. * When it gets updated, a check is made if a push is possible.
  1833. */
  1834. if (has_pushable_tasks(rq)) {
  1835. raw_spin_rq_lock(rq);
  1836. while (push_rt_task(rq, true))
  1837. ;
  1838. raw_spin_rq_unlock(rq);
  1839. }
  1840. raw_spin_lock(&rd->rto_lock);
  1841. /* Pass the IPI to the next rt overloaded queue */
  1842. cpu = rto_next_cpu(rd);
  1843. raw_spin_unlock(&rd->rto_lock);
  1844. if (cpu < 0) {
  1845. sched_put_rd(rd);
  1846. return;
  1847. }
  1848. /* Try the next RT overloaded CPU */
  1849. irq_work_queue_on(&rd->rto_push_work, cpu);
  1850. }
  1851. #endif /* HAVE_RT_PUSH_IPI */
  1852. static void pull_rt_task(struct rq *this_rq)
  1853. {
  1854. int this_cpu = this_rq->cpu, cpu;
  1855. bool resched = false;
  1856. struct task_struct *p, *push_task;
  1857. struct rq *src_rq;
  1858. int rt_overload_count = rt_overloaded(this_rq);
  1859. if (likely(!rt_overload_count))
  1860. return;
  1861. /*
  1862. * Match the barrier from rt_set_overloaded; this guarantees that if we
  1863. * see overloaded we must also see the rto_mask bit.
  1864. */
  1865. smp_rmb();
  1866. /* If we are the only overloaded CPU do nothing */
  1867. if (rt_overload_count == 1 &&
  1868. cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
  1869. return;
  1870. #ifdef HAVE_RT_PUSH_IPI
  1871. if (sched_feat(RT_PUSH_IPI)) {
  1872. tell_cpu_to_push(this_rq);
  1873. return;
  1874. }
  1875. #endif
  1876. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1877. if (this_cpu == cpu)
  1878. continue;
  1879. src_rq = cpu_rq(cpu);
  1880. /*
  1881. * Don't bother taking the src_rq->lock if the next highest
  1882. * task is known to be lower-priority than our current task.
  1883. * This may look racy, but if this value is about to go
  1884. * logically higher, the src_rq will push this task away.
  1885. * And if its going logically lower, we do not care
  1886. */
  1887. if (src_rq->rt.highest_prio.next >=
  1888. this_rq->rt.highest_prio.curr)
  1889. continue;
  1890. /*
  1891. * We can potentially drop this_rq's lock in
  1892. * double_lock_balance, and another CPU could
  1893. * alter this_rq
  1894. */
  1895. push_task = NULL;
  1896. double_lock_balance(this_rq, src_rq);
  1897. /*
  1898. * We can pull only a task, which is pushable
  1899. * on its rq, and no others.
  1900. */
  1901. p = pick_highest_pushable_task(src_rq, this_cpu);
  1902. /*
  1903. * Do we have an RT task that preempts
  1904. * the to-be-scheduled task?
  1905. */
  1906. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1907. WARN_ON(p == src_rq->curr);
  1908. WARN_ON(!task_on_rq_queued(p));
  1909. /*
  1910. * There's a chance that p is higher in priority
  1911. * than what's currently running on its CPU.
  1912. * This is just that p is waking up and hasn't
  1913. * had a chance to schedule. We only pull
  1914. * p if it is lower in priority than the
  1915. * current task on the run queue
  1916. */
  1917. if (p->prio < src_rq->curr->prio)
  1918. goto skip;
  1919. if (is_migration_disabled(p)) {
  1920. push_task = get_push_task(src_rq);
  1921. } else {
  1922. deactivate_task(src_rq, p, 0);
  1923. set_task_cpu(p, this_cpu);
  1924. activate_task(this_rq, p, 0);
  1925. resched = true;
  1926. }
  1927. /*
  1928. * We continue with the search, just in
  1929. * case there's an even higher prio task
  1930. * in another runqueue. (low likelihood
  1931. * but possible)
  1932. */
  1933. }
  1934. skip:
  1935. double_unlock_balance(this_rq, src_rq);
  1936. if (push_task) {
  1937. preempt_disable();
  1938. raw_spin_rq_unlock(this_rq);
  1939. stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
  1940. push_task, &src_rq->push_work);
  1941. preempt_enable();
  1942. raw_spin_rq_lock(this_rq);
  1943. }
  1944. }
  1945. if (resched)
  1946. resched_curr(this_rq);
  1947. }
  1948. /*
  1949. * If we are not running and we are not going to reschedule soon, we should
  1950. * try to push tasks away now
  1951. */
  1952. static void task_woken_rt(struct rq *rq, struct task_struct *p)
  1953. {
  1954. bool need_to_push = !task_on_cpu(rq, p) &&
  1955. !test_tsk_need_resched(rq->curr) &&
  1956. p->nr_cpus_allowed > 1 &&
  1957. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  1958. (rq->curr->nr_cpus_allowed < 2 ||
  1959. rq->curr->prio <= p->prio);
  1960. if (need_to_push)
  1961. push_rt_tasks(rq);
  1962. }
  1963. /* Assumes rq->lock is held */
  1964. static void rq_online_rt(struct rq *rq)
  1965. {
  1966. if (rq->rt.overloaded)
  1967. rt_set_overload(rq);
  1968. __enable_runtime(rq);
  1969. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1970. }
  1971. /* Assumes rq->lock is held */
  1972. static void rq_offline_rt(struct rq *rq)
  1973. {
  1974. if (rq->rt.overloaded)
  1975. rt_clear_overload(rq);
  1976. __disable_runtime(rq);
  1977. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1978. }
  1979. /*
  1980. * When switch from the rt queue, we bring ourselves to a position
  1981. * that we might want to pull RT tasks from other runqueues.
  1982. */
  1983. static void switched_from_rt(struct rq *rq, struct task_struct *p)
  1984. {
  1985. /*
  1986. * If there are other RT tasks then we will reschedule
  1987. * and the scheduling of the other RT tasks will handle
  1988. * the balancing. But if we are the last RT task
  1989. * we may need to handle the pulling of RT tasks
  1990. * now.
  1991. */
  1992. if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
  1993. return;
  1994. rt_queue_pull_task(rq);
  1995. }
  1996. void __init init_sched_rt_class(void)
  1997. {
  1998. unsigned int i;
  1999. for_each_possible_cpu(i) {
  2000. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  2001. GFP_KERNEL, cpu_to_node(i));
  2002. }
  2003. }
  2004. #endif /* CONFIG_SMP */
  2005. /*
  2006. * When switching a task to RT, we may overload the runqueue
  2007. * with RT tasks. In this case we try to push them off to
  2008. * other runqueues.
  2009. */
  2010. static void switched_to_rt(struct rq *rq, struct task_struct *p)
  2011. {
  2012. /*
  2013. * If we are running, update the avg_rt tracking, as the running time
  2014. * will now on be accounted into the latter.
  2015. */
  2016. if (task_current(rq, p)) {
  2017. update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
  2018. return;
  2019. }
  2020. /*
  2021. * If we are not running we may need to preempt the current
  2022. * running task. If that current running task is also an RT task
  2023. * then see if we can move to another run queue.
  2024. */
  2025. if (task_on_rq_queued(p)) {
  2026. #ifdef CONFIG_SMP
  2027. if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  2028. rt_queue_push_tasks(rq);
  2029. #endif /* CONFIG_SMP */
  2030. if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
  2031. resched_curr(rq);
  2032. }
  2033. }
  2034. /*
  2035. * Priority of the task has changed. This may cause
  2036. * us to initiate a push or pull.
  2037. */
  2038. static void
  2039. prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
  2040. {
  2041. if (!task_on_rq_queued(p))
  2042. return;
  2043. if (task_current(rq, p)) {
  2044. #ifdef CONFIG_SMP
  2045. /*
  2046. * If our priority decreases while running, we
  2047. * may need to pull tasks to this runqueue.
  2048. */
  2049. if (oldprio < p->prio)
  2050. rt_queue_pull_task(rq);
  2051. /*
  2052. * If there's a higher priority task waiting to run
  2053. * then reschedule.
  2054. */
  2055. if (p->prio > rq->rt.highest_prio.curr)
  2056. resched_curr(rq);
  2057. #else
  2058. /* For UP simply resched on drop of prio */
  2059. if (oldprio < p->prio)
  2060. resched_curr(rq);
  2061. #endif /* CONFIG_SMP */
  2062. } else {
  2063. /*
  2064. * This task is not running, but if it is
  2065. * greater than the current running task
  2066. * then reschedule.
  2067. */
  2068. if (p->prio < rq->curr->prio)
  2069. resched_curr(rq);
  2070. }
  2071. }
  2072. #ifdef CONFIG_POSIX_TIMERS
  2073. static void watchdog(struct rq *rq, struct task_struct *p)
  2074. {
  2075. unsigned long soft, hard;
  2076. /* max may change after cur was read, this will be fixed next tick */
  2077. soft = task_rlimit(p, RLIMIT_RTTIME);
  2078. hard = task_rlimit_max(p, RLIMIT_RTTIME);
  2079. if (soft != RLIM_INFINITY) {
  2080. unsigned long next;
  2081. if (p->rt.watchdog_stamp != jiffies) {
  2082. p->rt.timeout++;
  2083. p->rt.watchdog_stamp = jiffies;
  2084. }
  2085. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  2086. if (p->rt.timeout > next) {
  2087. posix_cputimers_rt_watchdog(&p->posix_cputimers,
  2088. p->se.sum_exec_runtime);
  2089. }
  2090. }
  2091. }
  2092. #else
  2093. static inline void watchdog(struct rq *rq, struct task_struct *p) { }
  2094. #endif
  2095. /*
  2096. * scheduler tick hitting a task of our scheduling class.
  2097. *
  2098. * NOTE: This function can be called remotely by the tick offload that
  2099. * goes along full dynticks. Therefore no local assumption can be made
  2100. * and everything must be accessed through the @rq and @curr passed in
  2101. * parameters.
  2102. */
  2103. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  2104. {
  2105. struct sched_rt_entity *rt_se = &p->rt;
  2106. update_curr_rt(rq);
  2107. update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
  2108. watchdog(rq, p);
  2109. /*
  2110. * RR tasks need a special form of time-slice management.
  2111. * FIFO tasks have no timeslices.
  2112. */
  2113. if (p->policy != SCHED_RR)
  2114. return;
  2115. if (--p->rt.time_slice)
  2116. return;
  2117. p->rt.time_slice = sched_rr_timeslice;
  2118. /*
  2119. * Requeue to the end of queue if we (and all of our ancestors) are not
  2120. * the only element on the queue
  2121. */
  2122. for_each_sched_rt_entity(rt_se) {
  2123. if (rt_se->run_list.prev != rt_se->run_list.next) {
  2124. requeue_task_rt(rq, p, 0);
  2125. resched_curr(rq);
  2126. return;
  2127. }
  2128. }
  2129. }
  2130. static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
  2131. {
  2132. /*
  2133. * Time slice is 0 for SCHED_FIFO tasks
  2134. */
  2135. if (task->policy == SCHED_RR)
  2136. return sched_rr_timeslice;
  2137. else
  2138. return 0;
  2139. }
  2140. #ifdef CONFIG_SCHED_CORE
  2141. static int task_is_throttled_rt(struct task_struct *p, int cpu)
  2142. {
  2143. struct rt_rq *rt_rq;
  2144. #ifdef CONFIG_RT_GROUP_SCHED
  2145. rt_rq = task_group(p)->rt_rq[cpu];
  2146. #else
  2147. rt_rq = &cpu_rq(cpu)->rt;
  2148. #endif
  2149. return rt_rq_throttled(rt_rq);
  2150. }
  2151. #endif
  2152. DEFINE_SCHED_CLASS(rt) = {
  2153. .enqueue_task = enqueue_task_rt,
  2154. .dequeue_task = dequeue_task_rt,
  2155. .yield_task = yield_task_rt,
  2156. .wakeup_preempt = wakeup_preempt_rt,
  2157. .pick_task = pick_task_rt,
  2158. .put_prev_task = put_prev_task_rt,
  2159. .set_next_task = set_next_task_rt,
  2160. #ifdef CONFIG_SMP
  2161. .balance = balance_rt,
  2162. .select_task_rq = select_task_rq_rt,
  2163. .set_cpus_allowed = set_cpus_allowed_common,
  2164. .rq_online = rq_online_rt,
  2165. .rq_offline = rq_offline_rt,
  2166. .task_woken = task_woken_rt,
  2167. .switched_from = switched_from_rt,
  2168. .find_lock_rq = find_lock_lowest_rq,
  2169. #endif
  2170. .task_tick = task_tick_rt,
  2171. .get_rr_interval = get_rr_interval_rt,
  2172. .prio_changed = prio_changed_rt,
  2173. .switched_to = switched_to_rt,
  2174. .update_curr = update_curr_rt,
  2175. #ifdef CONFIG_SCHED_CORE
  2176. .task_is_throttled = task_is_throttled_rt,
  2177. #endif
  2178. #ifdef CONFIG_UCLAMP_TASK
  2179. .uclamp_enabled = 1,
  2180. #endif
  2181. };
  2182. #ifdef CONFIG_RT_GROUP_SCHED
  2183. /*
  2184. * Ensure that the real time constraints are schedulable.
  2185. */
  2186. static DEFINE_MUTEX(rt_constraints_mutex);
  2187. static inline int tg_has_rt_tasks(struct task_group *tg)
  2188. {
  2189. struct task_struct *task;
  2190. struct css_task_iter it;
  2191. int ret = 0;
  2192. /*
  2193. * Autogroups do not have RT tasks; see autogroup_create().
  2194. */
  2195. if (task_group_is_autogroup(tg))
  2196. return 0;
  2197. css_task_iter_start(&tg->css, 0, &it);
  2198. while (!ret && (task = css_task_iter_next(&it)))
  2199. ret |= rt_task(task);
  2200. css_task_iter_end(&it);
  2201. return ret;
  2202. }
  2203. struct rt_schedulable_data {
  2204. struct task_group *tg;
  2205. u64 rt_period;
  2206. u64 rt_runtime;
  2207. };
  2208. static int tg_rt_schedulable(struct task_group *tg, void *data)
  2209. {
  2210. struct rt_schedulable_data *d = data;
  2211. struct task_group *child;
  2212. unsigned long total, sum = 0;
  2213. u64 period, runtime;
  2214. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  2215. runtime = tg->rt_bandwidth.rt_runtime;
  2216. if (tg == d->tg) {
  2217. period = d->rt_period;
  2218. runtime = d->rt_runtime;
  2219. }
  2220. /*
  2221. * Cannot have more runtime than the period.
  2222. */
  2223. if (runtime > period && runtime != RUNTIME_INF)
  2224. return -EINVAL;
  2225. /*
  2226. * Ensure we don't starve existing RT tasks if runtime turns zero.
  2227. */
  2228. if (rt_bandwidth_enabled() && !runtime &&
  2229. tg->rt_bandwidth.rt_runtime && tg_has_rt_tasks(tg))
  2230. return -EBUSY;
  2231. total = to_ratio(period, runtime);
  2232. /*
  2233. * Nobody can have more than the global setting allows.
  2234. */
  2235. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  2236. return -EINVAL;
  2237. /*
  2238. * The sum of our children's runtime should not exceed our own.
  2239. */
  2240. list_for_each_entry_rcu(child, &tg->children, siblings) {
  2241. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  2242. runtime = child->rt_bandwidth.rt_runtime;
  2243. if (child == d->tg) {
  2244. period = d->rt_period;
  2245. runtime = d->rt_runtime;
  2246. }
  2247. sum += to_ratio(period, runtime);
  2248. }
  2249. if (sum > total)
  2250. return -EINVAL;
  2251. return 0;
  2252. }
  2253. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  2254. {
  2255. int ret;
  2256. struct rt_schedulable_data data = {
  2257. .tg = tg,
  2258. .rt_period = period,
  2259. .rt_runtime = runtime,
  2260. };
  2261. rcu_read_lock();
  2262. ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
  2263. rcu_read_unlock();
  2264. return ret;
  2265. }
  2266. static int tg_set_rt_bandwidth(struct task_group *tg,
  2267. u64 rt_period, u64 rt_runtime)
  2268. {
  2269. int i, err = 0;
  2270. /*
  2271. * Disallowing the root group RT runtime is BAD, it would disallow the
  2272. * kernel creating (and or operating) RT threads.
  2273. */
  2274. if (tg == &root_task_group && rt_runtime == 0)
  2275. return -EINVAL;
  2276. /* No period doesn't make any sense. */
  2277. if (rt_period == 0)
  2278. return -EINVAL;
  2279. /*
  2280. * Bound quota to defend quota against overflow during bandwidth shift.
  2281. */
  2282. if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
  2283. return -EINVAL;
  2284. mutex_lock(&rt_constraints_mutex);
  2285. err = __rt_schedulable(tg, rt_period, rt_runtime);
  2286. if (err)
  2287. goto unlock;
  2288. raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  2289. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  2290. tg->rt_bandwidth.rt_runtime = rt_runtime;
  2291. for_each_possible_cpu(i) {
  2292. struct rt_rq *rt_rq = tg->rt_rq[i];
  2293. raw_spin_lock(&rt_rq->rt_runtime_lock);
  2294. rt_rq->rt_runtime = rt_runtime;
  2295. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  2296. }
  2297. raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  2298. unlock:
  2299. mutex_unlock(&rt_constraints_mutex);
  2300. return err;
  2301. }
  2302. int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  2303. {
  2304. u64 rt_runtime, rt_period;
  2305. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  2306. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  2307. if (rt_runtime_us < 0)
  2308. rt_runtime = RUNTIME_INF;
  2309. else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
  2310. return -EINVAL;
  2311. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  2312. }
  2313. long sched_group_rt_runtime(struct task_group *tg)
  2314. {
  2315. u64 rt_runtime_us;
  2316. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  2317. return -1;
  2318. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  2319. do_div(rt_runtime_us, NSEC_PER_USEC);
  2320. return rt_runtime_us;
  2321. }
  2322. int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
  2323. {
  2324. u64 rt_runtime, rt_period;
  2325. if (rt_period_us > U64_MAX / NSEC_PER_USEC)
  2326. return -EINVAL;
  2327. rt_period = rt_period_us * NSEC_PER_USEC;
  2328. rt_runtime = tg->rt_bandwidth.rt_runtime;
  2329. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  2330. }
  2331. long sched_group_rt_period(struct task_group *tg)
  2332. {
  2333. u64 rt_period_us;
  2334. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  2335. do_div(rt_period_us, NSEC_PER_USEC);
  2336. return rt_period_us;
  2337. }
  2338. #ifdef CONFIG_SYSCTL
  2339. static int sched_rt_global_constraints(void)
  2340. {
  2341. int ret = 0;
  2342. mutex_lock(&rt_constraints_mutex);
  2343. ret = __rt_schedulable(NULL, 0, 0);
  2344. mutex_unlock(&rt_constraints_mutex);
  2345. return ret;
  2346. }
  2347. #endif /* CONFIG_SYSCTL */
  2348. int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  2349. {
  2350. /* Don't accept real-time tasks when there is no way for them to run */
  2351. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  2352. return 0;
  2353. return 1;
  2354. }
  2355. #else /* !CONFIG_RT_GROUP_SCHED */
  2356. #ifdef CONFIG_SYSCTL
  2357. static int sched_rt_global_constraints(void)
  2358. {
  2359. return 0;
  2360. }
  2361. #endif /* CONFIG_SYSCTL */
  2362. #endif /* CONFIG_RT_GROUP_SCHED */
  2363. #ifdef CONFIG_SYSCTL
  2364. static int sched_rt_global_validate(void)
  2365. {
  2366. if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
  2367. ((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
  2368. ((u64)sysctl_sched_rt_runtime *
  2369. NSEC_PER_USEC > max_rt_runtime)))
  2370. return -EINVAL;
  2371. return 0;
  2372. }
  2373. static void sched_rt_do_global(void)
  2374. {
  2375. }
  2376. static int sched_rt_handler(const struct ctl_table *table, int write, void *buffer,
  2377. size_t *lenp, loff_t *ppos)
  2378. {
  2379. int old_period, old_runtime;
  2380. static DEFINE_MUTEX(mutex);
  2381. int ret;
  2382. mutex_lock(&mutex);
  2383. old_period = sysctl_sched_rt_period;
  2384. old_runtime = sysctl_sched_rt_runtime;
  2385. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  2386. if (!ret && write) {
  2387. ret = sched_rt_global_validate();
  2388. if (ret)
  2389. goto undo;
  2390. ret = sched_dl_global_validate();
  2391. if (ret)
  2392. goto undo;
  2393. ret = sched_rt_global_constraints();
  2394. if (ret)
  2395. goto undo;
  2396. sched_rt_do_global();
  2397. sched_dl_do_global();
  2398. }
  2399. if (0) {
  2400. undo:
  2401. sysctl_sched_rt_period = old_period;
  2402. sysctl_sched_rt_runtime = old_runtime;
  2403. }
  2404. mutex_unlock(&mutex);
  2405. /*
  2406. * After changing maximum available bandwidth for DEADLINE, we need to
  2407. * recompute per root domain and per cpus variables accordingly.
  2408. */
  2409. rebuild_sched_domains();
  2410. return ret;
  2411. }
  2412. static int sched_rr_handler(const struct ctl_table *table, int write, void *buffer,
  2413. size_t *lenp, loff_t *ppos)
  2414. {
  2415. int ret;
  2416. static DEFINE_MUTEX(mutex);
  2417. mutex_lock(&mutex);
  2418. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2419. /*
  2420. * Make sure that internally we keep jiffies.
  2421. * Also, writing zero resets the time-slice to default:
  2422. */
  2423. if (!ret && write) {
  2424. sched_rr_timeslice =
  2425. sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
  2426. msecs_to_jiffies(sysctl_sched_rr_timeslice);
  2427. if (sysctl_sched_rr_timeslice <= 0)
  2428. sysctl_sched_rr_timeslice = jiffies_to_msecs(RR_TIMESLICE);
  2429. }
  2430. mutex_unlock(&mutex);
  2431. return ret;
  2432. }
  2433. #endif /* CONFIG_SYSCTL */
  2434. #ifdef CONFIG_SCHED_DEBUG
  2435. void print_rt_stats(struct seq_file *m, int cpu)
  2436. {
  2437. rt_rq_iter_t iter;
  2438. struct rt_rq *rt_rq;
  2439. rcu_read_lock();
  2440. for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
  2441. print_rt_rq(m, cpu, rt_rq);
  2442. rcu_read_unlock();
  2443. }
  2444. #endif /* CONFIG_SCHED_DEBUG */