percpu.c 32 KB

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