percpu.c 24 KB

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