PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/x86/kernel/setup_percpu.c

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