percpu-vm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * mm/percpu-vm.c - vmalloc area based chunk allocation
  3. *
  4. * Copyright (C) 2010 SUSE Linux Products GmbH
  5. * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
  6. *
  7. * This file is released under the GPLv2.
  8. *
  9. * Chunks are mapped into vmalloc areas and populated page by page.
  10. * This is the default chunk allocator.
  11. */
  12. static struct page *pcpu_chunk_page(struct pcpu_chunk *chunk,
  13. unsigned int cpu, int page_idx)
  14. {
  15. /* must not be used on pre-mapped chunk */
  16. WARN_ON(chunk->immutable);
  17. return vmalloc_to_page((void *)pcpu_chunk_addr(chunk, cpu, page_idx));
  18. }
  19. /**
  20. * pcpu_get_pages - get temp pages array
  21. *
  22. * Returns pointer to array of pointers to struct page which can be indexed
  23. * with pcpu_page_idx(). Note that there is only one array and accesses
  24. * should be serialized by pcpu_alloc_mutex.
  25. *
  26. * RETURNS:
  27. * Pointer to temp pages array on success.
  28. */
  29. static struct page **pcpu_get_pages(void)
  30. {
  31. static struct page **pages;
  32. size_t pages_size = pcpu_nr_units * pcpu_unit_pages * sizeof(pages[0]);
  33. lockdep_assert_held(&pcpu_alloc_mutex);
  34. if (!pages)
  35. pages = pcpu_mem_zalloc(pages_size, GFP_KERNEL);
  36. return pages;
  37. }
  38. /**
  39. * pcpu_free_pages - free pages which were allocated for @chunk
  40. * @chunk: chunk pages were allocated for
  41. * @pages: array of pages to be freed, indexed by pcpu_page_idx()
  42. * @page_start: page index of the first page to be freed
  43. * @page_end: page index of the last page to be freed + 1
  44. *
  45. * Free pages [@page_start and @page_end) in @pages for all units.
  46. * The pages were allocated for @chunk.
  47. */
  48. static void pcpu_free_pages(struct pcpu_chunk *chunk,
  49. struct page **pages, int page_start, int page_end)
  50. {
  51. unsigned int cpu;
  52. int i;
  53. for_each_possible_cpu(cpu) {
  54. for (i = page_start; i < page_end; i++) {
  55. struct page *page = pages[pcpu_page_idx(cpu, i)];
  56. if (page)
  57. __free_page(page);
  58. }
  59. }
  60. }
  61. /**
  62. * pcpu_alloc_pages - allocates pages for @chunk
  63. * @chunk: target chunk
  64. * @pages: array to put the allocated pages into, indexed by pcpu_page_idx()
  65. * @page_start: page index of the first page to be allocated
  66. * @page_end: page index of the last page to be allocated + 1
  67. * @gfp: allocation flags passed to the underlying allocator
  68. *
  69. * Allocate pages [@page_start,@page_end) into @pages for all units.
  70. * The allocation is for @chunk. Percpu core doesn't care about the
  71. * content of @pages and will pass it verbatim to pcpu_map_pages().
  72. */
  73. static int pcpu_alloc_pages(struct pcpu_chunk *chunk,
  74. struct page **pages, int page_start, int page_end,
  75. gfp_t gfp)
  76. {
  77. unsigned int cpu, tcpu;
  78. int i;
  79. gfp |= __GFP_HIGHMEM;
  80. for_each_possible_cpu(cpu) {
  81. for (i = page_start; i < page_end; i++) {
  82. struct page **pagep = &pages[pcpu_page_idx(cpu, i)];
  83. *pagep = alloc_pages_node(cpu_to_node(cpu), gfp, 0);
  84. if (!*pagep)
  85. goto err;
  86. }
  87. }
  88. return 0;
  89. err:
  90. while (--i >= page_start)
  91. __free_page(pages[pcpu_page_idx(cpu, i)]);
  92. for_each_possible_cpu(tcpu) {
  93. if (tcpu == cpu)
  94. break;
  95. for (i = page_start; i < page_end; i++)
  96. __free_page(pages[pcpu_page_idx(tcpu, i)]);
  97. }
  98. return -ENOMEM;
  99. }
  100. /**
  101. * pcpu_pre_unmap_flush - flush cache prior to unmapping
  102. * @chunk: chunk the regions to be flushed belongs to
  103. * @page_start: page index of the first page to be flushed
  104. * @page_end: page index of the last page to be flushed + 1
  105. *
  106. * Pages in [@page_start,@page_end) of @chunk are about to be
  107. * unmapped. Flush cache. As each flushing trial can be very
  108. * expensive, issue flush on the whole region at once rather than
  109. * doing it for each cpu. This could be an overkill but is more
  110. * scalable.
  111. */
  112. static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk,
  113. int page_start, int page_end)
  114. {
  115. flush_cache_vunmap(
  116. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  117. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  118. }
  119. static void __pcpu_unmap_pages(unsigned long addr, int nr_pages)
  120. {
  121. unmap_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT);
  122. }
  123. /**
  124. * pcpu_unmap_pages - unmap pages out of a pcpu_chunk
  125. * @chunk: chunk of interest
  126. * @pages: pages array which can be used to pass information to free
  127. * @page_start: page index of the first page to unmap
  128. * @page_end: page index of the last page to unmap + 1
  129. *
  130. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  131. * Corresponding elements in @pages were cleared by the caller and can
  132. * be used to carry information to pcpu_free_pages() which will be
  133. * called after all unmaps are finished. The caller should call
  134. * proper pre/post flush functions.
  135. */
  136. static void pcpu_unmap_pages(struct pcpu_chunk *chunk,
  137. struct page **pages, int page_start, int page_end)
  138. {
  139. unsigned int cpu;
  140. int i;
  141. for_each_possible_cpu(cpu) {
  142. for (i = page_start; i < page_end; i++) {
  143. struct page *page;
  144. page = pcpu_chunk_page(chunk, cpu, i);
  145. WARN_ON(!page);
  146. pages[pcpu_page_idx(cpu, i)] = page;
  147. }
  148. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  149. page_end - page_start);
  150. }
  151. }
  152. /**
  153. * pcpu_post_unmap_tlb_flush - flush TLB after unmapping
  154. * @chunk: pcpu_chunk the regions to be flushed belong to
  155. * @page_start: page index of the first page to be flushed
  156. * @page_end: page index of the last page to be flushed + 1
  157. *
  158. * Pages [@page_start,@page_end) of @chunk have been unmapped. Flush
  159. * TLB for the regions. This can be skipped if the area is to be
  160. * returned to vmalloc as vmalloc will handle TLB flushing lazily.
  161. *
  162. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  163. * for the whole region.
  164. */
  165. static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk,
  166. int page_start, int page_end)
  167. {
  168. flush_tlb_kernel_range(
  169. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  170. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  171. }
  172. static int __pcpu_map_pages(unsigned long addr, struct page **pages,
  173. int nr_pages)
  174. {
  175. return map_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT,
  176. PAGE_KERNEL, pages);
  177. }
  178. /**
  179. * pcpu_map_pages - map pages into a pcpu_chunk
  180. * @chunk: chunk of interest
  181. * @pages: pages array containing pages to be mapped
  182. * @page_start: page index of the first page to map
  183. * @page_end: page index of the last page to map + 1
  184. *
  185. * For each cpu, map pages [@page_start,@page_end) into @chunk. The
  186. * caller is responsible for calling pcpu_post_map_flush() after all
  187. * mappings are complete.
  188. *
  189. * This function is responsible for setting up whatever is necessary for
  190. * reverse lookup (addr -> chunk).
  191. */
  192. static int pcpu_map_pages(struct pcpu_chunk *chunk,
  193. struct page **pages, int page_start, int page_end)
  194. {
  195. unsigned int cpu, tcpu;
  196. int i, err;
  197. for_each_possible_cpu(cpu) {
  198. err = __pcpu_map_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  199. &pages[pcpu_page_idx(cpu, page_start)],
  200. page_end - page_start);
  201. if (err < 0)
  202. goto err;
  203. for (i = page_start; i < page_end; i++)
  204. pcpu_set_page_chunk(pages[pcpu_page_idx(cpu, i)],
  205. chunk);
  206. }
  207. return 0;
  208. err:
  209. for_each_possible_cpu(tcpu) {
  210. if (tcpu == cpu)
  211. break;
  212. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
  213. page_end - page_start);
  214. }
  215. pcpu_post_unmap_tlb_flush(chunk, page_start, page_end);
  216. return err;
  217. }
  218. /**
  219. * pcpu_post_map_flush - flush cache after mapping
  220. * @chunk: pcpu_chunk the regions to be flushed belong to
  221. * @page_start: page index of the first page to be flushed
  222. * @page_end: page index of the last page to be flushed + 1
  223. *
  224. * Pages [@page_start,@page_end) of @chunk have been mapped. Flush
  225. * cache.
  226. *
  227. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  228. * for the whole region.
  229. */
  230. static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
  231. int page_start, int page_end)
  232. {
  233. flush_cache_vmap(
  234. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  235. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  236. }
  237. /**
  238. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  239. * @chunk: chunk of interest
  240. * @page_start: the start page
  241. * @page_end: the end page
  242. * @gfp: allocation flags passed to the underlying memory allocator
  243. *
  244. * For each cpu, populate and map pages [@page_start,@page_end) into
  245. * @chunk.
  246. *
  247. * CONTEXT:
  248. * pcpu_alloc_mutex, does GFP_KERNEL allocation.
  249. */
  250. static int pcpu_populate_chunk(struct pcpu_chunk *chunk,
  251. int page_start, int page_end, gfp_t gfp)
  252. {
  253. struct page **pages;
  254. pages = pcpu_get_pages();
  255. if (!pages)
  256. return -ENOMEM;
  257. if (pcpu_alloc_pages(chunk, pages, page_start, page_end, gfp))
  258. return -ENOMEM;
  259. if (pcpu_map_pages(chunk, pages, page_start, page_end)) {
  260. pcpu_free_pages(chunk, pages, page_start, page_end);
  261. return -ENOMEM;
  262. }
  263. pcpu_post_map_flush(chunk, page_start, page_end);
  264. return 0;
  265. }
  266. /**
  267. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  268. * @chunk: chunk to depopulate
  269. * @page_start: the start page
  270. * @page_end: the end page
  271. *
  272. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  273. * from @chunk.
  274. *
  275. * CONTEXT:
  276. * pcpu_alloc_mutex.
  277. */
  278. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
  279. int page_start, int page_end)
  280. {
  281. struct page **pages;
  282. /*
  283. * If control reaches here, there must have been at least one
  284. * successful population attempt so the temp pages array must
  285. * be available now.
  286. */
  287. pages = pcpu_get_pages();
  288. BUG_ON(!pages);
  289. /* unmap and free */
  290. pcpu_pre_unmap_flush(chunk, page_start, page_end);
  291. pcpu_unmap_pages(chunk, pages, page_start, page_end);
  292. /* no need to flush tlb, vmalloc will handle it lazily */
  293. pcpu_free_pages(chunk, pages, page_start, page_end);
  294. }
  295. static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp)
  296. {
  297. struct pcpu_chunk *chunk;
  298. struct vm_struct **vms;
  299. chunk = pcpu_alloc_chunk(gfp);
  300. if (!chunk)
  301. return NULL;
  302. vms = pcpu_get_vm_areas(pcpu_group_offsets, pcpu_group_sizes,
  303. pcpu_nr_groups, pcpu_atom_size);
  304. if (!vms) {
  305. pcpu_free_chunk(chunk);
  306. return NULL;
  307. }
  308. chunk->data = vms;
  309. chunk->base_addr = vms[0]->addr - pcpu_group_offsets[0];
  310. pcpu_stats_chunk_alloc();
  311. trace_percpu_create_chunk(chunk->base_addr);
  312. return chunk;
  313. }
  314. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk)
  315. {
  316. if (!chunk)
  317. return;
  318. pcpu_stats_chunk_dealloc();
  319. trace_percpu_destroy_chunk(chunk->base_addr);
  320. if (chunk->data)
  321. pcpu_free_vm_areas(chunk->data, pcpu_nr_groups);
  322. pcpu_free_chunk(chunk);
  323. }
  324. static struct page *pcpu_addr_to_page(void *addr)
  325. {
  326. return vmalloc_to_page(addr);
  327. }
  328. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai)
  329. {
  330. /* no extra restriction */
  331. return 0;
  332. }