setup_percpu.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <linux/kernel.h>
  4. #include <linux/export.h>
  5. #include <linux/init.h>
  6. #include <linux/memblock.h>
  7. #include <linux/percpu.h>
  8. #include <linux/kexec.h>
  9. #include <linux/crash_dump.h>
  10. #include <linux/smp.h>
  11. #include <linux/topology.h>
  12. #include <linux/pfn.h>
  13. #include <linux/stackprotector.h>
  14. #include <asm/sections.h>
  15. #include <asm/processor.h>
  16. #include <asm/desc.h>
  17. #include <asm/setup.h>
  18. #include <asm/mpspec.h>
  19. #include <asm/apicdef.h>
  20. #include <asm/highmem.h>
  21. #include <asm/proto.h>
  22. #include <asm/cpumask.h>
  23. #include <asm/cpu.h>
  24. #ifdef CONFIG_X86_64
  25. #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
  26. #else
  27. #define BOOT_PERCPU_OFFSET 0
  28. #endif
  29. DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
  30. EXPORT_PER_CPU_SYMBOL(this_cpu_off);
  31. unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init = {
  32. [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
  33. };
  34. EXPORT_SYMBOL(__per_cpu_offset);
  35. /*
  36. * On x86_64 symbols referenced from code should be reachable using
  37. * 32bit relocations. Reserve space for static percpu variables in
  38. * modules so that they are always served from the first chunk which
  39. * is located at the percpu segment base. On x86_32, anything can
  40. * address anywhere. No need to reserve space in the first chunk.
  41. */
  42. #ifdef CONFIG_X86_64
  43. #define PERCPU_FIRST_CHUNK_RESERVE PERCPU_MODULE_RESERVE
  44. #else
  45. #define PERCPU_FIRST_CHUNK_RESERVE 0
  46. #endif
  47. #ifdef CONFIG_X86_32
  48. /**
  49. * pcpu_need_numa - determine percpu allocation needs to consider NUMA
  50. *
  51. * If NUMA is not configured or there is only one NUMA node available,
  52. * there is no reason to consider NUMA. This function determines
  53. * whether percpu allocation should consider NUMA or not.
  54. *
  55. * RETURNS:
  56. * true if NUMA should be considered; otherwise, false.
  57. */
  58. static bool __init pcpu_need_numa(void)
  59. {
  60. #ifdef CONFIG_NUMA
  61. pg_data_t *last = NULL;
  62. unsigned int cpu;
  63. for_each_possible_cpu(cpu) {
  64. int node = early_cpu_to_node(cpu);
  65. if (node_online(node) && NODE_DATA(node) &&
  66. last && last != NODE_DATA(node))
  67. return true;
  68. last = NODE_DATA(node);
  69. }
  70. #endif
  71. return false;
  72. }
  73. #endif
  74. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  75. {
  76. #ifdef CONFIG_NUMA
  77. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  78. return LOCAL_DISTANCE;
  79. else
  80. return REMOTE_DISTANCE;
  81. #else
  82. return LOCAL_DISTANCE;
  83. #endif
  84. }
  85. static int __init pcpu_cpu_to_node(int cpu)
  86. {
  87. return early_cpu_to_node(cpu);
  88. }
  89. void __init pcpu_populate_pte(unsigned long addr)
  90. {
  91. populate_extra_pte(addr);
  92. }
  93. static inline void setup_percpu_segment(int cpu)
  94. {
  95. #ifdef CONFIG_X86_32
  96. struct desc_struct d = GDT_ENTRY_INIT(DESC_DATA32,
  97. per_cpu_offset(cpu), 0xFFFFF);
  98. write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
  99. #endif
  100. }
  101. void __init setup_per_cpu_areas(void)
  102. {
  103. unsigned int cpu;
  104. unsigned long delta;
  105. int rc;
  106. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n",
  107. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  108. /*
  109. * Allocate percpu area. Embedding allocator is our favorite;
  110. * however, on NUMA configurations, it can result in very
  111. * sparse unit mapping and vmalloc area isn't spacious enough
  112. * on 32bit. Use page in that case.
  113. */
  114. #ifdef CONFIG_X86_32
  115. if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
  116. pcpu_chosen_fc = PCPU_FC_PAGE;
  117. #endif
  118. rc = -EINVAL;
  119. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  120. const size_t dyn_size = PERCPU_MODULE_RESERVE +
  121. PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
  122. size_t atom_size;
  123. /*
  124. * On 64bit, use PMD_SIZE for atom_size so that embedded
  125. * percpu areas are aligned to PMD. This, in the future,
  126. * can also allow using PMD mappings in vmalloc area. Use
  127. * PAGE_SIZE on 32bit as vmalloc space is highly contended
  128. * and large vmalloc area allocs can easily fail.
  129. */
  130. #ifdef CONFIG_X86_64
  131. atom_size = PMD_SIZE;
  132. #else
  133. atom_size = PAGE_SIZE;
  134. #endif
  135. rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
  136. dyn_size, atom_size,
  137. pcpu_cpu_distance,
  138. pcpu_cpu_to_node);
  139. if (rc < 0)
  140. pr_warn("%s allocator failed (%d), falling back to page size\n",
  141. pcpu_fc_names[pcpu_chosen_fc], rc);
  142. }
  143. if (rc < 0)
  144. rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
  145. pcpu_cpu_to_node);
  146. if (rc < 0)
  147. panic("cannot initialize percpu area (err=%d)", rc);
  148. /* alrighty, percpu areas up and running */
  149. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  150. for_each_possible_cpu(cpu) {
  151. per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
  152. per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
  153. per_cpu(pcpu_hot.cpu_number, cpu) = cpu;
  154. setup_percpu_segment(cpu);
  155. /*
  156. * Copy data used in early init routines from the
  157. * initial arrays to the per cpu data areas. These
  158. * arrays then become expendable and the *_early_ptr's
  159. * are zeroed indicating that the static arrays are
  160. * gone.
  161. */
  162. #ifdef CONFIG_X86_LOCAL_APIC
  163. per_cpu(x86_cpu_to_apicid, cpu) =
  164. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  165. per_cpu(x86_cpu_to_acpiid, cpu) =
  166. early_per_cpu_map(x86_cpu_to_acpiid, cpu);
  167. #endif
  168. #ifdef CONFIG_NUMA
  169. per_cpu(x86_cpu_to_node_map, cpu) =
  170. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  171. /*
  172. * Ensure that the boot cpu numa_node is correct when the boot
  173. * cpu is on a node that doesn't have memory installed.
  174. * Also cpu_up() will call cpu_to_node() for APs when
  175. * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set
  176. * up later with c_init aka intel_init/amd_init.
  177. * So set them all (boot cpu and all APs).
  178. */
  179. set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
  180. #endif
  181. /*
  182. * Up to this point, the boot CPU has been using .init.data
  183. * area. Reload any changed state for the boot CPU.
  184. */
  185. if (!cpu)
  186. switch_gdt_and_percpu_base(cpu);
  187. }
  188. /* indicate the early static arrays will soon be gone */
  189. #ifdef CONFIG_X86_LOCAL_APIC
  190. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  191. early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL;
  192. #endif
  193. #ifdef CONFIG_NUMA
  194. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  195. #endif
  196. /* Setup node to cpumask map */
  197. setup_node_to_cpumask_map();
  198. /* Setup cpu initialized, callin, callout masks */
  199. setup_cpu_local_masks();
  200. /*
  201. * Sync back kernel address range again. We already did this in
  202. * setup_arch(), but percpu data also needs to be available in
  203. * the smpboot asm and arch_sync_kernel_mappings() doesn't sync to
  204. * swapper_pg_dir on 32-bit. The per-cpu mappings need to be available
  205. * there too.
  206. *
  207. * FIXME: Can the later sync in setup_cpu_entry_areas() replace
  208. * this call?
  209. */
  210. sync_initial_page_table();
  211. }