percpu-vm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mm/percpu-vm.c - vmalloc area based chunk allocation
  4. *
  5. * Copyright (C) 2010 SUSE Linux Products GmbH
  6. * Copyright (C) 2010 Tejun Heo <[email protected]>
  7. *
  8. * Chunks are mapped into vmalloc areas and populated page by page.
  9. * This is the default chunk allocator.
  10. */
  11. #include "internal.h"
  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. vunmap_range_noflush(addr, 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 vmap_pages_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT),
  176. PAGE_KERNEL, pages, PAGE_SHIFT);
  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. * Caller is required to call pcpu_post_unmap_tlb_flush() if not returning the
  276. * region back to vmalloc() which will lazily flush the tlb.
  277. *
  278. * CONTEXT:
  279. * pcpu_alloc_mutex.
  280. */
  281. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
  282. int page_start, int page_end)
  283. {
  284. struct page **pages;
  285. /*
  286. * If control reaches here, there must have been at least one
  287. * successful population attempt so the temp pages array must
  288. * be available now.
  289. */
  290. pages = pcpu_get_pages();
  291. BUG_ON(!pages);
  292. /* unmap and free */
  293. pcpu_pre_unmap_flush(chunk, page_start, page_end);
  294. pcpu_unmap_pages(chunk, pages, page_start, page_end);
  295. pcpu_free_pages(chunk, pages, page_start, page_end);
  296. }
  297. static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp)
  298. {
  299. struct pcpu_chunk *chunk;
  300. struct vm_struct **vms;
  301. chunk = pcpu_alloc_chunk(gfp);
  302. if (!chunk)
  303. return NULL;
  304. vms = pcpu_get_vm_areas(pcpu_group_offsets, pcpu_group_sizes,
  305. pcpu_nr_groups, pcpu_atom_size);
  306. if (!vms) {
  307. pcpu_free_chunk(chunk);
  308. return NULL;
  309. }
  310. chunk->data = vms;
  311. chunk->base_addr = vms[0]->addr - pcpu_group_offsets[0];
  312. pcpu_stats_chunk_alloc();
  313. trace_percpu_create_chunk(chunk->base_addr);
  314. return chunk;
  315. }
  316. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk)
  317. {
  318. if (!chunk)
  319. return;
  320. pcpu_stats_chunk_dealloc();
  321. trace_percpu_destroy_chunk(chunk->base_addr);
  322. if (chunk->data)
  323. pcpu_free_vm_areas(chunk->data, pcpu_nr_groups);
  324. pcpu_free_chunk(chunk);
  325. }
  326. static struct page *pcpu_addr_to_page(void *addr)
  327. {
  328. return vmalloc_to_page(addr);
  329. }
  330. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai)
  331. {
  332. /* no extra restriction */
  333. return 0;
  334. }
  335. /**
  336. * pcpu_should_reclaim_chunk - determine if a chunk should go into reclaim
  337. * @chunk: chunk of interest
  338. *
  339. * This is the entry point for percpu reclaim. If a chunk qualifies, it is then
  340. * isolated and managed in separate lists at the back of pcpu_slot: sidelined
  341. * and to_depopulate respectively. The to_depopulate list holds chunks slated
  342. * for depopulation. They no longer contribute to pcpu_nr_empty_pop_pages once
  343. * they are on this list. Once depopulated, they are moved onto the sidelined
  344. * list which enables them to be pulled back in for allocation if no other chunk
  345. * can suffice the allocation.
  346. */
  347. static bool pcpu_should_reclaim_chunk(struct pcpu_chunk *chunk)
  348. {
  349. /* do not reclaim either the first chunk or reserved chunk */
  350. if (chunk == pcpu_first_chunk || chunk == pcpu_reserved_chunk)
  351. return false;
  352. /*
  353. * If it is isolated, it may be on the sidelined list so move it back to
  354. * the to_depopulate list. If we hit at least 1/4 pages empty pages AND
  355. * there is no system-wide shortage of empty pages aside from this
  356. * chunk, move it to the to_depopulate list.
  357. */
  358. return ((chunk->isolated && chunk->nr_empty_pop_pages) ||
  359. (pcpu_nr_empty_pop_pages >
  360. (PCPU_EMPTY_POP_PAGES_HIGH + chunk->nr_empty_pop_pages) &&
  361. chunk->nr_empty_pop_pages >= chunk->nr_pages / 4));
  362. }