percpu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * linux/mm/percpu.c - percpu memory allocator
  3. *
  4. * Copyright (C) 2009 SUSE Linux Products GmbH
  5. * Copyright (C) 2009 Tejun Heo <tj@kernel.org>
  6. *
  7. * This file is released under the GPLv2.
  8. *
  9. * This is percpu allocator which can handle both static and dynamic
  10. * areas. Percpu areas are allocated in chunks in vmalloc area. Each
  11. * chunk is consisted of num_possible_cpus() units and the first chunk
  12. * is used for static percpu variables in the kernel image (special
  13. * boot time alloc/init handling necessary as these areas need to be
  14. * brought up before allocation services are running). Unit grows as
  15. * necessary and all units grow or shrink in unison. When a chunk is
  16. * filled up, another chunk is allocated. ie. in vmalloc area
  17. *
  18. * c0 c1 c2
  19. * ------------------- ------------------- ------------
  20. * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u
  21. * ------------------- ...... ------------------- .... ------------
  22. *
  23. * Allocation is done in offset-size areas of single unit space. Ie,
  24. * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0,
  25. * c1:u1, c1:u2 and c1:u3. Percpu access can be done by configuring
  26. * percpu base registers UNIT_SIZE apart.
  27. *
  28. * There are usually many small percpu allocations many of them as
  29. * small as 4 bytes. The allocator organizes chunks into lists
  30. * according to free size and tries to allocate from the fullest one.
  31. * Each chunk keeps the maximum contiguous area size hint which is
  32. * guaranteed to be eqaul to or larger than the maximum contiguous
  33. * area in the chunk. This helps the allocator not to iterate the
  34. * chunk maps unnecessarily.
  35. *
  36. * Allocation state in each chunk is kept using an array of integers
  37. * on chunk->map. A positive value in the map represents a free
  38. * region and negative allocated. Allocation inside a chunk is done
  39. * by scanning this map sequentially and serving the first matching
  40. * entry. This is mostly copied from the percpu_modalloc() allocator.
  41. * Chunks are also linked into a rb tree to ease address to chunk
  42. * mapping during free.
  43. *
  44. * To use this allocator, arch code should do the followings.
  45. *
  46. * - define CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
  47. *
  48. * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
  49. * regular address to percpu pointer and back
  50. *
  51. * - use pcpu_setup_static() during percpu area initialization to
  52. * setup kernel static percpu area
  53. */
  54. #include <linux/bitmap.h>
  55. #include <linux/bootmem.h>
  56. #include <linux/list.h>
  57. #include <linux/mm.h>
  58. #include <linux/module.h>
  59. #include <linux/mutex.h>
  60. #include <linux/percpu.h>
  61. #include <linux/pfn.h>
  62. #include <linux/rbtree.h>
  63. #include <linux/slab.h>
  64. #include <linux/vmalloc.h>
  65. #include <asm/cacheflush.h>
  66. #include <asm/tlbflush.h>
  67. #define PCPU_MIN_UNIT_PAGES_SHIFT 4 /* also max alloc size */
  68. #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
  69. #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
  70. struct pcpu_chunk {
  71. struct list_head list; /* linked to pcpu_slot lists */
  72. struct rb_node rb_node; /* key is chunk->vm->addr */
  73. int free_size; /* free bytes in the chunk */
  74. int contig_hint; /* max contiguous size hint */
  75. struct vm_struct *vm; /* mapped vmalloc region */
  76. int map_used; /* # of map entries used */
  77. int map_alloc; /* # of map entries allocated */
  78. int *map; /* allocation map */
  79. struct page *page[]; /* #cpus * UNIT_PAGES */
  80. };
  81. static int pcpu_unit_pages_shift;
  82. static int pcpu_unit_pages;
  83. static int pcpu_unit_shift;
  84. static int pcpu_unit_size;
  85. static int pcpu_chunk_size;
  86. static int pcpu_nr_slots;
  87. static size_t pcpu_chunk_struct_size;
  88. /* the address of the first chunk which starts with the kernel static area */
  89. void *pcpu_base_addr;
  90. EXPORT_SYMBOL_GPL(pcpu_base_addr);
  91. /* the size of kernel static area */
  92. static int pcpu_static_size;
  93. /*
  94. * One mutex to rule them all.
  95. *
  96. * The following mutex is grabbed in the outermost public alloc/free
  97. * interface functions and released only when the operation is
  98. * complete. As such, every function in this file other than the
  99. * outermost functions are called under pcpu_mutex.
  100. *
  101. * It can easily be switched to use spinlock such that only the area
  102. * allocation and page population commit are protected with it doing
  103. * actual [de]allocation without holding any lock. However, given
  104. * what this allocator does, I think it's better to let them run
  105. * sequentially.
  106. */
  107. static DEFINE_MUTEX(pcpu_mutex);
  108. static struct list_head *pcpu_slot; /* chunk list slots */
  109. static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */
  110. static int pcpu_size_to_slot(int size)
  111. {
  112. int highbit = fls(size); /* size is in bytes */
  113. return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
  114. }
  115. static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
  116. {
  117. if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
  118. return 0;
  119. return pcpu_size_to_slot(chunk->free_size);
  120. }
  121. static int pcpu_page_idx(unsigned int cpu, int page_idx)
  122. {
  123. return (cpu << pcpu_unit_pages_shift) + page_idx;
  124. }
  125. static struct page **pcpu_chunk_pagep(struct pcpu_chunk *chunk,
  126. unsigned int cpu, int page_idx)
  127. {
  128. return &chunk->page[pcpu_page_idx(cpu, page_idx)];
  129. }
  130. static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
  131. unsigned int cpu, int page_idx)
  132. {
  133. return (unsigned long)chunk->vm->addr +
  134. (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
  135. }
  136. static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
  137. int page_idx)
  138. {
  139. return *pcpu_chunk_pagep(chunk, 0, page_idx) != NULL;
  140. }
  141. /**
  142. * pcpu_realloc - versatile realloc
  143. * @p: the current pointer (can be NULL for new allocations)
  144. * @size: the current size in bytes (can be 0 for new allocations)
  145. * @new_size: the wanted new size in bytes (can be 0 for free)
  146. *
  147. * More robust realloc which can be used to allocate, resize or free a
  148. * memory area of arbitrary size. If the needed size goes over
  149. * PAGE_SIZE, kernel VM is used.
  150. *
  151. * RETURNS:
  152. * The new pointer on success, NULL on failure.
  153. */
  154. static void *pcpu_realloc(void *p, size_t size, size_t new_size)
  155. {
  156. void *new;
  157. if (new_size <= PAGE_SIZE)
  158. new = kmalloc(new_size, GFP_KERNEL);
  159. else
  160. new = vmalloc(new_size);
  161. if (new_size && !new)
  162. return NULL;
  163. memcpy(new, p, min(size, new_size));
  164. if (new_size > size)
  165. memset(new + size, 0, new_size - size);
  166. if (size <= PAGE_SIZE)
  167. kfree(p);
  168. else
  169. vfree(p);
  170. return new;
  171. }
  172. /**
  173. * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
  174. * @chunk: chunk of interest
  175. * @oslot: the previous slot it was on
  176. *
  177. * This function is called after an allocation or free changed @chunk.
  178. * New slot according to the changed state is determined and @chunk is
  179. * moved to the slot.
  180. */
  181. static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
  182. {
  183. int nslot = pcpu_chunk_slot(chunk);
  184. if (oslot != nslot) {
  185. if (oslot < nslot)
  186. list_move(&chunk->list, &pcpu_slot[nslot]);
  187. else
  188. list_move_tail(&chunk->list, &pcpu_slot[nslot]);
  189. }
  190. }
  191. static struct rb_node **pcpu_chunk_rb_search(void *addr,
  192. struct rb_node **parentp)
  193. {
  194. struct rb_node **p = &pcpu_addr_root.rb_node;
  195. struct rb_node *parent = NULL;
  196. struct pcpu_chunk *chunk;
  197. while (*p) {
  198. parent = *p;
  199. chunk = rb_entry(parent, struct pcpu_chunk, rb_node);
  200. if (addr < chunk->vm->addr)
  201. p = &(*p)->rb_left;
  202. else if (addr > chunk->vm->addr)
  203. p = &(*p)->rb_right;
  204. else
  205. break;
  206. }
  207. if (parentp)
  208. *parentp = parent;
  209. return p;
  210. }
  211. /**
  212. * pcpu_chunk_addr_search - search for chunk containing specified address
  213. * @addr: address to search for
  214. *
  215. * Look for chunk which might contain @addr. More specifically, it
  216. * searchs for the chunk with the highest start address which isn't
  217. * beyond @addr.
  218. *
  219. * RETURNS:
  220. * The address of the found chunk.
  221. */
  222. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  223. {
  224. struct rb_node *n, *parent;
  225. struct pcpu_chunk *chunk;
  226. n = *pcpu_chunk_rb_search(addr, &parent);
  227. if (!n) {
  228. /* no exactly matching chunk, the parent is the closest */
  229. n = parent;
  230. BUG_ON(!n);
  231. }
  232. chunk = rb_entry(n, struct pcpu_chunk, rb_node);
  233. if (addr < chunk->vm->addr) {
  234. /* the parent was the next one, look for the previous one */
  235. n = rb_prev(n);
  236. BUG_ON(!n);
  237. chunk = rb_entry(n, struct pcpu_chunk, rb_node);
  238. }
  239. return chunk;
  240. }
  241. /**
  242. * pcpu_chunk_addr_insert - insert chunk into address rb tree
  243. * @new: chunk to insert
  244. *
  245. * Insert @new into address rb tree.
  246. */
  247. static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
  248. {
  249. struct rb_node **p, *parent;
  250. p = pcpu_chunk_rb_search(new->vm->addr, &parent);
  251. BUG_ON(*p);
  252. rb_link_node(&new->rb_node, parent, p);
  253. rb_insert_color(&new->rb_node, &pcpu_addr_root);
  254. }
  255. /**
  256. * pcpu_split_block - split a map block
  257. * @chunk: chunk of interest
  258. * @i: index of map block to split
  259. * @head: head size in bytes (can be 0)
  260. * @tail: tail size in bytes (can be 0)
  261. *
  262. * Split the @i'th map block into two or three blocks. If @head is
  263. * non-zero, @head bytes block is inserted before block @i moving it
  264. * to @i+1 and reducing its size by @head bytes.
  265. *
  266. * If @tail is non-zero, the target block, which can be @i or @i+1
  267. * depending on @head, is reduced by @tail bytes and @tail byte block
  268. * is inserted after the target block.
  269. *
  270. * RETURNS:
  271. * 0 on success, -errno on failure.
  272. */
  273. static int pcpu_split_block(struct pcpu_chunk *chunk, int i, int head, int tail)
  274. {
  275. int nr_extra = !!head + !!tail;
  276. int target = chunk->map_used + nr_extra;
  277. /* reallocation required? */
  278. if (chunk->map_alloc < target) {
  279. int new_alloc = chunk->map_alloc;
  280. int *new;
  281. while (new_alloc < target)
  282. new_alloc *= 2;
  283. new = pcpu_realloc(chunk->map,
  284. chunk->map_alloc * sizeof(new[0]),
  285. new_alloc * sizeof(new[0]));
  286. if (!new)
  287. return -ENOMEM;
  288. chunk->map_alloc = new_alloc;
  289. chunk->map = new;
  290. }
  291. /* insert a new subblock */
  292. memmove(&chunk->map[i + nr_extra], &chunk->map[i],
  293. sizeof(chunk->map[0]) * (chunk->map_used - i));
  294. chunk->map_used += nr_extra;
  295. if (head) {
  296. chunk->map[i + 1] = chunk->map[i] - head;
  297. chunk->map[i++] = head;
  298. }
  299. if (tail) {
  300. chunk->map[i++] -= tail;
  301. chunk->map[i] = tail;
  302. }
  303. return 0;
  304. }
  305. /**
  306. * pcpu_alloc_area - allocate area from a pcpu_chunk
  307. * @chunk: chunk of interest
  308. * @size: wanted size in bytes
  309. * @align: wanted align
  310. *
  311. * Try to allocate @size bytes area aligned at @align from @chunk.
  312. * Note that this function only allocates the offset. It doesn't
  313. * populate or map the area.
  314. *
  315. * RETURNS:
  316. * Allocated offset in @chunk on success, -errno on failure.
  317. */
  318. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
  319. {
  320. int oslot = pcpu_chunk_slot(chunk);
  321. int max_contig = 0;
  322. int i, off;
  323. /*
  324. * The static chunk initially doesn't have map attached
  325. * because kmalloc wasn't available during init. Give it one.
  326. */
  327. if (unlikely(!chunk->map)) {
  328. chunk->map = pcpu_realloc(NULL, 0,
  329. PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  330. if (!chunk->map)
  331. return -ENOMEM;
  332. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  333. chunk->map[chunk->map_used++] = -pcpu_static_size;
  334. if (chunk->free_size)
  335. chunk->map[chunk->map_used++] = chunk->free_size;
  336. }
  337. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
  338. bool is_last = i + 1 == chunk->map_used;
  339. int head, tail;
  340. /* extra for alignment requirement */
  341. head = ALIGN(off, align) - off;
  342. BUG_ON(i == 0 && head != 0);
  343. if (chunk->map[i] < 0)
  344. continue;
  345. if (chunk->map[i] < head + size) {
  346. max_contig = max(chunk->map[i], max_contig);
  347. continue;
  348. }
  349. /*
  350. * If head is small or the previous block is free,
  351. * merge'em. Note that 'small' is defined as smaller
  352. * than sizeof(int), which is very small but isn't too
  353. * uncommon for percpu allocations.
  354. */
  355. if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
  356. if (chunk->map[i - 1] > 0)
  357. chunk->map[i - 1] += head;
  358. else {
  359. chunk->map[i - 1] -= head;
  360. chunk->free_size -= head;
  361. }
  362. chunk->map[i] -= head;
  363. off += head;
  364. head = 0;
  365. }
  366. /* if tail is small, just keep it around */
  367. tail = chunk->map[i] - head - size;
  368. if (tail < sizeof(int))
  369. tail = 0;
  370. /* split if warranted */
  371. if (head || tail) {
  372. if (pcpu_split_block(chunk, i, head, tail))
  373. return -ENOMEM;
  374. if (head) {
  375. i++;
  376. off += head;
  377. max_contig = max(chunk->map[i - 1], max_contig);
  378. }
  379. if (tail)
  380. max_contig = max(chunk->map[i + 1], max_contig);
  381. }
  382. /* update hint and mark allocated */
  383. if (is_last)
  384. chunk->contig_hint = max_contig; /* fully scanned */
  385. else
  386. chunk->contig_hint = max(chunk->contig_hint,
  387. max_contig);
  388. chunk->free_size -= chunk->map[i];
  389. chunk->map[i] = -chunk->map[i];
  390. pcpu_chunk_relocate(chunk, oslot);
  391. return off;
  392. }
  393. chunk->contig_hint = max_contig; /* fully scanned */
  394. pcpu_chunk_relocate(chunk, oslot);
  395. /*
  396. * Tell the upper layer that this chunk has no area left.
  397. * Note that this is not an error condition but a notification
  398. * to upper layer that it needs to look at other chunks.
  399. * -ENOSPC is chosen as it isn't used in memory subsystem and
  400. * matches the meaning in a way.
  401. */
  402. return -ENOSPC;
  403. }
  404. /**
  405. * pcpu_free_area - free area to a pcpu_chunk
  406. * @chunk: chunk of interest
  407. * @freeme: offset of area to free
  408. *
  409. * Free area starting from @freeme to @chunk. Note that this function
  410. * only modifies the allocation map. It doesn't depopulate or unmap
  411. * the area.
  412. */
  413. static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
  414. {
  415. int oslot = pcpu_chunk_slot(chunk);
  416. int i, off;
  417. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
  418. if (off == freeme)
  419. break;
  420. BUG_ON(off != freeme);
  421. BUG_ON(chunk->map[i] > 0);
  422. chunk->map[i] = -chunk->map[i];
  423. chunk->free_size += chunk->map[i];
  424. /* merge with previous? */
  425. if (i > 0 && chunk->map[i - 1] >= 0) {
  426. chunk->map[i - 1] += chunk->map[i];
  427. chunk->map_used--;
  428. memmove(&chunk->map[i], &chunk->map[i + 1],
  429. (chunk->map_used - i) * sizeof(chunk->map[0]));
  430. i--;
  431. }
  432. /* merge with next? */
  433. if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
  434. chunk->map[i] += chunk->map[i + 1];
  435. chunk->map_used--;
  436. memmove(&chunk->map[i + 1], &chunk->map[i + 2],
  437. (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
  438. }
  439. chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
  440. pcpu_chunk_relocate(chunk, oslot);
  441. }
  442. /**
  443. * pcpu_unmap - unmap pages out of a pcpu_chunk
  444. * @chunk: chunk of interest
  445. * @page_start: page index of the first page to unmap
  446. * @page_end: page index of the last page to unmap + 1
  447. * @flush: whether to flush cache and tlb or not
  448. *
  449. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  450. * If @flush is true, vcache is flushed before unmapping and tlb
  451. * after.
  452. */
  453. static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
  454. bool flush)
  455. {
  456. unsigned int last = num_possible_cpus() - 1;
  457. unsigned int cpu;
  458. /*
  459. * Each flushing trial can be very expensive, issue flush on
  460. * the whole region at once rather than doing it for each cpu.
  461. * This could be an overkill but is more scalable.
  462. */
  463. if (flush)
  464. flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
  465. pcpu_chunk_addr(chunk, last, page_end));
  466. for_each_possible_cpu(cpu)
  467. unmap_kernel_range_noflush(
  468. pcpu_chunk_addr(chunk, cpu, page_start),
  469. (page_end - page_start) << PAGE_SHIFT);
  470. /* ditto as flush_cache_vunmap() */
  471. if (flush)
  472. flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
  473. pcpu_chunk_addr(chunk, last, page_end));
  474. }
  475. /**
  476. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  477. * @chunk: chunk to depopulate
  478. * @off: offset to the area to depopulate
  479. * @size: size of the area to depopulate in bytes
  480. * @flush: whether to flush cache and tlb or not
  481. *
  482. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  483. * from @chunk. If @flush is true, vcache is flushed before unmapping
  484. * and tlb after.
  485. */
  486. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
  487. bool flush)
  488. {
  489. int page_start = PFN_DOWN(off);
  490. int page_end = PFN_UP(off + size);
  491. int unmap_start = -1;
  492. int uninitialized_var(unmap_end);
  493. unsigned int cpu;
  494. int i;
  495. for (i = page_start; i < page_end; i++) {
  496. for_each_possible_cpu(cpu) {
  497. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  498. if (!*pagep)
  499. continue;
  500. __free_page(*pagep);
  501. /*
  502. * If it's partial depopulation, it might get
  503. * populated or depopulated again. Mark the
  504. * page gone.
  505. */
  506. *pagep = NULL;
  507. unmap_start = unmap_start < 0 ? i : unmap_start;
  508. unmap_end = i + 1;
  509. }
  510. }
  511. if (unmap_start >= 0)
  512. pcpu_unmap(chunk, unmap_start, unmap_end, flush);
  513. }
  514. /**
  515. * pcpu_map - map pages into a pcpu_chunk
  516. * @chunk: chunk of interest
  517. * @page_start: page index of the first page to map
  518. * @page_end: page index of the last page to map + 1
  519. *
  520. * For each cpu, map pages [@page_start,@page_end) into @chunk.
  521. * vcache is flushed afterwards.
  522. */
  523. static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
  524. {
  525. unsigned int last = num_possible_cpus() - 1;
  526. unsigned int cpu;
  527. int err;
  528. for_each_possible_cpu(cpu) {
  529. err = map_kernel_range_noflush(
  530. pcpu_chunk_addr(chunk, cpu, page_start),
  531. (page_end - page_start) << PAGE_SHIFT,
  532. PAGE_KERNEL,
  533. pcpu_chunk_pagep(chunk, cpu, page_start));
  534. if (err < 0)
  535. return err;
  536. }
  537. /* flush at once, please read comments in pcpu_unmap() */
  538. flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
  539. pcpu_chunk_addr(chunk, last, page_end));
  540. return 0;
  541. }
  542. /**
  543. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  544. * @chunk: chunk of interest
  545. * @off: offset to the area to populate
  546. * @size: size of the area to populate in bytes
  547. *
  548. * For each cpu, populate and map pages [@page_start,@page_end) into
  549. * @chunk. The area is cleared on return.
  550. */
  551. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
  552. {
  553. const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  554. int page_start = PFN_DOWN(off);
  555. int page_end = PFN_UP(off + size);
  556. int map_start = -1;
  557. int map_end;
  558. unsigned int cpu;
  559. int i;
  560. for (i = page_start; i < page_end; i++) {
  561. if (pcpu_chunk_page_occupied(chunk, i)) {
  562. if (map_start >= 0) {
  563. if (pcpu_map(chunk, map_start, map_end))
  564. goto err;
  565. map_start = -1;
  566. }
  567. continue;
  568. }
  569. map_start = map_start < 0 ? i : map_start;
  570. map_end = i + 1;
  571. for_each_possible_cpu(cpu) {
  572. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  573. *pagep = alloc_pages_node(cpu_to_node(cpu),
  574. alloc_mask, 0);
  575. if (!*pagep)
  576. goto err;
  577. }
  578. }
  579. if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
  580. goto err;
  581. for_each_possible_cpu(cpu)
  582. memset(chunk->vm->addr + (cpu << pcpu_unit_shift) + off, 0,
  583. size);
  584. return 0;
  585. err:
  586. /* likely under heavy memory pressure, give memory back */
  587. pcpu_depopulate_chunk(chunk, off, size, true);
  588. return -ENOMEM;
  589. }
  590. static void free_pcpu_chunk(struct pcpu_chunk *chunk)
  591. {
  592. if (!chunk)
  593. return;
  594. if (chunk->vm)
  595. free_vm_area(chunk->vm);
  596. pcpu_realloc(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]), 0);
  597. kfree(chunk);
  598. }
  599. static struct pcpu_chunk *alloc_pcpu_chunk(void)
  600. {
  601. struct pcpu_chunk *chunk;
  602. chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
  603. if (!chunk)
  604. return NULL;
  605. chunk->map = pcpu_realloc(NULL, 0,
  606. PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  607. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  608. chunk->map[chunk->map_used++] = pcpu_unit_size;
  609. chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
  610. if (!chunk->vm) {
  611. free_pcpu_chunk(chunk);
  612. return NULL;
  613. }
  614. INIT_LIST_HEAD(&chunk->list);
  615. chunk->free_size = pcpu_unit_size;
  616. chunk->contig_hint = pcpu_unit_size;
  617. return chunk;
  618. }
  619. /**
  620. * __alloc_percpu - allocate percpu area
  621. * @size: size of area to allocate in bytes
  622. * @align: alignment of area (max PAGE_SIZE)
  623. *
  624. * Allocate percpu area of @size bytes aligned at @align. Might
  625. * sleep. Might trigger writeouts.
  626. *
  627. * RETURNS:
  628. * Percpu pointer to the allocated area on success, NULL on failure.
  629. */
  630. void *__alloc_percpu(size_t size, size_t align)
  631. {
  632. void *ptr = NULL;
  633. struct pcpu_chunk *chunk;
  634. int slot, off;
  635. if (unlikely(!size || size > PAGE_SIZE << PCPU_MIN_UNIT_PAGES_SHIFT ||
  636. align > PAGE_SIZE)) {
  637. WARN(true, "illegal size (%zu) or align (%zu) for "
  638. "percpu allocation\n", size, align);
  639. return NULL;
  640. }
  641. mutex_lock(&pcpu_mutex);
  642. /* allocate area */
  643. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  644. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  645. if (size > chunk->contig_hint)
  646. continue;
  647. off = pcpu_alloc_area(chunk, size, align);
  648. if (off >= 0)
  649. goto area_found;
  650. if (off != -ENOSPC)
  651. goto out_unlock;
  652. }
  653. }
  654. /* hmmm... no space left, create a new chunk */
  655. chunk = alloc_pcpu_chunk();
  656. if (!chunk)
  657. goto out_unlock;
  658. pcpu_chunk_relocate(chunk, -1);
  659. pcpu_chunk_addr_insert(chunk);
  660. off = pcpu_alloc_area(chunk, size, align);
  661. if (off < 0)
  662. goto out_unlock;
  663. area_found:
  664. /* populate, map and clear the area */
  665. if (pcpu_populate_chunk(chunk, off, size)) {
  666. pcpu_free_area(chunk, off);
  667. goto out_unlock;
  668. }
  669. ptr = __addr_to_pcpu_ptr(chunk->vm->addr + off);
  670. out_unlock:
  671. mutex_unlock(&pcpu_mutex);
  672. return ptr;
  673. }
  674. EXPORT_SYMBOL_GPL(__alloc_percpu);
  675. static void pcpu_kill_chunk(struct pcpu_chunk *chunk)
  676. {
  677. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
  678. list_del(&chunk->list);
  679. rb_erase(&chunk->rb_node, &pcpu_addr_root);
  680. free_pcpu_chunk(chunk);
  681. }
  682. /**
  683. * free_percpu - free percpu area
  684. * @ptr: pointer to area to free
  685. *
  686. * Free percpu area @ptr. Might sleep.
  687. */
  688. void free_percpu(void *ptr)
  689. {
  690. void *addr = __pcpu_ptr_to_addr(ptr);
  691. struct pcpu_chunk *chunk;
  692. int off;
  693. if (!ptr)
  694. return;
  695. mutex_lock(&pcpu_mutex);
  696. chunk = pcpu_chunk_addr_search(addr);
  697. off = addr - chunk->vm->addr;
  698. pcpu_free_area(chunk, off);
  699. /* the chunk became fully free, kill one if there are other free ones */
  700. if (chunk->free_size == pcpu_unit_size) {
  701. struct pcpu_chunk *pos;
  702. list_for_each_entry(pos,
  703. &pcpu_slot[pcpu_chunk_slot(chunk)], list)
  704. if (pos != chunk) {
  705. pcpu_kill_chunk(pos);
  706. break;
  707. }
  708. }
  709. mutex_unlock(&pcpu_mutex);
  710. }
  711. EXPORT_SYMBOL_GPL(free_percpu);
  712. /**
  713. * pcpu_setup_static - initialize kernel static percpu area
  714. * @populate_pte_fn: callback to allocate pagetable
  715. * @pages: num_possible_cpus() * PFN_UP(cpu_size) pages
  716. * @cpu_size: the size of static percpu area in bytes
  717. *
  718. * Initialize kernel static percpu area. The caller should allocate
  719. * all the necessary pages and pass them in @pages.
  720. * @populate_pte_fn() is called on each page to be used for percpu
  721. * mapping and is responsible for making sure all the necessary page
  722. * tables for the page is allocated.
  723. *
  724. * RETURNS:
  725. * The determined pcpu_unit_size which can be used to initialize
  726. * percpu access.
  727. */
  728. size_t __init pcpu_setup_static(pcpu_populate_pte_fn_t populate_pte_fn,
  729. struct page **pages, size_t cpu_size)
  730. {
  731. static struct vm_struct static_vm;
  732. struct pcpu_chunk *static_chunk;
  733. int nr_cpu_pages = DIV_ROUND_UP(cpu_size, PAGE_SIZE);
  734. unsigned int cpu;
  735. int err, i;
  736. pcpu_unit_pages_shift = max_t(int, PCPU_MIN_UNIT_PAGES_SHIFT,
  737. order_base_2(cpu_size) - PAGE_SHIFT);
  738. pcpu_static_size = cpu_size;
  739. pcpu_unit_pages = 1 << pcpu_unit_pages_shift;
  740. pcpu_unit_shift = PAGE_SHIFT + pcpu_unit_pages_shift;
  741. pcpu_unit_size = 1 << pcpu_unit_shift;
  742. pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
  743. pcpu_nr_slots = pcpu_size_to_slot(pcpu_unit_size) + 1;
  744. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
  745. + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
  746. /* allocate chunk slots */
  747. pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
  748. for (i = 0; i < pcpu_nr_slots; i++)
  749. INIT_LIST_HEAD(&pcpu_slot[i]);
  750. /* init and register vm area */
  751. static_vm.flags = VM_ALLOC;
  752. static_vm.size = pcpu_chunk_size;
  753. vm_area_register_early(&static_vm);
  754. /* init static_chunk */
  755. static_chunk = alloc_bootmem(pcpu_chunk_struct_size);
  756. INIT_LIST_HEAD(&static_chunk->list);
  757. static_chunk->vm = &static_vm;
  758. static_chunk->free_size = pcpu_unit_size - pcpu_static_size;
  759. static_chunk->contig_hint = static_chunk->free_size;
  760. /* assign pages and map them */
  761. for_each_possible_cpu(cpu) {
  762. for (i = 0; i < nr_cpu_pages; i++) {
  763. *pcpu_chunk_pagep(static_chunk, cpu, i) = *pages++;
  764. populate_pte_fn(pcpu_chunk_addr(static_chunk, cpu, i));
  765. }
  766. }
  767. err = pcpu_map(static_chunk, 0, nr_cpu_pages);
  768. if (err)
  769. panic("failed to setup static percpu area, err=%d\n", err);
  770. /* link static_chunk in */
  771. pcpu_chunk_relocate(static_chunk, -1);
  772. pcpu_chunk_addr_insert(static_chunk);
  773. /* we're done */
  774. pcpu_base_addr = (void *)pcpu_chunk_addr(static_chunk, 0, 0);
  775. return pcpu_unit_size;
  776. }