cpupri.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <ghaskins@novell.com>
  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), IDLE, NORMAL, RT1, ... RT99
  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(102, nr_domcpus)), though the scenario that
  23. * yields the worst case search is fairly contrived.
  24. */
  25. #include "sched.h"
  26. /* Convert between a 140 based task->prio, and our 102 based cpupri */
  27. static int convert_prio(int prio)
  28. {
  29. int cpupri;
  30. if (prio == CPUPRI_INVALID)
  31. cpupri = CPUPRI_INVALID;
  32. else if (prio == MAX_PRIO)
  33. cpupri = CPUPRI_IDLE;
  34. else if (prio >= MAX_RT_PRIO)
  35. cpupri = CPUPRI_NORMAL;
  36. else
  37. cpupri = MAX_RT_PRIO - prio + 1;
  38. return cpupri;
  39. }
  40. #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
  41. /**
  42. * drop_nopreempt_cpus - remove likely nonpreemptible cpus from the mask
  43. * @lowest_mask: mask with selected CPUs (non-NULL)
  44. */
  45. static void
  46. drop_nopreempt_cpus(struct cpumask *lowest_mask)
  47. {
  48. unsigned int cpu = cpumask_first(lowest_mask);
  49. while (cpu < nr_cpu_ids) {
  50. /* unlocked access */
  51. struct task_struct *task = READ_ONCE(cpu_rq(cpu)->curr);
  52. if (task_may_not_preempt(task, cpu)) {
  53. cpumask_clear_cpu(cpu, lowest_mask);
  54. }
  55. cpu = cpumask_next(cpu, lowest_mask);
  56. }
  57. }
  58. #endif
  59. static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p,
  60. struct cpumask *lowest_mask, int idx,
  61. bool drop_nopreempts)
  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 racey, 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_ptr, vec->mask) >= nr_cpu_ids)
  90. return 0;
  91. if (lowest_mask) {
  92. cpumask_and(lowest_mask, p->cpus_ptr, vec->mask);
  93. cpumask_and(lowest_mask, lowest_mask, cpu_active_mask);
  94. #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
  95. if (drop_nopreempts)
  96. drop_nopreempt_cpus(lowest_mask);
  97. #endif
  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_empty(lowest_mask))
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. int cpupri_find(struct cpupri *cp, struct task_struct *p,
  112. struct cpumask *lowest_mask)
  113. {
  114. return cpupri_find_fitness(cp, p, lowest_mask, NULL);
  115. }
  116. /**
  117. * cpupri_find_fitness - find the best (lowest-pri) CPU in the system
  118. * @cp: The cpupri context
  119. * @p: The task
  120. * @lowest_mask: A mask to fill in with selected CPUs (or NULL)
  121. * @fitness_fn: A pointer to a function to do custom checks whether the CPU
  122. * fits a specific criteria so that we only return those CPUs.
  123. *
  124. * Note: This function returns the recommended CPUs as calculated during the
  125. * current invocation. By the time the call returns, the CPUs may have in
  126. * fact changed priorities any number of times. While not ideal, it is not
  127. * an issue of correctness since the normal rebalancer logic will correct
  128. * any discrepancies created by racing against the uncertainty of the current
  129. * priority configuration.
  130. *
  131. * Return: (int)bool - CPUs were found
  132. */
  133. int cpupri_find_fitness(struct cpupri *cp, struct task_struct *p,
  134. struct cpumask *lowest_mask,
  135. bool (*fitness_fn)(struct task_struct *p, int cpu))
  136. {
  137. int task_pri = convert_prio(p->prio);
  138. int idx, cpu;
  139. bool drop_nopreempts = task_pri <= MAX_RT_PRIO;
  140. BUG_ON(task_pri >= CPUPRI_NR_PRIORITIES);
  141. #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
  142. retry:
  143. #endif
  144. for (idx = 0; idx < task_pri; idx++) {
  145. if (!__cpupri_find(cp, p, lowest_mask, idx, drop_nopreempts))
  146. continue;
  147. if (!lowest_mask || !fitness_fn)
  148. return 1;
  149. /* Ensure the capacity of the CPUs fit the task */
  150. for_each_cpu(cpu, lowest_mask) {
  151. if (!fitness_fn(p, cpu))
  152. cpumask_clear_cpu(cpu, lowest_mask);
  153. }
  154. /*
  155. * If no CPU at the current priority can fit the task
  156. * continue looking
  157. */
  158. if (cpumask_empty(lowest_mask))
  159. continue;
  160. return 1;
  161. }
  162. /*
  163. * If we can't find any non-preemptible cpu's, retry so we can
  164. * find the lowest priority target and avoid priority inversion.
  165. */
  166. #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
  167. if (drop_nopreempts) {
  168. drop_nopreempts = false;
  169. goto retry;
  170. }
  171. #endif
  172. /*
  173. * If we failed to find a fitting lowest_mask, kick off a new search
  174. * but without taking into account any fitness criteria this time.
  175. *
  176. * This rule favours honouring priority over fitting the task in the
  177. * correct CPU (Capacity Awareness being the only user now).
  178. * The idea is that if a higher priority task can run, then it should
  179. * run even if this ends up being on unfitting CPU.
  180. *
  181. * The cost of this trade-off is not entirely clear and will probably
  182. * be good for some workloads and bad for others.
  183. *
  184. * The main idea here is that if some CPUs were overcommitted, we try
  185. * to spread which is what the scheduler traditionally did. Sys admins
  186. * must do proper RT planning to avoid overloading the system if they
  187. * really care.
  188. */
  189. if (fitness_fn)
  190. return cpupri_find(cp, p, lowest_mask);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(cpupri_find_fitness);
  194. /**
  195. * cpupri_set - update the CPU priority setting
  196. * @cp: The cpupri context
  197. * @cpu: The target CPU
  198. * @newpri: The priority (INVALID-RT99) to assign to this CPU
  199. *
  200. * Note: Assumes cpu_rq(cpu)->lock is locked
  201. *
  202. * Returns: (void)
  203. */
  204. void cpupri_set(struct cpupri *cp, int cpu, int newpri)
  205. {
  206. int *currpri = &cp->cpu_to_pri[cpu];
  207. int oldpri = *currpri;
  208. int do_mb = 0;
  209. newpri = convert_prio(newpri);
  210. BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
  211. if (newpri == oldpri)
  212. return;
  213. /*
  214. * If the CPU was currently mapped to a different value, we
  215. * need to map it to the new value then remove the old value.
  216. * Note, we must add the new value first, otherwise we risk the
  217. * cpu being missed by the priority loop in cpupri_find.
  218. */
  219. if (likely(newpri != CPUPRI_INVALID)) {
  220. struct cpupri_vec *vec = &cp->pri_to_cpu[newpri];
  221. cpumask_set_cpu(cpu, vec->mask);
  222. /*
  223. * When adding a new vector, we update the mask first,
  224. * do a write memory barrier, and then update the count, to
  225. * make sure the vector is visible when count is set.
  226. */
  227. smp_mb__before_atomic();
  228. atomic_inc(&(vec)->count);
  229. do_mb = 1;
  230. }
  231. if (likely(oldpri != CPUPRI_INVALID)) {
  232. struct cpupri_vec *vec = &cp->pri_to_cpu[oldpri];
  233. /*
  234. * Because the order of modification of the vec->count
  235. * is important, we must make sure that the update
  236. * of the new prio is seen before we decrement the
  237. * old prio. This makes sure that the loop sees
  238. * one or the other when we raise the priority of
  239. * the run queue. We don't care about when we lower the
  240. * priority, as that will trigger an rt pull anyway.
  241. *
  242. * We only need to do a memory barrier if we updated
  243. * the new priority vec.
  244. */
  245. if (do_mb)
  246. smp_mb__after_atomic();
  247. /*
  248. * When removing from the vector, we decrement the counter first
  249. * do a memory barrier and then clear the mask.
  250. */
  251. atomic_dec(&(vec)->count);
  252. smp_mb__after_atomic();
  253. cpumask_clear_cpu(cpu, vec->mask);
  254. }
  255. *currpri = newpri;
  256. }
  257. /**
  258. * cpupri_init - initialize the cpupri structure
  259. * @cp: The cpupri context
  260. *
  261. * Return: -ENOMEM on memory allocation failure.
  262. */
  263. int cpupri_init(struct cpupri *cp)
  264. {
  265. int i;
  266. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++) {
  267. struct cpupri_vec *vec = &cp->pri_to_cpu[i];
  268. atomic_set(&vec->count, 0);
  269. if (!zalloc_cpumask_var(&vec->mask, GFP_KERNEL))
  270. goto cleanup;
  271. }
  272. cp->cpu_to_pri = kcalloc(nr_cpu_ids, sizeof(int), GFP_KERNEL);
  273. if (!cp->cpu_to_pri)
  274. goto cleanup;
  275. for_each_possible_cpu(i)
  276. cp->cpu_to_pri[i] = CPUPRI_INVALID;
  277. return 0;
  278. cleanup:
  279. for (i--; i >= 0; i--)
  280. free_cpumask_var(cp->pri_to_cpu[i].mask);
  281. return -ENOMEM;
  282. }
  283. /**
  284. * cpupri_cleanup - clean up the cpupri structure
  285. * @cp: The cpupri context
  286. */
  287. void cpupri_cleanup(struct cpupri *cp)
  288. {
  289. int i;
  290. kfree(cp->cpu_to_pri);
  291. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++)
  292. free_cpumask_var(cp->pri_to_cpu[i].mask);
  293. }
  294. #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
  295. /*
  296. * cpupri_check_rt - check if CPU has a RT task
  297. * should be called from rcu-sched read section.
  298. */
  299. bool cpupri_check_rt(void)
  300. {
  301. int cpu = raw_smp_processor_id();
  302. return cpu_rq(cpu)->rd->cpupri.cpu_to_pri[cpu] > CPUPRI_NORMAL;
  303. }
  304. #endif