sched_cpupri.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "sched_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. #define for_each_cpupri_active(array, idx) \
  46. for_each_set_bit(idx, array, CPUPRI_NR_PRIORITIES)
  47. /**
  48. * cpupri_find - find the best (lowest-pri) CPU in the system
  49. * @cp: The cpupri context
  50. * @p: The task
  51. * @lowest_mask: A mask to fill in with selected CPUs (or NULL)
  52. *
  53. * Note: This function returns the recommended CPUs as calculated during the
  54. * current invocation. By the time the call returns, the CPUs may have in
  55. * fact changed priorities any number of times. While not ideal, it is not
  56. * an issue of correctness since the normal rebalancer logic will correct
  57. * any discrepancies created by racing against the uncertainty of the current
  58. * priority configuration.
  59. *
  60. * Returns: (int)bool - CPUs were found
  61. */
  62. int cpupri_find(struct cpupri *cp, struct task_struct *p,
  63. struct cpumask *lowest_mask)
  64. {
  65. int idx = 0;
  66. int task_pri = convert_prio(p->prio);
  67. for_each_cpupri_active(cp->pri_active, idx) {
  68. struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
  69. if (idx >= task_pri)
  70. break;
  71. if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  72. continue;
  73. if (lowest_mask) {
  74. cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  75. /*
  76. * We have to ensure that we have at least one bit
  77. * still set in the array, since the map could have
  78. * been concurrently emptied between the first and
  79. * second reads of vec->mask. If we hit this
  80. * condition, simply act as though we never hit this
  81. * priority level and continue on.
  82. */
  83. if (cpumask_any(lowest_mask) >= nr_cpu_ids)
  84. continue;
  85. }
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * cpupri_set - update the cpu priority setting
  92. * @cp: The cpupri context
  93. * @cpu: The target cpu
  94. * @pri: The priority (INVALID-RT99) to assign to this CPU
  95. *
  96. * Note: Assumes cpu_rq(cpu)->lock is locked
  97. *
  98. * Returns: (void)
  99. */
  100. void cpupri_set(struct cpupri *cp, int cpu, int newpri)
  101. {
  102. int *currpri = &cp->cpu_to_pri[cpu];
  103. int oldpri = *currpri;
  104. unsigned long flags;
  105. newpri = convert_prio(newpri);
  106. BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
  107. if (newpri == oldpri)
  108. return;
  109. /*
  110. * If the cpu was currently mapped to a different value, we
  111. * need to map it to the new value then remove the old value.
  112. * Note, we must add the new value first, otherwise we risk the
  113. * cpu being cleared from pri_active, and this cpu could be
  114. * missed for a push or pull.
  115. */
  116. if (likely(newpri != CPUPRI_INVALID)) {
  117. struct cpupri_vec *vec = &cp->pri_to_cpu[newpri];
  118. raw_spin_lock_irqsave(&vec->lock, flags);
  119. cpumask_set_cpu(cpu, vec->mask);
  120. vec->count++;
  121. if (vec->count == 1)
  122. set_bit(newpri, cp->pri_active);
  123. raw_spin_unlock_irqrestore(&vec->lock, flags);
  124. }
  125. if (likely(oldpri != CPUPRI_INVALID)) {
  126. struct cpupri_vec *vec = &cp->pri_to_cpu[oldpri];
  127. raw_spin_lock_irqsave(&vec->lock, flags);
  128. vec->count--;
  129. if (!vec->count)
  130. clear_bit(oldpri, cp->pri_active);
  131. cpumask_clear_cpu(cpu, vec->mask);
  132. raw_spin_unlock_irqrestore(&vec->lock, flags);
  133. }
  134. *currpri = newpri;
  135. }
  136. /**
  137. * cpupri_init - initialize the cpupri structure
  138. * @cp: The cpupri context
  139. * @bootmem: true if allocations need to use bootmem
  140. *
  141. * Returns: -ENOMEM if memory fails.
  142. */
  143. int cpupri_init(struct cpupri *cp)
  144. {
  145. int i;
  146. memset(cp, 0, sizeof(*cp));
  147. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++) {
  148. struct cpupri_vec *vec = &cp->pri_to_cpu[i];
  149. raw_spin_lock_init(&vec->lock);
  150. vec->count = 0;
  151. if (!zalloc_cpumask_var(&vec->mask, GFP_KERNEL))
  152. goto cleanup;
  153. }
  154. for_each_possible_cpu(i)
  155. cp->cpu_to_pri[i] = CPUPRI_INVALID;
  156. return 0;
  157. cleanup:
  158. for (i--; i >= 0; i--)
  159. free_cpumask_var(cp->pri_to_cpu[i].mask);
  160. return -ENOMEM;
  161. }
  162. /**
  163. * cpupri_cleanup - clean up the cpupri structure
  164. * @cp: The cpupri context
  165. */
  166. void cpupri_cleanup(struct cpupri *cp)
  167. {
  168. int i;
  169. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++)
  170. free_cpumask_var(cp->pri_to_cpu[i].mask);
  171. }