cpupri.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kernel/sched/cpupri.c
  4. *
  5. * CPU priority management
  6. *
  7. * Copyright (C) 2007-2008 Novell
  8. *
  9. * Author: Gregory Haskins <[email protected]>
  10. *
  11. * This code tracks the priority of each CPU so that global migration
  12. * decisions are easy to calculate. Each CPU can be in a state as follows:
  13. *
  14. * (INVALID), NORMAL, RT1, ... RT99, HIGHER
  15. *
  16. * going from the lowest priority to the highest. CPUs in the INVALID state
  17. * are not eligible for routing. The system maintains this state with
  18. * a 2 dimensional bitmap (the first for priority class, the second for CPUs
  19. * in that class). Therefore a typical application without affinity
  20. * restrictions can find a suitable CPU with O(1) complexity (e.g. two bit
  21. * searches). For tasks with affinity restrictions, the algorithm has a
  22. * worst case complexity of O(min(101, nr_domcpus)), though the scenario that
  23. * yields the worst case search is fairly contrived.
  24. */
  25. /*
  26. * p->rt_priority p->prio newpri cpupri
  27. *
  28. * -1 -1 (CPUPRI_INVALID)
  29. *
  30. * 99 0 (CPUPRI_NORMAL)
  31. *
  32. * 1 98 98 1
  33. * ...
  34. * 49 50 50 49
  35. * 50 49 49 50
  36. * ...
  37. * 99 0 0 99
  38. *
  39. * 100 100 (CPUPRI_HIGHER)
  40. */
  41. static int convert_prio(int prio)
  42. {
  43. int cpupri;
  44. switch (prio) {
  45. case CPUPRI_INVALID:
  46. cpupri = CPUPRI_INVALID; /* -1 */
  47. break;
  48. case 0 ... 98:
  49. cpupri = MAX_RT_PRIO-1 - prio; /* 1 ... 99 */
  50. break;
  51. case MAX_RT_PRIO-1:
  52. cpupri = CPUPRI_NORMAL; /* 0 */
  53. break;
  54. case MAX_RT_PRIO:
  55. cpupri = CPUPRI_HIGHER; /* 100 */
  56. break;
  57. }
  58. return cpupri;
  59. }
  60. static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p,
  61. struct cpumask *lowest_mask, int idx)
  62. {
  63. struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
  64. int skip = 0;
  65. if (!atomic_read(&(vec)->count))
  66. skip = 1;
  67. /*
  68. * When looking at the vector, we need to read the counter,
  69. * do a memory barrier, then read the mask.
  70. *
  71. * Note: This is still all racy, but we can deal with it.
  72. * Ideally, we only want to look at masks that are set.
  73. *
  74. * If a mask is not set, then the only thing wrong is that we
  75. * did a little more work than necessary.
  76. *
  77. * If we read a zero count but the mask is set, because of the
  78. * memory barriers, that can only happen when the highest prio
  79. * task for a run queue has left the run queue, in which case,
  80. * it will be followed by a pull. If the task we are processing
  81. * fails to find a proper place to go, that pull request will
  82. * pull this task if the run queue is running at a lower
  83. * priority.
  84. */
  85. smp_rmb();
  86. /* Need to do the rmb for every iteration */
  87. if (skip)
  88. return 0;
  89. if (cpumask_any_and(&p->cpus_mask, vec->mask) >= nr_cpu_ids)
  90. return 0;
  91. if (lowest_mask) {
  92. cpumask_and(lowest_mask, &p->cpus_mask, vec->mask);
  93. cpumask_and(lowest_mask, lowest_mask, cpu_active_mask);
  94. /*
  95. * We have to ensure that we have at least one bit
  96. * still set in the array, since the map could have
  97. * been concurrently emptied between the first and
  98. * second reads of vec->mask. If we hit this
  99. * condition, simply act as though we never hit this
  100. * priority level and continue on.
  101. */
  102. if (cpumask_empty(lowest_mask))
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. int cpupri_find(struct cpupri *cp, struct task_struct *p,
  108. struct cpumask *lowest_mask)
  109. {
  110. return cpupri_find_fitness(cp, p, lowest_mask, NULL);
  111. }
  112. /**
  113. * cpupri_find_fitness - find the best (lowest-pri) CPU in the system
  114. * @cp: The cpupri context
  115. * @p: The task
  116. * @lowest_mask: A mask to fill in with selected CPUs (or NULL)
  117. * @fitness_fn: A pointer to a function to do custom checks whether the CPU
  118. * fits a specific criteria so that we only return those CPUs.
  119. *
  120. * Note: This function returns the recommended CPUs as calculated during the
  121. * current invocation. By the time the call returns, the CPUs may have in
  122. * fact changed priorities any number of times. While not ideal, it is not
  123. * an issue of correctness since the normal rebalancer logic will correct
  124. * any discrepancies created by racing against the uncertainty of the current
  125. * priority configuration.
  126. *
  127. * Return: (int)bool - CPUs were found
  128. */
  129. int cpupri_find_fitness(struct cpupri *cp, struct task_struct *p,
  130. struct cpumask *lowest_mask,
  131. bool (*fitness_fn)(struct task_struct *p, int cpu))
  132. {
  133. int task_pri = convert_prio(p->prio);
  134. int idx, cpu;
  135. WARN_ON_ONCE(task_pri >= CPUPRI_NR_PRIORITIES);
  136. for (idx = 0; idx < task_pri; idx++) {
  137. if (!__cpupri_find(cp, p, lowest_mask, idx))
  138. continue;
  139. if (!lowest_mask || !fitness_fn)
  140. return 1;
  141. /* Ensure the capacity of the CPUs fit the task */
  142. for_each_cpu(cpu, lowest_mask) {
  143. if (!fitness_fn(p, cpu))
  144. cpumask_clear_cpu(cpu, lowest_mask);
  145. }
  146. /*
  147. * If no CPU at the current priority can fit the task
  148. * continue looking
  149. */
  150. if (cpumask_empty(lowest_mask))
  151. continue;
  152. return 1;
  153. }
  154. /*
  155. * If we failed to find a fitting lowest_mask, kick off a new search
  156. * but without taking into account any fitness criteria this time.
  157. *
  158. * This rule favours honouring priority over fitting the task in the
  159. * correct CPU (Capacity Awareness being the only user now).
  160. * The idea is that if a higher priority task can run, then it should
  161. * run even if this ends up being on unfitting CPU.
  162. *
  163. * The cost of this trade-off is not entirely clear and will probably
  164. * be good for some workloads and bad for others.
  165. *
  166. * The main idea here is that if some CPUs were over-committed, we try
  167. * to spread which is what the scheduler traditionally did. Sys admins
  168. * must do proper RT planning to avoid overloading the system if they
  169. * really care.
  170. */
  171. if (fitness_fn)
  172. return cpupri_find(cp, p, lowest_mask);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(cpupri_find_fitness);
  176. /**
  177. * cpupri_set - update the CPU priority setting
  178. * @cp: The cpupri context
  179. * @cpu: The target CPU
  180. * @newpri: The priority (INVALID,NORMAL,RT1-RT99,HIGHER) to assign to this CPU
  181. *
  182. * Note: Assumes cpu_rq(cpu)->lock is locked
  183. *
  184. * Returns: (void)
  185. */
  186. void cpupri_set(struct cpupri *cp, int cpu, int newpri)
  187. {
  188. int *currpri = &cp->cpu_to_pri[cpu];
  189. int oldpri = *currpri;
  190. int do_mb = 0;
  191. newpri = convert_prio(newpri);
  192. BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
  193. if (newpri == oldpri)
  194. return;
  195. /*
  196. * If the CPU was currently mapped to a different value, we
  197. * need to map it to the new value then remove the old value.
  198. * Note, we must add the new value first, otherwise we risk the
  199. * cpu being missed by the priority loop in cpupri_find.
  200. */
  201. if (likely(newpri != CPUPRI_INVALID)) {
  202. struct cpupri_vec *vec = &cp->pri_to_cpu[newpri];
  203. cpumask_set_cpu(cpu, vec->mask);
  204. /*
  205. * When adding a new vector, we update the mask first,
  206. * do a write memory barrier, and then update the count, to
  207. * make sure the vector is visible when count is set.
  208. */
  209. smp_mb__before_atomic();
  210. atomic_inc(&(vec)->count);
  211. do_mb = 1;
  212. }
  213. if (likely(oldpri != CPUPRI_INVALID)) {
  214. struct cpupri_vec *vec = &cp->pri_to_cpu[oldpri];
  215. /*
  216. * Because the order of modification of the vec->count
  217. * is important, we must make sure that the update
  218. * of the new prio is seen before we decrement the
  219. * old prio. This makes sure that the loop sees
  220. * one or the other when we raise the priority of
  221. * the run queue. We don't care about when we lower the
  222. * priority, as that will trigger an rt pull anyway.
  223. *
  224. * We only need to do a memory barrier if we updated
  225. * the new priority vec.
  226. */
  227. if (do_mb)
  228. smp_mb__after_atomic();
  229. /*
  230. * When removing from the vector, we decrement the counter first
  231. * do a memory barrier and then clear the mask.
  232. */
  233. atomic_dec(&(vec)->count);
  234. smp_mb__after_atomic();
  235. cpumask_clear_cpu(cpu, vec->mask);
  236. }
  237. *currpri = newpri;
  238. }
  239. /**
  240. * cpupri_init - initialize the cpupri structure
  241. * @cp: The cpupri context
  242. *
  243. * Return: -ENOMEM on memory allocation failure.
  244. */
  245. int cpupri_init(struct cpupri *cp)
  246. {
  247. int i;
  248. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++) {
  249. struct cpupri_vec *vec = &cp->pri_to_cpu[i];
  250. atomic_set(&vec->count, 0);
  251. if (!zalloc_cpumask_var(&vec->mask, GFP_KERNEL))
  252. goto cleanup;
  253. }
  254. cp->cpu_to_pri = kcalloc(nr_cpu_ids, sizeof(int), GFP_KERNEL);
  255. if (!cp->cpu_to_pri)
  256. goto cleanup;
  257. for_each_possible_cpu(i)
  258. cp->cpu_to_pri[i] = CPUPRI_INVALID;
  259. return 0;
  260. cleanup:
  261. for (i--; i >= 0; i--)
  262. free_cpumask_var(cp->pri_to_cpu[i].mask);
  263. return -ENOMEM;
  264. }
  265. /**
  266. * cpupri_cleanup - clean up the cpupri structure
  267. * @cp: The cpupri context
  268. */
  269. void cpupri_cleanup(struct cpupri *cp)
  270. {
  271. int i;
  272. kfree(cp->cpu_to_pri);
  273. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++)
  274. free_cpumask_var(cp->pri_to_cpu[i].mask);
  275. }