cpudeadline.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 && cpumask_and(later_mask, cp->free_cpus,
  94. &p->cpus_allowed) && cpumask_and(later_mask,
  95. later_mask, cpu_active_mask)) {
  96. best_cpu = cpumask_any(later_mask);
  97. goto out;
  98. } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  99. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  100. best_cpu = cpudl_maximum(cp);
  101. if (later_mask)
  102. cpumask_set_cpu(best_cpu, later_mask);
  103. }
  104. out:
  105. WARN_ON(best_cpu != -1 && !cpu_present(best_cpu));
  106. return best_cpu;
  107. }
  108. /*
  109. * cpudl_set - update the cpudl max-heap
  110. * @cp: the cpudl max-heap context
  111. * @cpu: the target cpu
  112. * @dl: the new earliest deadline for this cpu
  113. *
  114. * Notes: assumes cpu_rq(cpu)->lock is locked
  115. *
  116. * Returns: (void)
  117. */
  118. void cpudl_set(struct cpudl *cp, int cpu, u64 dl, int is_valid)
  119. {
  120. int old_idx, new_cpu;
  121. unsigned long flags;
  122. WARN_ON(!cpu_present(cpu));
  123. raw_spin_lock_irqsave(&cp->lock, flags);
  124. old_idx = cp->elements[cpu].idx;
  125. if (!is_valid) {
  126. /* remove item */
  127. if (old_idx == IDX_INVALID) {
  128. /*
  129. * Nothing to remove if old_idx was invalid.
  130. * This could happen if a rq_offline_dl is
  131. * called for a CPU without -dl tasks running.
  132. */
  133. goto out;
  134. }
  135. new_cpu = cp->elements[cp->size - 1].cpu;
  136. cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl;
  137. cp->elements[old_idx].cpu = new_cpu;
  138. cp->size--;
  139. cp->elements[new_cpu].idx = old_idx;
  140. cp->elements[cpu].idx = IDX_INVALID;
  141. while (old_idx > 0 && dl_time_before(
  142. cp->elements[parent(old_idx)].dl,
  143. cp->elements[old_idx].dl)) {
  144. cpudl_exchange(cp, old_idx, parent(old_idx));
  145. old_idx = parent(old_idx);
  146. }
  147. cpumask_set_cpu(cpu, cp->free_cpus);
  148. cpudl_heapify(cp, old_idx);
  149. goto out;
  150. }
  151. if (old_idx == IDX_INVALID) {
  152. cp->size++;
  153. cp->elements[cp->size - 1].dl = 0;
  154. cp->elements[cp->size - 1].cpu = cpu;
  155. cp->elements[cpu].idx = cp->size - 1;
  156. cpudl_change_key(cp, cp->size - 1, dl);
  157. cpumask_clear_cpu(cpu, cp->free_cpus);
  158. } else {
  159. cpudl_change_key(cp, old_idx, dl);
  160. }
  161. out:
  162. raw_spin_unlock_irqrestore(&cp->lock, flags);
  163. }
  164. /*
  165. * cpudl_init - initialize the cpudl structure
  166. * @cp: the cpudl max-heap context
  167. */
  168. int cpudl_init(struct cpudl *cp)
  169. {
  170. int i;
  171. memset(cp, 0, sizeof(*cp));
  172. raw_spin_lock_init(&cp->lock);
  173. cp->size = 0;
  174. cp->elements = kcalloc(nr_cpu_ids,
  175. sizeof(struct cpudl_item),
  176. GFP_KERNEL);
  177. if (!cp->elements)
  178. return -ENOMEM;
  179. if (!alloc_cpumask_var(&cp->free_cpus, GFP_KERNEL)) {
  180. kfree(cp->elements);
  181. return -ENOMEM;
  182. }
  183. for_each_possible_cpu(i)
  184. cp->elements[i].idx = IDX_INVALID;
  185. cpumask_setall(cp->free_cpus);
  186. return 0;
  187. }
  188. /*
  189. * cpudl_cleanup - clean up the cpudl structure
  190. * @cp: the cpudl max-heap context
  191. */
  192. void cpudl_cleanup(struct cpudl *cp)
  193. {
  194. free_cpumask_var(cp->free_cpus);
  195. kfree(cp->elements);
  196. }