percpu-vm.c 13 KB

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