percpu.c 36 KB

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