percpu-vm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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);
  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. *
  68. * Allocate pages [@page_start,@page_end) into @pages for all units.
  69. * The allocation is for @chunk. Percpu core doesn't care about the
  70. * content of @pages and will pass it verbatim to pcpu_map_pages().
  71. */
  72. static int pcpu_alloc_pages(struct pcpu_chunk *chunk,
  73. struct page **pages, int page_start, int page_end)
  74. {
  75. const gfp_t gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  76. unsigned int cpu, tcpu;
  77. int i;
  78. for_each_possible_cpu(cpu) {
  79. for (i = page_start; i < page_end; i++) {
  80. struct page **pagep = &pages[pcpu_page_idx(cpu, i)];
  81. *pagep = alloc_pages_node(cpu_to_node(cpu), gfp, 0);
  82. if (!*pagep)
  83. goto err;
  84. }
  85. }
  86. return 0;
  87. err:
  88. while (--i >= page_start)
  89. __free_page(pages[pcpu_page_idx(cpu, i)]);
  90. for_each_possible_cpu(tcpu) {
  91. if (tcpu == cpu)
  92. break;
  93. for (i = page_start; i < page_end; i++)
  94. __free_page(pages[pcpu_page_idx(tcpu, i)]);
  95. }
  96. return -ENOMEM;
  97. }
  98. /**
  99. * pcpu_pre_unmap_flush - flush cache prior to unmapping
  100. * @chunk: chunk the regions to be flushed belongs to
  101. * @page_start: page index of the first page to be flushed
  102. * @page_end: page index of the last page to be flushed + 1
  103. *
  104. * Pages in [@page_start,@page_end) of @chunk are about to be
  105. * unmapped. Flush cache. As each flushing trial can be very
  106. * expensive, issue flush on the whole region at once rather than
  107. * doing it for each cpu. This could be an overkill but is more
  108. * scalable.
  109. */
  110. static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk,
  111. int page_start, int page_end)
  112. {
  113. flush_cache_vunmap(
  114. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  115. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  116. }
  117. static void __pcpu_unmap_pages(unsigned long addr, int nr_pages)
  118. {
  119. unmap_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT);
  120. }
  121. /**
  122. * pcpu_unmap_pages - unmap pages out of a pcpu_chunk
  123. * @chunk: chunk of interest
  124. * @pages: pages array which can be used to pass information to free
  125. * @page_start: page index of the first page to unmap
  126. * @page_end: page index of the last page to unmap + 1
  127. *
  128. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  129. * Corresponding elements in @pages were cleared by the caller and can
  130. * be used to carry information to pcpu_free_pages() which will be
  131. * called after all unmaps are finished. The caller should call
  132. * proper pre/post flush functions.
  133. */
  134. static void pcpu_unmap_pages(struct pcpu_chunk *chunk,
  135. struct page **pages, int page_start, int page_end)
  136. {
  137. unsigned int cpu;
  138. int i;
  139. for_each_possible_cpu(cpu) {
  140. for (i = page_start; i < page_end; i++) {
  141. struct page *page;
  142. page = pcpu_chunk_page(chunk, cpu, i);
  143. WARN_ON(!page);
  144. pages[pcpu_page_idx(cpu, i)] = page;
  145. }
  146. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  147. page_end - page_start);
  148. }
  149. }
  150. /**
  151. * pcpu_post_unmap_tlb_flush - flush TLB after unmapping
  152. * @chunk: pcpu_chunk the regions to be flushed belong to
  153. * @page_start: page index of the first page to be flushed
  154. * @page_end: page index of the last page to be flushed + 1
  155. *
  156. * Pages [@page_start,@page_end) of @chunk have been unmapped. Flush
  157. * TLB for the regions. This can be skipped if the area is to be
  158. * returned to vmalloc as vmalloc will handle TLB flushing lazily.
  159. *
  160. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  161. * for the whole region.
  162. */
  163. static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk,
  164. int page_start, int page_end)
  165. {
  166. flush_tlb_kernel_range(
  167. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  168. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  169. }
  170. static int __pcpu_map_pages(unsigned long addr, struct page **pages,
  171. int nr_pages)
  172. {
  173. return map_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT,
  174. PAGE_KERNEL, pages);
  175. }
  176. /**
  177. * pcpu_map_pages - map pages into a pcpu_chunk
  178. * @chunk: chunk of interest
  179. * @pages: pages array containing pages to be mapped
  180. * @page_start: page index of the first page to map
  181. * @page_end: page index of the last page to map + 1
  182. *
  183. * For each cpu, map pages [@page_start,@page_end) into @chunk. The
  184. * caller is responsible for calling pcpu_post_map_flush() after all
  185. * mappings are complete.
  186. *
  187. * This function is responsible for setting up whatever is necessary for
  188. * reverse lookup (addr -> chunk).
  189. */
  190. static int pcpu_map_pages(struct pcpu_chunk *chunk,
  191. struct page **pages, int page_start, int page_end)
  192. {
  193. unsigned int cpu, tcpu;
  194. int i, err;
  195. for_each_possible_cpu(cpu) {
  196. err = __pcpu_map_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  197. &pages[pcpu_page_idx(cpu, page_start)],
  198. page_end - page_start);
  199. if (err < 0)
  200. goto err;
  201. for (i = page_start; i < page_end; i++)
  202. pcpu_set_page_chunk(pages[pcpu_page_idx(cpu, i)],
  203. chunk);
  204. }
  205. return 0;
  206. err:
  207. for_each_possible_cpu(tcpu) {
  208. if (tcpu == cpu)
  209. break;
  210. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
  211. page_end - page_start);
  212. }
  213. pcpu_post_unmap_tlb_flush(chunk, page_start, page_end);
  214. return err;
  215. }
  216. /**
  217. * pcpu_post_map_flush - flush cache after mapping
  218. * @chunk: pcpu_chunk the regions to be flushed belong to
  219. * @page_start: page index of the first page to be flushed
  220. * @page_end: page index of the last page to be flushed + 1
  221. *
  222. * Pages [@page_start,@page_end) of @chunk have been mapped. Flush
  223. * cache.
  224. *
  225. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  226. * for the whole region.
  227. */
  228. static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
  229. int page_start, int page_end)
  230. {
  231. flush_cache_vmap(
  232. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  233. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  234. }
  235. /**
  236. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  237. * @chunk: chunk of interest
  238. * @page_start: the start page
  239. * @page_end: the end page
  240. *
  241. * For each cpu, populate and map pages [@page_start,@page_end) into
  242. * @chunk.
  243. *
  244. * CONTEXT:
  245. * pcpu_alloc_mutex, does GFP_KERNEL allocation.
  246. */
  247. static int pcpu_populate_chunk(struct pcpu_chunk *chunk,
  248. int page_start, int page_end)
  249. {
  250. struct page **pages;
  251. pages = pcpu_get_pages();
  252. if (!pages)
  253. return -ENOMEM;
  254. if (pcpu_alloc_pages(chunk, pages, page_start, page_end))
  255. return -ENOMEM;
  256. if (pcpu_map_pages(chunk, pages, page_start, page_end)) {
  257. pcpu_free_pages(chunk, pages, page_start, page_end);
  258. return -ENOMEM;
  259. }
  260. pcpu_post_map_flush(chunk, page_start, page_end);
  261. return 0;
  262. }
  263. /**
  264. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  265. * @chunk: chunk to depopulate
  266. * @page_start: the start page
  267. * @page_end: the end page
  268. *
  269. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  270. * from @chunk.
  271. *
  272. * CONTEXT:
  273. * pcpu_alloc_mutex.
  274. */
  275. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
  276. int page_start, int page_end)
  277. {
  278. struct page **pages;
  279. /*
  280. * If control reaches here, there must have been at least one
  281. * successful population attempt so the temp pages array must
  282. * be available now.
  283. */
  284. pages = pcpu_get_pages();
  285. BUG_ON(!pages);
  286. /* unmap and free */
  287. pcpu_pre_unmap_flush(chunk, page_start, page_end);
  288. pcpu_unmap_pages(chunk, pages, page_start, page_end);
  289. /* no need to flush tlb, vmalloc will handle it lazily */
  290. pcpu_free_pages(chunk, pages, page_start, page_end);
  291. }
  292. static struct pcpu_chunk *pcpu_create_chunk(void)
  293. {
  294. struct pcpu_chunk *chunk;
  295. struct vm_struct **vms;
  296. chunk = pcpu_alloc_chunk();
  297. if (!chunk)
  298. return NULL;
  299. vms = pcpu_get_vm_areas(pcpu_group_offsets, pcpu_group_sizes,
  300. pcpu_nr_groups, pcpu_atom_size);
  301. if (!vms) {
  302. pcpu_free_chunk(chunk);
  303. return NULL;
  304. }
  305. chunk->data = vms;
  306. chunk->base_addr = vms[0]->addr - pcpu_group_offsets[0];
  307. pcpu_stats_chunk_alloc();
  308. trace_percpu_create_chunk(chunk->base_addr);
  309. return chunk;
  310. }
  311. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk)
  312. {
  313. if (!chunk)
  314. return;
  315. pcpu_stats_chunk_dealloc();
  316. trace_percpu_destroy_chunk(chunk->base_addr);
  317. if (chunk->data)
  318. pcpu_free_vm_areas(chunk->data, pcpu_nr_groups);
  319. pcpu_free_chunk(chunk);
  320. }
  321. static struct page *pcpu_addr_to_page(void *addr)
  322. {
  323. return vmalloc_to_page(addr);
  324. }
  325. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai)
  326. {
  327. /* no extra restriction */
  328. return 0;
  329. }