cpupri.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * kernel/sched/cpupri.c
  3. *
  4. * CPU priority management
  5. *
  6. * Copyright (C) 2007-2008 Novell
  7. *
  8. * Author: Gregory Haskins <ghaskins@novell.com>
  9. *
  10. * This code tracks the priority of each CPU so that global migration
  11. * decisions are easy to calculate. Each CPU can be in a state as follows:
  12. *
  13. * (INVALID), IDLE, NORMAL, RT1, ... RT99
  14. *
  15. * going from the lowest priority to the highest. CPUs in the INVALID state
  16. * are not eligible for routing. The system maintains this state with
  17. * a 2 dimensional bitmap (the first for priority class, the second for cpus
  18. * in that class). Therefore a typical application without affinity
  19. * restrictions can find a suitable CPU with O(1) complexity (e.g. two bit
  20. * searches). For tasks with affinity restrictions, the algorithm has a
  21. * worst case complexity of O(min(102, nr_domcpus)), though the scenario that
  22. * yields the worst case search is fairly contrived.
  23. *
  24. * This program is free software; you can redistribute it and/or
  25. * modify it under the terms of the GNU General Public License
  26. * as published by the Free Software Foundation; version 2
  27. * of the License.
  28. */
  29. #include <linux/gfp.h>
  30. #include <linux/sched.h>
  31. #include <linux/sched/rt.h>
  32. #include <linux/slab.h>
  33. #include "cpupri.h"
  34. /* Convert between a 140 based task->prio, and our 102 based cpupri */
  35. static int convert_prio(int prio)
  36. {
  37. int cpupri;
  38. if (prio == CPUPRI_INVALID)
  39. cpupri = CPUPRI_INVALID;
  40. else if (prio == MAX_PRIO)
  41. cpupri = CPUPRI_IDLE;
  42. else if (prio >= MAX_RT_PRIO)
  43. cpupri = CPUPRI_NORMAL;
  44. else
  45. cpupri = MAX_RT_PRIO - prio + 1;
  46. return cpupri;
  47. }
  48. /**
  49. * cpupri_find - find the best (lowest-pri) CPU in the system
  50. * @cp: The cpupri context
  51. * @p: The task
  52. * @lowest_mask: A mask to fill in with selected CPUs (or NULL)
  53. *
  54. * Note: This function returns the recommended CPUs as calculated during the
  55. * current invocation. By the time the call returns, the CPUs may have in
  56. * fact changed priorities any number of times. While not ideal, it is not
  57. * an issue of correctness since the normal rebalancer logic will correct
  58. * any discrepancies created by racing against the uncertainty of the current
  59. * priority configuration.
  60. *
  61. * Return: (int)bool - CPUs were found
  62. */
  63. int cpupri_find(struct cpupri *cp, struct task_struct *p,
  64. struct cpumask *lowest_mask)
  65. {
  66. int idx = 0;
  67. int task_pri = convert_prio(p->prio);
  68. BUG_ON(task_pri >= CPUPRI_NR_PRIORITIES);
  69. for (idx = 0; idx < task_pri; idx++) {
  70. struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
  71. int skip = 0;
  72. if (!atomic_read(&(vec)->count))
  73. skip = 1;
  74. /*
  75. * When looking at the vector, we need to read the counter,
  76. * do a memory barrier, then read the mask.
  77. *
  78. * Note: This is still all racey, but we can deal with it.
  79. * Ideally, we only want to look at masks that are set.
  80. *
  81. * If a mask is not set, then the only thing wrong is that we
  82. * did a little more work than necessary.
  83. *
  84. * If we read a zero count but the mask is set, because of the
  85. * memory barriers, that can only happen when the highest prio
  86. * task for a run queue has left the run queue, in which case,
  87. * it will be followed by a pull. If the task we are processing
  88. * fails to find a proper place to go, that pull request will
  89. * pull this task if the run queue is running at a lower
  90. * priority.
  91. */
  92. smp_rmb();
  93. /* Need to do the rmb for every iteration */
  94. if (skip)
  95. continue;
  96. if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  97. continue;
  98. if (lowest_mask) {
  99. cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  100. /*
  101. * We have to ensure that we have at least one bit
  102. * still set in the array, since the map could have
  103. * been concurrently emptied between the first and
  104. * second reads of vec->mask. If we hit this
  105. * condition, simply act as though we never hit this
  106. * priority level and continue on.
  107. */
  108. if (cpumask_any(lowest_mask) >= nr_cpu_ids)
  109. continue;
  110. }
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. /**
  116. * cpupri_set - update the cpu priority setting
  117. * @cp: The cpupri context
  118. * @cpu: The target cpu
  119. * @newpri: The priority (INVALID-RT99) to assign to this CPU
  120. *
  121. * Note: Assumes cpu_rq(cpu)->lock is locked
  122. *
  123. * Returns: (void)
  124. */
  125. void cpupri_set(struct cpupri *cp, int cpu, int newpri)
  126. {
  127. int *currpri = &cp->cpu_to_pri[cpu];
  128. int oldpri = *currpri;
  129. int do_mb = 0;
  130. newpri = convert_prio(newpri);
  131. BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
  132. if (newpri == oldpri)
  133. return;
  134. /*
  135. * If the cpu was currently mapped to a different value, we
  136. * need to map it to the new value then remove the old value.
  137. * Note, we must add the new value first, otherwise we risk the
  138. * cpu being missed by the priority loop in cpupri_find.
  139. */
  140. if (likely(newpri != CPUPRI_INVALID)) {
  141. struct cpupri_vec *vec = &cp->pri_to_cpu[newpri];
  142. cpumask_set_cpu(cpu, vec->mask);
  143. /*
  144. * When adding a new vector, we update the mask first,
  145. * do a write memory barrier, and then update the count, to
  146. * make sure the vector is visible when count is set.
  147. */
  148. smp_mb__before_atomic();
  149. atomic_inc(&(vec)->count);
  150. do_mb = 1;
  151. }
  152. if (likely(oldpri != CPUPRI_INVALID)) {
  153. struct cpupri_vec *vec = &cp->pri_to_cpu[oldpri];
  154. /*
  155. * Because the order of modification of the vec->count
  156. * is important, we must make sure that the update
  157. * of the new prio is seen before we decrement the
  158. * old prio. This makes sure that the loop sees
  159. * one or the other when we raise the priority of
  160. * the run queue. We don't care about when we lower the
  161. * priority, as that will trigger an rt pull anyway.
  162. *
  163. * We only need to do a memory barrier if we updated
  164. * the new priority vec.
  165. */
  166. if (do_mb)
  167. smp_mb__after_atomic();
  168. /*
  169. * When removing from the vector, we decrement the counter first
  170. * do a memory barrier and then clear the mask.
  171. */
  172. atomic_dec(&(vec)->count);
  173. smp_mb__after_atomic();
  174. cpumask_clear_cpu(cpu, vec->mask);
  175. }
  176. *currpri = newpri;
  177. }
  178. /**
  179. * cpupri_init - initialize the cpupri structure
  180. * @cp: The cpupri context
  181. *
  182. * Return: -ENOMEM on memory allocation failure.
  183. */
  184. int cpupri_init(struct cpupri *cp)
  185. {
  186. int i;
  187. memset(cp, 0, sizeof(*cp));
  188. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++) {
  189. struct cpupri_vec *vec = &cp->pri_to_cpu[i];
  190. atomic_set(&vec->count, 0);
  191. if (!zalloc_cpumask_var(&vec->mask, GFP_KERNEL))
  192. goto cleanup;
  193. }
  194. cp->cpu_to_pri = kcalloc(nr_cpu_ids, sizeof(int), GFP_KERNEL);
  195. if (!cp->cpu_to_pri)
  196. goto cleanup;
  197. for_each_possible_cpu(i)
  198. cp->cpu_to_pri[i] = CPUPRI_INVALID;
  199. return 0;
  200. cleanup:
  201. for (i--; i >= 0; i--)
  202. free_cpumask_var(cp->pri_to_cpu[i].mask);
  203. return -ENOMEM;
  204. }
  205. /**
  206. * cpupri_cleanup - clean up the cpupri structure
  207. * @cp: The cpupri context
  208. */
  209. void cpupri_cleanup(struct cpupri *cp)
  210. {
  211. int i;
  212. kfree(cp->cpu_to_pri);
  213. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++)
  214. free_cpumask_var(cp->pri_to_cpu[i].mask);
  215. }