percpu.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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_extend_area_map - extend area map for allocation
  277. * @chunk: target chunk
  278. *
  279. * Extend area map of @chunk so that it can accomodate an allocation.
  280. * A single allocation can split an area into three areas, so this
  281. * function makes sure that @chunk->map has at least two extra slots.
  282. *
  283. * RETURNS:
  284. * 0 if noop, 1 if successfully extended, -errno on failure.
  285. */
  286. static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
  287. {
  288. int new_alloc;
  289. int *new;
  290. size_t size;
  291. /* has enough? */
  292. if (chunk->map_alloc >= chunk->map_used + 2)
  293. return 0;
  294. new_alloc = PCPU_DFL_MAP_ALLOC;
  295. while (new_alloc < chunk->map_used + 2)
  296. new_alloc *= 2;
  297. new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
  298. if (!new)
  299. return -ENOMEM;
  300. size = chunk->map_alloc * sizeof(chunk->map[0]);
  301. memcpy(new, chunk->map, size);
  302. /*
  303. * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
  304. * one of the first chunks and still using static map.
  305. */
  306. if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
  307. pcpu_mem_free(chunk->map, size);
  308. chunk->map_alloc = new_alloc;
  309. chunk->map = new;
  310. return 0;
  311. }
  312. /**
  313. * pcpu_split_block - split a map block
  314. * @chunk: chunk of interest
  315. * @i: index of map block to split
  316. * @head: head size in bytes (can be 0)
  317. * @tail: tail size in bytes (can be 0)
  318. *
  319. * Split the @i'th map block into two or three blocks. If @head is
  320. * non-zero, @head bytes block is inserted before block @i moving it
  321. * to @i+1 and reducing its size by @head bytes.
  322. *
  323. * If @tail is non-zero, the target block, which can be @i or @i+1
  324. * depending on @head, is reduced by @tail bytes and @tail byte block
  325. * is inserted after the target block.
  326. *
  327. * @chunk->map must have enough free slots to accomodate the split.
  328. */
  329. static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
  330. int head, int tail)
  331. {
  332. int nr_extra = !!head + !!tail;
  333. BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
  334. /* insert new subblocks */
  335. memmove(&chunk->map[i + nr_extra], &chunk->map[i],
  336. sizeof(chunk->map[0]) * (chunk->map_used - i));
  337. chunk->map_used += nr_extra;
  338. if (head) {
  339. chunk->map[i + 1] = chunk->map[i] - head;
  340. chunk->map[i++] = head;
  341. }
  342. if (tail) {
  343. chunk->map[i++] -= tail;
  344. chunk->map[i] = tail;
  345. }
  346. }
  347. /**
  348. * pcpu_alloc_area - allocate area from a pcpu_chunk
  349. * @chunk: chunk of interest
  350. * @size: wanted size in bytes
  351. * @align: wanted align
  352. *
  353. * Try to allocate @size bytes area aligned at @align from @chunk.
  354. * Note that this function only allocates the offset. It doesn't
  355. * populate or map the area.
  356. *
  357. * @chunk->map must have at least two free slots.
  358. *
  359. * RETURNS:
  360. * Allocated offset in @chunk on success, -1 if no matching area is
  361. * found.
  362. */
  363. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
  364. {
  365. int oslot = pcpu_chunk_slot(chunk);
  366. int max_contig = 0;
  367. int i, off;
  368. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
  369. bool is_last = i + 1 == chunk->map_used;
  370. int head, tail;
  371. /* extra for alignment requirement */
  372. head = ALIGN(off, align) - off;
  373. BUG_ON(i == 0 && head != 0);
  374. if (chunk->map[i] < 0)
  375. continue;
  376. if (chunk->map[i] < head + size) {
  377. max_contig = max(chunk->map[i], max_contig);
  378. continue;
  379. }
  380. /*
  381. * If head is small or the previous block is free,
  382. * merge'em. Note that 'small' is defined as smaller
  383. * than sizeof(int), which is very small but isn't too
  384. * uncommon for percpu allocations.
  385. */
  386. if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
  387. if (chunk->map[i - 1] > 0)
  388. chunk->map[i - 1] += head;
  389. else {
  390. chunk->map[i - 1] -= head;
  391. chunk->free_size -= head;
  392. }
  393. chunk->map[i] -= head;
  394. off += head;
  395. head = 0;
  396. }
  397. /* if tail is small, just keep it around */
  398. tail = chunk->map[i] - head - size;
  399. if (tail < sizeof(int))
  400. tail = 0;
  401. /* split if warranted */
  402. if (head || tail) {
  403. pcpu_split_block(chunk, i, head, tail);
  404. if (head) {
  405. i++;
  406. off += head;
  407. max_contig = max(chunk->map[i - 1], max_contig);
  408. }
  409. if (tail)
  410. max_contig = max(chunk->map[i + 1], max_contig);
  411. }
  412. /* update hint and mark allocated */
  413. if (is_last)
  414. chunk->contig_hint = max_contig; /* fully scanned */
  415. else
  416. chunk->contig_hint = max(chunk->contig_hint,
  417. max_contig);
  418. chunk->free_size -= chunk->map[i];
  419. chunk->map[i] = -chunk->map[i];
  420. pcpu_chunk_relocate(chunk, oslot);
  421. return off;
  422. }
  423. chunk->contig_hint = max_contig; /* fully scanned */
  424. pcpu_chunk_relocate(chunk, oslot);
  425. /* tell the upper layer that this chunk has no matching area */
  426. return -1;
  427. }
  428. /**
  429. * pcpu_free_area - free area to a pcpu_chunk
  430. * @chunk: chunk of interest
  431. * @freeme: offset of area to free
  432. *
  433. * Free area starting from @freeme to @chunk. Note that this function
  434. * only modifies the allocation map. It doesn't depopulate or unmap
  435. * the area.
  436. */
  437. static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
  438. {
  439. int oslot = pcpu_chunk_slot(chunk);
  440. int i, off;
  441. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
  442. if (off == freeme)
  443. break;
  444. BUG_ON(off != freeme);
  445. BUG_ON(chunk->map[i] > 0);
  446. chunk->map[i] = -chunk->map[i];
  447. chunk->free_size += chunk->map[i];
  448. /* merge with previous? */
  449. if (i > 0 && chunk->map[i - 1] >= 0) {
  450. chunk->map[i - 1] += chunk->map[i];
  451. chunk->map_used--;
  452. memmove(&chunk->map[i], &chunk->map[i + 1],
  453. (chunk->map_used - i) * sizeof(chunk->map[0]));
  454. i--;
  455. }
  456. /* merge with next? */
  457. if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
  458. chunk->map[i] += chunk->map[i + 1];
  459. chunk->map_used--;
  460. memmove(&chunk->map[i + 1], &chunk->map[i + 2],
  461. (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
  462. }
  463. chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
  464. pcpu_chunk_relocate(chunk, oslot);
  465. }
  466. /**
  467. * pcpu_unmap - unmap pages out of a pcpu_chunk
  468. * @chunk: chunk of interest
  469. * @page_start: page index of the first page to unmap
  470. * @page_end: page index of the last page to unmap + 1
  471. * @flush: whether to flush cache and tlb or not
  472. *
  473. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  474. * If @flush is true, vcache is flushed before unmapping and tlb
  475. * after.
  476. */
  477. static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
  478. bool flush)
  479. {
  480. unsigned int last = num_possible_cpus() - 1;
  481. unsigned int cpu;
  482. /* unmap must not be done on immutable chunk */
  483. WARN_ON(chunk->immutable);
  484. /*
  485. * Each flushing trial can be very expensive, issue flush on
  486. * the whole region at once rather than doing it for each cpu.
  487. * This could be an overkill but is more scalable.
  488. */
  489. if (flush)
  490. flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
  491. pcpu_chunk_addr(chunk, last, page_end));
  492. for_each_possible_cpu(cpu)
  493. unmap_kernel_range_noflush(
  494. pcpu_chunk_addr(chunk, cpu, page_start),
  495. (page_end - page_start) << PAGE_SHIFT);
  496. /* ditto as flush_cache_vunmap() */
  497. if (flush)
  498. flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
  499. pcpu_chunk_addr(chunk, last, page_end));
  500. }
  501. /**
  502. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  503. * @chunk: chunk to depopulate
  504. * @off: offset to the area to depopulate
  505. * @size: size of the area to depopulate in bytes
  506. * @flush: whether to flush cache and tlb or not
  507. *
  508. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  509. * from @chunk. If @flush is true, vcache is flushed before unmapping
  510. * and tlb after.
  511. */
  512. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
  513. bool flush)
  514. {
  515. int page_start = PFN_DOWN(off);
  516. int page_end = PFN_UP(off + size);
  517. int unmap_start = -1;
  518. int uninitialized_var(unmap_end);
  519. unsigned int cpu;
  520. int i;
  521. for (i = page_start; i < page_end; i++) {
  522. for_each_possible_cpu(cpu) {
  523. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  524. if (!*pagep)
  525. continue;
  526. __free_page(*pagep);
  527. /*
  528. * If it's partial depopulation, it might get
  529. * populated or depopulated again. Mark the
  530. * page gone.
  531. */
  532. *pagep = NULL;
  533. unmap_start = unmap_start < 0 ? i : unmap_start;
  534. unmap_end = i + 1;
  535. }
  536. }
  537. if (unmap_start >= 0)
  538. pcpu_unmap(chunk, unmap_start, unmap_end, flush);
  539. }
  540. /**
  541. * pcpu_map - map pages into a pcpu_chunk
  542. * @chunk: chunk of interest
  543. * @page_start: page index of the first page to map
  544. * @page_end: page index of the last page to map + 1
  545. *
  546. * For each cpu, map pages [@page_start,@page_end) into @chunk.
  547. * vcache is flushed afterwards.
  548. */
  549. static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
  550. {
  551. unsigned int last = num_possible_cpus() - 1;
  552. unsigned int cpu;
  553. int err;
  554. /* map must not be done on immutable chunk */
  555. WARN_ON(chunk->immutable);
  556. for_each_possible_cpu(cpu) {
  557. err = map_kernel_range_noflush(
  558. pcpu_chunk_addr(chunk, cpu, page_start),
  559. (page_end - page_start) << PAGE_SHIFT,
  560. PAGE_KERNEL,
  561. pcpu_chunk_pagep(chunk, cpu, page_start));
  562. if (err < 0)
  563. return err;
  564. }
  565. /* flush at once, please read comments in pcpu_unmap() */
  566. flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
  567. pcpu_chunk_addr(chunk, last, page_end));
  568. return 0;
  569. }
  570. /**
  571. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  572. * @chunk: chunk of interest
  573. * @off: offset to the area to populate
  574. * @size: size of the area to populate in bytes
  575. *
  576. * For each cpu, populate and map pages [@page_start,@page_end) into
  577. * @chunk. The area is cleared on return.
  578. */
  579. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
  580. {
  581. const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  582. int page_start = PFN_DOWN(off);
  583. int page_end = PFN_UP(off + size);
  584. int map_start = -1;
  585. int uninitialized_var(map_end);
  586. unsigned int cpu;
  587. int i;
  588. for (i = page_start; i < page_end; i++) {
  589. if (pcpu_chunk_page_occupied(chunk, i)) {
  590. if (map_start >= 0) {
  591. if (pcpu_map(chunk, map_start, map_end))
  592. goto err;
  593. map_start = -1;
  594. }
  595. continue;
  596. }
  597. map_start = map_start < 0 ? i : map_start;
  598. map_end = i + 1;
  599. for_each_possible_cpu(cpu) {
  600. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  601. *pagep = alloc_pages_node(cpu_to_node(cpu),
  602. alloc_mask, 0);
  603. if (!*pagep)
  604. goto err;
  605. }
  606. }
  607. if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
  608. goto err;
  609. for_each_possible_cpu(cpu)
  610. memset(chunk->vm->addr + cpu * pcpu_unit_size + off, 0,
  611. size);
  612. return 0;
  613. err:
  614. /* likely under heavy memory pressure, give memory back */
  615. pcpu_depopulate_chunk(chunk, off, size, true);
  616. return -ENOMEM;
  617. }
  618. static void free_pcpu_chunk(struct pcpu_chunk *chunk)
  619. {
  620. if (!chunk)
  621. return;
  622. if (chunk->vm)
  623. free_vm_area(chunk->vm);
  624. pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
  625. kfree(chunk);
  626. }
  627. static struct pcpu_chunk *alloc_pcpu_chunk(void)
  628. {
  629. struct pcpu_chunk *chunk;
  630. chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
  631. if (!chunk)
  632. return NULL;
  633. chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  634. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  635. chunk->map[chunk->map_used++] = pcpu_unit_size;
  636. chunk->page = chunk->page_ar;
  637. chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
  638. if (!chunk->vm) {
  639. free_pcpu_chunk(chunk);
  640. return NULL;
  641. }
  642. INIT_LIST_HEAD(&chunk->list);
  643. chunk->free_size = pcpu_unit_size;
  644. chunk->contig_hint = pcpu_unit_size;
  645. return chunk;
  646. }
  647. /**
  648. * pcpu_alloc - the percpu allocator
  649. * @size: size of area to allocate in bytes
  650. * @align: alignment of area (max PAGE_SIZE)
  651. * @reserved: allocate from the reserved chunk if available
  652. *
  653. * Allocate percpu area of @size bytes aligned at @align. Might
  654. * sleep. Might trigger writeouts.
  655. *
  656. * RETURNS:
  657. * Percpu pointer to the allocated area on success, NULL on failure.
  658. */
  659. static void *pcpu_alloc(size_t size, size_t align, bool reserved)
  660. {
  661. void *ptr = NULL;
  662. struct pcpu_chunk *chunk;
  663. int slot, off;
  664. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
  665. WARN(true, "illegal size (%zu) or align (%zu) for "
  666. "percpu allocation\n", size, align);
  667. return NULL;
  668. }
  669. mutex_lock(&pcpu_mutex);
  670. /* serve reserved allocations from the reserved chunk if available */
  671. if (reserved && pcpu_reserved_chunk) {
  672. chunk = pcpu_reserved_chunk;
  673. if (size > chunk->contig_hint ||
  674. pcpu_extend_area_map(chunk) < 0)
  675. goto out_unlock;
  676. off = pcpu_alloc_area(chunk, size, align);
  677. if (off >= 0)
  678. goto area_found;
  679. goto out_unlock;
  680. }
  681. /* search through normal chunks */
  682. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  683. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  684. if (size > chunk->contig_hint)
  685. continue;
  686. if (pcpu_extend_area_map(chunk) < 0)
  687. goto out_unlock;
  688. off = pcpu_alloc_area(chunk, size, align);
  689. if (off >= 0)
  690. goto area_found;
  691. }
  692. }
  693. /* hmmm... no space left, create a new chunk */
  694. chunk = alloc_pcpu_chunk();
  695. if (!chunk)
  696. goto out_unlock;
  697. pcpu_chunk_relocate(chunk, -1);
  698. pcpu_chunk_addr_insert(chunk);
  699. off = pcpu_alloc_area(chunk, size, align);
  700. if (off < 0)
  701. goto out_unlock;
  702. area_found:
  703. /* populate, map and clear the area */
  704. if (pcpu_populate_chunk(chunk, off, size)) {
  705. pcpu_free_area(chunk, off);
  706. goto out_unlock;
  707. }
  708. ptr = __addr_to_pcpu_ptr(chunk->vm->addr + off);
  709. out_unlock:
  710. mutex_unlock(&pcpu_mutex);
  711. return ptr;
  712. }
  713. /**
  714. * __alloc_percpu - allocate dynamic percpu area
  715. * @size: size of area to allocate in bytes
  716. * @align: alignment of area (max PAGE_SIZE)
  717. *
  718. * Allocate percpu area of @size bytes aligned at @align. Might
  719. * sleep. Might trigger writeouts.
  720. *
  721. * RETURNS:
  722. * Percpu pointer to the allocated area on success, NULL on failure.
  723. */
  724. void *__alloc_percpu(size_t size, size_t align)
  725. {
  726. return pcpu_alloc(size, align, false);
  727. }
  728. EXPORT_SYMBOL_GPL(__alloc_percpu);
  729. /**
  730. * __alloc_reserved_percpu - allocate reserved percpu area
  731. * @size: size of area to allocate in bytes
  732. * @align: alignment of area (max PAGE_SIZE)
  733. *
  734. * Allocate percpu area of @size bytes aligned at @align from reserved
  735. * percpu area if arch has set it up; otherwise, allocation is served
  736. * from the same dynamic area. Might sleep. Might trigger writeouts.
  737. *
  738. * RETURNS:
  739. * Percpu pointer to the allocated area on success, NULL on failure.
  740. */
  741. void *__alloc_reserved_percpu(size_t size, size_t align)
  742. {
  743. return pcpu_alloc(size, align, true);
  744. }
  745. static void pcpu_kill_chunk(struct pcpu_chunk *chunk)
  746. {
  747. WARN_ON(chunk->immutable);
  748. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
  749. list_del(&chunk->list);
  750. rb_erase(&chunk->rb_node, &pcpu_addr_root);
  751. free_pcpu_chunk(chunk);
  752. }
  753. /**
  754. * free_percpu - free percpu area
  755. * @ptr: pointer to area to free
  756. *
  757. * Free percpu area @ptr. Might sleep.
  758. */
  759. void free_percpu(void *ptr)
  760. {
  761. void *addr = __pcpu_ptr_to_addr(ptr);
  762. struct pcpu_chunk *chunk;
  763. int off;
  764. if (!ptr)
  765. return;
  766. mutex_lock(&pcpu_mutex);
  767. chunk = pcpu_chunk_addr_search(addr);
  768. off = addr - chunk->vm->addr;
  769. pcpu_free_area(chunk, off);
  770. /* the chunk became fully free, kill one if there are other free ones */
  771. if (chunk->free_size == pcpu_unit_size) {
  772. struct pcpu_chunk *pos;
  773. list_for_each_entry(pos,
  774. &pcpu_slot[pcpu_chunk_slot(chunk)], list)
  775. if (pos != chunk) {
  776. pcpu_kill_chunk(pos);
  777. break;
  778. }
  779. }
  780. mutex_unlock(&pcpu_mutex);
  781. }
  782. EXPORT_SYMBOL_GPL(free_percpu);
  783. /**
  784. * pcpu_setup_first_chunk - initialize the first percpu chunk
  785. * @get_page_fn: callback to fetch page pointer
  786. * @static_size: the size of static percpu area in bytes
  787. * @reserved_size: the size of reserved percpu area in bytes
  788. * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
  789. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  790. * @base_addr: mapped address, NULL for auto
  791. * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
  792. *
  793. * Initialize the first percpu chunk which contains the kernel static
  794. * perpcu area. This function is to be called from arch percpu area
  795. * setup path. The first two parameters are mandatory. The rest are
  796. * optional.
  797. *
  798. * @get_page_fn() should return pointer to percpu page given cpu
  799. * number and page number. It should at least return enough pages to
  800. * cover the static area. The returned pages for static area should
  801. * have been initialized with valid data. If @unit_size is specified,
  802. * it can also return pages after the static area. NULL return
  803. * indicates end of pages for the cpu. Note that @get_page_fn() must
  804. * return the same number of pages for all cpus.
  805. *
  806. * @reserved_size, if non-zero, specifies the amount of bytes to
  807. * reserve after the static area in the first chunk. This reserves
  808. * the first chunk such that it's available only through reserved
  809. * percpu allocation. This is primarily used to serve module percpu
  810. * static areas on architectures where the addressing model has
  811. * limited offset range for symbol relocations to guarantee module
  812. * percpu symbols fall inside the relocatable range.
  813. *
  814. * @unit_size, if non-negative, specifies unit size and must be
  815. * aligned to PAGE_SIZE and equal to or larger than @static_size +
  816. * @reserved_size + @dyn_size.
  817. *
  818. * @dyn_size, if non-negative, limits the number of bytes available
  819. * for dynamic allocation in the first chunk. Specifying non-negative
  820. * value make percpu leave alone the area beyond @static_size +
  821. * @reserved_size + @dyn_size.
  822. *
  823. * Non-null @base_addr means that the caller already allocated virtual
  824. * region for the first chunk and mapped it. percpu must not mess
  825. * with the chunk. Note that @base_addr with 0 @unit_size or non-NULL
  826. * @populate_pte_fn doesn't make any sense.
  827. *
  828. * @populate_pte_fn is used to populate the pagetable. NULL means the
  829. * caller already populated the pagetable.
  830. *
  831. * If the first chunk ends up with both reserved and dynamic areas, it
  832. * is served by two chunks - one to serve the core static and reserved
  833. * areas and the other for the dynamic area. They share the same vm
  834. * and page map but uses different area allocation map to stay away
  835. * from each other. The latter chunk is circulated in the chunk slots
  836. * and available for dynamic allocation like any other chunks.
  837. *
  838. * RETURNS:
  839. * The determined pcpu_unit_size which can be used to initialize
  840. * percpu access.
  841. */
  842. size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
  843. size_t static_size, size_t reserved_size,
  844. ssize_t unit_size, ssize_t dyn_size,
  845. void *base_addr,
  846. pcpu_populate_pte_fn_t populate_pte_fn)
  847. {
  848. static struct vm_struct first_vm;
  849. static int smap[2], dmap[2];
  850. struct pcpu_chunk *schunk, *dchunk = NULL;
  851. unsigned int cpu;
  852. int nr_pages;
  853. int err, i;
  854. /* santiy checks */
  855. BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
  856. ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
  857. BUG_ON(!static_size);
  858. if (unit_size >= 0) {
  859. BUG_ON(unit_size < static_size + reserved_size +
  860. (dyn_size >= 0 ? dyn_size : 0));
  861. BUG_ON(unit_size & ~PAGE_MASK);
  862. } else {
  863. BUG_ON(dyn_size >= 0);
  864. BUG_ON(base_addr);
  865. }
  866. BUG_ON(base_addr && populate_pte_fn);
  867. if (unit_size >= 0)
  868. pcpu_unit_pages = unit_size >> PAGE_SHIFT;
  869. else
  870. pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
  871. PFN_UP(static_size + reserved_size));
  872. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  873. pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
  874. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
  875. + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
  876. if (dyn_size < 0)
  877. dyn_size = pcpu_unit_size - static_size - reserved_size;
  878. /*
  879. * Allocate chunk slots. The additional last slot is for
  880. * empty chunks.
  881. */
  882. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  883. pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
  884. for (i = 0; i < pcpu_nr_slots; i++)
  885. INIT_LIST_HEAD(&pcpu_slot[i]);
  886. /*
  887. * Initialize static chunk. If reserved_size is zero, the
  888. * static chunk covers static area + dynamic allocation area
  889. * in the first chunk. If reserved_size is not zero, it
  890. * covers static area + reserved area (mostly used for module
  891. * static percpu allocation).
  892. */
  893. schunk = alloc_bootmem(pcpu_chunk_struct_size);
  894. INIT_LIST_HEAD(&schunk->list);
  895. schunk->vm = &first_vm;
  896. schunk->map = smap;
  897. schunk->map_alloc = ARRAY_SIZE(smap);
  898. schunk->page = schunk->page_ar;
  899. if (reserved_size) {
  900. schunk->free_size = reserved_size;
  901. pcpu_reserved_chunk = schunk; /* not for dynamic alloc */
  902. } else {
  903. schunk->free_size = dyn_size;
  904. dyn_size = 0; /* dynamic area covered */
  905. }
  906. schunk->contig_hint = schunk->free_size;
  907. schunk->map[schunk->map_used++] = -static_size;
  908. if (schunk->free_size)
  909. schunk->map[schunk->map_used++] = schunk->free_size;
  910. pcpu_reserved_chunk_limit = static_size + schunk->free_size;
  911. /* init dynamic chunk if necessary */
  912. if (dyn_size) {
  913. dchunk = alloc_bootmem(sizeof(struct pcpu_chunk));
  914. INIT_LIST_HEAD(&dchunk->list);
  915. dchunk->vm = &first_vm;
  916. dchunk->map = dmap;
  917. dchunk->map_alloc = ARRAY_SIZE(dmap);
  918. dchunk->page = schunk->page_ar; /* share page map with schunk */
  919. dchunk->contig_hint = dchunk->free_size = dyn_size;
  920. dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
  921. dchunk->map[dchunk->map_used++] = dchunk->free_size;
  922. }
  923. /* allocate vm address */
  924. first_vm.flags = VM_ALLOC;
  925. first_vm.size = pcpu_chunk_size;
  926. if (!base_addr)
  927. vm_area_register_early(&first_vm, PAGE_SIZE);
  928. else {
  929. /*
  930. * Pages already mapped. No need to remap into
  931. * vmalloc area. In this case the first chunks can't
  932. * be mapped or unmapped by percpu and are marked
  933. * immutable.
  934. */
  935. first_vm.addr = base_addr;
  936. schunk->immutable = true;
  937. if (dchunk)
  938. dchunk->immutable = true;
  939. }
  940. /* assign pages */
  941. nr_pages = -1;
  942. for_each_possible_cpu(cpu) {
  943. for (i = 0; i < pcpu_unit_pages; i++) {
  944. struct page *page = get_page_fn(cpu, i);
  945. if (!page)
  946. break;
  947. *pcpu_chunk_pagep(schunk, cpu, i) = page;
  948. }
  949. BUG_ON(i < PFN_UP(static_size));
  950. if (nr_pages < 0)
  951. nr_pages = i;
  952. else
  953. BUG_ON(nr_pages != i);
  954. }
  955. /* map them */
  956. if (populate_pte_fn) {
  957. for_each_possible_cpu(cpu)
  958. for (i = 0; i < nr_pages; i++)
  959. populate_pte_fn(pcpu_chunk_addr(schunk,
  960. cpu, i));
  961. err = pcpu_map(schunk, 0, nr_pages);
  962. if (err)
  963. panic("failed to setup static percpu area, err=%d\n",
  964. err);
  965. }
  966. /* link the first chunk in */
  967. if (!dchunk) {
  968. pcpu_chunk_relocate(schunk, -1);
  969. pcpu_chunk_addr_insert(schunk);
  970. } else {
  971. pcpu_chunk_relocate(dchunk, -1);
  972. pcpu_chunk_addr_insert(dchunk);
  973. }
  974. /* we're done */
  975. pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
  976. return pcpu_unit_size;
  977. }