percpu.c 27 KB

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