percpu-vm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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_and_bitmap - get temp pages array and bitmap
  21. * @chunk: chunk of interest
  22. * @bitmapp: output parameter for bitmap
  23. * @may_alloc: may allocate the array
  24. *
  25. * Returns pointer to array of pointers to struct page and bitmap,
  26. * both of which can be indexed with pcpu_page_idx(). The returned
  27. * array is cleared to zero and *@bitmapp is copied from
  28. * @chunk->populated. Note that there is only one array and bitmap
  29. * and access exclusion is the caller's responsibility.
  30. *
  31. * CONTEXT:
  32. * pcpu_alloc_mutex and does GFP_KERNEL allocation if @may_alloc.
  33. * Otherwise, don't care.
  34. *
  35. * RETURNS:
  36. * Pointer to temp pages array on success, NULL on failure.
  37. */
  38. static struct page **pcpu_get_pages_and_bitmap(struct pcpu_chunk *chunk,
  39. unsigned long **bitmapp,
  40. bool may_alloc)
  41. {
  42. static struct page **pages;
  43. static unsigned long *bitmap;
  44. size_t pages_size = pcpu_nr_units * pcpu_unit_pages * sizeof(pages[0]);
  45. size_t bitmap_size = BITS_TO_LONGS(pcpu_unit_pages) *
  46. sizeof(unsigned long);
  47. if (!pages || !bitmap) {
  48. if (may_alloc && !pages)
  49. pages = pcpu_mem_zalloc(pages_size);
  50. if (may_alloc && !bitmap)
  51. bitmap = pcpu_mem_zalloc(bitmap_size);
  52. if (!pages || !bitmap)
  53. return NULL;
  54. }
  55. bitmap_copy(bitmap, chunk->populated, pcpu_unit_pages);
  56. *bitmapp = bitmap;
  57. return pages;
  58. }
  59. /**
  60. * pcpu_free_pages - free pages which were allocated for @chunk
  61. * @chunk: chunk pages were allocated for
  62. * @pages: array of pages to be freed, indexed by pcpu_page_idx()
  63. * @populated: populated bitmap
  64. * @page_start: page index of the first page to be freed
  65. * @page_end: page index of the last page to be freed + 1
  66. *
  67. * Free pages [@page_start and @page_end) in @pages for all units.
  68. * The pages were allocated for @chunk.
  69. */
  70. static void pcpu_free_pages(struct pcpu_chunk *chunk,
  71. struct page **pages, unsigned long *populated,
  72. int page_start, int page_end)
  73. {
  74. unsigned int cpu;
  75. int i;
  76. for_each_possible_cpu(cpu) {
  77. for (i = page_start; i < page_end; i++) {
  78. struct page *page = pages[pcpu_page_idx(cpu, i)];
  79. if (page)
  80. __free_page(page);
  81. }
  82. }
  83. }
  84. /**
  85. * pcpu_alloc_pages - allocates pages for @chunk
  86. * @chunk: target chunk
  87. * @pages: array to put the allocated pages into, indexed by pcpu_page_idx()
  88. * @populated: populated bitmap
  89. * @page_start: page index of the first page to be allocated
  90. * @page_end: page index of the last page to be allocated + 1
  91. *
  92. * Allocate pages [@page_start,@page_end) into @pages for all units.
  93. * The allocation is for @chunk. Percpu core doesn't care about the
  94. * content of @pages and will pass it verbatim to pcpu_map_pages().
  95. */
  96. static int pcpu_alloc_pages(struct pcpu_chunk *chunk,
  97. struct page **pages, unsigned long *populated,
  98. int page_start, int page_end)
  99. {
  100. const gfp_t gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  101. unsigned int cpu;
  102. int i;
  103. for_each_possible_cpu(cpu) {
  104. for (i = page_start; i < page_end; i++) {
  105. struct page **pagep = &pages[pcpu_page_idx(cpu, i)];
  106. *pagep = alloc_pages_node(cpu_to_node(cpu), gfp, 0);
  107. if (!*pagep) {
  108. pcpu_free_pages(chunk, pages, populated,
  109. page_start, page_end);
  110. return -ENOMEM;
  111. }
  112. }
  113. }
  114. return 0;
  115. }
  116. /**
  117. * pcpu_pre_unmap_flush - flush cache prior to unmapping
  118. * @chunk: chunk the regions to be flushed belongs to
  119. * @page_start: page index of the first page to be flushed
  120. * @page_end: page index of the last page to be flushed + 1
  121. *
  122. * Pages in [@page_start,@page_end) of @chunk are about to be
  123. * unmapped. Flush cache. As each flushing trial can be very
  124. * expensive, issue flush on the whole region at once rather than
  125. * doing it for each cpu. This could be an overkill but is more
  126. * scalable.
  127. */
  128. static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk,
  129. int page_start, int page_end)
  130. {
  131. flush_cache_vunmap(
  132. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  133. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  134. }
  135. static void __pcpu_unmap_pages(unsigned long addr, int nr_pages)
  136. {
  137. unmap_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT);
  138. }
  139. /**
  140. * pcpu_unmap_pages - unmap pages out of a pcpu_chunk
  141. * @chunk: chunk of interest
  142. * @pages: pages array which can be used to pass information to free
  143. * @populated: populated bitmap
  144. * @page_start: page index of the first page to unmap
  145. * @page_end: page index of the last page to unmap + 1
  146. *
  147. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  148. * Corresponding elements in @pages were cleared by the caller and can
  149. * be used to carry information to pcpu_free_pages() which will be
  150. * called after all unmaps are finished. The caller should call
  151. * proper pre/post flush functions.
  152. */
  153. static void pcpu_unmap_pages(struct pcpu_chunk *chunk,
  154. struct page **pages, unsigned long *populated,
  155. int page_start, int page_end)
  156. {
  157. unsigned int cpu;
  158. int i;
  159. for_each_possible_cpu(cpu) {
  160. for (i = page_start; i < page_end; i++) {
  161. struct page *page;
  162. page = pcpu_chunk_page(chunk, cpu, i);
  163. WARN_ON(!page);
  164. pages[pcpu_page_idx(cpu, i)] = page;
  165. }
  166. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  167. page_end - page_start);
  168. }
  169. bitmap_clear(populated, page_start, page_end - page_start);
  170. }
  171. /**
  172. * pcpu_post_unmap_tlb_flush - flush TLB after unmapping
  173. * @chunk: pcpu_chunk the regions to be flushed belong to
  174. * @page_start: page index of the first page to be flushed
  175. * @page_end: page index of the last page to be flushed + 1
  176. *
  177. * Pages [@page_start,@page_end) of @chunk have been unmapped. Flush
  178. * TLB for the regions. This can be skipped if the area is to be
  179. * returned to vmalloc as vmalloc will handle TLB flushing lazily.
  180. *
  181. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  182. * for the whole region.
  183. */
  184. static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk,
  185. int page_start, int page_end)
  186. {
  187. flush_tlb_kernel_range(
  188. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  189. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  190. }
  191. static int __pcpu_map_pages(unsigned long addr, struct page **pages,
  192. int nr_pages)
  193. {
  194. return map_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT,
  195. PAGE_KERNEL, pages);
  196. }
  197. /**
  198. * pcpu_map_pages - map pages into a pcpu_chunk
  199. * @chunk: chunk of interest
  200. * @pages: pages array containing pages to be mapped
  201. * @populated: populated bitmap
  202. * @page_start: page index of the first page to map
  203. * @page_end: page index of the last page to map + 1
  204. *
  205. * For each cpu, map pages [@page_start,@page_end) into @chunk. The
  206. * caller is responsible for calling pcpu_post_map_flush() after all
  207. * mappings are complete.
  208. *
  209. * This function is responsible for setting corresponding bits in
  210. * @chunk->populated bitmap and whatever is necessary for reverse
  211. * lookup (addr -> chunk).
  212. */
  213. static int pcpu_map_pages(struct pcpu_chunk *chunk,
  214. struct page **pages, unsigned long *populated,
  215. int page_start, int page_end)
  216. {
  217. unsigned int cpu, tcpu;
  218. int i, err;
  219. for_each_possible_cpu(cpu) {
  220. err = __pcpu_map_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  221. &pages[pcpu_page_idx(cpu, page_start)],
  222. page_end - page_start);
  223. if (err < 0)
  224. goto err;
  225. }
  226. /* mapping successful, link chunk and mark populated */
  227. for (i = page_start; i < page_end; i++) {
  228. for_each_possible_cpu(cpu)
  229. pcpu_set_page_chunk(pages[pcpu_page_idx(cpu, i)],
  230. chunk);
  231. __set_bit(i, populated);
  232. }
  233. return 0;
  234. err:
  235. for_each_possible_cpu(tcpu) {
  236. if (tcpu == cpu)
  237. break;
  238. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
  239. page_end - page_start);
  240. }
  241. return err;
  242. }
  243. /**
  244. * pcpu_post_map_flush - flush cache after mapping
  245. * @chunk: pcpu_chunk the regions to be flushed belong to
  246. * @page_start: page index of the first page to be flushed
  247. * @page_end: page index of the last page to be flushed + 1
  248. *
  249. * Pages [@page_start,@page_end) of @chunk have been mapped. Flush
  250. * cache.
  251. *
  252. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  253. * for the whole region.
  254. */
  255. static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
  256. int page_start, int page_end)
  257. {
  258. flush_cache_vmap(
  259. pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
  260. pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
  261. }
  262. /**
  263. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  264. * @chunk: chunk of interest
  265. * @off: offset to the area to populate
  266. * @size: size of the area to populate in bytes
  267. *
  268. * For each cpu, populate and map pages [@page_start,@page_end) into
  269. * @chunk. The area is cleared on return.
  270. *
  271. * CONTEXT:
  272. * pcpu_alloc_mutex, does GFP_KERNEL allocation.
  273. */
  274. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
  275. {
  276. int page_start = PFN_DOWN(off);
  277. int page_end = PFN_UP(off + size);
  278. int free_end = page_start, unmap_end = page_start;
  279. struct page **pages;
  280. unsigned long *populated;
  281. unsigned int cpu;
  282. int rs, re, rc;
  283. /* quick path, check whether all pages are already there */
  284. rs = page_start;
  285. pcpu_next_pop(chunk, &rs, &re, page_end);
  286. if (rs == page_start && re == page_end)
  287. goto clear;
  288. /* need to allocate and map pages, this chunk can't be immutable */
  289. WARN_ON(chunk->immutable);
  290. pages = pcpu_get_pages_and_bitmap(chunk, &populated, true);
  291. if (!pages)
  292. return -ENOMEM;
  293. /* alloc and map */
  294. pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
  295. rc = pcpu_alloc_pages(chunk, pages, populated, rs, re);
  296. if (rc)
  297. goto err_free;
  298. free_end = re;
  299. }
  300. pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
  301. rc = pcpu_map_pages(chunk, pages, populated, rs, re);
  302. if (rc)
  303. goto err_unmap;
  304. unmap_end = re;
  305. }
  306. pcpu_post_map_flush(chunk, page_start, page_end);
  307. /* commit new bitmap */
  308. bitmap_copy(chunk->populated, populated, pcpu_unit_pages);
  309. clear:
  310. for_each_possible_cpu(cpu)
  311. memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
  312. return 0;
  313. err_unmap:
  314. pcpu_pre_unmap_flush(chunk, page_start, unmap_end);
  315. pcpu_for_each_unpop_region(chunk, rs, re, page_start, unmap_end)
  316. pcpu_unmap_pages(chunk, pages, populated, rs, re);
  317. pcpu_post_unmap_tlb_flush(chunk, page_start, unmap_end);
  318. err_free:
  319. pcpu_for_each_unpop_region(chunk, rs, re, page_start, free_end)
  320. pcpu_free_pages(chunk, pages, populated, rs, re);
  321. return rc;
  322. }
  323. /**
  324. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  325. * @chunk: chunk to depopulate
  326. * @off: offset to the area to depopulate
  327. * @size: size of the area to depopulate in bytes
  328. * @flush: whether to flush cache and tlb or not
  329. *
  330. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  331. * from @chunk. If @flush is true, vcache is flushed before unmapping
  332. * and tlb after.
  333. *
  334. * CONTEXT:
  335. * pcpu_alloc_mutex.
  336. */
  337. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
  338. {
  339. int page_start = PFN_DOWN(off);
  340. int page_end = PFN_UP(off + size);
  341. struct page **pages;
  342. unsigned long *populated;
  343. int rs, re;
  344. /* quick path, check whether it's empty already */
  345. rs = page_start;
  346. pcpu_next_unpop(chunk, &rs, &re, page_end);
  347. if (rs == page_start && re == page_end)
  348. return;
  349. /* immutable chunks can't be depopulated */
  350. WARN_ON(chunk->immutable);
  351. /*
  352. * If control reaches here, there must have been at least one
  353. * successful population attempt so the temp pages array must
  354. * be available now.
  355. */
  356. pages = pcpu_get_pages_and_bitmap(chunk, &populated, false);
  357. BUG_ON(!pages);
  358. /* unmap and free */
  359. pcpu_pre_unmap_flush(chunk, page_start, page_end);
  360. pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
  361. pcpu_unmap_pages(chunk, pages, populated, rs, re);
  362. /* no need to flush tlb, vmalloc will handle it lazily */
  363. pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
  364. pcpu_free_pages(chunk, pages, populated, rs, re);
  365. /* commit new bitmap */
  366. bitmap_copy(chunk->populated, populated, pcpu_unit_pages);
  367. }
  368. static struct pcpu_chunk *pcpu_create_chunk(void)
  369. {
  370. struct pcpu_chunk *chunk;
  371. struct vm_struct **vms;
  372. chunk = pcpu_alloc_chunk();
  373. if (!chunk)
  374. return NULL;
  375. vms = pcpu_get_vm_areas(pcpu_group_offsets, pcpu_group_sizes,
  376. pcpu_nr_groups, pcpu_atom_size);
  377. if (!vms) {
  378. pcpu_free_chunk(chunk);
  379. return NULL;
  380. }
  381. chunk->data = vms;
  382. chunk->base_addr = vms[0]->addr - pcpu_group_offsets[0];
  383. return chunk;
  384. }
  385. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk)
  386. {
  387. if (chunk && chunk->data)
  388. pcpu_free_vm_areas(chunk->data, pcpu_nr_groups);
  389. pcpu_free_chunk(chunk);
  390. }
  391. static struct page *pcpu_addr_to_page(void *addr)
  392. {
  393. return vmalloc_to_page(addr);
  394. }
  395. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai)
  396. {
  397. /* no extra restriction */
  398. return 0;
  399. }