sched_cpupri.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "sched_cpupri.h"
  30. /* Convert between a 140 based task->prio, and our 102 based cpupri */
  31. static int convert_prio(int prio)
  32. {
  33. int cpupri;
  34. if (prio == CPUPRI_INVALID)
  35. cpupri = CPUPRI_INVALID;
  36. else if (prio == MAX_PRIO)
  37. cpupri = CPUPRI_IDLE;
  38. else if (prio >= MAX_RT_PRIO)
  39. cpupri = CPUPRI_NORMAL;
  40. else
  41. cpupri = MAX_RT_PRIO - prio + 1;
  42. return cpupri;
  43. }
  44. #define for_each_cpupri_active(array, idx) \
  45. for (idx = find_first_bit(array, CPUPRI_NR_PRIORITIES); \
  46. idx < CPUPRI_NR_PRIORITIES; \
  47. idx = find_next_bit(array, CPUPRI_NR_PRIORITIES, idx+1))
  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 invokation. 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. * Returns: (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. for_each_cpupri_active(cp->pri_active, idx) {
  69. struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
  70. if (idx >= task_pri)
  71. break;
  72. if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  73. continue;
  74. if (lowest_mask) {
  75. cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  76. /*
  77. * We have to ensure that we have at least one bit
  78. * still set in the array, since the map could have
  79. * been concurrently emptied between the first and
  80. * second reads of vec->mask. If we hit this
  81. * condition, simply act as though we never hit this
  82. * priority level and continue on.
  83. */
  84. if (cpumask_any(lowest_mask) >= nr_cpu_ids)
  85. continue;
  86. }
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. /**
  92. * cpupri_set - update the cpu priority setting
  93. * @cp: The cpupri context
  94. * @cpu: The target cpu
  95. * @pri: The priority (INVALID-RT99) to assign to this CPU
  96. *
  97. * Note: Assumes cpu_rq(cpu)->lock is locked
  98. *
  99. * Returns: (void)
  100. */
  101. void cpupri_set(struct cpupri *cp, int cpu, int newpri)
  102. {
  103. int *currpri = &cp->cpu_to_pri[cpu];
  104. int oldpri = *currpri;
  105. unsigned long flags;
  106. newpri = convert_prio(newpri);
  107. BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
  108. if (newpri == oldpri)
  109. return;
  110. /*
  111. * If the cpu was currently mapped to a different value, we
  112. * first need to unmap the old value
  113. */
  114. if (likely(oldpri != CPUPRI_INVALID)) {
  115. struct cpupri_vec *vec = &cp->pri_to_cpu[oldpri];
  116. spin_lock_irqsave(&vec->lock, flags);
  117. vec->count--;
  118. if (!vec->count)
  119. clear_bit(oldpri, cp->pri_active);
  120. cpumask_clear_cpu(cpu, vec->mask);
  121. spin_unlock_irqrestore(&vec->lock, flags);
  122. }
  123. if (likely(newpri != CPUPRI_INVALID)) {
  124. struct cpupri_vec *vec = &cp->pri_to_cpu[newpri];
  125. spin_lock_irqsave(&vec->lock, flags);
  126. cpumask_set_cpu(cpu, vec->mask);
  127. vec->count++;
  128. if (vec->count == 1)
  129. set_bit(newpri, cp->pri_active);
  130. spin_unlock_irqrestore(&vec->lock, flags);
  131. }
  132. *currpri = newpri;
  133. }
  134. /**
  135. * cpupri_init - initialize the cpupri structure
  136. * @cp: The cpupri context
  137. * @bootmem: true if allocations need to use bootmem
  138. *
  139. * Returns: -ENOMEM if memory fails.
  140. */
  141. int cpupri_init(struct cpupri *cp, bool bootmem)
  142. {
  143. gfp_t gfp = GFP_KERNEL;
  144. int i;
  145. if (bootmem)
  146. gfp = GFP_NOWAIT;
  147. memset(cp, 0, sizeof(*cp));
  148. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++) {
  149. struct cpupri_vec *vec = &cp->pri_to_cpu[i];
  150. spin_lock_init(&vec->lock);
  151. vec->count = 0;
  152. if (!zalloc_cpumask_var(&vec->mask, gfp))
  153. goto cleanup;
  154. }
  155. for_each_possible_cpu(i)
  156. cp->cpu_to_pri[i] = CPUPRI_INVALID;
  157. return 0;
  158. cleanup:
  159. for (i--; i >= 0; i--)
  160. free_cpumask_var(cp->pri_to_cpu[i].mask);
  161. return -ENOMEM;
  162. }
  163. /**
  164. * cpupri_cleanup - clean up the cpupri structure
  165. * @cp: The cpupri context
  166. */
  167. void cpupri_cleanup(struct cpupri *cp)
  168. {
  169. int i;
  170. for (i = 0; i < CPUPRI_NR_PRIORITIES; i++)
  171. free_cpumask_var(cp->pri_to_cpu[i].mask);
  172. }