numa.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Common code for 32 and 64-bit NUMA */
  2. #include <linux/topology.h>
  3. #include <linux/module.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/random.h>
  6. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  7. # define DBG(x...) printk(KERN_DEBUG x)
  8. #else
  9. # define DBG(x...)
  10. #endif
  11. /*
  12. * Which logical CPUs are on which nodes
  13. */
  14. cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
  15. EXPORT_SYMBOL(node_to_cpumask_map);
  16. /*
  17. * Allocate node_to_cpumask_map based on number of available nodes
  18. * Requires node_possible_map to be valid.
  19. *
  20. * Note: node_to_cpumask() is not valid until after this is done.
  21. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  22. */
  23. void __init setup_node_to_cpumask_map(void)
  24. {
  25. unsigned int node, num = 0;
  26. /* setup nr_node_ids if not done yet */
  27. if (nr_node_ids == MAX_NUMNODES) {
  28. for_each_node_mask(node, node_possible_map)
  29. num = node;
  30. nr_node_ids = num + 1;
  31. }
  32. /* allocate the map */
  33. for (node = 0; node < nr_node_ids; node++)
  34. alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
  35. /* cpumask_of_node() will now work */
  36. pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
  37. }
  38. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  39. /*
  40. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  41. */
  42. const struct cpumask *cpumask_of_node(int node)
  43. {
  44. if (node >= nr_node_ids) {
  45. printk(KERN_WARNING
  46. "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
  47. node, nr_node_ids);
  48. dump_stack();
  49. return cpu_none_mask;
  50. }
  51. if (node_to_cpumask_map[node] == NULL) {
  52. printk(KERN_WARNING
  53. "cpumask_of_node(%d): no node_to_cpumask_map!\n",
  54. node);
  55. dump_stack();
  56. return cpu_online_mask;
  57. }
  58. return node_to_cpumask_map[node];
  59. }
  60. EXPORT_SYMBOL(cpumask_of_node);
  61. #endif
  62. /*
  63. * Return the bit number of a random bit set in the nodemask.
  64. * (returns -1 if nodemask is empty)
  65. */
  66. int __node_random(const nodemask_t *maskp)
  67. {
  68. int w, bit = -1;
  69. w = nodes_weight(*maskp);
  70. if (w)
  71. bit = bitmap_ord_to_pos(maskp->bits,
  72. get_random_int() % w, MAX_NUMNODES);
  73. return bit;
  74. }
  75. EXPORT_SYMBOL(__node_random);