percpu.c 27 KB

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