setup_percpu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/percpu.h>
  6. #include <linux/kexec.h>
  7. #include <linux/crash_dump.h>
  8. #include <linux/smp.h>
  9. #include <linux/topology.h>
  10. #include <linux/pfn.h>
  11. #include <asm/sections.h>
  12. #include <asm/processor.h>
  13. #include <asm/setup.h>
  14. #include <asm/mpspec.h>
  15. #include <asm/apicdef.h>
  16. #include <asm/highmem.h>
  17. #include <asm/proto.h>
  18. #include <asm/cpumask.h>
  19. #include <asm/cpu.h>
  20. #include <asm/stackprotector.h>
  21. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  22. # define DBG(x...) printk(KERN_DEBUG x)
  23. #else
  24. # define DBG(x...)
  25. #endif
  26. DEFINE_PER_CPU(int, cpu_number);
  27. EXPORT_PER_CPU_SYMBOL(cpu_number);
  28. #ifdef CONFIG_X86_64
  29. #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
  30. #else
  31. #define BOOT_PERCPU_OFFSET 0
  32. #endif
  33. DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
  34. EXPORT_PER_CPU_SYMBOL(this_cpu_off);
  35. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
  36. [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
  37. };
  38. EXPORT_SYMBOL(__per_cpu_offset);
  39. /*
  40. * On x86_64 symbols referenced from code should be reachable using
  41. * 32bit relocations. Reserve space for static percpu variables in
  42. * modules so that they are always served from the first chunk which
  43. * is located at the percpu segment base. On x86_32, anything can
  44. * address anywhere. No need to reserve space in the first chunk.
  45. */
  46. #ifdef CONFIG_X86_64
  47. #define PERCPU_FIRST_CHUNK_RESERVE PERCPU_MODULE_RESERVE
  48. #else
  49. #define PERCPU_FIRST_CHUNK_RESERVE 0
  50. #endif
  51. /**
  52. * pcpu_need_numa - determine percpu allocation needs to consider NUMA
  53. *
  54. * If NUMA is not configured or there is only one NUMA node available,
  55. * there is no reason to consider NUMA. This function determines
  56. * whether percpu allocation should consider NUMA or not.
  57. *
  58. * RETURNS:
  59. * true if NUMA should be considered; otherwise, false.
  60. */
  61. static bool __init pcpu_need_numa(void)
  62. {
  63. #ifdef CONFIG_NEED_MULTIPLE_NODES
  64. pg_data_t *last = NULL;
  65. unsigned int cpu;
  66. for_each_possible_cpu(cpu) {
  67. int node = early_cpu_to_node(cpu);
  68. if (node_online(node) && NODE_DATA(node) &&
  69. last && last != NODE_DATA(node))
  70. return true;
  71. last = NODE_DATA(node);
  72. }
  73. #endif
  74. return false;
  75. }
  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 "
  106. "%016lx\n", 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. /*
  125. * Large page remapping allocator
  126. */
  127. #ifdef CONFIG_NEED_MULTIPLE_NODES
  128. static void __init pcpul_map(void *ptr, size_t size, void *addr)
  129. {
  130. pmd_t *pmd, pmd_v;
  131. pmd = populate_extra_pmd((unsigned long)addr);
  132. pmd_v = pfn_pmd(page_to_pfn(virt_to_page(ptr)), PAGE_KERNEL_LARGE);
  133. set_pmd(pmd, pmd_v);
  134. }
  135. static int pcpu_lpage_cpu_distance(unsigned int from, unsigned int to)
  136. {
  137. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  138. return LOCAL_DISTANCE;
  139. else
  140. return REMOTE_DISTANCE;
  141. }
  142. static ssize_t __init setup_pcpu_lpage(bool chosen)
  143. {
  144. size_t reserve = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  145. size_t dyn_size = reserve - PERCPU_FIRST_CHUNK_RESERVE;
  146. struct pcpu_alloc_info *ai;
  147. ssize_t ret;
  148. /* on non-NUMA, embedding is better */
  149. if (!chosen && !pcpu_need_numa())
  150. return -EINVAL;
  151. /* need PSE */
  152. if (!cpu_has_pse) {
  153. pr_warning("PERCPU: lpage allocator requires PSE\n");
  154. return -EINVAL;
  155. }
  156. /* allocate and build unit_map */
  157. ai = pcpu_build_alloc_info(PERCPU_FIRST_CHUNK_RESERVE, dyn_size,
  158. PMD_SIZE, pcpu_lpage_cpu_distance);
  159. if (IS_ERR(ai)) {
  160. pr_warning("PERCPU: failed to build unit_map (%ld)\n",
  161. PTR_ERR(ai));
  162. return PTR_ERR(ai);
  163. }
  164. /* do the parameters look okay? */
  165. if (!chosen) {
  166. size_t vm_size = VMALLOC_END - VMALLOC_START;
  167. size_t tot_size = 0;
  168. int group;
  169. for (group = 0; group < ai->nr_groups; group++)
  170. tot_size += ai->unit_size * ai->groups[group].nr_units;
  171. /* don't consume more than 20% of vmalloc area */
  172. if (tot_size > vm_size / 5) {
  173. pr_info("PERCPU: too large chunk size %zuMB for "
  174. "large page remap\n", tot_size >> 20);
  175. ret = -EINVAL;
  176. goto out_free;
  177. }
  178. }
  179. ret = pcpu_lpage_first_chunk(ai, pcpu_fc_alloc, pcpu_fc_free,
  180. pcpul_map);
  181. out_free:
  182. pcpu_free_alloc_info(ai);
  183. return ret;
  184. }
  185. #else
  186. static ssize_t __init setup_pcpu_lpage(bool chosen)
  187. {
  188. return -EINVAL;
  189. }
  190. #endif
  191. /*
  192. * Embedding allocator
  193. *
  194. * The first chunk is sized to just contain the static area plus
  195. * module and dynamic reserves and embedded into linear physical
  196. * mapping so that it can use PMD mapping without additional TLB
  197. * pressure.
  198. */
  199. static ssize_t __init setup_pcpu_embed(bool chosen)
  200. {
  201. size_t reserve = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  202. /*
  203. * If large page isn't supported, there's no benefit in doing
  204. * this. Also, embedding allocation doesn't play well with
  205. * NUMA.
  206. */
  207. if (!chosen && (!cpu_has_pse || pcpu_need_numa()))
  208. return -EINVAL;
  209. return pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
  210. reserve - PERCPU_FIRST_CHUNK_RESERVE);
  211. }
  212. /*
  213. * Page allocator
  214. *
  215. * Boring fallback 4k page allocator. This allocator puts more
  216. * pressure on PTE TLBs but other than that behaves nicely on both UMA
  217. * and NUMA.
  218. */
  219. static void __init pcpup_populate_pte(unsigned long addr)
  220. {
  221. populate_extra_pte(addr);
  222. }
  223. static ssize_t __init setup_pcpu_page(void)
  224. {
  225. return pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
  226. pcpu_fc_alloc, pcpu_fc_free,
  227. pcpup_populate_pte);
  228. }
  229. static inline void setup_percpu_segment(int cpu)
  230. {
  231. #ifdef CONFIG_X86_32
  232. struct desc_struct gdt;
  233. pack_descriptor(&gdt, per_cpu_offset(cpu), 0xFFFFF,
  234. 0x2 | DESCTYPE_S, 0x8);
  235. gdt.s = 1;
  236. write_gdt_entry(get_cpu_gdt_table(cpu),
  237. GDT_ENTRY_PERCPU, &gdt, DESCTYPE_S);
  238. #endif
  239. }
  240. void __init setup_per_cpu_areas(void)
  241. {
  242. unsigned int cpu;
  243. unsigned long delta;
  244. size_t pcpu_unit_size;
  245. ssize_t ret;
  246. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
  247. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  248. /*
  249. * Allocate percpu area. If PSE is supported, try to make use
  250. * of large page mappings. Please read comments on top of
  251. * each allocator for details.
  252. */
  253. ret = -EINVAL;
  254. if (pcpu_chosen_fc != PCPU_FC_AUTO) {
  255. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  256. if (pcpu_chosen_fc == PCPU_FC_LPAGE)
  257. ret = setup_pcpu_lpage(true);
  258. else
  259. ret = setup_pcpu_embed(true);
  260. if (ret < 0)
  261. pr_warning("PERCPU: %s allocator failed (%zd), "
  262. "falling back to page size\n",
  263. pcpu_fc_names[pcpu_chosen_fc], ret);
  264. }
  265. } else {
  266. ret = setup_pcpu_lpage(false);
  267. if (ret < 0)
  268. ret = setup_pcpu_embed(false);
  269. }
  270. if (ret < 0)
  271. ret = setup_pcpu_page();
  272. if (ret < 0)
  273. panic("cannot initialize percpu area (err=%zd)", ret);
  274. pcpu_unit_size = ret;
  275. /* alrighty, percpu areas up and running */
  276. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  277. for_each_possible_cpu(cpu) {
  278. per_cpu_offset(cpu) =
  279. delta + pcpu_unit_map[cpu] * pcpu_unit_size;
  280. per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
  281. per_cpu(cpu_number, cpu) = cpu;
  282. setup_percpu_segment(cpu);
  283. setup_stack_canary_segment(cpu);
  284. /*
  285. * Copy data used in early init routines from the
  286. * initial arrays to the per cpu data areas. These
  287. * arrays then become expendable and the *_early_ptr's
  288. * are zeroed indicating that the static arrays are
  289. * gone.
  290. */
  291. #ifdef CONFIG_X86_LOCAL_APIC
  292. per_cpu(x86_cpu_to_apicid, cpu) =
  293. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  294. per_cpu(x86_bios_cpu_apicid, cpu) =
  295. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  296. #endif
  297. #ifdef CONFIG_X86_64
  298. per_cpu(irq_stack_ptr, cpu) =
  299. per_cpu(irq_stack_union.irq_stack, cpu) +
  300. IRQ_STACK_SIZE - 64;
  301. #ifdef CONFIG_NUMA
  302. per_cpu(x86_cpu_to_node_map, cpu) =
  303. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  304. #endif
  305. #endif
  306. /*
  307. * Up to this point, the boot CPU has been using .data.init
  308. * area. Reload any changed state for the boot CPU.
  309. */
  310. if (cpu == boot_cpu_id)
  311. switch_to_new_gdt(cpu);
  312. }
  313. /* indicate the early static arrays will soon be gone */
  314. #ifdef CONFIG_X86_LOCAL_APIC
  315. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  316. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  317. #endif
  318. #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
  319. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  320. #endif
  321. #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
  322. /*
  323. * make sure boot cpu node_number is right, when boot cpu is on the
  324. * node that doesn't have mem installed
  325. */
  326. per_cpu(node_number, boot_cpu_id) = cpu_to_node(boot_cpu_id);
  327. #endif
  328. /* Setup node to cpumask map */
  329. setup_node_to_cpumask_map();
  330. /* Setup cpu initialized, callin, callout masks */
  331. setup_cpu_local_masks();
  332. }