cpudeadline.c 4.9 KB

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