sched_rt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3. * policies)
  4. */
  5. #ifdef CONFIG_SMP
  6. static inline int rt_overloaded(struct rq *rq)
  7. {
  8. return atomic_read(&rq->rd->rto_count);
  9. }
  10. static inline void rt_set_overload(struct rq *rq)
  11. {
  12. cpu_set(rq->cpu, rq->rd->rto_mask);
  13. /*
  14. * Make sure the mask is visible before we set
  15. * the overload count. That is checked to determine
  16. * if we should look at the mask. It would be a shame
  17. * if we looked at the mask, but the mask was not
  18. * updated yet.
  19. */
  20. wmb();
  21. atomic_inc(&rq->rd->rto_count);
  22. }
  23. static inline void rt_clear_overload(struct rq *rq)
  24. {
  25. /* the order here really doesn't matter */
  26. atomic_dec(&rq->rd->rto_count);
  27. cpu_clear(rq->cpu, rq->rd->rto_mask);
  28. }
  29. static void update_rt_migration(struct rq *rq)
  30. {
  31. if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
  32. if (!rq->rt.overloaded) {
  33. rt_set_overload(rq);
  34. rq->rt.overloaded = 1;
  35. }
  36. } else if (rq->rt.overloaded) {
  37. rt_clear_overload(rq);
  38. rq->rt.overloaded = 0;
  39. }
  40. }
  41. #endif /* CONFIG_SMP */
  42. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  43. {
  44. return container_of(rt_se, struct task_struct, rt);
  45. }
  46. static inline int on_rt_rq(struct sched_rt_entity *rt_se)
  47. {
  48. return !list_empty(&rt_se->run_list);
  49. }
  50. #ifdef CONFIG_RT_GROUP_SCHED
  51. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  52. {
  53. if (!rt_rq->tg)
  54. return RUNTIME_INF;
  55. return rt_rq->tg->rt_runtime;
  56. }
  57. #define for_each_leaf_rt_rq(rt_rq, rq) \
  58. list_for_each_entry(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
  59. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  60. {
  61. return rt_rq->rq;
  62. }
  63. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  64. {
  65. return rt_se->rt_rq;
  66. }
  67. #define for_each_sched_rt_entity(rt_se) \
  68. for (; rt_se; rt_se = rt_se->parent)
  69. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  70. {
  71. return rt_se->my_q;
  72. }
  73. static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
  74. static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
  75. static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  76. {
  77. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  78. if (rt_se && !on_rt_rq(rt_se) && rt_rq->rt_nr_running) {
  79. struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
  80. enqueue_rt_entity(rt_se);
  81. if (rt_rq->highest_prio < curr->prio)
  82. resched_task(curr);
  83. }
  84. }
  85. static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  86. {
  87. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  88. if (rt_se && on_rt_rq(rt_se))
  89. dequeue_rt_entity(rt_se);
  90. }
  91. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  92. {
  93. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  94. }
  95. static int rt_se_boosted(struct sched_rt_entity *rt_se)
  96. {
  97. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  98. struct task_struct *p;
  99. if (rt_rq)
  100. return !!rt_rq->rt_nr_boosted;
  101. p = rt_task_of(rt_se);
  102. return p->prio != p->normal_prio;
  103. }
  104. #else
  105. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  106. {
  107. if (sysctl_sched_rt_runtime == -1)
  108. return RUNTIME_INF;
  109. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  110. }
  111. #define for_each_leaf_rt_rq(rt_rq, rq) \
  112. for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
  113. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  114. {
  115. return container_of(rt_rq, struct rq, rt);
  116. }
  117. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  118. {
  119. struct task_struct *p = rt_task_of(rt_se);
  120. struct rq *rq = task_rq(p);
  121. return &rq->rt;
  122. }
  123. #define for_each_sched_rt_entity(rt_se) \
  124. for (; rt_se; rt_se = NULL)
  125. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  126. {
  127. return NULL;
  128. }
  129. static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  130. {
  131. }
  132. static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  133. {
  134. }
  135. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  136. {
  137. return rt_rq->rt_throttled;
  138. }
  139. #endif
  140. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  141. {
  142. #ifdef CONFIG_RT_GROUP_SCHED
  143. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  144. if (rt_rq)
  145. return rt_rq->highest_prio;
  146. #endif
  147. return rt_task_of(rt_se)->prio;
  148. }
  149. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  150. {
  151. u64 runtime = sched_rt_runtime(rt_rq);
  152. if (runtime == RUNTIME_INF)
  153. return 0;
  154. if (rt_rq->rt_throttled)
  155. return rt_rq_throttled(rt_rq);
  156. if (rt_rq->rt_time > runtime) {
  157. struct rq *rq = rq_of_rt_rq(rt_rq);
  158. rq->rt_throttled = 1;
  159. rt_rq->rt_throttled = 1;
  160. if (rt_rq_throttled(rt_rq)) {
  161. sched_rt_rq_dequeue(rt_rq);
  162. return 1;
  163. }
  164. }
  165. return 0;
  166. }
  167. static void update_sched_rt_period(struct rq *rq)
  168. {
  169. struct rt_rq *rt_rq;
  170. u64 period;
  171. while (rq->clock > rq->rt_period_expire) {
  172. period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  173. rq->rt_period_expire += period;
  174. for_each_leaf_rt_rq(rt_rq, rq) {
  175. u64 runtime = sched_rt_runtime(rt_rq);
  176. rt_rq->rt_time -= min(rt_rq->rt_time, runtime);
  177. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  178. rt_rq->rt_throttled = 0;
  179. sched_rt_rq_enqueue(rt_rq);
  180. }
  181. }
  182. rq->rt_throttled = 0;
  183. }
  184. }
  185. /*
  186. * Update the current task's runtime statistics. Skip current tasks that
  187. * are not in our scheduling class.
  188. */
  189. static void update_curr_rt(struct rq *rq)
  190. {
  191. struct task_struct *curr = rq->curr;
  192. struct sched_rt_entity *rt_se = &curr->rt;
  193. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  194. u64 delta_exec;
  195. if (!task_has_rt_policy(curr))
  196. return;
  197. delta_exec = rq->clock - curr->se.exec_start;
  198. if (unlikely((s64)delta_exec < 0))
  199. delta_exec = 0;
  200. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  201. curr->se.sum_exec_runtime += delta_exec;
  202. curr->se.exec_start = rq->clock;
  203. cpuacct_charge(curr, delta_exec);
  204. rt_rq->rt_time += delta_exec;
  205. if (sched_rt_runtime_exceeded(rt_rq))
  206. resched_task(curr);
  207. }
  208. static inline
  209. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  210. {
  211. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  212. rt_rq->rt_nr_running++;
  213. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  214. if (rt_se_prio(rt_se) < rt_rq->highest_prio)
  215. rt_rq->highest_prio = rt_se_prio(rt_se);
  216. #endif
  217. #ifdef CONFIG_SMP
  218. if (rt_se->nr_cpus_allowed > 1) {
  219. struct rq *rq = rq_of_rt_rq(rt_rq);
  220. rq->rt.rt_nr_migratory++;
  221. }
  222. update_rt_migration(rq_of_rt_rq(rt_rq));
  223. #endif
  224. #ifdef CONFIG_RT_GROUP_SCHED
  225. if (rt_se_boosted(rt_se))
  226. rt_rq->rt_nr_boosted++;
  227. #endif
  228. }
  229. static inline
  230. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  231. {
  232. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  233. WARN_ON(!rt_rq->rt_nr_running);
  234. rt_rq->rt_nr_running--;
  235. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  236. if (rt_rq->rt_nr_running) {
  237. struct rt_prio_array *array;
  238. WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
  239. if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
  240. /* recalculate */
  241. array = &rt_rq->active;
  242. rt_rq->highest_prio =
  243. sched_find_first_bit(array->bitmap);
  244. } /* otherwise leave rq->highest prio alone */
  245. } else
  246. rt_rq->highest_prio = MAX_RT_PRIO;
  247. #endif
  248. #ifdef CONFIG_SMP
  249. if (rt_se->nr_cpus_allowed > 1) {
  250. struct rq *rq = rq_of_rt_rq(rt_rq);
  251. rq->rt.rt_nr_migratory--;
  252. }
  253. update_rt_migration(rq_of_rt_rq(rt_rq));
  254. #endif /* CONFIG_SMP */
  255. #ifdef CONFIG_RT_GROUP_SCHED
  256. if (rt_se_boosted(rt_se))
  257. rt_rq->rt_nr_boosted--;
  258. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  259. #endif
  260. }
  261. static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
  262. {
  263. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  264. struct rt_prio_array *array = &rt_rq->active;
  265. struct rt_rq *group_rq = group_rt_rq(rt_se);
  266. if (group_rq && rt_rq_throttled(group_rq))
  267. return;
  268. list_add_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
  269. __set_bit(rt_se_prio(rt_se), array->bitmap);
  270. inc_rt_tasks(rt_se, rt_rq);
  271. }
  272. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  273. {
  274. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  275. struct rt_prio_array *array = &rt_rq->active;
  276. list_del_init(&rt_se->run_list);
  277. if (list_empty(array->queue + rt_se_prio(rt_se)))
  278. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  279. dec_rt_tasks(rt_se, rt_rq);
  280. }
  281. /*
  282. * Because the prio of an upper entry depends on the lower
  283. * entries, we must remove entries top - down.
  284. *
  285. * XXX: O(1/2 h^2) because we can only walk up, not down the chain.
  286. * doesn't matter much for now, as h=2 for GROUP_SCHED.
  287. */
  288. static void dequeue_rt_stack(struct task_struct *p)
  289. {
  290. struct sched_rt_entity *rt_se, *top_se;
  291. /*
  292. * dequeue all, top - down.
  293. */
  294. do {
  295. rt_se = &p->rt;
  296. top_se = NULL;
  297. for_each_sched_rt_entity(rt_se) {
  298. if (on_rt_rq(rt_se))
  299. top_se = rt_se;
  300. }
  301. if (top_se)
  302. dequeue_rt_entity(top_se);
  303. } while (top_se);
  304. }
  305. /*
  306. * Adding/removing a task to/from a priority array:
  307. */
  308. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  309. {
  310. struct sched_rt_entity *rt_se = &p->rt;
  311. if (wakeup)
  312. rt_se->timeout = 0;
  313. dequeue_rt_stack(p);
  314. /*
  315. * enqueue everybody, bottom - up.
  316. */
  317. for_each_sched_rt_entity(rt_se)
  318. enqueue_rt_entity(rt_se);
  319. inc_cpu_load(rq, p->se.load.weight);
  320. }
  321. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  322. {
  323. struct sched_rt_entity *rt_se = &p->rt;
  324. struct rt_rq *rt_rq;
  325. update_curr_rt(rq);
  326. dequeue_rt_stack(p);
  327. /*
  328. * re-enqueue all non-empty rt_rq entities.
  329. */
  330. for_each_sched_rt_entity(rt_se) {
  331. rt_rq = group_rt_rq(rt_se);
  332. if (rt_rq && rt_rq->rt_nr_running)
  333. enqueue_rt_entity(rt_se);
  334. }
  335. dec_cpu_load(rq, p->se.load.weight);
  336. }
  337. /*
  338. * Put task to the end of the run list without the overhead of dequeue
  339. * followed by enqueue.
  340. */
  341. static
  342. void requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
  343. {
  344. struct rt_prio_array *array = &rt_rq->active;
  345. list_move_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
  346. }
  347. static void requeue_task_rt(struct rq *rq, struct task_struct *p)
  348. {
  349. struct sched_rt_entity *rt_se = &p->rt;
  350. struct rt_rq *rt_rq;
  351. for_each_sched_rt_entity(rt_se) {
  352. rt_rq = rt_rq_of_se(rt_se);
  353. requeue_rt_entity(rt_rq, rt_se);
  354. }
  355. }
  356. static void yield_task_rt(struct rq *rq)
  357. {
  358. requeue_task_rt(rq, rq->curr);
  359. }
  360. #ifdef CONFIG_SMP
  361. static int find_lowest_rq(struct task_struct *task);
  362. static int select_task_rq_rt(struct task_struct *p, int sync)
  363. {
  364. struct rq *rq = task_rq(p);
  365. /*
  366. * If the current task is an RT task, then
  367. * try to see if we can wake this RT task up on another
  368. * runqueue. Otherwise simply start this RT task
  369. * on its current runqueue.
  370. *
  371. * We want to avoid overloading runqueues. Even if
  372. * the RT task is of higher priority than the current RT task.
  373. * RT tasks behave differently than other tasks. If
  374. * one gets preempted, we try to push it off to another queue.
  375. * So trying to keep a preempting RT task on the same
  376. * cache hot CPU will force the running RT task to
  377. * a cold CPU. So we waste all the cache for the lower
  378. * RT task in hopes of saving some of a RT task
  379. * that is just being woken and probably will have
  380. * cold cache anyway.
  381. */
  382. if (unlikely(rt_task(rq->curr)) &&
  383. (p->rt.nr_cpus_allowed > 1)) {
  384. int cpu = find_lowest_rq(p);
  385. return (cpu == -1) ? task_cpu(p) : cpu;
  386. }
  387. /*
  388. * Otherwise, just let it ride on the affined RQ and the
  389. * post-schedule router will push the preempted task away
  390. */
  391. return task_cpu(p);
  392. }
  393. #endif /* CONFIG_SMP */
  394. /*
  395. * Preempt the current task with a newly woken task if needed:
  396. */
  397. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
  398. {
  399. if (p->prio < rq->curr->prio)
  400. resched_task(rq->curr);
  401. }
  402. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  403. struct rt_rq *rt_rq)
  404. {
  405. struct rt_prio_array *array = &rt_rq->active;
  406. struct sched_rt_entity *next = NULL;
  407. struct list_head *queue;
  408. int idx;
  409. idx = sched_find_first_bit(array->bitmap);
  410. BUG_ON(idx >= MAX_RT_PRIO);
  411. queue = array->queue + idx;
  412. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  413. return next;
  414. }
  415. static struct task_struct *pick_next_task_rt(struct rq *rq)
  416. {
  417. struct sched_rt_entity *rt_se;
  418. struct task_struct *p;
  419. struct rt_rq *rt_rq;
  420. rt_rq = &rq->rt;
  421. if (unlikely(!rt_rq->rt_nr_running))
  422. return NULL;
  423. if (rt_rq_throttled(rt_rq))
  424. return NULL;
  425. do {
  426. rt_se = pick_next_rt_entity(rq, rt_rq);
  427. BUG_ON(!rt_se);
  428. rt_rq = group_rt_rq(rt_se);
  429. } while (rt_rq);
  430. p = rt_task_of(rt_se);
  431. p->se.exec_start = rq->clock;
  432. return p;
  433. }
  434. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  435. {
  436. update_curr_rt(rq);
  437. p->se.exec_start = 0;
  438. }
  439. #ifdef CONFIG_SMP
  440. /* Only try algorithms three times */
  441. #define RT_MAX_TRIES 3
  442. static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
  443. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  444. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  445. {
  446. if (!task_running(rq, p) &&
  447. (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
  448. (p->rt.nr_cpus_allowed > 1))
  449. return 1;
  450. return 0;
  451. }
  452. /* Return the second highest RT task, NULL otherwise */
  453. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  454. {
  455. struct task_struct *next = NULL;
  456. struct sched_rt_entity *rt_se;
  457. struct rt_prio_array *array;
  458. struct rt_rq *rt_rq;
  459. int idx;
  460. for_each_leaf_rt_rq(rt_rq, rq) {
  461. array = &rt_rq->active;
  462. idx = sched_find_first_bit(array->bitmap);
  463. next_idx:
  464. if (idx >= MAX_RT_PRIO)
  465. continue;
  466. if (next && next->prio < idx)
  467. continue;
  468. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  469. struct task_struct *p = rt_task_of(rt_se);
  470. if (pick_rt_task(rq, p, cpu)) {
  471. next = p;
  472. break;
  473. }
  474. }
  475. if (!next) {
  476. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  477. goto next_idx;
  478. }
  479. }
  480. return next;
  481. }
  482. static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
  483. static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
  484. {
  485. int lowest_prio = -1;
  486. int lowest_cpu = -1;
  487. int count = 0;
  488. int cpu;
  489. cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
  490. /*
  491. * Scan each rq for the lowest prio.
  492. */
  493. for_each_cpu_mask(cpu, *lowest_mask) {
  494. struct rq *rq = cpu_rq(cpu);
  495. /* We look for lowest RT prio or non-rt CPU */
  496. if (rq->rt.highest_prio >= MAX_RT_PRIO) {
  497. /*
  498. * if we already found a low RT queue
  499. * and now we found this non-rt queue
  500. * clear the mask and set our bit.
  501. * Otherwise just return the queue as is
  502. * and the count==1 will cause the algorithm
  503. * to use the first bit found.
  504. */
  505. if (lowest_cpu != -1) {
  506. cpus_clear(*lowest_mask);
  507. cpu_set(rq->cpu, *lowest_mask);
  508. }
  509. return 1;
  510. }
  511. /* no locking for now */
  512. if ((rq->rt.highest_prio > task->prio)
  513. && (rq->rt.highest_prio >= lowest_prio)) {
  514. if (rq->rt.highest_prio > lowest_prio) {
  515. /* new low - clear old data */
  516. lowest_prio = rq->rt.highest_prio;
  517. lowest_cpu = cpu;
  518. count = 0;
  519. }
  520. count++;
  521. } else
  522. cpu_clear(cpu, *lowest_mask);
  523. }
  524. /*
  525. * Clear out all the set bits that represent
  526. * runqueues that were of higher prio than
  527. * the lowest_prio.
  528. */
  529. if (lowest_cpu > 0) {
  530. /*
  531. * Perhaps we could add another cpumask op to
  532. * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
  533. * Then that could be optimized to use memset and such.
  534. */
  535. for_each_cpu_mask(cpu, *lowest_mask) {
  536. if (cpu >= lowest_cpu)
  537. break;
  538. cpu_clear(cpu, *lowest_mask);
  539. }
  540. }
  541. return count;
  542. }
  543. static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
  544. {
  545. int first;
  546. /* "this_cpu" is cheaper to preempt than a remote processor */
  547. if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
  548. return this_cpu;
  549. first = first_cpu(*mask);
  550. if (first != NR_CPUS)
  551. return first;
  552. return -1;
  553. }
  554. static int find_lowest_rq(struct task_struct *task)
  555. {
  556. struct sched_domain *sd;
  557. cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
  558. int this_cpu = smp_processor_id();
  559. int cpu = task_cpu(task);
  560. int count = find_lowest_cpus(task, lowest_mask);
  561. if (!count)
  562. return -1; /* No targets found */
  563. /*
  564. * There is no sense in performing an optimal search if only one
  565. * target is found.
  566. */
  567. if (count == 1)
  568. return first_cpu(*lowest_mask);
  569. /*
  570. * At this point we have built a mask of cpus representing the
  571. * lowest priority tasks in the system. Now we want to elect
  572. * the best one based on our affinity and topology.
  573. *
  574. * We prioritize the last cpu that the task executed on since
  575. * it is most likely cache-hot in that location.
  576. */
  577. if (cpu_isset(cpu, *lowest_mask))
  578. return cpu;
  579. /*
  580. * Otherwise, we consult the sched_domains span maps to figure
  581. * out which cpu is logically closest to our hot cache data.
  582. */
  583. if (this_cpu == cpu)
  584. this_cpu = -1; /* Skip this_cpu opt if the same */
  585. for_each_domain(cpu, sd) {
  586. if (sd->flags & SD_WAKE_AFFINE) {
  587. cpumask_t domain_mask;
  588. int best_cpu;
  589. cpus_and(domain_mask, sd->span, *lowest_mask);
  590. best_cpu = pick_optimal_cpu(this_cpu,
  591. &domain_mask);
  592. if (best_cpu != -1)
  593. return best_cpu;
  594. }
  595. }
  596. /*
  597. * And finally, if there were no matches within the domains
  598. * just give the caller *something* to work with from the compatible
  599. * locations.
  600. */
  601. return pick_optimal_cpu(this_cpu, lowest_mask);
  602. }
  603. /* Will lock the rq it finds */
  604. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  605. {
  606. struct rq *lowest_rq = NULL;
  607. int tries;
  608. int cpu;
  609. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  610. cpu = find_lowest_rq(task);
  611. if ((cpu == -1) || (cpu == rq->cpu))
  612. break;
  613. lowest_rq = cpu_rq(cpu);
  614. /* if the prio of this runqueue changed, try again */
  615. if (double_lock_balance(rq, lowest_rq)) {
  616. /*
  617. * We had to unlock the run queue. In
  618. * the mean time, task could have
  619. * migrated already or had its affinity changed.
  620. * Also make sure that it wasn't scheduled on its rq.
  621. */
  622. if (unlikely(task_rq(task) != rq ||
  623. !cpu_isset(lowest_rq->cpu,
  624. task->cpus_allowed) ||
  625. task_running(rq, task) ||
  626. !task->se.on_rq)) {
  627. spin_unlock(&lowest_rq->lock);
  628. lowest_rq = NULL;
  629. break;
  630. }
  631. }
  632. /* If this rq is still suitable use it. */
  633. if (lowest_rq->rt.highest_prio > task->prio)
  634. break;
  635. /* try again */
  636. spin_unlock(&lowest_rq->lock);
  637. lowest_rq = NULL;
  638. }
  639. return lowest_rq;
  640. }
  641. /*
  642. * If the current CPU has more than one RT task, see if the non
  643. * running task can migrate over to a CPU that is running a task
  644. * of lesser priority.
  645. */
  646. static int push_rt_task(struct rq *rq)
  647. {
  648. struct task_struct *next_task;
  649. struct rq *lowest_rq;
  650. int ret = 0;
  651. int paranoid = RT_MAX_TRIES;
  652. if (!rq->rt.overloaded)
  653. return 0;
  654. next_task = pick_next_highest_task_rt(rq, -1);
  655. if (!next_task)
  656. return 0;
  657. retry:
  658. if (unlikely(next_task == rq->curr)) {
  659. WARN_ON(1);
  660. return 0;
  661. }
  662. /*
  663. * It's possible that the next_task slipped in of
  664. * higher priority than current. If that's the case
  665. * just reschedule current.
  666. */
  667. if (unlikely(next_task->prio < rq->curr->prio)) {
  668. resched_task(rq->curr);
  669. return 0;
  670. }
  671. /* We might release rq lock */
  672. get_task_struct(next_task);
  673. /* find_lock_lowest_rq locks the rq if found */
  674. lowest_rq = find_lock_lowest_rq(next_task, rq);
  675. if (!lowest_rq) {
  676. struct task_struct *task;
  677. /*
  678. * find lock_lowest_rq releases rq->lock
  679. * so it is possible that next_task has changed.
  680. * If it has, then try again.
  681. */
  682. task = pick_next_highest_task_rt(rq, -1);
  683. if (unlikely(task != next_task) && task && paranoid--) {
  684. put_task_struct(next_task);
  685. next_task = task;
  686. goto retry;
  687. }
  688. goto out;
  689. }
  690. deactivate_task(rq, next_task, 0);
  691. set_task_cpu(next_task, lowest_rq->cpu);
  692. activate_task(lowest_rq, next_task, 0);
  693. resched_task(lowest_rq->curr);
  694. spin_unlock(&lowest_rq->lock);
  695. ret = 1;
  696. out:
  697. put_task_struct(next_task);
  698. return ret;
  699. }
  700. /*
  701. * TODO: Currently we just use the second highest prio task on
  702. * the queue, and stop when it can't migrate (or there's
  703. * no more RT tasks). There may be a case where a lower
  704. * priority RT task has a different affinity than the
  705. * higher RT task. In this case the lower RT task could
  706. * possibly be able to migrate where as the higher priority
  707. * RT task could not. We currently ignore this issue.
  708. * Enhancements are welcome!
  709. */
  710. static void push_rt_tasks(struct rq *rq)
  711. {
  712. /* push_rt_task will return true if it moved an RT */
  713. while (push_rt_task(rq))
  714. ;
  715. }
  716. static int pull_rt_task(struct rq *this_rq)
  717. {
  718. int this_cpu = this_rq->cpu, ret = 0, cpu;
  719. struct task_struct *p, *next;
  720. struct rq *src_rq;
  721. if (likely(!rt_overloaded(this_rq)))
  722. return 0;
  723. next = pick_next_task_rt(this_rq);
  724. for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
  725. if (this_cpu == cpu)
  726. continue;
  727. src_rq = cpu_rq(cpu);
  728. /*
  729. * We can potentially drop this_rq's lock in
  730. * double_lock_balance, and another CPU could
  731. * steal our next task - hence we must cause
  732. * the caller to recalculate the next task
  733. * in that case:
  734. */
  735. if (double_lock_balance(this_rq, src_rq)) {
  736. struct task_struct *old_next = next;
  737. next = pick_next_task_rt(this_rq);
  738. if (next != old_next)
  739. ret = 1;
  740. }
  741. /*
  742. * Are there still pullable RT tasks?
  743. */
  744. if (src_rq->rt.rt_nr_running <= 1)
  745. goto skip;
  746. p = pick_next_highest_task_rt(src_rq, this_cpu);
  747. /*
  748. * Do we have an RT task that preempts
  749. * the to-be-scheduled task?
  750. */
  751. if (p && (!next || (p->prio < next->prio))) {
  752. WARN_ON(p == src_rq->curr);
  753. WARN_ON(!p->se.on_rq);
  754. /*
  755. * There's a chance that p is higher in priority
  756. * than what's currently running on its cpu.
  757. * This is just that p is wakeing up and hasn't
  758. * had a chance to schedule. We only pull
  759. * p if it is lower in priority than the
  760. * current task on the run queue or
  761. * this_rq next task is lower in prio than
  762. * the current task on that rq.
  763. */
  764. if (p->prio < src_rq->curr->prio ||
  765. (next && next->prio < src_rq->curr->prio))
  766. goto skip;
  767. ret = 1;
  768. deactivate_task(src_rq, p, 0);
  769. set_task_cpu(p, this_cpu);
  770. activate_task(this_rq, p, 0);
  771. /*
  772. * We continue with the search, just in
  773. * case there's an even higher prio task
  774. * in another runqueue. (low likelyhood
  775. * but possible)
  776. *
  777. * Update next so that we won't pick a task
  778. * on another cpu with a priority lower (or equal)
  779. * than the one we just picked.
  780. */
  781. next = p;
  782. }
  783. skip:
  784. spin_unlock(&src_rq->lock);
  785. }
  786. return ret;
  787. }
  788. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  789. {
  790. /* Try to pull RT tasks here if we lower this rq's prio */
  791. if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
  792. pull_rt_task(rq);
  793. }
  794. static void post_schedule_rt(struct rq *rq)
  795. {
  796. /*
  797. * If we have more than one rt_task queued, then
  798. * see if we can push the other rt_tasks off to other CPUS.
  799. * Note we may release the rq lock, and since
  800. * the lock was owned by prev, we need to release it
  801. * first via finish_lock_switch and then reaquire it here.
  802. */
  803. if (unlikely(rq->rt.overloaded)) {
  804. spin_lock_irq(&rq->lock);
  805. push_rt_tasks(rq);
  806. spin_unlock_irq(&rq->lock);
  807. }
  808. }
  809. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  810. {
  811. if (!task_running(rq, p) &&
  812. (p->prio >= rq->rt.highest_prio) &&
  813. rq->rt.overloaded)
  814. push_rt_tasks(rq);
  815. }
  816. static unsigned long
  817. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  818. unsigned long max_load_move,
  819. struct sched_domain *sd, enum cpu_idle_type idle,
  820. int *all_pinned, int *this_best_prio)
  821. {
  822. /* don't touch RT tasks */
  823. return 0;
  824. }
  825. static int
  826. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  827. struct sched_domain *sd, enum cpu_idle_type idle)
  828. {
  829. /* don't touch RT tasks */
  830. return 0;
  831. }
  832. static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
  833. {
  834. int weight = cpus_weight(*new_mask);
  835. BUG_ON(!rt_task(p));
  836. /*
  837. * Update the migration status of the RQ if we have an RT task
  838. * which is running AND changing its weight value.
  839. */
  840. if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
  841. struct rq *rq = task_rq(p);
  842. if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
  843. rq->rt.rt_nr_migratory++;
  844. } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
  845. BUG_ON(!rq->rt.rt_nr_migratory);
  846. rq->rt.rt_nr_migratory--;
  847. }
  848. update_rt_migration(rq);
  849. }
  850. p->cpus_allowed = *new_mask;
  851. p->rt.nr_cpus_allowed = weight;
  852. }
  853. /* Assumes rq->lock is held */
  854. static void join_domain_rt(struct rq *rq)
  855. {
  856. if (rq->rt.overloaded)
  857. rt_set_overload(rq);
  858. }
  859. /* Assumes rq->lock is held */
  860. static void leave_domain_rt(struct rq *rq)
  861. {
  862. if (rq->rt.overloaded)
  863. rt_clear_overload(rq);
  864. }
  865. /*
  866. * When switch from the rt queue, we bring ourselves to a position
  867. * that we might want to pull RT tasks from other runqueues.
  868. */
  869. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  870. int running)
  871. {
  872. /*
  873. * If there are other RT tasks then we will reschedule
  874. * and the scheduling of the other RT tasks will handle
  875. * the balancing. But if we are the last RT task
  876. * we may need to handle the pulling of RT tasks
  877. * now.
  878. */
  879. if (!rq->rt.rt_nr_running)
  880. pull_rt_task(rq);
  881. }
  882. #endif /* CONFIG_SMP */
  883. /*
  884. * When switching a task to RT, we may overload the runqueue
  885. * with RT tasks. In this case we try to push them off to
  886. * other runqueues.
  887. */
  888. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  889. int running)
  890. {
  891. int check_resched = 1;
  892. /*
  893. * If we are already running, then there's nothing
  894. * that needs to be done. But if we are not running
  895. * we may need to preempt the current running task.
  896. * If that current running task is also an RT task
  897. * then see if we can move to another run queue.
  898. */
  899. if (!running) {
  900. #ifdef CONFIG_SMP
  901. if (rq->rt.overloaded && push_rt_task(rq) &&
  902. /* Don't resched if we changed runqueues */
  903. rq != task_rq(p))
  904. check_resched = 0;
  905. #endif /* CONFIG_SMP */
  906. if (check_resched && p->prio < rq->curr->prio)
  907. resched_task(rq->curr);
  908. }
  909. }
  910. /*
  911. * Priority of the task has changed. This may cause
  912. * us to initiate a push or pull.
  913. */
  914. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  915. int oldprio, int running)
  916. {
  917. if (running) {
  918. #ifdef CONFIG_SMP
  919. /*
  920. * If our priority decreases while running, we
  921. * may need to pull tasks to this runqueue.
  922. */
  923. if (oldprio < p->prio)
  924. pull_rt_task(rq);
  925. /*
  926. * If there's a higher priority task waiting to run
  927. * then reschedule.
  928. */
  929. if (p->prio > rq->rt.highest_prio)
  930. resched_task(p);
  931. #else
  932. /* For UP simply resched on drop of prio */
  933. if (oldprio < p->prio)
  934. resched_task(p);
  935. #endif /* CONFIG_SMP */
  936. } else {
  937. /*
  938. * This task is not running, but if it is
  939. * greater than the current running task
  940. * then reschedule.
  941. */
  942. if (p->prio < rq->curr->prio)
  943. resched_task(rq->curr);
  944. }
  945. }
  946. static void watchdog(struct rq *rq, struct task_struct *p)
  947. {
  948. unsigned long soft, hard;
  949. if (!p->signal)
  950. return;
  951. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  952. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  953. if (soft != RLIM_INFINITY) {
  954. unsigned long next;
  955. p->rt.timeout++;
  956. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  957. if (p->rt.timeout > next)
  958. p->it_sched_expires = p->se.sum_exec_runtime;
  959. }
  960. }
  961. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  962. {
  963. update_curr_rt(rq);
  964. watchdog(rq, p);
  965. /*
  966. * RR tasks need a special form of timeslice management.
  967. * FIFO tasks have no timeslices.
  968. */
  969. if (p->policy != SCHED_RR)
  970. return;
  971. if (--p->rt.time_slice)
  972. return;
  973. p->rt.time_slice = DEF_TIMESLICE;
  974. /*
  975. * Requeue to the end of queue if we are not the only element
  976. * on the queue:
  977. */
  978. if (p->rt.run_list.prev != p->rt.run_list.next) {
  979. requeue_task_rt(rq, p);
  980. set_tsk_need_resched(p);
  981. }
  982. }
  983. static void set_curr_task_rt(struct rq *rq)
  984. {
  985. struct task_struct *p = rq->curr;
  986. p->se.exec_start = rq->clock;
  987. }
  988. const struct sched_class rt_sched_class = {
  989. .next = &fair_sched_class,
  990. .enqueue_task = enqueue_task_rt,
  991. .dequeue_task = dequeue_task_rt,
  992. .yield_task = yield_task_rt,
  993. #ifdef CONFIG_SMP
  994. .select_task_rq = select_task_rq_rt,
  995. #endif /* CONFIG_SMP */
  996. .check_preempt_curr = check_preempt_curr_rt,
  997. .pick_next_task = pick_next_task_rt,
  998. .put_prev_task = put_prev_task_rt,
  999. #ifdef CONFIG_SMP
  1000. .load_balance = load_balance_rt,
  1001. .move_one_task = move_one_task_rt,
  1002. .set_cpus_allowed = set_cpus_allowed_rt,
  1003. .join_domain = join_domain_rt,
  1004. .leave_domain = leave_domain_rt,
  1005. .pre_schedule = pre_schedule_rt,
  1006. .post_schedule = post_schedule_rt,
  1007. .task_wake_up = task_wake_up_rt,
  1008. .switched_from = switched_from_rt,
  1009. #endif
  1010. .set_curr_task = set_curr_task_rt,
  1011. .task_tick = task_tick_rt,
  1012. .prio_changed = prio_changed_rt,
  1013. .switched_to = switched_to_rt,
  1014. };