rt.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714
  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 = kcalloc(nr_cpu_ids, sizeof(rt_rq), GFP_KERNEL);
  152. if (!tg->rt_rq)
  153. goto err;
  154. tg->rt_se = kcalloc(nr_cpu_ids, sizeof(rt_se), 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. update_rq_clock(rq);
  681. if (rt_rq->rt_time) {
  682. u64 runtime;
  683. raw_spin_lock(&rt_rq->rt_runtime_lock);
  684. if (rt_rq->rt_throttled)
  685. balance_runtime(rt_rq);
  686. runtime = rt_rq->rt_runtime;
  687. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  688. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  689. rt_rq->rt_throttled = 0;
  690. enqueue = 1;
  691. /*
  692. * When we're idle and a woken (rt) task is
  693. * throttled check_preempt_curr() will set
  694. * skip_update and the time between the wakeup
  695. * and this unthrottle will get accounted as
  696. * 'runtime'.
  697. */
  698. if (rt_rq->rt_nr_running && rq->curr == rq->idle)
  699. rq_clock_cancel_skipupdate(rq);
  700. }
  701. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  702. idle = 0;
  703. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  704. } else if (rt_rq->rt_nr_running) {
  705. idle = 0;
  706. if (!rt_rq_throttled(rt_rq))
  707. enqueue = 1;
  708. }
  709. if (rt_rq->rt_throttled)
  710. throttled = 1;
  711. if (enqueue)
  712. sched_rt_rq_enqueue(rt_rq);
  713. raw_spin_unlock(&rq->lock);
  714. }
  715. if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
  716. return 1;
  717. return idle;
  718. }
  719. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  720. {
  721. #ifdef CONFIG_RT_GROUP_SCHED
  722. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  723. if (rt_rq)
  724. return rt_rq->highest_prio.curr;
  725. #endif
  726. return rt_task_of(rt_se)->prio;
  727. }
  728. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  729. {
  730. u64 runtime = sched_rt_runtime(rt_rq);
  731. if (rt_rq->rt_throttled)
  732. return rt_rq_throttled(rt_rq);
  733. if (runtime >= sched_rt_period(rt_rq))
  734. return 0;
  735. balance_runtime(rt_rq);
  736. runtime = sched_rt_runtime(rt_rq);
  737. if (runtime == RUNTIME_INF)
  738. return 0;
  739. if (rt_rq->rt_time > runtime) {
  740. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  741. /*
  742. * Don't actually throttle groups that have no runtime assigned
  743. * but accrue some time due to boosting.
  744. */
  745. if (likely(rt_b->rt_runtime)) {
  746. rt_rq->rt_throttled = 1;
  747. printk_deferred_once("sched: RT throttling activated\n");
  748. } else {
  749. /*
  750. * In case we did anyway, make it go away,
  751. * replenishment is a joke, since it will replenish us
  752. * with exactly 0 ns.
  753. */
  754. rt_rq->rt_time = 0;
  755. }
  756. if (rt_rq_throttled(rt_rq)) {
  757. sched_rt_rq_dequeue(rt_rq);
  758. return 1;
  759. }
  760. }
  761. return 0;
  762. }
  763. /*
  764. * Update the current task's runtime statistics. Skip current tasks that
  765. * are not in our scheduling class.
  766. */
  767. static void update_curr_rt(struct rq *rq)
  768. {
  769. struct task_struct *curr = rq->curr;
  770. struct sched_rt_entity *rt_se = &curr->rt;
  771. u64 delta_exec;
  772. u64 now;
  773. if (curr->sched_class != &rt_sched_class)
  774. return;
  775. now = rq_clock_task(rq);
  776. delta_exec = now - curr->se.exec_start;
  777. if (unlikely((s64)delta_exec <= 0))
  778. return;
  779. schedstat_set(curr->se.statistics.exec_max,
  780. max(curr->se.statistics.exec_max, delta_exec));
  781. curr->se.sum_exec_runtime += delta_exec;
  782. account_group_exec_runtime(curr, delta_exec);
  783. curr->se.exec_start = now;
  784. cgroup_account_cputime(curr, delta_exec);
  785. sched_rt_avg_update(rq, delta_exec);
  786. if (!rt_bandwidth_enabled())
  787. return;
  788. for_each_sched_rt_entity(rt_se) {
  789. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  790. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  791. raw_spin_lock(&rt_rq->rt_runtime_lock);
  792. rt_rq->rt_time += delta_exec;
  793. if (sched_rt_runtime_exceeded(rt_rq))
  794. resched_curr(rq);
  795. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  796. }
  797. }
  798. }
  799. static void
  800. dequeue_top_rt_rq(struct rt_rq *rt_rq)
  801. {
  802. struct rq *rq = rq_of_rt_rq(rt_rq);
  803. BUG_ON(&rq->rt != rt_rq);
  804. if (!rt_rq->rt_queued)
  805. return;
  806. BUG_ON(!rq->nr_running);
  807. sub_nr_running(rq, rt_rq->rt_nr_running);
  808. rt_rq->rt_queued = 0;
  809. /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
  810. cpufreq_update_util(rq, 0);
  811. }
  812. static void
  813. enqueue_top_rt_rq(struct rt_rq *rt_rq)
  814. {
  815. struct rq *rq = rq_of_rt_rq(rt_rq);
  816. BUG_ON(&rq->rt != rt_rq);
  817. if (rt_rq->rt_queued)
  818. return;
  819. if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
  820. return;
  821. add_nr_running(rq, rt_rq->rt_nr_running);
  822. rt_rq->rt_queued = 1;
  823. /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
  824. cpufreq_update_util(rq, 0);
  825. }
  826. #if defined CONFIG_SMP
  827. static void
  828. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  829. {
  830. struct rq *rq = rq_of_rt_rq(rt_rq);
  831. #ifdef CONFIG_RT_GROUP_SCHED
  832. /*
  833. * Change rq's cpupri only if rt_rq is the top queue.
  834. */
  835. if (&rq->rt != rt_rq)
  836. return;
  837. #endif
  838. if (rq->online && prio < prev_prio)
  839. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  840. }
  841. static void
  842. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  843. {
  844. struct rq *rq = rq_of_rt_rq(rt_rq);
  845. #ifdef CONFIG_RT_GROUP_SCHED
  846. /*
  847. * Change rq's cpupri only if rt_rq is the top queue.
  848. */
  849. if (&rq->rt != rt_rq)
  850. return;
  851. #endif
  852. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  853. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  854. }
  855. #else /* CONFIG_SMP */
  856. static inline
  857. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  858. static inline
  859. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  860. #endif /* CONFIG_SMP */
  861. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  862. static void
  863. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  864. {
  865. int prev_prio = rt_rq->highest_prio.curr;
  866. if (prio < prev_prio)
  867. rt_rq->highest_prio.curr = prio;
  868. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  869. }
  870. static void
  871. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  872. {
  873. int prev_prio = rt_rq->highest_prio.curr;
  874. if (rt_rq->rt_nr_running) {
  875. WARN_ON(prio < prev_prio);
  876. /*
  877. * This may have been our highest task, and therefore
  878. * we may have some recomputation to do
  879. */
  880. if (prio == prev_prio) {
  881. struct rt_prio_array *array = &rt_rq->active;
  882. rt_rq->highest_prio.curr =
  883. sched_find_first_bit(array->bitmap);
  884. }
  885. } else
  886. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  887. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  888. }
  889. #else
  890. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  891. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  892. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  893. #ifdef CONFIG_RT_GROUP_SCHED
  894. static void
  895. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  896. {
  897. if (rt_se_boosted(rt_se))
  898. rt_rq->rt_nr_boosted++;
  899. if (rt_rq->tg)
  900. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  901. }
  902. static void
  903. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  904. {
  905. if (rt_se_boosted(rt_se))
  906. rt_rq->rt_nr_boosted--;
  907. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  908. }
  909. #else /* CONFIG_RT_GROUP_SCHED */
  910. static void
  911. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  912. {
  913. start_rt_bandwidth(&def_rt_bandwidth);
  914. }
  915. static inline
  916. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  917. #endif /* CONFIG_RT_GROUP_SCHED */
  918. static inline
  919. unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
  920. {
  921. struct rt_rq *group_rq = group_rt_rq(rt_se);
  922. if (group_rq)
  923. return group_rq->rt_nr_running;
  924. else
  925. return 1;
  926. }
  927. static inline
  928. unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
  929. {
  930. struct rt_rq *group_rq = group_rt_rq(rt_se);
  931. struct task_struct *tsk;
  932. if (group_rq)
  933. return group_rq->rr_nr_running;
  934. tsk = rt_task_of(rt_se);
  935. return (tsk->policy == SCHED_RR) ? 1 : 0;
  936. }
  937. static inline
  938. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  939. {
  940. int prio = rt_se_prio(rt_se);
  941. WARN_ON(!rt_prio(prio));
  942. rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
  943. rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
  944. inc_rt_prio(rt_rq, prio);
  945. inc_rt_migration(rt_se, rt_rq);
  946. inc_rt_group(rt_se, rt_rq);
  947. }
  948. static inline
  949. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  950. {
  951. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  952. WARN_ON(!rt_rq->rt_nr_running);
  953. rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
  954. rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
  955. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  956. dec_rt_migration(rt_se, rt_rq);
  957. dec_rt_group(rt_se, rt_rq);
  958. }
  959. /*
  960. * Change rt_se->run_list location unless SAVE && !MOVE
  961. *
  962. * assumes ENQUEUE/DEQUEUE flags match
  963. */
  964. static inline bool move_entity(unsigned int flags)
  965. {
  966. if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
  967. return false;
  968. return true;
  969. }
  970. static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
  971. {
  972. list_del_init(&rt_se->run_list);
  973. if (list_empty(array->queue + rt_se_prio(rt_se)))
  974. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  975. rt_se->on_list = 0;
  976. }
  977. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  978. {
  979. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  980. struct rt_prio_array *array = &rt_rq->active;
  981. struct rt_rq *group_rq = group_rt_rq(rt_se);
  982. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  983. /*
  984. * Don't enqueue the group if its throttled, or when empty.
  985. * The latter is a consequence of the former when a child group
  986. * get throttled and the current group doesn't have any other
  987. * active members.
  988. */
  989. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
  990. if (rt_se->on_list)
  991. __delist_rt_entity(rt_se, array);
  992. return;
  993. }
  994. if (move_entity(flags)) {
  995. WARN_ON_ONCE(rt_se->on_list);
  996. if (flags & ENQUEUE_HEAD)
  997. list_add(&rt_se->run_list, queue);
  998. else
  999. list_add_tail(&rt_se->run_list, queue);
  1000. __set_bit(rt_se_prio(rt_se), array->bitmap);
  1001. rt_se->on_list = 1;
  1002. }
  1003. rt_se->on_rq = 1;
  1004. inc_rt_tasks(rt_se, rt_rq);
  1005. }
  1006. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1007. {
  1008. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  1009. struct rt_prio_array *array = &rt_rq->active;
  1010. if (move_entity(flags)) {
  1011. WARN_ON_ONCE(!rt_se->on_list);
  1012. __delist_rt_entity(rt_se, array);
  1013. }
  1014. rt_se->on_rq = 0;
  1015. dec_rt_tasks(rt_se, rt_rq);
  1016. }
  1017. /*
  1018. * Because the prio of an upper entry depends on the lower
  1019. * entries, we must remove entries top - down.
  1020. */
  1021. static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
  1022. {
  1023. struct sched_rt_entity *back = NULL;
  1024. for_each_sched_rt_entity(rt_se) {
  1025. rt_se->back = back;
  1026. back = rt_se;
  1027. }
  1028. dequeue_top_rt_rq(rt_rq_of_se(back));
  1029. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  1030. if (on_rt_rq(rt_se))
  1031. __dequeue_rt_entity(rt_se, flags);
  1032. }
  1033. }
  1034. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1035. {
  1036. struct rq *rq = rq_of_rt_se(rt_se);
  1037. dequeue_rt_stack(rt_se, flags);
  1038. for_each_sched_rt_entity(rt_se)
  1039. __enqueue_rt_entity(rt_se, flags);
  1040. enqueue_top_rt_rq(&rq->rt);
  1041. }
  1042. static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
  1043. {
  1044. struct rq *rq = rq_of_rt_se(rt_se);
  1045. dequeue_rt_stack(rt_se, flags);
  1046. for_each_sched_rt_entity(rt_se) {
  1047. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  1048. if (rt_rq && rt_rq->rt_nr_running)
  1049. __enqueue_rt_entity(rt_se, flags);
  1050. }
  1051. enqueue_top_rt_rq(&rq->rt);
  1052. }
  1053. /*
  1054. * Adding/removing a task to/from a priority array:
  1055. */
  1056. static void
  1057. enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1058. {
  1059. struct sched_rt_entity *rt_se = &p->rt;
  1060. if (flags & ENQUEUE_WAKEUP)
  1061. rt_se->timeout = 0;
  1062. enqueue_rt_entity(rt_se, flags);
  1063. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  1064. enqueue_pushable_task(rq, p);
  1065. }
  1066. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1067. {
  1068. struct sched_rt_entity *rt_se = &p->rt;
  1069. update_curr_rt(rq);
  1070. dequeue_rt_entity(rt_se, flags);
  1071. dequeue_pushable_task(rq, p);
  1072. }
  1073. /*
  1074. * Put task to the head or the end of the run list without the overhead of
  1075. * dequeue followed by enqueue.
  1076. */
  1077. static void
  1078. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  1079. {
  1080. if (on_rt_rq(rt_se)) {
  1081. struct rt_prio_array *array = &rt_rq->active;
  1082. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  1083. if (head)
  1084. list_move(&rt_se->run_list, queue);
  1085. else
  1086. list_move_tail(&rt_se->run_list, queue);
  1087. }
  1088. }
  1089. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  1090. {
  1091. struct sched_rt_entity *rt_se = &p->rt;
  1092. struct rt_rq *rt_rq;
  1093. for_each_sched_rt_entity(rt_se) {
  1094. rt_rq = rt_rq_of_se(rt_se);
  1095. requeue_rt_entity(rt_rq, rt_se, head);
  1096. }
  1097. }
  1098. static void yield_task_rt(struct rq *rq)
  1099. {
  1100. requeue_task_rt(rq, rq->curr, 0);
  1101. }
  1102. #ifdef CONFIG_SMP
  1103. static int find_lowest_rq(struct task_struct *task);
  1104. static int
  1105. select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
  1106. {
  1107. struct task_struct *curr;
  1108. struct rq *rq;
  1109. /* For anything but wake ups, just return the task_cpu */
  1110. if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
  1111. goto out;
  1112. rq = cpu_rq(cpu);
  1113. rcu_read_lock();
  1114. curr = READ_ONCE(rq->curr); /* unlocked access */
  1115. /*
  1116. * If the current task on @p's runqueue is an RT task, then
  1117. * try to see if we can wake this RT task up on another
  1118. * runqueue. Otherwise simply start this RT task
  1119. * on its current runqueue.
  1120. *
  1121. * We want to avoid overloading runqueues. If the woken
  1122. * task is a higher priority, then it will stay on this CPU
  1123. * and the lower prio task should be moved to another CPU.
  1124. * Even though this will probably make the lower prio task
  1125. * lose its cache, we do not want to bounce a higher task
  1126. * around just because it gave up its CPU, perhaps for a
  1127. * lock?
  1128. *
  1129. * For equal prio tasks, we just let the scheduler sort it out.
  1130. *
  1131. * Otherwise, just let it ride on the affined RQ and the
  1132. * post-schedule router will push the preempted task away
  1133. *
  1134. * This test is optimistic, if we get it wrong the load-balancer
  1135. * will have to sort it out.
  1136. */
  1137. if (curr && unlikely(rt_task(curr)) &&
  1138. (curr->nr_cpus_allowed < 2 ||
  1139. curr->prio <= p->prio)) {
  1140. int target = find_lowest_rq(p);
  1141. /*
  1142. * Don't bother moving it if the destination CPU is
  1143. * not running a lower priority task.
  1144. */
  1145. if (target != -1 &&
  1146. p->prio < cpu_rq(target)->rt.highest_prio.curr)
  1147. cpu = target;
  1148. }
  1149. rcu_read_unlock();
  1150. out:
  1151. return cpu;
  1152. }
  1153. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  1154. {
  1155. /*
  1156. * Current can't be migrated, useless to reschedule,
  1157. * let's hope p can move out.
  1158. */
  1159. if (rq->curr->nr_cpus_allowed == 1 ||
  1160. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  1161. return;
  1162. /*
  1163. * p is migratable, so let's not schedule it and
  1164. * see if it is pushed or pulled somewhere else.
  1165. */
  1166. if (p->nr_cpus_allowed != 1
  1167. && cpupri_find(&rq->rd->cpupri, p, NULL))
  1168. return;
  1169. /*
  1170. * There appear to be other CPUs that can accept
  1171. * the current task but none can run 'p', so lets reschedule
  1172. * to try and push the current task away:
  1173. */
  1174. requeue_task_rt(rq, p, 1);
  1175. resched_curr(rq);
  1176. }
  1177. #endif /* CONFIG_SMP */
  1178. /*
  1179. * Preempt the current task with a newly woken task if needed:
  1180. */
  1181. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
  1182. {
  1183. if (p->prio < rq->curr->prio) {
  1184. resched_curr(rq);
  1185. return;
  1186. }
  1187. #ifdef CONFIG_SMP
  1188. /*
  1189. * If:
  1190. *
  1191. * - the newly woken task is of equal priority to the current task
  1192. * - the newly woken task is non-migratable while current is migratable
  1193. * - current will be preempted on the next reschedule
  1194. *
  1195. * we should check to see if current can readily move to a different
  1196. * cpu. If so, we will reschedule to allow the push logic to try
  1197. * to move current somewhere else, making room for our non-migratable
  1198. * task.
  1199. */
  1200. if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
  1201. check_preempt_equal_prio(rq, p);
  1202. #endif
  1203. }
  1204. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  1205. struct rt_rq *rt_rq)
  1206. {
  1207. struct rt_prio_array *array = &rt_rq->active;
  1208. struct sched_rt_entity *next = NULL;
  1209. struct list_head *queue;
  1210. int idx;
  1211. idx = sched_find_first_bit(array->bitmap);
  1212. BUG_ON(idx >= MAX_RT_PRIO);
  1213. queue = array->queue + idx;
  1214. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  1215. return next;
  1216. }
  1217. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  1218. {
  1219. struct sched_rt_entity *rt_se;
  1220. struct task_struct *p;
  1221. struct rt_rq *rt_rq = &rq->rt;
  1222. do {
  1223. rt_se = pick_next_rt_entity(rq, rt_rq);
  1224. BUG_ON(!rt_se);
  1225. rt_rq = group_rt_rq(rt_se);
  1226. } while (rt_rq);
  1227. p = rt_task_of(rt_se);
  1228. p->se.exec_start = rq_clock_task(rq);
  1229. return p;
  1230. }
  1231. static struct task_struct *
  1232. pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
  1233. {
  1234. struct task_struct *p;
  1235. struct rt_rq *rt_rq = &rq->rt;
  1236. if (need_pull_rt_task(rq, prev)) {
  1237. /*
  1238. * This is OK, because current is on_cpu, which avoids it being
  1239. * picked for load-balance and preemption/IRQs are still
  1240. * disabled avoiding further scheduler activity on it and we're
  1241. * being very careful to re-start the picking loop.
  1242. */
  1243. rq_unpin_lock(rq, rf);
  1244. pull_rt_task(rq);
  1245. rq_repin_lock(rq, rf);
  1246. /*
  1247. * pull_rt_task() can drop (and re-acquire) rq->lock; this
  1248. * means a dl or stop task can slip in, in which case we need
  1249. * to re-start task selection.
  1250. */
  1251. if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
  1252. rq->dl.dl_nr_running))
  1253. return RETRY_TASK;
  1254. }
  1255. /*
  1256. * We may dequeue prev's rt_rq in put_prev_task().
  1257. * So, we update time before rt_nr_running check.
  1258. */
  1259. if (prev->sched_class == &rt_sched_class)
  1260. update_curr_rt(rq);
  1261. if (!rt_rq->rt_queued)
  1262. return NULL;
  1263. put_prev_task(rq, prev);
  1264. p = _pick_next_task_rt(rq);
  1265. /* The running task is never eligible for pushing */
  1266. dequeue_pushable_task(rq, p);
  1267. rt_queue_push_tasks(rq);
  1268. return p;
  1269. }
  1270. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  1271. {
  1272. update_curr_rt(rq);
  1273. /*
  1274. * The previous task needs to be made eligible for pushing
  1275. * if it is still active
  1276. */
  1277. if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  1278. enqueue_pushable_task(rq, p);
  1279. }
  1280. #ifdef CONFIG_SMP
  1281. /* Only try algorithms three times */
  1282. #define RT_MAX_TRIES 3
  1283. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  1284. {
  1285. if (!task_running(rq, p) &&
  1286. cpumask_test_cpu(cpu, &p->cpus_allowed))
  1287. return 1;
  1288. return 0;
  1289. }
  1290. /*
  1291. * Return the highest pushable rq's task, which is suitable to be executed
  1292. * on the CPU, NULL otherwise
  1293. */
  1294. static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
  1295. {
  1296. struct plist_head *head = &rq->rt.pushable_tasks;
  1297. struct task_struct *p;
  1298. if (!has_pushable_tasks(rq))
  1299. return NULL;
  1300. plist_for_each_entry(p, head, pushable_tasks) {
  1301. if (pick_rt_task(rq, p, cpu))
  1302. return p;
  1303. }
  1304. return NULL;
  1305. }
  1306. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  1307. static int find_lowest_rq(struct task_struct *task)
  1308. {
  1309. struct sched_domain *sd;
  1310. struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
  1311. int this_cpu = smp_processor_id();
  1312. int cpu = task_cpu(task);
  1313. /* Make sure the mask is initialized first */
  1314. if (unlikely(!lowest_mask))
  1315. return -1;
  1316. if (task->nr_cpus_allowed == 1)
  1317. return -1; /* No other targets possible */
  1318. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  1319. return -1; /* No targets found */
  1320. /*
  1321. * At this point we have built a mask of CPUs representing the
  1322. * lowest priority tasks in the system. Now we want to elect
  1323. * the best one based on our affinity and topology.
  1324. *
  1325. * We prioritize the last CPU that the task executed on since
  1326. * it is most likely cache-hot in that location.
  1327. */
  1328. if (cpumask_test_cpu(cpu, lowest_mask))
  1329. return cpu;
  1330. /*
  1331. * Otherwise, we consult the sched_domains span maps to figure
  1332. * out which CPU is logically closest to our hot cache data.
  1333. */
  1334. if (!cpumask_test_cpu(this_cpu, lowest_mask))
  1335. this_cpu = -1; /* Skip this_cpu opt if not among lowest */
  1336. rcu_read_lock();
  1337. for_each_domain(cpu, sd) {
  1338. if (sd->flags & SD_WAKE_AFFINE) {
  1339. int best_cpu;
  1340. /*
  1341. * "this_cpu" is cheaper to preempt than a
  1342. * remote processor.
  1343. */
  1344. if (this_cpu != -1 &&
  1345. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1346. rcu_read_unlock();
  1347. return this_cpu;
  1348. }
  1349. best_cpu = cpumask_first_and(lowest_mask,
  1350. sched_domain_span(sd));
  1351. if (best_cpu < nr_cpu_ids) {
  1352. rcu_read_unlock();
  1353. return best_cpu;
  1354. }
  1355. }
  1356. }
  1357. rcu_read_unlock();
  1358. /*
  1359. * And finally, if there were no matches within the domains
  1360. * just give the caller *something* to work with from the compatible
  1361. * locations.
  1362. */
  1363. if (this_cpu != -1)
  1364. return this_cpu;
  1365. cpu = cpumask_any(lowest_mask);
  1366. if (cpu < nr_cpu_ids)
  1367. return cpu;
  1368. return -1;
  1369. }
  1370. /* Will lock the rq it finds */
  1371. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  1372. {
  1373. struct rq *lowest_rq = NULL;
  1374. int tries;
  1375. int cpu;
  1376. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  1377. cpu = find_lowest_rq(task);
  1378. if ((cpu == -1) || (cpu == rq->cpu))
  1379. break;
  1380. lowest_rq = cpu_rq(cpu);
  1381. if (lowest_rq->rt.highest_prio.curr <= task->prio) {
  1382. /*
  1383. * Target rq has tasks of equal or higher priority,
  1384. * retrying does not release any lock and is unlikely
  1385. * to yield a different result.
  1386. */
  1387. lowest_rq = NULL;
  1388. break;
  1389. }
  1390. /* if the prio of this runqueue changed, try again */
  1391. if (double_lock_balance(rq, lowest_rq)) {
  1392. /*
  1393. * We had to unlock the run queue. In
  1394. * the mean time, task could have
  1395. * migrated already or had its affinity changed.
  1396. * Also make sure that it wasn't scheduled on its rq.
  1397. */
  1398. if (unlikely(task_rq(task) != rq ||
  1399. !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
  1400. task_running(rq, task) ||
  1401. !rt_task(task) ||
  1402. !task_on_rq_queued(task))) {
  1403. double_unlock_balance(rq, lowest_rq);
  1404. lowest_rq = NULL;
  1405. break;
  1406. }
  1407. }
  1408. /* If this rq is still suitable use it. */
  1409. if (lowest_rq->rt.highest_prio.curr > task->prio)
  1410. break;
  1411. /* try again */
  1412. double_unlock_balance(rq, lowest_rq);
  1413. lowest_rq = NULL;
  1414. }
  1415. return lowest_rq;
  1416. }
  1417. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1418. {
  1419. struct task_struct *p;
  1420. if (!has_pushable_tasks(rq))
  1421. return NULL;
  1422. p = plist_first_entry(&rq->rt.pushable_tasks,
  1423. struct task_struct, pushable_tasks);
  1424. BUG_ON(rq->cpu != task_cpu(p));
  1425. BUG_ON(task_current(rq, p));
  1426. BUG_ON(p->nr_cpus_allowed <= 1);
  1427. BUG_ON(!task_on_rq_queued(p));
  1428. BUG_ON(!rt_task(p));
  1429. return p;
  1430. }
  1431. /*
  1432. * If the current CPU has more than one RT task, see if the non
  1433. * running task can migrate over to a CPU that is running a task
  1434. * of lesser priority.
  1435. */
  1436. static int push_rt_task(struct rq *rq)
  1437. {
  1438. struct task_struct *next_task;
  1439. struct rq *lowest_rq;
  1440. int ret = 0;
  1441. if (!rq->rt.overloaded)
  1442. return 0;
  1443. next_task = pick_next_pushable_task(rq);
  1444. if (!next_task)
  1445. return 0;
  1446. retry:
  1447. if (unlikely(next_task == rq->curr)) {
  1448. WARN_ON(1);
  1449. return 0;
  1450. }
  1451. /*
  1452. * It's possible that the next_task slipped in of
  1453. * higher priority than current. If that's the case
  1454. * just reschedule current.
  1455. */
  1456. if (unlikely(next_task->prio < rq->curr->prio)) {
  1457. resched_curr(rq);
  1458. return 0;
  1459. }
  1460. /* We might release rq lock */
  1461. get_task_struct(next_task);
  1462. /* find_lock_lowest_rq locks the rq if found */
  1463. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1464. if (!lowest_rq) {
  1465. struct task_struct *task;
  1466. /*
  1467. * find_lock_lowest_rq releases rq->lock
  1468. * so it is possible that next_task has migrated.
  1469. *
  1470. * We need to make sure that the task is still on the same
  1471. * run-queue and is also still the next task eligible for
  1472. * pushing.
  1473. */
  1474. task = pick_next_pushable_task(rq);
  1475. if (task == next_task) {
  1476. /*
  1477. * The task hasn't migrated, and is still the next
  1478. * eligible task, but we failed to find a run-queue
  1479. * to push it to. Do not retry in this case, since
  1480. * other CPUs will pull from us when ready.
  1481. */
  1482. goto out;
  1483. }
  1484. if (!task)
  1485. /* No more tasks, just exit */
  1486. goto out;
  1487. /*
  1488. * Something has shifted, try again.
  1489. */
  1490. put_task_struct(next_task);
  1491. next_task = task;
  1492. goto retry;
  1493. }
  1494. deactivate_task(rq, next_task, 0);
  1495. set_task_cpu(next_task, lowest_rq->cpu);
  1496. activate_task(lowest_rq, next_task, 0);
  1497. ret = 1;
  1498. resched_curr(lowest_rq);
  1499. double_unlock_balance(rq, lowest_rq);
  1500. out:
  1501. put_task_struct(next_task);
  1502. return ret;
  1503. }
  1504. static void push_rt_tasks(struct rq *rq)
  1505. {
  1506. /* push_rt_task will return true if it moved an RT */
  1507. while (push_rt_task(rq))
  1508. ;
  1509. }
  1510. #ifdef HAVE_RT_PUSH_IPI
  1511. /*
  1512. * When a high priority task schedules out from a CPU and a lower priority
  1513. * task is scheduled in, a check is made to see if there's any RT tasks
  1514. * on other CPUs that are waiting to run because a higher priority RT task
  1515. * is currently running on its CPU. In this case, the CPU with multiple RT
  1516. * tasks queued on it (overloaded) needs to be notified that a CPU has opened
  1517. * up that may be able to run one of its non-running queued RT tasks.
  1518. *
  1519. * All CPUs with overloaded RT tasks need to be notified as there is currently
  1520. * no way to know which of these CPUs have the highest priority task waiting
  1521. * to run. Instead of trying to take a spinlock on each of these CPUs,
  1522. * which has shown to cause large latency when done on machines with many
  1523. * CPUs, sending an IPI to the CPUs to have them push off the overloaded
  1524. * RT tasks waiting to run.
  1525. *
  1526. * Just sending an IPI to each of the CPUs is also an issue, as on large
  1527. * count CPU machines, this can cause an IPI storm on a CPU, especially
  1528. * if its the only CPU with multiple RT tasks queued, and a large number
  1529. * of CPUs scheduling a lower priority task at the same time.
  1530. *
  1531. * Each root domain has its own irq work function that can iterate over
  1532. * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
  1533. * tassk must be checked if there's one or many CPUs that are lowering
  1534. * their priority, there's a single irq work iterator that will try to
  1535. * push off RT tasks that are waiting to run.
  1536. *
  1537. * When a CPU schedules a lower priority task, it will kick off the
  1538. * irq work iterator that will jump to each CPU with overloaded RT tasks.
  1539. * As it only takes the first CPU that schedules a lower priority task
  1540. * to start the process, the rto_start variable is incremented and if
  1541. * the atomic result is one, then that CPU will try to take the rto_lock.
  1542. * This prevents high contention on the lock as the process handles all
  1543. * CPUs scheduling lower priority tasks.
  1544. *
  1545. * All CPUs that are scheduling a lower priority task will increment the
  1546. * rt_loop_next variable. This will make sure that the irq work iterator
  1547. * checks all RT overloaded CPUs whenever a CPU schedules a new lower
  1548. * priority task, even if the iterator is in the middle of a scan. Incrementing
  1549. * the rt_loop_next will cause the iterator to perform another scan.
  1550. *
  1551. */
  1552. static int rto_next_cpu(struct root_domain *rd)
  1553. {
  1554. int next;
  1555. int cpu;
  1556. /*
  1557. * When starting the IPI RT pushing, the rto_cpu is set to -1,
  1558. * rt_next_cpu() will simply return the first CPU found in
  1559. * the rto_mask.
  1560. *
  1561. * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
  1562. * will return the next CPU found in the rto_mask.
  1563. *
  1564. * If there are no more CPUs left in the rto_mask, then a check is made
  1565. * against rto_loop and rto_loop_next. rto_loop is only updated with
  1566. * the rto_lock held, but any CPU may increment the rto_loop_next
  1567. * without any locking.
  1568. */
  1569. for (;;) {
  1570. /* When rto_cpu is -1 this acts like cpumask_first() */
  1571. cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
  1572. rd->rto_cpu = cpu;
  1573. if (cpu < nr_cpu_ids)
  1574. return cpu;
  1575. rd->rto_cpu = -1;
  1576. /*
  1577. * ACQUIRE ensures we see the @rto_mask changes
  1578. * made prior to the @next value observed.
  1579. *
  1580. * Matches WMB in rt_set_overload().
  1581. */
  1582. next = atomic_read_acquire(&rd->rto_loop_next);
  1583. if (rd->rto_loop == next)
  1584. break;
  1585. rd->rto_loop = next;
  1586. }
  1587. return -1;
  1588. }
  1589. static inline bool rto_start_trylock(atomic_t *v)
  1590. {
  1591. return !atomic_cmpxchg_acquire(v, 0, 1);
  1592. }
  1593. static inline void rto_start_unlock(atomic_t *v)
  1594. {
  1595. atomic_set_release(v, 0);
  1596. }
  1597. static void tell_cpu_to_push(struct rq *rq)
  1598. {
  1599. int cpu = -1;
  1600. /* Keep the loop going if the IPI is currently active */
  1601. atomic_inc(&rq->rd->rto_loop_next);
  1602. /* Only one CPU can initiate a loop at a time */
  1603. if (!rto_start_trylock(&rq->rd->rto_loop_start))
  1604. return;
  1605. raw_spin_lock(&rq->rd->rto_lock);
  1606. /*
  1607. * The rto_cpu is updated under the lock, if it has a valid CPU
  1608. * then the IPI is still running and will continue due to the
  1609. * update to loop_next, and nothing needs to be done here.
  1610. * Otherwise it is finishing up and an ipi needs to be sent.
  1611. */
  1612. if (rq->rd->rto_cpu < 0)
  1613. cpu = rto_next_cpu(rq->rd);
  1614. raw_spin_unlock(&rq->rd->rto_lock);
  1615. rto_start_unlock(&rq->rd->rto_loop_start);
  1616. if (cpu >= 0) {
  1617. /* Make sure the rd does not get freed while pushing */
  1618. sched_get_rd(rq->rd);
  1619. irq_work_queue_on(&rq->rd->rto_push_work, cpu);
  1620. }
  1621. }
  1622. /* Called from hardirq context */
  1623. void rto_push_irq_work_func(struct irq_work *work)
  1624. {
  1625. struct root_domain *rd =
  1626. container_of(work, struct root_domain, rto_push_work);
  1627. struct rq *rq;
  1628. int cpu;
  1629. rq = this_rq();
  1630. /*
  1631. * We do not need to grab the lock to check for has_pushable_tasks.
  1632. * When it gets updated, a check is made if a push is possible.
  1633. */
  1634. if (has_pushable_tasks(rq)) {
  1635. raw_spin_lock(&rq->lock);
  1636. push_rt_tasks(rq);
  1637. raw_spin_unlock(&rq->lock);
  1638. }
  1639. raw_spin_lock(&rd->rto_lock);
  1640. /* Pass the IPI to the next rt overloaded queue */
  1641. cpu = rto_next_cpu(rd);
  1642. raw_spin_unlock(&rd->rto_lock);
  1643. if (cpu < 0) {
  1644. sched_put_rd(rd);
  1645. return;
  1646. }
  1647. /* Try the next RT overloaded CPU */
  1648. irq_work_queue_on(&rd->rto_push_work, cpu);
  1649. }
  1650. #endif /* HAVE_RT_PUSH_IPI */
  1651. static void pull_rt_task(struct rq *this_rq)
  1652. {
  1653. int this_cpu = this_rq->cpu, cpu;
  1654. bool resched = false;
  1655. struct task_struct *p;
  1656. struct rq *src_rq;
  1657. int rt_overload_count = rt_overloaded(this_rq);
  1658. if (likely(!rt_overload_count))
  1659. return;
  1660. /*
  1661. * Match the barrier from rt_set_overloaded; this guarantees that if we
  1662. * see overloaded we must also see the rto_mask bit.
  1663. */
  1664. smp_rmb();
  1665. /* If we are the only overloaded CPU do nothing */
  1666. if (rt_overload_count == 1 &&
  1667. cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
  1668. return;
  1669. #ifdef HAVE_RT_PUSH_IPI
  1670. if (sched_feat(RT_PUSH_IPI)) {
  1671. tell_cpu_to_push(this_rq);
  1672. return;
  1673. }
  1674. #endif
  1675. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1676. if (this_cpu == cpu)
  1677. continue;
  1678. src_rq = cpu_rq(cpu);
  1679. /*
  1680. * Don't bother taking the src_rq->lock if the next highest
  1681. * task is known to be lower-priority than our current task.
  1682. * This may look racy, but if this value is about to go
  1683. * logically higher, the src_rq will push this task away.
  1684. * And if its going logically lower, we do not care
  1685. */
  1686. if (src_rq->rt.highest_prio.next >=
  1687. this_rq->rt.highest_prio.curr)
  1688. continue;
  1689. /*
  1690. * We can potentially drop this_rq's lock in
  1691. * double_lock_balance, and another CPU could
  1692. * alter this_rq
  1693. */
  1694. double_lock_balance(this_rq, src_rq);
  1695. /*
  1696. * We can pull only a task, which is pushable
  1697. * on its rq, and no others.
  1698. */
  1699. p = pick_highest_pushable_task(src_rq, this_cpu);
  1700. /*
  1701. * Do we have an RT task that preempts
  1702. * the to-be-scheduled task?
  1703. */
  1704. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1705. WARN_ON(p == src_rq->curr);
  1706. WARN_ON(!task_on_rq_queued(p));
  1707. /*
  1708. * There's a chance that p is higher in priority
  1709. * than what's currently running on its CPU.
  1710. * This is just that p is wakeing up and hasn't
  1711. * had a chance to schedule. We only pull
  1712. * p if it is lower in priority than the
  1713. * current task on the run queue
  1714. */
  1715. if (p->prio < src_rq->curr->prio)
  1716. goto skip;
  1717. resched = true;
  1718. deactivate_task(src_rq, p, 0);
  1719. set_task_cpu(p, this_cpu);
  1720. activate_task(this_rq, p, 0);
  1721. /*
  1722. * We continue with the search, just in
  1723. * case there's an even higher prio task
  1724. * in another runqueue. (low likelihood
  1725. * but possible)
  1726. */
  1727. }
  1728. skip:
  1729. double_unlock_balance(this_rq, src_rq);
  1730. }
  1731. if (resched)
  1732. resched_curr(this_rq);
  1733. }
  1734. /*
  1735. * If we are not running and we are not going to reschedule soon, we should
  1736. * try to push tasks away now
  1737. */
  1738. static void task_woken_rt(struct rq *rq, struct task_struct *p)
  1739. {
  1740. if (!task_running(rq, p) &&
  1741. !test_tsk_need_resched(rq->curr) &&
  1742. p->nr_cpus_allowed > 1 &&
  1743. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  1744. (rq->curr->nr_cpus_allowed < 2 ||
  1745. rq->curr->prio <= p->prio))
  1746. push_rt_tasks(rq);
  1747. }
  1748. /* Assumes rq->lock is held */
  1749. static void rq_online_rt(struct rq *rq)
  1750. {
  1751. if (rq->rt.overloaded)
  1752. rt_set_overload(rq);
  1753. __enable_runtime(rq);
  1754. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1755. }
  1756. /* Assumes rq->lock is held */
  1757. static void rq_offline_rt(struct rq *rq)
  1758. {
  1759. if (rq->rt.overloaded)
  1760. rt_clear_overload(rq);
  1761. __disable_runtime(rq);
  1762. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1763. }
  1764. /*
  1765. * When switch from the rt queue, we bring ourselves to a position
  1766. * that we might want to pull RT tasks from other runqueues.
  1767. */
  1768. static void switched_from_rt(struct rq *rq, struct task_struct *p)
  1769. {
  1770. /*
  1771. * If there are other RT tasks then we will reschedule
  1772. * and the scheduling of the other RT tasks will handle
  1773. * the balancing. But if we are the last RT task
  1774. * we may need to handle the pulling of RT tasks
  1775. * now.
  1776. */
  1777. if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
  1778. return;
  1779. rt_queue_pull_task(rq);
  1780. }
  1781. void __init init_sched_rt_class(void)
  1782. {
  1783. unsigned int i;
  1784. for_each_possible_cpu(i) {
  1785. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  1786. GFP_KERNEL, cpu_to_node(i));
  1787. }
  1788. }
  1789. #endif /* CONFIG_SMP */
  1790. /*
  1791. * When switching a task to RT, we may overload the runqueue
  1792. * with RT tasks. In this case we try to push them off to
  1793. * other runqueues.
  1794. */
  1795. static void switched_to_rt(struct rq *rq, struct task_struct *p)
  1796. {
  1797. /*
  1798. * If we are already running, then there's nothing
  1799. * that needs to be done. But if we are not running
  1800. * we may need to preempt the current running task.
  1801. * If that current running task is also an RT task
  1802. * then see if we can move to another run queue.
  1803. */
  1804. if (task_on_rq_queued(p) && rq->curr != p) {
  1805. #ifdef CONFIG_SMP
  1806. if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  1807. rt_queue_push_tasks(rq);
  1808. #endif /* CONFIG_SMP */
  1809. if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
  1810. resched_curr(rq);
  1811. }
  1812. }
  1813. /*
  1814. * Priority of the task has changed. This may cause
  1815. * us to initiate a push or pull.
  1816. */
  1817. static void
  1818. prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
  1819. {
  1820. if (!task_on_rq_queued(p))
  1821. return;
  1822. if (rq->curr == p) {
  1823. #ifdef CONFIG_SMP
  1824. /*
  1825. * If our priority decreases while running, we
  1826. * may need to pull tasks to this runqueue.
  1827. */
  1828. if (oldprio < p->prio)
  1829. rt_queue_pull_task(rq);
  1830. /*
  1831. * If there's a higher priority task waiting to run
  1832. * then reschedule.
  1833. */
  1834. if (p->prio > rq->rt.highest_prio.curr)
  1835. resched_curr(rq);
  1836. #else
  1837. /* For UP simply resched on drop of prio */
  1838. if (oldprio < p->prio)
  1839. resched_curr(rq);
  1840. #endif /* CONFIG_SMP */
  1841. } else {
  1842. /*
  1843. * This task is not running, but if it is
  1844. * greater than the current running task
  1845. * then reschedule.
  1846. */
  1847. if (p->prio < rq->curr->prio)
  1848. resched_curr(rq);
  1849. }
  1850. }
  1851. #ifdef CONFIG_POSIX_TIMERS
  1852. static void watchdog(struct rq *rq, struct task_struct *p)
  1853. {
  1854. unsigned long soft, hard;
  1855. /* max may change after cur was read, this will be fixed next tick */
  1856. soft = task_rlimit(p, RLIMIT_RTTIME);
  1857. hard = task_rlimit_max(p, RLIMIT_RTTIME);
  1858. if (soft != RLIM_INFINITY) {
  1859. unsigned long next;
  1860. if (p->rt.watchdog_stamp != jiffies) {
  1861. p->rt.timeout++;
  1862. p->rt.watchdog_stamp = jiffies;
  1863. }
  1864. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1865. if (p->rt.timeout > next)
  1866. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1867. }
  1868. }
  1869. #else
  1870. static inline void watchdog(struct rq *rq, struct task_struct *p) { }
  1871. #endif
  1872. /*
  1873. * scheduler tick hitting a task of our scheduling class.
  1874. *
  1875. * NOTE: This function can be called remotely by the tick offload that
  1876. * goes along full dynticks. Therefore no local assumption can be made
  1877. * and everything must be accessed through the @rq and @curr passed in
  1878. * parameters.
  1879. */
  1880. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1881. {
  1882. struct sched_rt_entity *rt_se = &p->rt;
  1883. update_curr_rt(rq);
  1884. watchdog(rq, p);
  1885. /*
  1886. * RR tasks need a special form of timeslice management.
  1887. * FIFO tasks have no timeslices.
  1888. */
  1889. if (p->policy != SCHED_RR)
  1890. return;
  1891. if (--p->rt.time_slice)
  1892. return;
  1893. p->rt.time_slice = sched_rr_timeslice;
  1894. /*
  1895. * Requeue to the end of queue if we (and all of our ancestors) are not
  1896. * the only element on the queue
  1897. */
  1898. for_each_sched_rt_entity(rt_se) {
  1899. if (rt_se->run_list.prev != rt_se->run_list.next) {
  1900. requeue_task_rt(rq, p, 0);
  1901. resched_curr(rq);
  1902. return;
  1903. }
  1904. }
  1905. }
  1906. static void set_curr_task_rt(struct rq *rq)
  1907. {
  1908. struct task_struct *p = rq->curr;
  1909. p->se.exec_start = rq_clock_task(rq);
  1910. /* The running task is never eligible for pushing */
  1911. dequeue_pushable_task(rq, p);
  1912. }
  1913. static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
  1914. {
  1915. /*
  1916. * Time slice is 0 for SCHED_FIFO tasks
  1917. */
  1918. if (task->policy == SCHED_RR)
  1919. return sched_rr_timeslice;
  1920. else
  1921. return 0;
  1922. }
  1923. const struct sched_class rt_sched_class = {
  1924. .next = &fair_sched_class,
  1925. .enqueue_task = enqueue_task_rt,
  1926. .dequeue_task = dequeue_task_rt,
  1927. .yield_task = yield_task_rt,
  1928. .check_preempt_curr = check_preempt_curr_rt,
  1929. .pick_next_task = pick_next_task_rt,
  1930. .put_prev_task = put_prev_task_rt,
  1931. #ifdef CONFIG_SMP
  1932. .select_task_rq = select_task_rq_rt,
  1933. .set_cpus_allowed = set_cpus_allowed_common,
  1934. .rq_online = rq_online_rt,
  1935. .rq_offline = rq_offline_rt,
  1936. .task_woken = task_woken_rt,
  1937. .switched_from = switched_from_rt,
  1938. #endif
  1939. .set_curr_task = set_curr_task_rt,
  1940. .task_tick = task_tick_rt,
  1941. .get_rr_interval = get_rr_interval_rt,
  1942. .prio_changed = prio_changed_rt,
  1943. .switched_to = switched_to_rt,
  1944. .update_curr = update_curr_rt,
  1945. };
  1946. #ifdef CONFIG_RT_GROUP_SCHED
  1947. /*
  1948. * Ensure that the real time constraints are schedulable.
  1949. */
  1950. static DEFINE_MUTEX(rt_constraints_mutex);
  1951. /* Must be called with tasklist_lock held */
  1952. static inline int tg_has_rt_tasks(struct task_group *tg)
  1953. {
  1954. struct task_struct *g, *p;
  1955. /*
  1956. * Autogroups do not have RT tasks; see autogroup_create().
  1957. */
  1958. if (task_group_is_autogroup(tg))
  1959. return 0;
  1960. for_each_process_thread(g, p) {
  1961. if (rt_task(p) && task_group(p) == tg)
  1962. return 1;
  1963. }
  1964. return 0;
  1965. }
  1966. struct rt_schedulable_data {
  1967. struct task_group *tg;
  1968. u64 rt_period;
  1969. u64 rt_runtime;
  1970. };
  1971. static int tg_rt_schedulable(struct task_group *tg, void *data)
  1972. {
  1973. struct rt_schedulable_data *d = data;
  1974. struct task_group *child;
  1975. unsigned long total, sum = 0;
  1976. u64 period, runtime;
  1977. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  1978. runtime = tg->rt_bandwidth.rt_runtime;
  1979. if (tg == d->tg) {
  1980. period = d->rt_period;
  1981. runtime = d->rt_runtime;
  1982. }
  1983. /*
  1984. * Cannot have more runtime than the period.
  1985. */
  1986. if (runtime > period && runtime != RUNTIME_INF)
  1987. return -EINVAL;
  1988. /*
  1989. * Ensure we don't starve existing RT tasks.
  1990. */
  1991. if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
  1992. return -EBUSY;
  1993. total = to_ratio(period, runtime);
  1994. /*
  1995. * Nobody can have more than the global setting allows.
  1996. */
  1997. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  1998. return -EINVAL;
  1999. /*
  2000. * The sum of our children's runtime should not exceed our own.
  2001. */
  2002. list_for_each_entry_rcu(child, &tg->children, siblings) {
  2003. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  2004. runtime = child->rt_bandwidth.rt_runtime;
  2005. if (child == d->tg) {
  2006. period = d->rt_period;
  2007. runtime = d->rt_runtime;
  2008. }
  2009. sum += to_ratio(period, runtime);
  2010. }
  2011. if (sum > total)
  2012. return -EINVAL;
  2013. return 0;
  2014. }
  2015. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  2016. {
  2017. int ret;
  2018. struct rt_schedulable_data data = {
  2019. .tg = tg,
  2020. .rt_period = period,
  2021. .rt_runtime = runtime,
  2022. };
  2023. rcu_read_lock();
  2024. ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
  2025. rcu_read_unlock();
  2026. return ret;
  2027. }
  2028. static int tg_set_rt_bandwidth(struct task_group *tg,
  2029. u64 rt_period, u64 rt_runtime)
  2030. {
  2031. int i, err = 0;
  2032. /*
  2033. * Disallowing the root group RT runtime is BAD, it would disallow the
  2034. * kernel creating (and or operating) RT threads.
  2035. */
  2036. if (tg == &root_task_group && rt_runtime == 0)
  2037. return -EINVAL;
  2038. /* No period doesn't make any sense. */
  2039. if (rt_period == 0)
  2040. return -EINVAL;
  2041. mutex_lock(&rt_constraints_mutex);
  2042. read_lock(&tasklist_lock);
  2043. err = __rt_schedulable(tg, rt_period, rt_runtime);
  2044. if (err)
  2045. goto unlock;
  2046. raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  2047. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  2048. tg->rt_bandwidth.rt_runtime = rt_runtime;
  2049. for_each_possible_cpu(i) {
  2050. struct rt_rq *rt_rq = tg->rt_rq[i];
  2051. raw_spin_lock(&rt_rq->rt_runtime_lock);
  2052. rt_rq->rt_runtime = rt_runtime;
  2053. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  2054. }
  2055. raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  2056. unlock:
  2057. read_unlock(&tasklist_lock);
  2058. mutex_unlock(&rt_constraints_mutex);
  2059. return err;
  2060. }
  2061. int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  2062. {
  2063. u64 rt_runtime, rt_period;
  2064. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  2065. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  2066. if (rt_runtime_us < 0)
  2067. rt_runtime = RUNTIME_INF;
  2068. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  2069. }
  2070. long sched_group_rt_runtime(struct task_group *tg)
  2071. {
  2072. u64 rt_runtime_us;
  2073. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  2074. return -1;
  2075. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  2076. do_div(rt_runtime_us, NSEC_PER_USEC);
  2077. return rt_runtime_us;
  2078. }
  2079. int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
  2080. {
  2081. u64 rt_runtime, rt_period;
  2082. rt_period = rt_period_us * NSEC_PER_USEC;
  2083. rt_runtime = tg->rt_bandwidth.rt_runtime;
  2084. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  2085. }
  2086. long sched_group_rt_period(struct task_group *tg)
  2087. {
  2088. u64 rt_period_us;
  2089. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  2090. do_div(rt_period_us, NSEC_PER_USEC);
  2091. return rt_period_us;
  2092. }
  2093. static int sched_rt_global_constraints(void)
  2094. {
  2095. int ret = 0;
  2096. mutex_lock(&rt_constraints_mutex);
  2097. read_lock(&tasklist_lock);
  2098. ret = __rt_schedulable(NULL, 0, 0);
  2099. read_unlock(&tasklist_lock);
  2100. mutex_unlock(&rt_constraints_mutex);
  2101. return ret;
  2102. }
  2103. int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  2104. {
  2105. /* Don't accept realtime tasks when there is no way for them to run */
  2106. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  2107. return 0;
  2108. return 1;
  2109. }
  2110. #else /* !CONFIG_RT_GROUP_SCHED */
  2111. static int sched_rt_global_constraints(void)
  2112. {
  2113. unsigned long flags;
  2114. int i;
  2115. raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  2116. for_each_possible_cpu(i) {
  2117. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  2118. raw_spin_lock(&rt_rq->rt_runtime_lock);
  2119. rt_rq->rt_runtime = global_rt_runtime();
  2120. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  2121. }
  2122. raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  2123. return 0;
  2124. }
  2125. #endif /* CONFIG_RT_GROUP_SCHED */
  2126. static int sched_rt_global_validate(void)
  2127. {
  2128. if (sysctl_sched_rt_period <= 0)
  2129. return -EINVAL;
  2130. if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
  2131. (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
  2132. return -EINVAL;
  2133. return 0;
  2134. }
  2135. static void sched_rt_do_global(void)
  2136. {
  2137. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  2138. def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
  2139. }
  2140. int sched_rt_handler(struct ctl_table *table, int write,
  2141. void __user *buffer, size_t *lenp,
  2142. loff_t *ppos)
  2143. {
  2144. int old_period, old_runtime;
  2145. static DEFINE_MUTEX(mutex);
  2146. int ret;
  2147. mutex_lock(&mutex);
  2148. old_period = sysctl_sched_rt_period;
  2149. old_runtime = sysctl_sched_rt_runtime;
  2150. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2151. if (!ret && write) {
  2152. ret = sched_rt_global_validate();
  2153. if (ret)
  2154. goto undo;
  2155. ret = sched_dl_global_validate();
  2156. if (ret)
  2157. goto undo;
  2158. ret = sched_rt_global_constraints();
  2159. if (ret)
  2160. goto undo;
  2161. sched_rt_do_global();
  2162. sched_dl_do_global();
  2163. }
  2164. if (0) {
  2165. undo:
  2166. sysctl_sched_rt_period = old_period;
  2167. sysctl_sched_rt_runtime = old_runtime;
  2168. }
  2169. mutex_unlock(&mutex);
  2170. return ret;
  2171. }
  2172. int sched_rr_handler(struct ctl_table *table, int write,
  2173. void __user *buffer, size_t *lenp,
  2174. loff_t *ppos)
  2175. {
  2176. int ret;
  2177. static DEFINE_MUTEX(mutex);
  2178. mutex_lock(&mutex);
  2179. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2180. /*
  2181. * Make sure that internally we keep jiffies.
  2182. * Also, writing zero resets the timeslice to default:
  2183. */
  2184. if (!ret && write) {
  2185. sched_rr_timeslice =
  2186. sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
  2187. msecs_to_jiffies(sysctl_sched_rr_timeslice);
  2188. }
  2189. mutex_unlock(&mutex);
  2190. return ret;
  2191. }
  2192. #ifdef CONFIG_SCHED_DEBUG
  2193. void print_rt_stats(struct seq_file *m, int cpu)
  2194. {
  2195. rt_rq_iter_t iter;
  2196. struct rt_rq *rt_rq;
  2197. rcu_read_lock();
  2198. for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
  2199. print_rt_rq(m, cpu, rt_rq);
  2200. rcu_read_unlock();
  2201. }
  2202. #endif /* CONFIG_SCHED_DEBUG */