rt.c 69 KB

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