cpudeadline.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * kernel/sched/cpudl.c
  3. *
  4. * Global CPU deadline management
  5. *
  6. * Author: Juri Lelli <j.lelli@sssup.it>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. */
  13. #include "sched.h"
  14. static inline int parent(int i)
  15. {
  16. return (i - 1) >> 1;
  17. }
  18. static inline int left_child(int i)
  19. {
  20. return (i << 1) + 1;
  21. }
  22. static inline int right_child(int i)
  23. {
  24. return (i << 1) + 2;
  25. }
  26. static void cpudl_heapify_down(struct cpudl *cp, int idx)
  27. {
  28. int l, r, largest;
  29. int orig_cpu = cp->elements[idx].cpu;
  30. u64 orig_dl = cp->elements[idx].dl;
  31. if (left_child(idx) >= cp->size)
  32. return;
  33. /* adapted from lib/prio_heap.c */
  34. while (1) {
  35. u64 largest_dl;
  36. l = left_child(idx);
  37. r = right_child(idx);
  38. largest = idx;
  39. largest_dl = orig_dl;
  40. if ((l < cp->size) && dl_time_before(orig_dl,
  41. cp->elements[l].dl)) {
  42. largest = l;
  43. largest_dl = cp->elements[l].dl;
  44. }
  45. if ((r < cp->size) && dl_time_before(largest_dl,
  46. cp->elements[r].dl))
  47. largest = r;
  48. if (largest == idx)
  49. break;
  50. /* pull largest child onto idx */
  51. cp->elements[idx].cpu = cp->elements[largest].cpu;
  52. cp->elements[idx].dl = cp->elements[largest].dl;
  53. cp->elements[cp->elements[idx].cpu].idx = idx;
  54. idx = largest;
  55. }
  56. /* actual push down of saved original values orig_* */
  57. cp->elements[idx].cpu = orig_cpu;
  58. cp->elements[idx].dl = orig_dl;
  59. cp->elements[cp->elements[idx].cpu].idx = idx;
  60. }
  61. static void cpudl_heapify_up(struct cpudl *cp, int idx)
  62. {
  63. int p;
  64. int orig_cpu = cp->elements[idx].cpu;
  65. u64 orig_dl = cp->elements[idx].dl;
  66. if (idx == 0)
  67. return;
  68. do {
  69. p = parent(idx);
  70. if (dl_time_before(orig_dl, cp->elements[p].dl))
  71. break;
  72. /* pull parent onto idx */
  73. cp->elements[idx].cpu = cp->elements[p].cpu;
  74. cp->elements[idx].dl = cp->elements[p].dl;
  75. cp->elements[cp->elements[idx].cpu].idx = idx;
  76. idx = p;
  77. } while (idx != 0);
  78. /* actual push up of saved original values orig_* */
  79. cp->elements[idx].cpu = orig_cpu;
  80. cp->elements[idx].dl = orig_dl;
  81. cp->elements[cp->elements[idx].cpu].idx = idx;
  82. }
  83. static void cpudl_heapify(struct cpudl *cp, int idx)
  84. {
  85. if (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl,
  86. cp->elements[idx].dl))
  87. cpudl_heapify_up(cp, idx);
  88. else
  89. cpudl_heapify_down(cp, idx);
  90. }
  91. static inline int cpudl_maximum(struct cpudl *cp)
  92. {
  93. return cp->elements[0].cpu;
  94. }
  95. /*
  96. * cpudl_find - find the best (later-dl) CPU in the system
  97. * @cp: the cpudl max-heap context
  98. * @p: the task
  99. * @later_mask: a mask to fill in with the selected CPUs (or NULL)
  100. *
  101. * Returns: int - CPUs were found
  102. */
  103. int cpudl_find(struct cpudl *cp, struct task_struct *p,
  104. struct cpumask *later_mask)
  105. {
  106. const struct sched_dl_entity *dl_se = &p->dl;
  107. if (later_mask &&
  108. cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  109. return 1;
  110. } else {
  111. int best_cpu = cpudl_maximum(cp);
  112. WARN_ON(best_cpu != -1 && !cpu_present(best_cpu));
  113. if (cpumask_test_cpu(best_cpu, &p->cpus_allowed) &&
  114. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  115. if (later_mask)
  116. cpumask_set_cpu(best_cpu, later_mask);
  117. return 1;
  118. }
  119. }
  120. return 0;
  121. }
  122. /*
  123. * cpudl_clear - remove a CPU from the cpudl max-heap
  124. * @cp: the cpudl max-heap context
  125. * @cpu: the target CPU
  126. *
  127. * Notes: assumes cpu_rq(cpu)->lock is locked
  128. *
  129. * Returns: (void)
  130. */
  131. void cpudl_clear(struct cpudl *cp, int cpu)
  132. {
  133. int old_idx, new_cpu;
  134. unsigned long flags;
  135. WARN_ON(!cpu_present(cpu));
  136. raw_spin_lock_irqsave(&cp->lock, flags);
  137. old_idx = cp->elements[cpu].idx;
  138. if (old_idx == IDX_INVALID) {
  139. /*
  140. * Nothing to remove if old_idx was invalid.
  141. * This could happen if a rq_offline_dl is
  142. * called for a CPU without -dl tasks running.
  143. */
  144. } else {
  145. new_cpu = cp->elements[cp->size - 1].cpu;
  146. cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl;
  147. cp->elements[old_idx].cpu = new_cpu;
  148. cp->size--;
  149. cp->elements[new_cpu].idx = old_idx;
  150. cp->elements[cpu].idx = IDX_INVALID;
  151. cpudl_heapify(cp, old_idx);
  152. cpumask_set_cpu(cpu, cp->free_cpus);
  153. }
  154. raw_spin_unlock_irqrestore(&cp->lock, flags);
  155. }
  156. /*
  157. * cpudl_set - update the cpudl max-heap
  158. * @cp: the cpudl max-heap context
  159. * @cpu: the target CPU
  160. * @dl: the new earliest deadline for this CPU
  161. *
  162. * Notes: assumes cpu_rq(cpu)->lock is locked
  163. *
  164. * Returns: (void)
  165. */
  166. void cpudl_set(struct cpudl *cp, int cpu, u64 dl)
  167. {
  168. int old_idx;
  169. unsigned long flags;
  170. WARN_ON(!cpu_present(cpu));
  171. raw_spin_lock_irqsave(&cp->lock, flags);
  172. old_idx = cp->elements[cpu].idx;
  173. if (old_idx == IDX_INVALID) {
  174. int new_idx = cp->size++;
  175. cp->elements[new_idx].dl = dl;
  176. cp->elements[new_idx].cpu = cpu;
  177. cp->elements[cpu].idx = new_idx;
  178. cpudl_heapify_up(cp, new_idx);
  179. cpumask_clear_cpu(cpu, cp->free_cpus);
  180. } else {
  181. cp->elements[old_idx].dl = dl;
  182. cpudl_heapify(cp, old_idx);
  183. }
  184. raw_spin_unlock_irqrestore(&cp->lock, flags);
  185. }
  186. /*
  187. * cpudl_set_freecpu - Set the cpudl.free_cpus
  188. * @cp: the cpudl max-heap context
  189. * @cpu: rd attached CPU
  190. */
  191. void cpudl_set_freecpu(struct cpudl *cp, int cpu)
  192. {
  193. cpumask_set_cpu(cpu, cp->free_cpus);
  194. }
  195. /*
  196. * cpudl_clear_freecpu - Clear the cpudl.free_cpus
  197. * @cp: the cpudl max-heap context
  198. * @cpu: rd attached CPU
  199. */
  200. void cpudl_clear_freecpu(struct cpudl *cp, int cpu)
  201. {
  202. cpumask_clear_cpu(cpu, cp->free_cpus);
  203. }
  204. /*
  205. * cpudl_init - initialize the cpudl structure
  206. * @cp: the cpudl max-heap context
  207. */
  208. int cpudl_init(struct cpudl *cp)
  209. {
  210. int i;
  211. raw_spin_lock_init(&cp->lock);
  212. cp->size = 0;
  213. cp->elements = kcalloc(nr_cpu_ids,
  214. sizeof(struct cpudl_item),
  215. GFP_KERNEL);
  216. if (!cp->elements)
  217. return -ENOMEM;
  218. if (!zalloc_cpumask_var(&cp->free_cpus, GFP_KERNEL)) {
  219. kfree(cp->elements);
  220. return -ENOMEM;
  221. }
  222. for_each_possible_cpu(i)
  223. cp->elements[i].idx = IDX_INVALID;
  224. return 0;
  225. }
  226. /*
  227. * cpudl_cleanup - clean up the cpudl structure
  228. * @cp: the cpudl max-heap context
  229. */
  230. void cpudl_cleanup(struct cpudl *cp)
  231. {
  232. free_cpumask_var(cp->free_cpus);
  233. kfree(cp->elements);
  234. }