rt.c 63 KB

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