setup_percpu.c 6.9 KB

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