cpudeadline.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 inline int dl_time_before(u64 a, u64 b)
  30. {
  31. return (s64)(a - b) < 0;
  32. }
  33. static void cpudl_exchange(struct cpudl *cp, int a, int b)
  34. {
  35. int cpu_a = cp->elements[a].cpu, cpu_b = cp->elements[b].cpu;
  36. swap(cp->elements[a].cpu, cp->elements[b].cpu);
  37. swap(cp->elements[a].dl , cp->elements[b].dl );
  38. swap(cp->elements[cpu_a].idx, cp->elements[cpu_b].idx);
  39. }
  40. static void cpudl_heapify(struct cpudl *cp, int idx)
  41. {
  42. int l, r, largest;
  43. /* adapted from lib/prio_heap.c */
  44. while(1) {
  45. l = left_child(idx);
  46. r = right_child(idx);
  47. largest = idx;
  48. if ((l < cp->size) && dl_time_before(cp->elements[idx].dl,
  49. cp->elements[l].dl))
  50. largest = l;
  51. if ((r < cp->size) && dl_time_before(cp->elements[largest].dl,
  52. cp->elements[r].dl))
  53. largest = r;
  54. if (largest == idx)
  55. break;
  56. /* Push idx down the heap one level and bump one up */
  57. cpudl_exchange(cp, largest, idx);
  58. idx = largest;
  59. }
  60. }
  61. static void cpudl_change_key(struct cpudl *cp, int idx, u64 new_dl)
  62. {
  63. WARN_ON(idx == IDX_INVALID || !cpu_present(idx));
  64. if (dl_time_before(new_dl, cp->elements[idx].dl)) {
  65. cp->elements[idx].dl = new_dl;
  66. cpudl_heapify(cp, idx);
  67. } else {
  68. cp->elements[idx].dl = new_dl;
  69. while (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl,
  70. cp->elements[idx].dl)) {
  71. cpudl_exchange(cp, idx, parent(idx));
  72. idx = parent(idx);
  73. }
  74. }
  75. }
  76. static inline int cpudl_maximum(struct cpudl *cp)
  77. {
  78. return cp->elements[0].cpu;
  79. }
  80. /*
  81. * cpudl_find - find the best (later-dl) CPU in the system
  82. * @cp: the cpudl max-heap context
  83. * @p: the task
  84. * @later_mask: a mask to fill in with the selected CPUs (or NULL)
  85. *
  86. * Returns: int - best CPU (heap maximum if suitable)
  87. */
  88. int cpudl_find(struct cpudl *cp, struct task_struct *p,
  89. struct cpumask *later_mask)
  90. {
  91. int best_cpu = -1;
  92. const struct sched_dl_entity *dl_se = &p->dl;
  93. if (later_mask &&
  94. cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  95. best_cpu = cpumask_any(later_mask);
  96. goto out;
  97. } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  98. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  99. best_cpu = cpudl_maximum(cp);
  100. if (later_mask)
  101. cpumask_set_cpu(best_cpu, later_mask);
  102. }
  103. out:
  104. WARN_ON(best_cpu != -1 && !cpu_present(best_cpu));
  105. return best_cpu;
  106. }
  107. /*
  108. * cpudl_set - update the cpudl max-heap
  109. * @cp: the cpudl max-heap context
  110. * @cpu: the target cpu
  111. * @dl: the new earliest deadline for this cpu
  112. *
  113. * Notes: assumes cpu_rq(cpu)->lock is locked
  114. *
  115. * Returns: (void)
  116. */
  117. void cpudl_set(struct cpudl *cp, int cpu, u64 dl, int is_valid)
  118. {
  119. int old_idx, new_cpu;
  120. unsigned long flags;
  121. WARN_ON(!cpu_present(cpu));
  122. raw_spin_lock_irqsave(&cp->lock, flags);
  123. old_idx = cp->elements[cpu].idx;
  124. if (!is_valid) {
  125. /* remove item */
  126. if (old_idx == IDX_INVALID) {
  127. /*
  128. * Nothing to remove if old_idx was invalid.
  129. * This could happen if a rq_offline_dl is
  130. * called for a CPU without -dl tasks running.
  131. */
  132. goto out;
  133. }
  134. new_cpu = cp->elements[cp->size - 1].cpu;
  135. cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl;
  136. cp->elements[old_idx].cpu = new_cpu;
  137. cp->size--;
  138. cp->elements[new_cpu].idx = old_idx;
  139. cp->elements[cpu].idx = IDX_INVALID;
  140. while (old_idx > 0 && dl_time_before(
  141. cp->elements[parent(old_idx)].dl,
  142. cp->elements[old_idx].dl)) {
  143. cpudl_exchange(cp, old_idx, parent(old_idx));
  144. old_idx = parent(old_idx);
  145. }
  146. cpumask_set_cpu(cpu, cp->free_cpus);
  147. cpudl_heapify(cp, old_idx);
  148. goto out;
  149. }
  150. if (old_idx == IDX_INVALID) {
  151. cp->size++;
  152. cp->elements[cp->size - 1].dl = 0;
  153. cp->elements[cp->size - 1].cpu = cpu;
  154. cp->elements[cpu].idx = cp->size - 1;
  155. cpudl_change_key(cp, cp->size - 1, dl);
  156. cpumask_clear_cpu(cpu, cp->free_cpus);
  157. } else {
  158. cpudl_change_key(cp, old_idx, dl);
  159. }
  160. out:
  161. raw_spin_unlock_irqrestore(&cp->lock, flags);
  162. }
  163. /*
  164. * cpudl_set_freecpu - Set the cpudl.free_cpus
  165. * @cp: the cpudl max-heap context
  166. * @cpu: rd attached cpu
  167. */
  168. void cpudl_set_freecpu(struct cpudl *cp, int cpu)
  169. {
  170. cpumask_set_cpu(cpu, cp->free_cpus);
  171. }
  172. /*
  173. * cpudl_clear_freecpu - Clear the cpudl.free_cpus
  174. * @cp: the cpudl max-heap context
  175. * @cpu: rd attached cpu
  176. */
  177. void cpudl_clear_freecpu(struct cpudl *cp, int cpu)
  178. {
  179. cpumask_clear_cpu(cpu, cp->free_cpus);
  180. }
  181. /*
  182. * cpudl_init - initialize the cpudl structure
  183. * @cp: the cpudl max-heap context
  184. */
  185. int cpudl_init(struct cpudl *cp)
  186. {
  187. int i;
  188. memset(cp, 0, sizeof(*cp));
  189. raw_spin_lock_init(&cp->lock);
  190. cp->size = 0;
  191. cp->elements = kcalloc(nr_cpu_ids,
  192. sizeof(struct cpudl_item),
  193. GFP_KERNEL);
  194. if (!cp->elements)
  195. return -ENOMEM;
  196. if (!zalloc_cpumask_var(&cp->free_cpus, GFP_KERNEL)) {
  197. kfree(cp->elements);
  198. return -ENOMEM;
  199. }
  200. for_each_possible_cpu(i)
  201. cp->elements[i].idx = IDX_INVALID;
  202. return 0;
  203. }
  204. /*
  205. * cpudl_cleanup - clean up the cpudl structure
  206. * @cp: the cpudl max-heap context
  207. */
  208. void cpudl_cleanup(struct cpudl *cp)
  209. {
  210. free_cpumask_var(cp->free_cpus);
  211. kfree(cp->elements);
  212. }