percpu.c 34 KB

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