cpupri.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "cpupri.h"
  31. /* Convert between a 140 based task->prio, and our 102 based cpupri */
  32. static int convert_prio(int prio)
  33. {
  34. int cpupri;
  35. if (prio == CPUPRI_INVALID)
  36. cpupri = CPUPRI_INVALID;
  37. else if (prio == MAX_PRIO)
  38. cpupri = CPUPRI_IDLE;
  39. else if (prio >= MAX_RT_PRIO)
  40. cpupri = CPUPRI_NORMAL;
  41. else
  42. cpupri = MAX_RT_PRIO - prio + 1;
  43. return cpupri;
  44. }
  45. /**
  46. * cpupri_find - find the best (lowest-pri) CPU in the system
  47. * @cp: The cpupri context
  48. * @p: The task
  49. * @lowest_mask: A mask to fill in with selected CPUs (or NULL)
  50. *
  51. * Note: This function returns the recommended CPUs as calculated during the
  52. * current invocation. By the time the call returns, the CPUs may have in
  53. * fact changed priorities any number of times. While not ideal, it is not
  54. * an issue of correctness since the normal rebalancer logic will correct
  55. * any discrepancies created by racing against the uncertainty of the current
  56. * priority configuration.
  57. *
  58. * Returns: (int)bool - CPUs were found
  59. */
  60. int cpupri_find(struct cpupri *cp, struct task_struct *p,
  61. struct cpumask *lowest_mask)
  62. {
  63. int idx = 0;
  64. int task_pri = convert_prio(p->prio);
  65. if (task_pri >= MAX_RT_PRIO)
  66. return 0;
  67. for (idx = 0; idx < task_pri; idx++) {
  68. struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
  69. int skip = 0;
  70. if (!atomic_read(&(vec)->count))
  71. skip = 1;
  72. /*
  73. * When looking at the vector, we need to read the counter,
  74. * do a memory barrier, then read the mask.
  75. *
  76. * Note: This is still all racey, but we can deal with it.
  77. * Ideally, we only want to look at masks that are set.
  78. *
  79. * If a mask is not set, then the only thing wrong is that we
  80. * did a little more work than necessary.
  81. *
  82. * If we read a zero count but the mask is set, because of the
  83. * memory barriers, that can only happen when the highest prio
  84. * task for a run queue has left the run queue, in which case,
  85. * it will be followed by a pull. If the task we are processing
  86. * fails to find a proper place to go, that pull request will
  87. * pull this task if the run queue is running at a lower
  88. * priority.
  89. */
  90. smp_rmb();
  91. /* Need to do the rmb for every iteration */
  92. if (skip)
  93. continue;
  94. if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  95. continue;
  96. if (lowest_mask) {
  97. cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  98. /*
  99. * We have to ensure that we have at least one bit
  100. * still set in the array, since the map could have
  101. * been concurrently emptied between the first and
  102. * second reads of vec->mask. If we hit this
  103. * condition, simply act as though we never hit this
  104. * priority level and continue on.
  105. */
  106. if (cpumask_any(lowest_mask) >= nr_cpu_ids)
  107. continue;
  108. }
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * cpupri_set - update the cpu priority setting
  115. * @cp: The cpupri context
  116. * @cpu: The target cpu
  117. * @newpri: The priority (INVALID-RT99) to assign to this CPU
  118. *
  119. * Note: Assumes cpu_rq(cpu)->lock is locked
  120. *
  121. * Returns: (void)
  122. */
  123. void cpupri_set(struct cpupri *cp, int cpu, int newpri)
  124. {
  125. int *currpri = &cp->cpu_to_pri[cpu];
  126. int oldpri = *currpri;
  127. int do_mb = 0;
  128. newpri = convert_prio(newpri);
  129. BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
  130. if (newpri == oldpri)
  131. return;
  132. /*
  133. * If the cpu was currently mapped to a different value, we
  134. * need to map it to the new value then remove the old value.
  135. * Note, we must add the new value first, otherwise we risk the
  136. * cpu being missed by the priority loop in cpupri_find.
  137. */
  138. if (likely(newpri != CPUPRI_INVALID)) {
  139. struct cpupri_vec *vec = &cp->pri_to_cpu[newpri];
  140. cpumask_set_cpu(cpu, vec->mask);
  141. /*
  142. * When adding a new vector, we update the mask first,
  143. * do a write memory barrier, and then update the count, to
  144. * make sure the vector is visible when count is set.
  145. */
  146. smp_mb__before_atomic_inc();
  147. atomic_inc(&(vec)->count);
  148. do_mb = 1;
  149. }
  150. if (likely(oldpri != CPUPRI_INVALID)) {
  151. struct cpupri_vec *vec = &cp->pri_to_cpu[oldpri];
  152. /*
  153. * Because the order of modification of the vec->count
  154. * is important, we must make sure that the update
  155. * of the new prio is seen before we decrement the
  156. * old prio. This makes sure that the loop sees
  157. * one or the other when we raise the priority of
  158. * the run queue. We don't care about when we lower the
  159. * priority, as that will trigger an rt pull anyway.
  160. *
  161. * We only need to do a memory barrier if we updated
  162. * the new priority vec.
  163. */
  164. if (do_mb)
  165. smp_mb__after_atomic_inc();
  166. /*
  167. * When removing from the vector, we decrement the counter first
  168. * do a memory barrier and then clear the mask.
  169. */
  170. atomic_dec(&(vec)->count);
  171. smp_mb__after_atomic_inc();
  172. cpumask_clear_cpu(cpu, vec->mask);
  173. }
  174. *currpri = newpri;
  175. }
  176. /**
  177. * cpupri_init - initialize the cpupri structure
  178. * @cp: The cpupri context
  179. *
  180. * Returns: -ENOMEM if memory fails.
  181. */
  182. int cpupri_init(struct cpupri *cp)
  183. {
  184. int i;
  185. memset(cp, 0, sizeof(*cp));
  186. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++) {
  187. struct cpupri_vec *vec = &cp->pri_to_cpu[i];
  188. atomic_set(&vec->count, 0);
  189. if (!zalloc_cpumask_var(&vec->mask, GFP_KERNEL))
  190. goto cleanup;
  191. }
  192. for_each_possible_cpu(i)
  193. cp->cpu_to_pri[i] = CPUPRI_INVALID;
  194. return 0;
  195. cleanup:
  196. for (i--; i >= 0; i--)
  197. free_cpumask_var(cp->pri_to_cpu[i].mask);
  198. return -ENOMEM;
  199. }
  200. /**
  201. * cpupri_cleanup - clean up the cpupri structure
  202. * @cp: The cpupri context
  203. */
  204. void cpupri_cleanup(struct cpupri *cp)
  205. {
  206. int i;
  207. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++)
  208. free_cpumask_var(cp->pri_to_cpu[i].mask);
  209. }