percpu-vm.c 13 KB

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