setup_percpu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/bootmem.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_NEED_MULTIPLE_NODES
  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. /**
  77. * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu
  78. * @cpu: cpu to allocate for
  79. * @size: size allocation in bytes
  80. * @align: alignment
  81. *
  82. * Allocate @size bytes aligned at @align for cpu @cpu. This wrapper
  83. * does the right thing for NUMA regardless of the current
  84. * configuration.
  85. *
  86. * RETURNS:
  87. * Pointer to the allocated area on success, NULL on failure.
  88. */
  89. static void * __init pcpu_alloc_bootmem(unsigned int cpu, unsigned long size,
  90. unsigned long align)
  91. {
  92. const unsigned long goal = __pa(MAX_DMA_ADDRESS);
  93. #ifdef CONFIG_NEED_MULTIPLE_NODES
  94. int node = early_cpu_to_node(cpu);
  95. void *ptr;
  96. if (!node_online(node) || !NODE_DATA(node)) {
  97. ptr = __alloc_bootmem_nopanic(size, align, goal);
  98. pr_info("cpu %d has no node %d or node-local memory\n",
  99. cpu, node);
  100. pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n",
  101. cpu, size, __pa(ptr));
  102. } else {
  103. ptr = __alloc_bootmem_node_nopanic(NODE_DATA(node),
  104. size, align, goal);
  105. pr_debug("per cpu data for cpu%d %lu bytes on node%d at %016lx\n",
  106. cpu, size, node, __pa(ptr));
  107. }
  108. return ptr;
  109. #else
  110. return __alloc_bootmem_nopanic(size, align, goal);
  111. #endif
  112. }
  113. /*
  114. * Helpers for first chunk memory allocation
  115. */
  116. static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
  117. {
  118. return pcpu_alloc_bootmem(cpu, size, align);
  119. }
  120. static void __init pcpu_fc_free(void *ptr, size_t size)
  121. {
  122. free_bootmem(__pa(ptr), size);
  123. }
  124. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  125. {
  126. #ifdef CONFIG_NEED_MULTIPLE_NODES
  127. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  128. return LOCAL_DISTANCE;
  129. else
  130. return REMOTE_DISTANCE;
  131. #else
  132. return LOCAL_DISTANCE;
  133. #endif
  134. }
  135. static void __init pcpup_populate_pte(unsigned long addr)
  136. {
  137. populate_extra_pte(addr);
  138. }
  139. static inline void setup_percpu_segment(int cpu)
  140. {
  141. #ifdef CONFIG_X86_32
  142. struct desc_struct d = GDT_ENTRY_INIT(0x8092, per_cpu_offset(cpu),
  143. 0xFFFFF);
  144. write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
  145. #endif
  146. }
  147. void __init setup_per_cpu_areas(void)
  148. {
  149. unsigned int cpu;
  150. unsigned long delta;
  151. int rc;
  152. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%d\n",
  153. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  154. /*
  155. * Allocate percpu area. Embedding allocator is our favorite;
  156. * however, on NUMA configurations, it can result in very
  157. * sparse unit mapping and vmalloc area isn't spacious enough
  158. * on 32bit. Use page in that case.
  159. */
  160. #ifdef CONFIG_X86_32
  161. if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
  162. pcpu_chosen_fc = PCPU_FC_PAGE;
  163. #endif
  164. rc = -EINVAL;
  165. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  166. const size_t dyn_size = PERCPU_MODULE_RESERVE +
  167. PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
  168. size_t atom_size;
  169. /*
  170. * On 64bit, use PMD_SIZE for atom_size so that embedded
  171. * percpu areas are aligned to PMD. This, in the future,
  172. * can also allow using PMD mappings in vmalloc area. Use
  173. * PAGE_SIZE on 32bit as vmalloc space is highly contended
  174. * and large vmalloc area allocs can easily fail.
  175. */
  176. #ifdef CONFIG_X86_64
  177. atom_size = PMD_SIZE;
  178. #else
  179. atom_size = PAGE_SIZE;
  180. #endif
  181. rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
  182. dyn_size, atom_size,
  183. pcpu_cpu_distance,
  184. pcpu_fc_alloc, pcpu_fc_free);
  185. if (rc < 0)
  186. pr_warning("%s allocator failed (%d), falling back to page size\n",
  187. pcpu_fc_names[pcpu_chosen_fc], rc);
  188. }
  189. if (rc < 0)
  190. rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
  191. pcpu_fc_alloc, pcpu_fc_free,
  192. pcpup_populate_pte);
  193. if (rc < 0)
  194. panic("cannot initialize percpu area (err=%d)", rc);
  195. /* alrighty, percpu areas up and running */
  196. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  197. for_each_possible_cpu(cpu) {
  198. per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
  199. per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
  200. per_cpu(cpu_number, cpu) = cpu;
  201. setup_percpu_segment(cpu);
  202. setup_stack_canary_segment(cpu);
  203. /*
  204. * Copy data used in early init routines from the
  205. * initial arrays to the per cpu data areas. These
  206. * arrays then become expendable and the *_early_ptr's
  207. * are zeroed indicating that the static arrays are
  208. * gone.
  209. */
  210. #ifdef CONFIG_X86_LOCAL_APIC
  211. per_cpu(x86_cpu_to_apicid, cpu) =
  212. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  213. per_cpu(x86_bios_cpu_apicid, cpu) =
  214. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  215. per_cpu(x86_cpu_to_acpiid, cpu) =
  216. early_per_cpu_map(x86_cpu_to_acpiid, cpu);
  217. #endif
  218. #ifdef CONFIG_X86_32
  219. per_cpu(x86_cpu_to_logical_apicid, cpu) =
  220. early_per_cpu_map(x86_cpu_to_logical_apicid, cpu);
  221. #endif
  222. #ifdef CONFIG_X86_64
  223. per_cpu(irq_stack_ptr, cpu) =
  224. per_cpu(irq_stack_union.irq_stack, cpu) +
  225. IRQ_STACK_SIZE;
  226. #endif
  227. #ifdef CONFIG_NUMA
  228. per_cpu(x86_cpu_to_node_map, cpu) =
  229. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  230. /*
  231. * Ensure that the boot cpu numa_node is correct when the boot
  232. * cpu is on a node that doesn't have memory installed.
  233. * Also cpu_up() will call cpu_to_node() for APs when
  234. * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set
  235. * up later with c_init aka intel_init/amd_init.
  236. * So set them all (boot cpu and all APs).
  237. */
  238. set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
  239. #endif
  240. /*
  241. * Up to this point, the boot CPU has been using .init.data
  242. * area. Reload any changed state for the boot CPU.
  243. */
  244. if (!cpu)
  245. switch_to_new_gdt(cpu);
  246. }
  247. /* indicate the early static arrays will soon be gone */
  248. #ifdef CONFIG_X86_LOCAL_APIC
  249. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  250. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  251. early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL;
  252. #endif
  253. #ifdef CONFIG_X86_32
  254. early_per_cpu_ptr(x86_cpu_to_logical_apicid) = NULL;
  255. #endif
  256. #ifdef CONFIG_NUMA
  257. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  258. #endif
  259. /* Setup node to cpumask map */
  260. setup_node_to_cpumask_map();
  261. /* Setup cpu initialized, callin, callout masks */
  262. setup_cpu_local_masks();
  263. /*
  264. * Sync back kernel address range again. We already did this in
  265. * setup_arch(), but percpu data also needs to be available in
  266. * the smpboot asm. We can't reliably pick up percpu mappings
  267. * using vmalloc_fault(), because exception dispatch needs
  268. * percpu data.
  269. *
  270. * FIXME: Can the later sync in setup_cpu_entry_areas() replace
  271. * this call?
  272. */
  273. sync_initial_page_table();
  274. }