cpudeadline.c 6.1 KB

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