percpu.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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_first_chunk() during percpu area initialization to
  52. * setup the first chunk containing the 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_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
  68. #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
  69. struct pcpu_chunk {
  70. struct list_head list; /* linked to pcpu_slot lists */
  71. struct rb_node rb_node; /* key is chunk->vm->addr */
  72. int free_size; /* free bytes in the chunk */
  73. int contig_hint; /* max contiguous size hint */
  74. struct vm_struct *vm; /* mapped vmalloc region */
  75. int map_used; /* # of map entries used */
  76. int map_alloc; /* # of map entries allocated */
  77. int *map; /* allocation map */
  78. bool immutable; /* no [de]population allowed */
  79. struct page **page; /* points to page array */
  80. struct page *page_ar[]; /* #cpus * UNIT_PAGES */
  81. };
  82. static int pcpu_unit_pages __read_mostly;
  83. static int pcpu_unit_size __read_mostly;
  84. static int pcpu_chunk_size __read_mostly;
  85. static int pcpu_nr_slots __read_mostly;
  86. static size_t pcpu_chunk_struct_size __read_mostly;
  87. /* the address of the first chunk which starts with the kernel static area */
  88. void *pcpu_base_addr __read_mostly;
  89. EXPORT_SYMBOL_GPL(pcpu_base_addr);
  90. /* optional reserved chunk, only accessible for reserved allocations */
  91. static struct pcpu_chunk *pcpu_reserved_chunk;
  92. /* offset limit of the reserved chunk */
  93. static int pcpu_reserved_chunk_limit;
  94. /*
  95. * One mutex to rule them all.
  96. *
  97. * The following mutex is grabbed in the outermost public alloc/free
  98. * interface functions and released only when the operation is
  99. * complete. As such, every function in this file other than the
  100. * outermost functions are called under pcpu_mutex.
  101. *
  102. * It can easily be switched to use spinlock such that only the area
  103. * allocation and page population commit are protected with it doing
  104. * actual [de]allocation without holding any lock. However, given
  105. * what this allocator does, I think it's better to let them run
  106. * sequentially.
  107. */
  108. static DEFINE_MUTEX(pcpu_mutex);
  109. static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
  110. static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */
  111. static int __pcpu_size_to_slot(int size)
  112. {
  113. int highbit = fls(size); /* size is in bytes */
  114. return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
  115. }
  116. static int pcpu_size_to_slot(int size)
  117. {
  118. if (size == pcpu_unit_size)
  119. return pcpu_nr_slots - 1;
  120. return __pcpu_size_to_slot(size);
  121. }
  122. static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
  123. {
  124. if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
  125. return 0;
  126. return pcpu_size_to_slot(chunk->free_size);
  127. }
  128. static int pcpu_page_idx(unsigned int cpu, int page_idx)
  129. {
  130. return cpu * pcpu_unit_pages + page_idx;
  131. }
  132. static struct page **pcpu_chunk_pagep(struct pcpu_chunk *chunk,
  133. unsigned int cpu, int page_idx)
  134. {
  135. return &chunk->page[pcpu_page_idx(cpu, page_idx)];
  136. }
  137. static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
  138. unsigned int cpu, int page_idx)
  139. {
  140. return (unsigned long)chunk->vm->addr +
  141. (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
  142. }
  143. static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
  144. int page_idx)
  145. {
  146. return *pcpu_chunk_pagep(chunk, 0, page_idx) != NULL;
  147. }
  148. /**
  149. * pcpu_mem_alloc - allocate memory
  150. * @size: bytes to allocate
  151. *
  152. * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
  153. * kzalloc() is used; otherwise, vmalloc() is used. The returned
  154. * memory is always zeroed.
  155. *
  156. * RETURNS:
  157. * Pointer to the allocated area on success, NULL on failure.
  158. */
  159. static void *pcpu_mem_alloc(size_t size)
  160. {
  161. if (size <= PAGE_SIZE)
  162. return kzalloc(size, GFP_KERNEL);
  163. else {
  164. void *ptr = vmalloc(size);
  165. if (ptr)
  166. memset(ptr, 0, size);
  167. return ptr;
  168. }
  169. }
  170. /**
  171. * pcpu_mem_free - free memory
  172. * @ptr: memory to free
  173. * @size: size of the area
  174. *
  175. * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc().
  176. */
  177. static void pcpu_mem_free(void *ptr, size_t size)
  178. {
  179. if (size <= PAGE_SIZE)
  180. kfree(ptr);
  181. else
  182. vfree(ptr);
  183. }
  184. /**
  185. * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
  186. * @chunk: chunk of interest
  187. * @oslot: the previous slot it was on
  188. *
  189. * This function is called after an allocation or free changed @chunk.
  190. * New slot according to the changed state is determined and @chunk is
  191. * moved to the slot. Note that the reserved chunk is never put on
  192. * chunk slots.
  193. */
  194. static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
  195. {
  196. int nslot = pcpu_chunk_slot(chunk);
  197. if (chunk != pcpu_reserved_chunk && oslot != nslot) {
  198. if (oslot < nslot)
  199. list_move(&chunk->list, &pcpu_slot[nslot]);
  200. else
  201. list_move_tail(&chunk->list, &pcpu_slot[nslot]);
  202. }
  203. }
  204. static struct rb_node **pcpu_chunk_rb_search(void *addr,
  205. struct rb_node **parentp)
  206. {
  207. struct rb_node **p = &pcpu_addr_root.rb_node;
  208. struct rb_node *parent = NULL;
  209. struct pcpu_chunk *chunk;
  210. while (*p) {
  211. parent = *p;
  212. chunk = rb_entry(parent, struct pcpu_chunk, rb_node);
  213. if (addr < chunk->vm->addr)
  214. p = &(*p)->rb_left;
  215. else if (addr > chunk->vm->addr)
  216. p = &(*p)->rb_right;
  217. else
  218. break;
  219. }
  220. if (parentp)
  221. *parentp = parent;
  222. return p;
  223. }
  224. /**
  225. * pcpu_chunk_addr_search - search for chunk containing specified address
  226. * @addr: address to search for
  227. *
  228. * Look for chunk which might contain @addr. More specifically, it
  229. * searchs for the chunk with the highest start address which isn't
  230. * beyond @addr.
  231. *
  232. * RETURNS:
  233. * The address of the found chunk.
  234. */
  235. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  236. {
  237. struct rb_node *n, *parent;
  238. struct pcpu_chunk *chunk;
  239. /* is it in the reserved chunk? */
  240. if (pcpu_reserved_chunk) {
  241. void *start = pcpu_reserved_chunk->vm->addr;
  242. if (addr >= start && addr < start + pcpu_reserved_chunk_limit)
  243. return pcpu_reserved_chunk;
  244. }
  245. /* nah... search the regular ones */
  246. n = *pcpu_chunk_rb_search(addr, &parent);
  247. if (!n) {
  248. /* no exactly matching chunk, the parent is the closest */
  249. n = parent;
  250. BUG_ON(!n);
  251. }
  252. chunk = rb_entry(n, struct pcpu_chunk, rb_node);
  253. if (addr < chunk->vm->addr) {
  254. /* the parent was the next one, look for the previous one */
  255. n = rb_prev(n);
  256. BUG_ON(!n);
  257. chunk = rb_entry(n, struct pcpu_chunk, rb_node);
  258. }
  259. return chunk;
  260. }
  261. /**
  262. * pcpu_chunk_addr_insert - insert chunk into address rb tree
  263. * @new: chunk to insert
  264. *
  265. * Insert @new into address rb tree.
  266. */
  267. static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
  268. {
  269. struct rb_node **p, *parent;
  270. p = pcpu_chunk_rb_search(new->vm->addr, &parent);
  271. BUG_ON(*p);
  272. rb_link_node(&new->rb_node, parent, p);
  273. rb_insert_color(&new->rb_node, &pcpu_addr_root);
  274. }
  275. /**
  276. * pcpu_split_block - split a map block
  277. * @chunk: chunk of interest
  278. * @i: index of map block to split
  279. * @head: head size in bytes (can be 0)
  280. * @tail: tail size in bytes (can be 0)
  281. *
  282. * Split the @i'th map block into two or three blocks. If @head is
  283. * non-zero, @head bytes block is inserted before block @i moving it
  284. * to @i+1 and reducing its size by @head bytes.
  285. *
  286. * If @tail is non-zero, the target block, which can be @i or @i+1
  287. * depending on @head, is reduced by @tail bytes and @tail byte block
  288. * is inserted after the target block.
  289. *
  290. * RETURNS:
  291. * 0 on success, -errno on failure.
  292. */
  293. static int pcpu_split_block(struct pcpu_chunk *chunk, int i, int head, int tail)
  294. {
  295. int nr_extra = !!head + !!tail;
  296. int target = chunk->map_used + nr_extra;
  297. /* reallocation required? */
  298. if (chunk->map_alloc < target) {
  299. int new_alloc;
  300. int *new;
  301. size_t size;
  302. new_alloc = PCPU_DFL_MAP_ALLOC;
  303. while (new_alloc < target)
  304. new_alloc *= 2;
  305. new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
  306. if (!new)
  307. return -ENOMEM;
  308. size = chunk->map_alloc * sizeof(chunk->map[0]);
  309. memcpy(new, chunk->map, size);
  310. /*
  311. * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the
  312. * chunk is one of the first chunks and still using
  313. * static map.
  314. */
  315. if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
  316. pcpu_mem_free(chunk->map, size);
  317. chunk->map_alloc = new_alloc;
  318. chunk->map = new;
  319. }
  320. /* insert a new subblock */
  321. memmove(&chunk->map[i + nr_extra], &chunk->map[i],
  322. sizeof(chunk->map[0]) * (chunk->map_used - i));
  323. chunk->map_used += nr_extra;
  324. if (head) {
  325. chunk->map[i + 1] = chunk->map[i] - head;
  326. chunk->map[i++] = head;
  327. }
  328. if (tail) {
  329. chunk->map[i++] -= tail;
  330. chunk->map[i] = tail;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * pcpu_alloc_area - allocate area from a pcpu_chunk
  336. * @chunk: chunk of interest
  337. * @size: wanted size in bytes
  338. * @align: wanted align
  339. *
  340. * Try to allocate @size bytes area aligned at @align from @chunk.
  341. * Note that this function only allocates the offset. It doesn't
  342. * populate or map the area.
  343. *
  344. * RETURNS:
  345. * Allocated offset in @chunk on success, -errno on failure.
  346. */
  347. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
  348. {
  349. int oslot = pcpu_chunk_slot(chunk);
  350. int max_contig = 0;
  351. int i, off;
  352. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
  353. bool is_last = i + 1 == chunk->map_used;
  354. int head, tail;
  355. /* extra for alignment requirement */
  356. head = ALIGN(off, align) - off;
  357. BUG_ON(i == 0 && head != 0);
  358. if (chunk->map[i] < 0)
  359. continue;
  360. if (chunk->map[i] < head + size) {
  361. max_contig = max(chunk->map[i], max_contig);
  362. continue;
  363. }
  364. /*
  365. * If head is small or the previous block is free,
  366. * merge'em. Note that 'small' is defined as smaller
  367. * than sizeof(int), which is very small but isn't too
  368. * uncommon for percpu allocations.
  369. */
  370. if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
  371. if (chunk->map[i - 1] > 0)
  372. chunk->map[i - 1] += head;
  373. else {
  374. chunk->map[i - 1] -= head;
  375. chunk->free_size -= head;
  376. }
  377. chunk->map[i] -= head;
  378. off += head;
  379. head = 0;
  380. }
  381. /* if tail is small, just keep it around */
  382. tail = chunk->map[i] - head - size;
  383. if (tail < sizeof(int))
  384. tail = 0;
  385. /* split if warranted */
  386. if (head || tail) {
  387. if (pcpu_split_block(chunk, i, head, tail))
  388. return -ENOMEM;
  389. if (head) {
  390. i++;
  391. off += head;
  392. max_contig = max(chunk->map[i - 1], max_contig);
  393. }
  394. if (tail)
  395. max_contig = max(chunk->map[i + 1], max_contig);
  396. }
  397. /* update hint and mark allocated */
  398. if (is_last)
  399. chunk->contig_hint = max_contig; /* fully scanned */
  400. else
  401. chunk->contig_hint = max(chunk->contig_hint,
  402. max_contig);
  403. chunk->free_size -= chunk->map[i];
  404. chunk->map[i] = -chunk->map[i];
  405. pcpu_chunk_relocate(chunk, oslot);
  406. return off;
  407. }
  408. chunk->contig_hint = max_contig; /* fully scanned */
  409. pcpu_chunk_relocate(chunk, oslot);
  410. /*
  411. * Tell the upper layer that this chunk has no area left.
  412. * Note that this is not an error condition but a notification
  413. * to upper layer that it needs to look at other chunks.
  414. * -ENOSPC is chosen as it isn't used in memory subsystem and
  415. * matches the meaning in a way.
  416. */
  417. return -ENOSPC;
  418. }
  419. /**
  420. * pcpu_free_area - free area to a pcpu_chunk
  421. * @chunk: chunk of interest
  422. * @freeme: offset of area to free
  423. *
  424. * Free area starting from @freeme to @chunk. Note that this function
  425. * only modifies the allocation map. It doesn't depopulate or unmap
  426. * the area.
  427. */
  428. static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
  429. {
  430. int oslot = pcpu_chunk_slot(chunk);
  431. int i, off;
  432. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
  433. if (off == freeme)
  434. break;
  435. BUG_ON(off != freeme);
  436. BUG_ON(chunk->map[i] > 0);
  437. chunk->map[i] = -chunk->map[i];
  438. chunk->free_size += chunk->map[i];
  439. /* merge with previous? */
  440. if (i > 0 && chunk->map[i - 1] >= 0) {
  441. chunk->map[i - 1] += chunk->map[i];
  442. chunk->map_used--;
  443. memmove(&chunk->map[i], &chunk->map[i + 1],
  444. (chunk->map_used - i) * sizeof(chunk->map[0]));
  445. i--;
  446. }
  447. /* merge with next? */
  448. if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
  449. chunk->map[i] += chunk->map[i + 1];
  450. chunk->map_used--;
  451. memmove(&chunk->map[i + 1], &chunk->map[i + 2],
  452. (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
  453. }
  454. chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
  455. pcpu_chunk_relocate(chunk, oslot);
  456. }
  457. /**
  458. * pcpu_unmap - unmap pages out of a pcpu_chunk
  459. * @chunk: chunk of interest
  460. * @page_start: page index of the first page to unmap
  461. * @page_end: page index of the last page to unmap + 1
  462. * @flush: whether to flush cache and tlb or not
  463. *
  464. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  465. * If @flush is true, vcache is flushed before unmapping and tlb
  466. * after.
  467. */
  468. static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
  469. bool flush)
  470. {
  471. unsigned int last = num_possible_cpus() - 1;
  472. unsigned int cpu;
  473. /* unmap must not be done on immutable chunk */
  474. WARN_ON(chunk->immutable);
  475. /*
  476. * Each flushing trial can be very expensive, issue flush on
  477. * the whole region at once rather than doing it for each cpu.
  478. * This could be an overkill but is more scalable.
  479. */
  480. if (flush)
  481. flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
  482. pcpu_chunk_addr(chunk, last, page_end));
  483. for_each_possible_cpu(cpu)
  484. unmap_kernel_range_noflush(
  485. pcpu_chunk_addr(chunk, cpu, page_start),
  486. (page_end - page_start) << PAGE_SHIFT);
  487. /* ditto as flush_cache_vunmap() */
  488. if (flush)
  489. flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
  490. pcpu_chunk_addr(chunk, last, page_end));
  491. }
  492. /**
  493. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  494. * @chunk: chunk to depopulate
  495. * @off: offset to the area to depopulate
  496. * @size: size of the area to depopulate in bytes
  497. * @flush: whether to flush cache and tlb or not
  498. *
  499. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  500. * from @chunk. If @flush is true, vcache is flushed before unmapping
  501. * and tlb after.
  502. */
  503. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
  504. bool flush)
  505. {
  506. int page_start = PFN_DOWN(off);
  507. int page_end = PFN_UP(off + size);
  508. int unmap_start = -1;
  509. int uninitialized_var(unmap_end);
  510. unsigned int cpu;
  511. int i;
  512. for (i = page_start; i < page_end; i++) {
  513. for_each_possible_cpu(cpu) {
  514. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  515. if (!*pagep)
  516. continue;
  517. __free_page(*pagep);
  518. /*
  519. * If it's partial depopulation, it might get
  520. * populated or depopulated again. Mark the
  521. * page gone.
  522. */
  523. *pagep = NULL;
  524. unmap_start = unmap_start < 0 ? i : unmap_start;
  525. unmap_end = i + 1;
  526. }
  527. }
  528. if (unmap_start >= 0)
  529. pcpu_unmap(chunk, unmap_start, unmap_end, flush);
  530. }
  531. /**
  532. * pcpu_map - map pages into a pcpu_chunk
  533. * @chunk: chunk of interest
  534. * @page_start: page index of the first page to map
  535. * @page_end: page index of the last page to map + 1
  536. *
  537. * For each cpu, map pages [@page_start,@page_end) into @chunk.
  538. * vcache is flushed afterwards.
  539. */
  540. static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
  541. {
  542. unsigned int last = num_possible_cpus() - 1;
  543. unsigned int cpu;
  544. int err;
  545. /* map must not be done on immutable chunk */
  546. WARN_ON(chunk->immutable);
  547. for_each_possible_cpu(cpu) {
  548. err = map_kernel_range_noflush(
  549. pcpu_chunk_addr(chunk, cpu, page_start),
  550. (page_end - page_start) << PAGE_SHIFT,
  551. PAGE_KERNEL,
  552. pcpu_chunk_pagep(chunk, cpu, page_start));
  553. if (err < 0)
  554. return err;
  555. }
  556. /* flush at once, please read comments in pcpu_unmap() */
  557. flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
  558. pcpu_chunk_addr(chunk, last, page_end));
  559. return 0;
  560. }
  561. /**
  562. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  563. * @chunk: chunk of interest
  564. * @off: offset to the area to populate
  565. * @size: size of the area to populate in bytes
  566. *
  567. * For each cpu, populate and map pages [@page_start,@page_end) into
  568. * @chunk. The area is cleared on return.
  569. */
  570. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
  571. {
  572. const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  573. int page_start = PFN_DOWN(off);
  574. int page_end = PFN_UP(off + size);
  575. int map_start = -1;
  576. int uninitialized_var(map_end);
  577. unsigned int cpu;
  578. int i;
  579. for (i = page_start; i < page_end; i++) {
  580. if (pcpu_chunk_page_occupied(chunk, i)) {
  581. if (map_start >= 0) {
  582. if (pcpu_map(chunk, map_start, map_end))
  583. goto err;
  584. map_start = -1;
  585. }
  586. continue;
  587. }
  588. map_start = map_start < 0 ? i : map_start;
  589. map_end = i + 1;
  590. for_each_possible_cpu(cpu) {
  591. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  592. *pagep = alloc_pages_node(cpu_to_node(cpu),
  593. alloc_mask, 0);
  594. if (!*pagep)
  595. goto err;
  596. }
  597. }
  598. if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
  599. goto err;
  600. for_each_possible_cpu(cpu)
  601. memset(chunk->vm->addr + cpu * pcpu_unit_size + off, 0,
  602. size);
  603. return 0;
  604. err:
  605. /* likely under heavy memory pressure, give memory back */
  606. pcpu_depopulate_chunk(chunk, off, size, true);
  607. return -ENOMEM;
  608. }
  609. static void free_pcpu_chunk(struct pcpu_chunk *chunk)
  610. {
  611. if (!chunk)
  612. return;
  613. if (chunk->vm)
  614. free_vm_area(chunk->vm);
  615. pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
  616. kfree(chunk);
  617. }
  618. static struct pcpu_chunk *alloc_pcpu_chunk(void)
  619. {
  620. struct pcpu_chunk *chunk;
  621. chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
  622. if (!chunk)
  623. return NULL;
  624. chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  625. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  626. chunk->map[chunk->map_used++] = pcpu_unit_size;
  627. chunk->page = chunk->page_ar;
  628. chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
  629. if (!chunk->vm) {
  630. free_pcpu_chunk(chunk);
  631. return NULL;
  632. }
  633. INIT_LIST_HEAD(&chunk->list);
  634. chunk->free_size = pcpu_unit_size;
  635. chunk->contig_hint = pcpu_unit_size;
  636. return chunk;
  637. }
  638. /**
  639. * pcpu_alloc - the percpu allocator
  640. * @size: size of area to allocate in bytes
  641. * @align: alignment of area (max PAGE_SIZE)
  642. * @reserved: allocate from the reserved chunk if available
  643. *
  644. * Allocate percpu area of @size bytes aligned at @align. Might
  645. * sleep. Might trigger writeouts.
  646. *
  647. * RETURNS:
  648. * Percpu pointer to the allocated area on success, NULL on failure.
  649. */
  650. static void *pcpu_alloc(size_t size, size_t align, bool reserved)
  651. {
  652. void *ptr = NULL;
  653. struct pcpu_chunk *chunk;
  654. int slot, off;
  655. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
  656. WARN(true, "illegal size (%zu) or align (%zu) for "
  657. "percpu allocation\n", size, align);
  658. return NULL;
  659. }
  660. mutex_lock(&pcpu_mutex);
  661. /* serve reserved allocations from the reserved chunk if available */
  662. if (reserved && pcpu_reserved_chunk) {
  663. chunk = pcpu_reserved_chunk;
  664. if (size > chunk->contig_hint)
  665. goto out_unlock;
  666. off = pcpu_alloc_area(chunk, size, align);
  667. if (off >= 0)
  668. goto area_found;
  669. goto out_unlock;
  670. }
  671. /* search through normal chunks */
  672. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  673. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  674. if (size > chunk->contig_hint)
  675. continue;
  676. off = pcpu_alloc_area(chunk, size, align);
  677. if (off >= 0)
  678. goto area_found;
  679. if (off != -ENOSPC)
  680. goto out_unlock;
  681. }
  682. }
  683. /* hmmm... no space left, create a new chunk */
  684. chunk = alloc_pcpu_chunk();
  685. if (!chunk)
  686. goto out_unlock;
  687. pcpu_chunk_relocate(chunk, -1);
  688. pcpu_chunk_addr_insert(chunk);
  689. off = pcpu_alloc_area(chunk, size, align);
  690. if (off < 0)
  691. goto out_unlock;
  692. area_found:
  693. /* populate, map and clear the area */
  694. if (pcpu_populate_chunk(chunk, off, size)) {
  695. pcpu_free_area(chunk, off);
  696. goto out_unlock;
  697. }
  698. ptr = __addr_to_pcpu_ptr(chunk->vm->addr + off);
  699. out_unlock:
  700. mutex_unlock(&pcpu_mutex);
  701. return ptr;
  702. }
  703. /**
  704. * __alloc_percpu - allocate dynamic percpu area
  705. * @size: size of area to allocate in bytes
  706. * @align: alignment of area (max PAGE_SIZE)
  707. *
  708. * Allocate percpu area of @size bytes aligned at @align. Might
  709. * sleep. Might trigger writeouts.
  710. *
  711. * RETURNS:
  712. * Percpu pointer to the allocated area on success, NULL on failure.
  713. */
  714. void *__alloc_percpu(size_t size, size_t align)
  715. {
  716. return pcpu_alloc(size, align, false);
  717. }
  718. EXPORT_SYMBOL_GPL(__alloc_percpu);
  719. /**
  720. * __alloc_reserved_percpu - allocate reserved percpu area
  721. * @size: size of area to allocate in bytes
  722. * @align: alignment of area (max PAGE_SIZE)
  723. *
  724. * Allocate percpu area of @size bytes aligned at @align from reserved
  725. * percpu area if arch has set it up; otherwise, allocation is served
  726. * from the same dynamic area. Might sleep. Might trigger writeouts.
  727. *
  728. * RETURNS:
  729. * Percpu pointer to the allocated area on success, NULL on failure.
  730. */
  731. void *__alloc_reserved_percpu(size_t size, size_t align)
  732. {
  733. return pcpu_alloc(size, align, true);
  734. }
  735. static void pcpu_kill_chunk(struct pcpu_chunk *chunk)
  736. {
  737. WARN_ON(chunk->immutable);
  738. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
  739. list_del(&chunk->list);
  740. rb_erase(&chunk->rb_node, &pcpu_addr_root);
  741. free_pcpu_chunk(chunk);
  742. }
  743. /**
  744. * free_percpu - free percpu area
  745. * @ptr: pointer to area to free
  746. *
  747. * Free percpu area @ptr. Might sleep.
  748. */
  749. void free_percpu(void *ptr)
  750. {
  751. void *addr = __pcpu_ptr_to_addr(ptr);
  752. struct pcpu_chunk *chunk;
  753. int off;
  754. if (!ptr)
  755. return;
  756. mutex_lock(&pcpu_mutex);
  757. chunk = pcpu_chunk_addr_search(addr);
  758. off = addr - chunk->vm->addr;
  759. pcpu_free_area(chunk, off);
  760. /* the chunk became fully free, kill one if there are other free ones */
  761. if (chunk->free_size == pcpu_unit_size) {
  762. struct pcpu_chunk *pos;
  763. list_for_each_entry(pos,
  764. &pcpu_slot[pcpu_chunk_slot(chunk)], list)
  765. if (pos != chunk) {
  766. pcpu_kill_chunk(pos);
  767. break;
  768. }
  769. }
  770. mutex_unlock(&pcpu_mutex);
  771. }
  772. EXPORT_SYMBOL_GPL(free_percpu);
  773. /**
  774. * pcpu_setup_first_chunk - initialize the first percpu chunk
  775. * @get_page_fn: callback to fetch page pointer
  776. * @static_size: the size of static percpu area in bytes
  777. * @reserved_size: the size of reserved percpu area in bytes
  778. * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
  779. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  780. * @base_addr: mapped address, NULL for auto
  781. * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
  782. *
  783. * Initialize the first percpu chunk which contains the kernel static
  784. * perpcu area. This function is to be called from arch percpu area
  785. * setup path. The first two parameters are mandatory. The rest are
  786. * optional.
  787. *
  788. * @get_page_fn() should return pointer to percpu page given cpu
  789. * number and page number. It should at least return enough pages to
  790. * cover the static area. The returned pages for static area should
  791. * have been initialized with valid data. If @unit_size is specified,
  792. * it can also return pages after the static area. NULL return
  793. * indicates end of pages for the cpu. Note that @get_page_fn() must
  794. * return the same number of pages for all cpus.
  795. *
  796. * @reserved_size, if non-zero, specifies the amount of bytes to
  797. * reserve after the static area in the first chunk. This reserves
  798. * the first chunk such that it's available only through reserved
  799. * percpu allocation. This is primarily used to serve module percpu
  800. * static areas on architectures where the addressing model has
  801. * limited offset range for symbol relocations to guarantee module
  802. * percpu symbols fall inside the relocatable range.
  803. *
  804. * @unit_size, if non-negative, specifies unit size and must be
  805. * aligned to PAGE_SIZE and equal to or larger than @static_size +
  806. * @reserved_size + @dyn_size.
  807. *
  808. * @dyn_size, if non-negative, limits the number of bytes available
  809. * for dynamic allocation in the first chunk. Specifying non-negative
  810. * value make percpu leave alone the area beyond @static_size +
  811. * @reserved_size + @dyn_size.
  812. *
  813. * Non-null @base_addr means that the caller already allocated virtual
  814. * region for the first chunk and mapped it. percpu must not mess
  815. * with the chunk. Note that @base_addr with 0 @unit_size or non-NULL
  816. * @populate_pte_fn doesn't make any sense.
  817. *
  818. * @populate_pte_fn is used to populate the pagetable. NULL means the
  819. * caller already populated the pagetable.
  820. *
  821. * If the first chunk ends up with both reserved and dynamic areas, it
  822. * is served by two chunks - one to serve the core static and reserved
  823. * areas and the other for the dynamic area. They share the same vm
  824. * and page map but uses different area allocation map to stay away
  825. * from each other. The latter chunk is circulated in the chunk slots
  826. * and available for dynamic allocation like any other chunks.
  827. *
  828. * RETURNS:
  829. * The determined pcpu_unit_size which can be used to initialize
  830. * percpu access.
  831. */
  832. size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
  833. size_t static_size, size_t reserved_size,
  834. ssize_t unit_size, ssize_t dyn_size,
  835. void *base_addr,
  836. pcpu_populate_pte_fn_t populate_pte_fn)
  837. {
  838. static struct vm_struct first_vm;
  839. static int smap[2], dmap[2];
  840. struct pcpu_chunk *schunk, *dchunk = NULL;
  841. unsigned int cpu;
  842. int nr_pages;
  843. int err, i;
  844. /* santiy checks */
  845. BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
  846. ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
  847. BUG_ON(!static_size);
  848. if (unit_size >= 0) {
  849. BUG_ON(unit_size < static_size + reserved_size +
  850. (dyn_size >= 0 ? dyn_size : 0));
  851. BUG_ON(unit_size & ~PAGE_MASK);
  852. } else {
  853. BUG_ON(dyn_size >= 0);
  854. BUG_ON(base_addr);
  855. }
  856. BUG_ON(base_addr && populate_pte_fn);
  857. if (unit_size >= 0)
  858. pcpu_unit_pages = unit_size >> PAGE_SHIFT;
  859. else
  860. pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
  861. PFN_UP(static_size + reserved_size));
  862. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  863. pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
  864. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
  865. + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
  866. if (dyn_size < 0)
  867. dyn_size = pcpu_unit_size - static_size - reserved_size;
  868. /*
  869. * Allocate chunk slots. The additional last slot is for
  870. * empty chunks.
  871. */
  872. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  873. pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
  874. for (i = 0; i < pcpu_nr_slots; i++)
  875. INIT_LIST_HEAD(&pcpu_slot[i]);
  876. /*
  877. * Initialize static chunk. If reserved_size is zero, the
  878. * static chunk covers static area + dynamic allocation area
  879. * in the first chunk. If reserved_size is not zero, it
  880. * covers static area + reserved area (mostly used for module
  881. * static percpu allocation).
  882. */
  883. schunk = alloc_bootmem(pcpu_chunk_struct_size);
  884. INIT_LIST_HEAD(&schunk->list);
  885. schunk->vm = &first_vm;
  886. schunk->map = smap;
  887. schunk->map_alloc = ARRAY_SIZE(smap);
  888. schunk->page = schunk->page_ar;
  889. if (reserved_size) {
  890. schunk->free_size = reserved_size;
  891. pcpu_reserved_chunk = schunk; /* not for dynamic alloc */
  892. } else {
  893. schunk->free_size = dyn_size;
  894. dyn_size = 0; /* dynamic area covered */
  895. }
  896. schunk->contig_hint = schunk->free_size;
  897. schunk->map[schunk->map_used++] = -static_size;
  898. if (schunk->free_size)
  899. schunk->map[schunk->map_used++] = schunk->free_size;
  900. pcpu_reserved_chunk_limit = static_size + schunk->free_size;
  901. /* init dynamic chunk if necessary */
  902. if (dyn_size) {
  903. dchunk = alloc_bootmem(sizeof(struct pcpu_chunk));
  904. INIT_LIST_HEAD(&dchunk->list);
  905. dchunk->vm = &first_vm;
  906. dchunk->map = dmap;
  907. dchunk->map_alloc = ARRAY_SIZE(dmap);
  908. dchunk->page = schunk->page_ar; /* share page map with schunk */
  909. dchunk->contig_hint = dchunk->free_size = dyn_size;
  910. dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
  911. dchunk->map[dchunk->map_used++] = dchunk->free_size;
  912. }
  913. /* allocate vm address */
  914. first_vm.flags = VM_ALLOC;
  915. first_vm.size = pcpu_chunk_size;
  916. if (!base_addr)
  917. vm_area_register_early(&first_vm, PAGE_SIZE);
  918. else {
  919. /*
  920. * Pages already mapped. No need to remap into
  921. * vmalloc area. In this case the first chunks can't
  922. * be mapped or unmapped by percpu and are marked
  923. * immutable.
  924. */
  925. first_vm.addr = base_addr;
  926. schunk->immutable = true;
  927. if (dchunk)
  928. dchunk->immutable = true;
  929. }
  930. /* assign pages */
  931. nr_pages = -1;
  932. for_each_possible_cpu(cpu) {
  933. for (i = 0; i < pcpu_unit_pages; i++) {
  934. struct page *page = get_page_fn(cpu, i);
  935. if (!page)
  936. break;
  937. *pcpu_chunk_pagep(schunk, cpu, i) = page;
  938. }
  939. BUG_ON(i < PFN_UP(static_size));
  940. if (nr_pages < 0)
  941. nr_pages = i;
  942. else
  943. BUG_ON(nr_pages != i);
  944. }
  945. /* map them */
  946. if (populate_pte_fn) {
  947. for_each_possible_cpu(cpu)
  948. for (i = 0; i < nr_pages; i++)
  949. populate_pte_fn(pcpu_chunk_addr(schunk,
  950. cpu, i));
  951. err = pcpu_map(schunk, 0, nr_pages);
  952. if (err)
  953. panic("failed to setup static percpu area, err=%d\n",
  954. err);
  955. }
  956. /* link the first chunk in */
  957. if (!dchunk) {
  958. pcpu_chunk_relocate(schunk, -1);
  959. pcpu_chunk_addr_insert(schunk);
  960. } else {
  961. pcpu_chunk_relocate(dchunk, -1);
  962. pcpu_chunk_addr_insert(dchunk);
  963. }
  964. /* we're done */
  965. pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
  966. return pcpu_unit_size;
  967. }