setup_percpu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. * Large page remap allocator
  115. *
  116. * This allocator uses PMD page as unit. A PMD page is allocated for
  117. * each cpu and each is remapped into vmalloc area using PMD mapping.
  118. * As PMD page is quite large, only part of it is used for the first
  119. * chunk. Unused part is returned to the bootmem allocator.
  120. *
  121. * So, the PMD pages are mapped twice - once to the physical mapping
  122. * and to the vmalloc area for the first percpu chunk. The double
  123. * mapping does add one more PMD TLB entry pressure but still is much
  124. * better than only using 4k mappings while still being NUMA friendly.
  125. */
  126. #ifdef CONFIG_NEED_MULTIPLE_NODES
  127. struct pcpul_ent {
  128. unsigned int cpu;
  129. void *ptr;
  130. };
  131. static size_t pcpul_size;
  132. static struct pcpul_ent *pcpul_map;
  133. static struct vm_struct pcpul_vm;
  134. static struct page * __init pcpul_get_page(unsigned int cpu, int pageno)
  135. {
  136. size_t off = (size_t)pageno << PAGE_SHIFT;
  137. if (off >= pcpul_size)
  138. return NULL;
  139. return virt_to_page(pcpul_map[cpu].ptr + off);
  140. }
  141. static ssize_t __init setup_pcpu_lpage(size_t static_size, bool chosen)
  142. {
  143. size_t map_size, dyn_size;
  144. unsigned int cpu;
  145. int i, j;
  146. ssize_t ret;
  147. if (!chosen) {
  148. size_t vm_size = VMALLOC_END - VMALLOC_START;
  149. size_t tot_size = num_possible_cpus() * PMD_SIZE;
  150. /* on non-NUMA, embedding is better */
  151. if (!pcpu_need_numa())
  152. return -EINVAL;
  153. /* don't consume more than 20% of vmalloc area */
  154. if (tot_size > vm_size / 5) {
  155. pr_info("PERCPU: too large chunk size %zuMB for "
  156. "large page remap\n", tot_size >> 20);
  157. return -EINVAL;
  158. }
  159. }
  160. /* need PSE */
  161. if (!cpu_has_pse) {
  162. pr_warning("PERCPU: lpage allocator requires PSE\n");
  163. return -EINVAL;
  164. }
  165. /*
  166. * Currently supports only single page. Supporting multiple
  167. * pages won't be too difficult if it ever becomes necessary.
  168. */
  169. pcpul_size = PFN_ALIGN(static_size + PERCPU_MODULE_RESERVE +
  170. PERCPU_DYNAMIC_RESERVE);
  171. if (pcpul_size > PMD_SIZE) {
  172. pr_warning("PERCPU: static data is larger than large page, "
  173. "can't use large page\n");
  174. return -EINVAL;
  175. }
  176. dyn_size = pcpul_size - static_size - PERCPU_FIRST_CHUNK_RESERVE;
  177. /* allocate pointer array and alloc large pages */
  178. map_size = PFN_ALIGN(num_possible_cpus() * sizeof(pcpul_map[0]));
  179. pcpul_map = alloc_bootmem(map_size);
  180. for_each_possible_cpu(cpu) {
  181. pcpul_map[cpu].cpu = cpu;
  182. pcpul_map[cpu].ptr = pcpu_alloc_bootmem(cpu, PMD_SIZE,
  183. PMD_SIZE);
  184. if (!pcpul_map[cpu].ptr) {
  185. pr_warning("PERCPU: failed to allocate large page "
  186. "for cpu%u\n", cpu);
  187. goto enomem;
  188. }
  189. /*
  190. * Only use pcpul_size bytes and give back the rest.
  191. *
  192. * Ingo: The 2MB up-rounding bootmem is needed to make
  193. * sure the partial 2MB page is still fully RAM - it's
  194. * not well-specified to have a PAT-incompatible area
  195. * (unmapped RAM, device memory, etc.) in that hole.
  196. */
  197. free_bootmem(__pa(pcpul_map[cpu].ptr + pcpul_size),
  198. PMD_SIZE - pcpul_size);
  199. memcpy(pcpul_map[cpu].ptr, __per_cpu_load, static_size);
  200. }
  201. /* allocate address and map */
  202. pcpul_vm.flags = VM_ALLOC;
  203. pcpul_vm.size = num_possible_cpus() * PMD_SIZE;
  204. vm_area_register_early(&pcpul_vm, PMD_SIZE);
  205. for_each_possible_cpu(cpu) {
  206. pmd_t *pmd, pmd_v;
  207. pmd = populate_extra_pmd((unsigned long)pcpul_vm.addr +
  208. cpu * PMD_SIZE);
  209. pmd_v = pfn_pmd(page_to_pfn(virt_to_page(pcpul_map[cpu].ptr)),
  210. PAGE_KERNEL_LARGE);
  211. set_pmd(pmd, pmd_v);
  212. }
  213. /* we're ready, commit */
  214. pr_info("PERCPU: Remapped at %p with large pages, static data "
  215. "%zu bytes\n", pcpul_vm.addr, static_size);
  216. ret = pcpu_setup_first_chunk(pcpul_get_page, static_size,
  217. PERCPU_FIRST_CHUNK_RESERVE, dyn_size,
  218. PMD_SIZE, pcpul_vm.addr, NULL);
  219. /* sort pcpul_map array for pcpu_lpage_remapped() */
  220. for (i = 0; i < num_possible_cpus() - 1; i++)
  221. for (j = i + 1; j < num_possible_cpus(); j++)
  222. if (pcpul_map[i].ptr > pcpul_map[j].ptr) {
  223. struct pcpul_ent tmp = pcpul_map[i];
  224. pcpul_map[i] = pcpul_map[j];
  225. pcpul_map[j] = tmp;
  226. }
  227. return ret;
  228. enomem:
  229. for_each_possible_cpu(cpu)
  230. if (pcpul_map[cpu].ptr)
  231. free_bootmem(__pa(pcpul_map[cpu].ptr), pcpul_size);
  232. free_bootmem(__pa(pcpul_map), map_size);
  233. return -ENOMEM;
  234. }
  235. /**
  236. * pcpu_lpage_remapped - determine whether a kaddr is in pcpul recycled area
  237. * @kaddr: the kernel address in question
  238. *
  239. * Determine whether @kaddr falls in the pcpul recycled area. This is
  240. * used by pageattr to detect VM aliases and break up the pcpu PMD
  241. * mapping such that the same physical page is not mapped under
  242. * different attributes.
  243. *
  244. * The recycled area is always at the tail of a partially used PMD
  245. * page.
  246. *
  247. * RETURNS:
  248. * Address of corresponding remapped pcpu address if match is found;
  249. * otherwise, NULL.
  250. */
  251. void *pcpu_lpage_remapped(void *kaddr)
  252. {
  253. void *pmd_addr = (void *)((unsigned long)kaddr & PMD_MASK);
  254. unsigned long offset = (unsigned long)kaddr & ~PMD_MASK;
  255. int left = 0, right = num_possible_cpus() - 1;
  256. int pos;
  257. /* pcpul in use at all? */
  258. if (!pcpul_map)
  259. return NULL;
  260. /* okay, perform binary search */
  261. while (left <= right) {
  262. pos = (left + right) / 2;
  263. if (pcpul_map[pos].ptr < pmd_addr)
  264. left = pos + 1;
  265. else if (pcpul_map[pos].ptr > pmd_addr)
  266. right = pos - 1;
  267. else {
  268. /* it shouldn't be in the area for the first chunk */
  269. WARN_ON(offset < pcpul_size);
  270. return pcpul_vm.addr +
  271. pcpul_map[pos].cpu * PMD_SIZE + offset;
  272. }
  273. }
  274. return NULL;
  275. }
  276. #else
  277. static ssize_t __init setup_pcpu_lpage(size_t static_size, bool chosen)
  278. {
  279. return -EINVAL;
  280. }
  281. #endif
  282. /*
  283. * Embedding allocator
  284. *
  285. * The first chunk is sized to just contain the static area plus
  286. * module and dynamic reserves and embedded into linear physical
  287. * mapping so that it can use PMD mapping without additional TLB
  288. * pressure.
  289. */
  290. static ssize_t __init setup_pcpu_embed(size_t static_size, bool chosen)
  291. {
  292. size_t reserve = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
  293. /*
  294. * If large page isn't supported, there's no benefit in doing
  295. * this. Also, embedding allocation doesn't play well with
  296. * NUMA.
  297. */
  298. if (!chosen && (!cpu_has_pse || pcpu_need_numa()))
  299. return -EINVAL;
  300. return pcpu_embed_first_chunk(static_size, PERCPU_FIRST_CHUNK_RESERVE,
  301. reserve - PERCPU_FIRST_CHUNK_RESERVE, -1);
  302. }
  303. /*
  304. * 4k page allocator
  305. *
  306. * This is the basic allocator. Static percpu area is allocated
  307. * page-by-page and most of initialization is done by the generic
  308. * setup function.
  309. */
  310. static struct page **pcpu4k_pages __initdata;
  311. static int pcpu4k_nr_static_pages __initdata;
  312. static struct page * __init pcpu4k_get_page(unsigned int cpu, int pageno)
  313. {
  314. if (pageno < pcpu4k_nr_static_pages)
  315. return pcpu4k_pages[cpu * pcpu4k_nr_static_pages + pageno];
  316. return NULL;
  317. }
  318. static void __init pcpu4k_populate_pte(unsigned long addr)
  319. {
  320. populate_extra_pte(addr);
  321. }
  322. static ssize_t __init setup_pcpu_4k(size_t static_size)
  323. {
  324. size_t pages_size;
  325. unsigned int cpu;
  326. int i, j;
  327. ssize_t ret;
  328. pcpu4k_nr_static_pages = PFN_UP(static_size);
  329. /* unaligned allocations can't be freed, round up to page size */
  330. pages_size = PFN_ALIGN(pcpu4k_nr_static_pages * num_possible_cpus()
  331. * sizeof(pcpu4k_pages[0]));
  332. pcpu4k_pages = alloc_bootmem(pages_size);
  333. /* allocate and copy */
  334. j = 0;
  335. for_each_possible_cpu(cpu)
  336. for (i = 0; i < pcpu4k_nr_static_pages; i++) {
  337. void *ptr;
  338. ptr = pcpu_alloc_bootmem(cpu, PAGE_SIZE, PAGE_SIZE);
  339. if (!ptr) {
  340. pr_warning("PERCPU: failed to allocate "
  341. "4k page for cpu%u\n", cpu);
  342. goto enomem;
  343. }
  344. memcpy(ptr, __per_cpu_load + i * PAGE_SIZE, PAGE_SIZE);
  345. pcpu4k_pages[j++] = virt_to_page(ptr);
  346. }
  347. /* we're ready, commit */
  348. pr_info("PERCPU: Allocated %d 4k pages, static data %zu bytes\n",
  349. pcpu4k_nr_static_pages, static_size);
  350. ret = pcpu_setup_first_chunk(pcpu4k_get_page, static_size,
  351. PERCPU_FIRST_CHUNK_RESERVE, -1,
  352. -1, NULL, pcpu4k_populate_pte);
  353. goto out_free_ar;
  354. enomem:
  355. while (--j >= 0)
  356. free_bootmem(__pa(page_address(pcpu4k_pages[j])), PAGE_SIZE);
  357. ret = -ENOMEM;
  358. out_free_ar:
  359. free_bootmem(__pa(pcpu4k_pages), pages_size);
  360. return ret;
  361. }
  362. /* for explicit first chunk allocator selection */
  363. static char pcpu_chosen_alloc[16] __initdata;
  364. static int __init percpu_alloc_setup(char *str)
  365. {
  366. strncpy(pcpu_chosen_alloc, str, sizeof(pcpu_chosen_alloc) - 1);
  367. return 0;
  368. }
  369. early_param("percpu_alloc", percpu_alloc_setup);
  370. static inline void setup_percpu_segment(int cpu)
  371. {
  372. #ifdef CONFIG_X86_32
  373. struct desc_struct gdt;
  374. pack_descriptor(&gdt, per_cpu_offset(cpu), 0xFFFFF,
  375. 0x2 | DESCTYPE_S, 0x8);
  376. gdt.s = 1;
  377. write_gdt_entry(get_cpu_gdt_table(cpu),
  378. GDT_ENTRY_PERCPU, &gdt, DESCTYPE_S);
  379. #endif
  380. }
  381. void __init setup_per_cpu_areas(void)
  382. {
  383. size_t static_size = __per_cpu_end - __per_cpu_start;
  384. unsigned int cpu;
  385. unsigned long delta;
  386. size_t pcpu_unit_size;
  387. ssize_t ret;
  388. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
  389. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  390. /*
  391. * Allocate percpu area. If PSE is supported, try to make use
  392. * of large page mappings. Please read comments on top of
  393. * each allocator for details.
  394. */
  395. ret = -EINVAL;
  396. if (strlen(pcpu_chosen_alloc)) {
  397. if (strcmp(pcpu_chosen_alloc, "4k")) {
  398. if (!strcmp(pcpu_chosen_alloc, "lpage"))
  399. ret = setup_pcpu_lpage(static_size, true);
  400. else if (!strcmp(pcpu_chosen_alloc, "embed"))
  401. ret = setup_pcpu_embed(static_size, true);
  402. else
  403. pr_warning("PERCPU: unknown allocator %s "
  404. "specified\n", pcpu_chosen_alloc);
  405. if (ret < 0)
  406. pr_warning("PERCPU: %s allocator failed (%zd), "
  407. "falling back to 4k\n",
  408. pcpu_chosen_alloc, ret);
  409. }
  410. } else {
  411. ret = setup_pcpu_lpage(static_size, false);
  412. if (ret < 0)
  413. ret = setup_pcpu_embed(static_size, false);
  414. }
  415. if (ret < 0)
  416. ret = setup_pcpu_4k(static_size);
  417. if (ret < 0)
  418. panic("cannot allocate static percpu area (%zu bytes, err=%zd)",
  419. static_size, ret);
  420. pcpu_unit_size = ret;
  421. /* alrighty, percpu areas up and running */
  422. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  423. for_each_possible_cpu(cpu) {
  424. per_cpu_offset(cpu) = delta + cpu * pcpu_unit_size;
  425. per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
  426. per_cpu(cpu_number, cpu) = cpu;
  427. setup_percpu_segment(cpu);
  428. setup_stack_canary_segment(cpu);
  429. /*
  430. * Copy data used in early init routines from the
  431. * initial arrays to the per cpu data areas. These
  432. * arrays then become expendable and the *_early_ptr's
  433. * are zeroed indicating that the static arrays are
  434. * gone.
  435. */
  436. #ifdef CONFIG_X86_LOCAL_APIC
  437. per_cpu(x86_cpu_to_apicid, cpu) =
  438. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  439. per_cpu(x86_bios_cpu_apicid, cpu) =
  440. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  441. #endif
  442. #ifdef CONFIG_X86_64
  443. per_cpu(irq_stack_ptr, cpu) =
  444. per_cpu(irq_stack_union.irq_stack, cpu) +
  445. IRQ_STACK_SIZE - 64;
  446. #ifdef CONFIG_NUMA
  447. per_cpu(x86_cpu_to_node_map, cpu) =
  448. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  449. #endif
  450. #endif
  451. /*
  452. * Up to this point, the boot CPU has been using .data.init
  453. * area. Reload any changed state for the boot CPU.
  454. */
  455. if (cpu == boot_cpu_id)
  456. switch_to_new_gdt(cpu);
  457. }
  458. /* indicate the early static arrays will soon be gone */
  459. #ifdef CONFIG_X86_LOCAL_APIC
  460. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  461. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  462. #endif
  463. #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
  464. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  465. #endif
  466. #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
  467. /*
  468. * make sure boot cpu node_number is right, when boot cpu is on the
  469. * node that doesn't have mem installed
  470. */
  471. per_cpu(node_number, boot_cpu_id) = cpu_to_node(boot_cpu_id);
  472. #endif
  473. /* Setup node to cpumask map */
  474. setup_node_to_cpumask_map();
  475. /* Setup cpu initialized, callin, callout masks */
  476. setup_cpu_local_masks();
  477. }