percpu.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. /*
  2. * 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. Each chunk is
  11. * consisted of boot-time determined number of units and the first
  12. * chunk is used for static percpu variables in the kernel image
  13. * (special boot time alloc/init handling necessary as these areas
  14. * need to be brought up before allocation services are running).
  15. * Unit grows as necessary and all units grow or shrink in unison.
  16. * When a chunk is filled up, another chunk is allocated.
  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. On UMA, units corresponds directly to
  26. * cpus. On NUMA, the mapping can be non-linear and even sparse.
  27. * Percpu access can be done by configuring percpu base registers
  28. * according to cpu to unit mapping and pcpu_unit_size.
  29. *
  30. * There are usually many small percpu allocations many of them being
  31. * as small as 4 bytes. The allocator organizes chunks into lists
  32. * according to free size and tries to allocate from the fullest one.
  33. * Each chunk keeps the maximum contiguous area size hint which is
  34. * guaranteed to be equal to or larger than the maximum contiguous
  35. * area in the chunk. This helps the allocator not to iterate the
  36. * chunk maps unnecessarily.
  37. *
  38. * Allocation state in each chunk is kept using an array of integers
  39. * on chunk->map. A positive value in the map represents a free
  40. * region and negative allocated. Allocation inside a chunk is done
  41. * by scanning this map sequentially and serving the first matching
  42. * entry. This is mostly copied from the percpu_modalloc() allocator.
  43. * Chunks can be determined from the address using the index field
  44. * in the page struct. The index field contains a pointer to the chunk.
  45. *
  46. * To use this allocator, arch code should do the followings.
  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/err.h>
  58. #include <linux/list.h>
  59. #include <linux/log2.h>
  60. #include <linux/mm.h>
  61. #include <linux/module.h>
  62. #include <linux/mutex.h>
  63. #include <linux/percpu.h>
  64. #include <linux/pfn.h>
  65. #include <linux/slab.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/vmalloc.h>
  68. #include <linux/workqueue.h>
  69. #include <asm/cacheflush.h>
  70. #include <asm/sections.h>
  71. #include <asm/tlbflush.h>
  72. #include <asm/io.h>
  73. #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
  74. #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
  75. #ifdef CONFIG_SMP
  76. /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */
  77. #ifndef __addr_to_pcpu_ptr
  78. #define __addr_to_pcpu_ptr(addr) \
  79. (void __percpu *)((unsigned long)(addr) - \
  80. (unsigned long)pcpu_base_addr + \
  81. (unsigned long)__per_cpu_start)
  82. #endif
  83. #ifndef __pcpu_ptr_to_addr
  84. #define __pcpu_ptr_to_addr(ptr) \
  85. (void __force *)((unsigned long)(ptr) + \
  86. (unsigned long)pcpu_base_addr - \
  87. (unsigned long)__per_cpu_start)
  88. #endif
  89. #else /* CONFIG_SMP */
  90. /* on UP, it's always identity mapped */
  91. #define __addr_to_pcpu_ptr(addr) (void __percpu *)(addr)
  92. #define __pcpu_ptr_to_addr(ptr) (void __force *)(ptr)
  93. #endif /* CONFIG_SMP */
  94. struct pcpu_chunk {
  95. struct list_head list; /* linked to pcpu_slot lists */
  96. int free_size; /* free bytes in the chunk */
  97. int contig_hint; /* max contiguous size hint */
  98. void *base_addr; /* base address of this chunk */
  99. int map_used; /* # of map entries used */
  100. int map_alloc; /* # of map entries allocated */
  101. int *map; /* allocation map */
  102. void *data; /* chunk data */
  103. bool immutable; /* no [de]population allowed */
  104. unsigned long populated[]; /* populated bitmap */
  105. };
  106. static int pcpu_unit_pages __read_mostly;
  107. static int pcpu_unit_size __read_mostly;
  108. static int pcpu_nr_units __read_mostly;
  109. static int pcpu_atom_size __read_mostly;
  110. static int pcpu_nr_slots __read_mostly;
  111. static size_t pcpu_chunk_struct_size __read_mostly;
  112. /* cpus with the lowest and highest unit numbers */
  113. static unsigned int pcpu_first_unit_cpu __read_mostly;
  114. static unsigned int pcpu_last_unit_cpu __read_mostly;
  115. /* the address of the first chunk which starts with the kernel static area */
  116. void *pcpu_base_addr __read_mostly;
  117. EXPORT_SYMBOL_GPL(pcpu_base_addr);
  118. static const int *pcpu_unit_map __read_mostly; /* cpu -> unit */
  119. const unsigned long *pcpu_unit_offsets __read_mostly; /* cpu -> unit offset */
  120. /* group information, used for vm allocation */
  121. static int pcpu_nr_groups __read_mostly;
  122. static const unsigned long *pcpu_group_offsets __read_mostly;
  123. static const size_t *pcpu_group_sizes __read_mostly;
  124. /*
  125. * The first chunk which always exists. Note that unlike other
  126. * chunks, this one can be allocated and mapped in several different
  127. * ways and thus often doesn't live in the vmalloc area.
  128. */
  129. static struct pcpu_chunk *pcpu_first_chunk;
  130. /*
  131. * Optional reserved chunk. This chunk reserves part of the first
  132. * chunk and serves it for reserved allocations. The amount of
  133. * reserved offset is in pcpu_reserved_chunk_limit. When reserved
  134. * area doesn't exist, the following variables contain NULL and 0
  135. * respectively.
  136. */
  137. static struct pcpu_chunk *pcpu_reserved_chunk;
  138. static int pcpu_reserved_chunk_limit;
  139. /*
  140. * Synchronization rules.
  141. *
  142. * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former
  143. * protects allocation/reclaim paths, chunks, populated bitmap and
  144. * vmalloc mapping. The latter is a spinlock and protects the index
  145. * data structures - chunk slots, chunks and area maps in chunks.
  146. *
  147. * During allocation, pcpu_alloc_mutex is kept locked all the time and
  148. * pcpu_lock is grabbed and released as necessary. All actual memory
  149. * allocations are done using GFP_KERNEL with pcpu_lock released. In
  150. * general, percpu memory can't be allocated with irq off but
  151. * irqsave/restore are still used in alloc path so that it can be used
  152. * from early init path - sched_init() specifically.
  153. *
  154. * Free path accesses and alters only the index data structures, so it
  155. * can be safely called from atomic context. When memory needs to be
  156. * returned to the system, free path schedules reclaim_work which
  157. * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be
  158. * reclaimed, release both locks and frees the chunks. Note that it's
  159. * necessary to grab both locks to remove a chunk from circulation as
  160. * allocation path might be referencing the chunk with only
  161. * pcpu_alloc_mutex locked.
  162. */
  163. static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */
  164. static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */
  165. static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
  166. /* reclaim work to release fully free chunks, scheduled from free path */
  167. static void pcpu_reclaim(struct work_struct *work);
  168. static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
  169. static bool pcpu_addr_in_first_chunk(void *addr)
  170. {
  171. void *first_start = pcpu_first_chunk->base_addr;
  172. return addr >= first_start && addr < first_start + pcpu_unit_size;
  173. }
  174. static bool pcpu_addr_in_reserved_chunk(void *addr)
  175. {
  176. void *first_start = pcpu_first_chunk->base_addr;
  177. return addr >= first_start &&
  178. addr < first_start + pcpu_reserved_chunk_limit;
  179. }
  180. static int __pcpu_size_to_slot(int size)
  181. {
  182. int highbit = fls(size); /* size is in bytes */
  183. return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
  184. }
  185. static int pcpu_size_to_slot(int size)
  186. {
  187. if (size == pcpu_unit_size)
  188. return pcpu_nr_slots - 1;
  189. return __pcpu_size_to_slot(size);
  190. }
  191. static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
  192. {
  193. if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
  194. return 0;
  195. return pcpu_size_to_slot(chunk->free_size);
  196. }
  197. /* set the pointer to a chunk in a page struct */
  198. static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu)
  199. {
  200. page->index = (unsigned long)pcpu;
  201. }
  202. /* obtain pointer to a chunk from a page struct */
  203. static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page)
  204. {
  205. return (struct pcpu_chunk *)page->index;
  206. }
  207. static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx)
  208. {
  209. return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx;
  210. }
  211. static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
  212. unsigned int cpu, int page_idx)
  213. {
  214. return (unsigned long)chunk->base_addr + pcpu_unit_offsets[cpu] +
  215. (page_idx << PAGE_SHIFT);
  216. }
  217. static void __maybe_unused pcpu_next_unpop(struct pcpu_chunk *chunk,
  218. int *rs, int *re, int end)
  219. {
  220. *rs = find_next_zero_bit(chunk->populated, end, *rs);
  221. *re = find_next_bit(chunk->populated, end, *rs + 1);
  222. }
  223. static void __maybe_unused pcpu_next_pop(struct pcpu_chunk *chunk,
  224. int *rs, int *re, int end)
  225. {
  226. *rs = find_next_bit(chunk->populated, end, *rs);
  227. *re = find_next_zero_bit(chunk->populated, end, *rs + 1);
  228. }
  229. /*
  230. * (Un)populated page region iterators. Iterate over (un)populated
  231. * page regions between @start and @end in @chunk. @rs and @re should
  232. * be integer variables and will be set to start and end page index of
  233. * the current region.
  234. */
  235. #define pcpu_for_each_unpop_region(chunk, rs, re, start, end) \
  236. for ((rs) = (start), pcpu_next_unpop((chunk), &(rs), &(re), (end)); \
  237. (rs) < (re); \
  238. (rs) = (re) + 1, pcpu_next_unpop((chunk), &(rs), &(re), (end)))
  239. #define pcpu_for_each_pop_region(chunk, rs, re, start, end) \
  240. for ((rs) = (start), pcpu_next_pop((chunk), &(rs), &(re), (end)); \
  241. (rs) < (re); \
  242. (rs) = (re) + 1, pcpu_next_pop((chunk), &(rs), &(re), (end)))
  243. /**
  244. * pcpu_mem_alloc - allocate memory
  245. * @size: bytes to allocate
  246. *
  247. * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
  248. * kzalloc() is used; otherwise, vmalloc() is used. The returned
  249. * memory is always zeroed.
  250. *
  251. * CONTEXT:
  252. * Does GFP_KERNEL allocation.
  253. *
  254. * RETURNS:
  255. * Pointer to the allocated area on success, NULL on failure.
  256. */
  257. static void *pcpu_mem_alloc(size_t size)
  258. {
  259. if (WARN_ON_ONCE(!slab_is_available()))
  260. return NULL;
  261. if (size <= PAGE_SIZE)
  262. return kzalloc(size, GFP_KERNEL);
  263. else
  264. return vzalloc(size);
  265. }
  266. /**
  267. * pcpu_mem_free - free memory
  268. * @ptr: memory to free
  269. * @size: size of the area
  270. *
  271. * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc().
  272. */
  273. static void pcpu_mem_free(void *ptr, size_t size)
  274. {
  275. if (size <= PAGE_SIZE)
  276. kfree(ptr);
  277. else
  278. vfree(ptr);
  279. }
  280. /**
  281. * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
  282. * @chunk: chunk of interest
  283. * @oslot: the previous slot it was on
  284. *
  285. * This function is called after an allocation or free changed @chunk.
  286. * New slot according to the changed state is determined and @chunk is
  287. * moved to the slot. Note that the reserved chunk is never put on
  288. * chunk slots.
  289. *
  290. * CONTEXT:
  291. * pcpu_lock.
  292. */
  293. static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
  294. {
  295. int nslot = pcpu_chunk_slot(chunk);
  296. if (chunk != pcpu_reserved_chunk && oslot != nslot) {
  297. if (oslot < nslot)
  298. list_move(&chunk->list, &pcpu_slot[nslot]);
  299. else
  300. list_move_tail(&chunk->list, &pcpu_slot[nslot]);
  301. }
  302. }
  303. /**
  304. * pcpu_need_to_extend - determine whether chunk area map needs to be extended
  305. * @chunk: chunk of interest
  306. *
  307. * Determine whether area map of @chunk needs to be extended to
  308. * accommodate a new allocation.
  309. *
  310. * CONTEXT:
  311. * pcpu_lock.
  312. *
  313. * RETURNS:
  314. * New target map allocation length if extension is necessary, 0
  315. * otherwise.
  316. */
  317. static int pcpu_need_to_extend(struct pcpu_chunk *chunk)
  318. {
  319. int new_alloc;
  320. if (chunk->map_alloc >= chunk->map_used + 2)
  321. return 0;
  322. new_alloc = PCPU_DFL_MAP_ALLOC;
  323. while (new_alloc < chunk->map_used + 2)
  324. new_alloc *= 2;
  325. return new_alloc;
  326. }
  327. /**
  328. * pcpu_extend_area_map - extend area map of a chunk
  329. * @chunk: chunk of interest
  330. * @new_alloc: new target allocation length of the area map
  331. *
  332. * Extend area map of @chunk to have @new_alloc entries.
  333. *
  334. * CONTEXT:
  335. * Does GFP_KERNEL allocation. Grabs and releases pcpu_lock.
  336. *
  337. * RETURNS:
  338. * 0 on success, -errno on failure.
  339. */
  340. static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
  341. {
  342. int *old = NULL, *new = NULL;
  343. size_t old_size = 0, new_size = new_alloc * sizeof(new[0]);
  344. unsigned long flags;
  345. new = pcpu_mem_alloc(new_size);
  346. if (!new)
  347. return -ENOMEM;
  348. /* acquire pcpu_lock and switch to new area map */
  349. spin_lock_irqsave(&pcpu_lock, flags);
  350. if (new_alloc <= chunk->map_alloc)
  351. goto out_unlock;
  352. old_size = chunk->map_alloc * sizeof(chunk->map[0]);
  353. old = chunk->map;
  354. memcpy(new, old, old_size);
  355. chunk->map_alloc = new_alloc;
  356. chunk->map = new;
  357. new = NULL;
  358. out_unlock:
  359. spin_unlock_irqrestore(&pcpu_lock, flags);
  360. /*
  361. * pcpu_mem_free() might end up calling vfree() which uses
  362. * IRQ-unsafe lock and thus can't be called under pcpu_lock.
  363. */
  364. pcpu_mem_free(old, old_size);
  365. pcpu_mem_free(new, new_size);
  366. return 0;
  367. }
  368. /**
  369. * pcpu_split_block - split a map block
  370. * @chunk: chunk of interest
  371. * @i: index of map block to split
  372. * @head: head size in bytes (can be 0)
  373. * @tail: tail size in bytes (can be 0)
  374. *
  375. * Split the @i'th map block into two or three blocks. If @head is
  376. * non-zero, @head bytes block is inserted before block @i moving it
  377. * to @i+1 and reducing its size by @head bytes.
  378. *
  379. * If @tail is non-zero, the target block, which can be @i or @i+1
  380. * depending on @head, is reduced by @tail bytes and @tail byte block
  381. * is inserted after the target block.
  382. *
  383. * @chunk->map must have enough free slots to accommodate the split.
  384. *
  385. * CONTEXT:
  386. * pcpu_lock.
  387. */
  388. static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
  389. int head, int tail)
  390. {
  391. int nr_extra = !!head + !!tail;
  392. BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
  393. /* insert new subblocks */
  394. memmove(&chunk->map[i + nr_extra], &chunk->map[i],
  395. sizeof(chunk->map[0]) * (chunk->map_used - i));
  396. chunk->map_used += nr_extra;
  397. if (head) {
  398. chunk->map[i + 1] = chunk->map[i] - head;
  399. chunk->map[i++] = head;
  400. }
  401. if (tail) {
  402. chunk->map[i++] -= tail;
  403. chunk->map[i] = tail;
  404. }
  405. }
  406. /**
  407. * pcpu_alloc_area - allocate area from a pcpu_chunk
  408. * @chunk: chunk of interest
  409. * @size: wanted size in bytes
  410. * @align: wanted align
  411. *
  412. * Try to allocate @size bytes area aligned at @align from @chunk.
  413. * Note that this function only allocates the offset. It doesn't
  414. * populate or map the area.
  415. *
  416. * @chunk->map must have at least two free slots.
  417. *
  418. * CONTEXT:
  419. * pcpu_lock.
  420. *
  421. * RETURNS:
  422. * Allocated offset in @chunk on success, -1 if no matching area is
  423. * found.
  424. */
  425. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
  426. {
  427. int oslot = pcpu_chunk_slot(chunk);
  428. int max_contig = 0;
  429. int i, off;
  430. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
  431. bool is_last = i + 1 == chunk->map_used;
  432. int head, tail;
  433. /* extra for alignment requirement */
  434. head = ALIGN(off, align) - off;
  435. BUG_ON(i == 0 && head != 0);
  436. if (chunk->map[i] < 0)
  437. continue;
  438. if (chunk->map[i] < head + size) {
  439. max_contig = max(chunk->map[i], max_contig);
  440. continue;
  441. }
  442. /*
  443. * If head is small or the previous block is free,
  444. * merge'em. Note that 'small' is defined as smaller
  445. * than sizeof(int), which is very small but isn't too
  446. * uncommon for percpu allocations.
  447. */
  448. if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
  449. if (chunk->map[i - 1] > 0)
  450. chunk->map[i - 1] += head;
  451. else {
  452. chunk->map[i - 1] -= head;
  453. chunk->free_size -= head;
  454. }
  455. chunk->map[i] -= head;
  456. off += head;
  457. head = 0;
  458. }
  459. /* if tail is small, just keep it around */
  460. tail = chunk->map[i] - head - size;
  461. if (tail < sizeof(int))
  462. tail = 0;
  463. /* split if warranted */
  464. if (head || tail) {
  465. pcpu_split_block(chunk, i, head, tail);
  466. if (head) {
  467. i++;
  468. off += head;
  469. max_contig = max(chunk->map[i - 1], max_contig);
  470. }
  471. if (tail)
  472. max_contig = max(chunk->map[i + 1], max_contig);
  473. }
  474. /* update hint and mark allocated */
  475. if (is_last)
  476. chunk->contig_hint = max_contig; /* fully scanned */
  477. else
  478. chunk->contig_hint = max(chunk->contig_hint,
  479. max_contig);
  480. chunk->free_size -= chunk->map[i];
  481. chunk->map[i] = -chunk->map[i];
  482. pcpu_chunk_relocate(chunk, oslot);
  483. return off;
  484. }
  485. chunk->contig_hint = max_contig; /* fully scanned */
  486. pcpu_chunk_relocate(chunk, oslot);
  487. /* tell the upper layer that this chunk has no matching area */
  488. return -1;
  489. }
  490. /**
  491. * pcpu_free_area - free area to a pcpu_chunk
  492. * @chunk: chunk of interest
  493. * @freeme: offset of area to free
  494. *
  495. * Free area starting from @freeme to @chunk. Note that this function
  496. * only modifies the allocation map. It doesn't depopulate or unmap
  497. * the area.
  498. *
  499. * CONTEXT:
  500. * pcpu_lock.
  501. */
  502. static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
  503. {
  504. int oslot = pcpu_chunk_slot(chunk);
  505. int i, off;
  506. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
  507. if (off == freeme)
  508. break;
  509. BUG_ON(off != freeme);
  510. BUG_ON(chunk->map[i] > 0);
  511. chunk->map[i] = -chunk->map[i];
  512. chunk->free_size += chunk->map[i];
  513. /* merge with previous? */
  514. if (i > 0 && chunk->map[i - 1] >= 0) {
  515. chunk->map[i - 1] += chunk->map[i];
  516. chunk->map_used--;
  517. memmove(&chunk->map[i], &chunk->map[i + 1],
  518. (chunk->map_used - i) * sizeof(chunk->map[0]));
  519. i--;
  520. }
  521. /* merge with next? */
  522. if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
  523. chunk->map[i] += chunk->map[i + 1];
  524. chunk->map_used--;
  525. memmove(&chunk->map[i + 1], &chunk->map[i + 2],
  526. (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
  527. }
  528. chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
  529. pcpu_chunk_relocate(chunk, oslot);
  530. }
  531. static struct pcpu_chunk *pcpu_alloc_chunk(void)
  532. {
  533. struct pcpu_chunk *chunk;
  534. chunk = pcpu_mem_alloc(pcpu_chunk_struct_size);
  535. if (!chunk)
  536. return NULL;
  537. chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  538. if (!chunk->map) {
  539. kfree(chunk);
  540. return NULL;
  541. }
  542. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  543. chunk->map[chunk->map_used++] = pcpu_unit_size;
  544. INIT_LIST_HEAD(&chunk->list);
  545. chunk->free_size = pcpu_unit_size;
  546. chunk->contig_hint = pcpu_unit_size;
  547. return chunk;
  548. }
  549. static void pcpu_free_chunk(struct pcpu_chunk *chunk)
  550. {
  551. if (!chunk)
  552. return;
  553. pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
  554. kfree(chunk);
  555. }
  556. /*
  557. * Chunk management implementation.
  558. *
  559. * To allow different implementations, chunk alloc/free and
  560. * [de]population are implemented in a separate file which is pulled
  561. * into this file and compiled together. The following functions
  562. * should be implemented.
  563. *
  564. * pcpu_populate_chunk - populate the specified range of a chunk
  565. * pcpu_depopulate_chunk - depopulate the specified range of a chunk
  566. * pcpu_create_chunk - create a new chunk
  567. * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop
  568. * pcpu_addr_to_page - translate address to physical address
  569. * pcpu_verify_alloc_info - check alloc_info is acceptable during init
  570. */
  571. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size);
  572. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size);
  573. static struct pcpu_chunk *pcpu_create_chunk(void);
  574. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk);
  575. static struct page *pcpu_addr_to_page(void *addr);
  576. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai);
  577. #ifdef CONFIG_NEED_PER_CPU_KM
  578. #include "percpu-km.c"
  579. #else
  580. #include "percpu-vm.c"
  581. #endif
  582. /**
  583. * pcpu_chunk_addr_search - determine chunk containing specified address
  584. * @addr: address for which the chunk needs to be determined.
  585. *
  586. * RETURNS:
  587. * The address of the found chunk.
  588. */
  589. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  590. {
  591. /* is it in the first chunk? */
  592. if (pcpu_addr_in_first_chunk(addr)) {
  593. /* is it in the reserved area? */
  594. if (pcpu_addr_in_reserved_chunk(addr))
  595. return pcpu_reserved_chunk;
  596. return pcpu_first_chunk;
  597. }
  598. /*
  599. * The address is relative to unit0 which might be unused and
  600. * thus unmapped. Offset the address to the unit space of the
  601. * current processor before looking it up in the vmalloc
  602. * space. Note that any possible cpu id can be used here, so
  603. * there's no need to worry about preemption or cpu hotplug.
  604. */
  605. addr += pcpu_unit_offsets[raw_smp_processor_id()];
  606. return pcpu_get_page_chunk(pcpu_addr_to_page(addr));
  607. }
  608. /**
  609. * pcpu_alloc - the percpu allocator
  610. * @size: size of area to allocate in bytes
  611. * @align: alignment of area (max PAGE_SIZE)
  612. * @reserved: allocate from the reserved chunk if available
  613. *
  614. * Allocate percpu area of @size bytes aligned at @align.
  615. *
  616. * CONTEXT:
  617. * Does GFP_KERNEL allocation.
  618. *
  619. * RETURNS:
  620. * Percpu pointer to the allocated area on success, NULL on failure.
  621. */
  622. static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved)
  623. {
  624. static int warn_limit = 10;
  625. struct pcpu_chunk *chunk;
  626. const char *err;
  627. int slot, off, new_alloc;
  628. unsigned long flags;
  629. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
  630. WARN(true, "illegal size (%zu) or align (%zu) for "
  631. "percpu allocation\n", size, align);
  632. return NULL;
  633. }
  634. mutex_lock(&pcpu_alloc_mutex);
  635. spin_lock_irqsave(&pcpu_lock, flags);
  636. /* serve reserved allocations from the reserved chunk if available */
  637. if (reserved && pcpu_reserved_chunk) {
  638. chunk = pcpu_reserved_chunk;
  639. if (size > chunk->contig_hint) {
  640. err = "alloc from reserved chunk failed";
  641. goto fail_unlock;
  642. }
  643. while ((new_alloc = pcpu_need_to_extend(chunk))) {
  644. spin_unlock_irqrestore(&pcpu_lock, flags);
  645. if (pcpu_extend_area_map(chunk, new_alloc) < 0) {
  646. err = "failed to extend area map of reserved chunk";
  647. goto fail_unlock_mutex;
  648. }
  649. spin_lock_irqsave(&pcpu_lock, flags);
  650. }
  651. off = pcpu_alloc_area(chunk, size, align);
  652. if (off >= 0)
  653. goto area_found;
  654. err = "alloc from reserved chunk failed";
  655. goto fail_unlock;
  656. }
  657. restart:
  658. /* search through normal chunks */
  659. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  660. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  661. if (size > chunk->contig_hint)
  662. continue;
  663. new_alloc = pcpu_need_to_extend(chunk);
  664. if (new_alloc) {
  665. spin_unlock_irqrestore(&pcpu_lock, flags);
  666. if (pcpu_extend_area_map(chunk,
  667. new_alloc) < 0) {
  668. err = "failed to extend area map";
  669. goto fail_unlock_mutex;
  670. }
  671. spin_lock_irqsave(&pcpu_lock, flags);
  672. /*
  673. * pcpu_lock has been dropped, need to
  674. * restart cpu_slot list walking.
  675. */
  676. goto restart;
  677. }
  678. off = pcpu_alloc_area(chunk, size, align);
  679. if (off >= 0)
  680. goto area_found;
  681. }
  682. }
  683. /* hmmm... no space left, create a new chunk */
  684. spin_unlock_irqrestore(&pcpu_lock, flags);
  685. chunk = pcpu_create_chunk();
  686. if (!chunk) {
  687. err = "failed to allocate new chunk";
  688. goto fail_unlock_mutex;
  689. }
  690. spin_lock_irqsave(&pcpu_lock, flags);
  691. pcpu_chunk_relocate(chunk, -1);
  692. goto restart;
  693. area_found:
  694. spin_unlock_irqrestore(&pcpu_lock, flags);
  695. /* populate, map and clear the area */
  696. if (pcpu_populate_chunk(chunk, off, size)) {
  697. spin_lock_irqsave(&pcpu_lock, flags);
  698. pcpu_free_area(chunk, off);
  699. err = "failed to populate";
  700. goto fail_unlock;
  701. }
  702. mutex_unlock(&pcpu_alloc_mutex);
  703. /* return address relative to base address */
  704. return __addr_to_pcpu_ptr(chunk->base_addr + off);
  705. fail_unlock:
  706. spin_unlock_irqrestore(&pcpu_lock, flags);
  707. fail_unlock_mutex:
  708. mutex_unlock(&pcpu_alloc_mutex);
  709. if (warn_limit) {
  710. pr_warning("PERCPU: allocation failed, size=%zu align=%zu, "
  711. "%s\n", size, align, err);
  712. dump_stack();
  713. if (!--warn_limit)
  714. pr_info("PERCPU: limit reached, disable warning\n");
  715. }
  716. return NULL;
  717. }
  718. /**
  719. * __alloc_percpu - allocate dynamic percpu area
  720. * @size: size of area to allocate in bytes
  721. * @align: alignment of area (max PAGE_SIZE)
  722. *
  723. * Allocate zero-filled percpu area of @size bytes aligned at @align.
  724. * Might sleep. Might trigger writeouts.
  725. *
  726. * CONTEXT:
  727. * Does GFP_KERNEL allocation.
  728. *
  729. * RETURNS:
  730. * Percpu pointer to the allocated area on success, NULL on failure.
  731. */
  732. void __percpu *__alloc_percpu(size_t size, size_t align)
  733. {
  734. return pcpu_alloc(size, align, false);
  735. }
  736. EXPORT_SYMBOL_GPL(__alloc_percpu);
  737. /**
  738. * __alloc_reserved_percpu - allocate reserved percpu area
  739. * @size: size of area to allocate in bytes
  740. * @align: alignment of area (max PAGE_SIZE)
  741. *
  742. * Allocate zero-filled percpu area of @size bytes aligned at @align
  743. * from reserved percpu area if arch has set it up; otherwise,
  744. * allocation is served from the same dynamic area. Might sleep.
  745. * Might trigger writeouts.
  746. *
  747. * CONTEXT:
  748. * Does GFP_KERNEL allocation.
  749. *
  750. * RETURNS:
  751. * Percpu pointer to the allocated area on success, NULL on failure.
  752. */
  753. void __percpu *__alloc_reserved_percpu(size_t size, size_t align)
  754. {
  755. return pcpu_alloc(size, align, true);
  756. }
  757. /**
  758. * pcpu_reclaim - reclaim fully free chunks, workqueue function
  759. * @work: unused
  760. *
  761. * Reclaim all fully free chunks except for the first one.
  762. *
  763. * CONTEXT:
  764. * workqueue context.
  765. */
  766. static void pcpu_reclaim(struct work_struct *work)
  767. {
  768. LIST_HEAD(todo);
  769. struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
  770. struct pcpu_chunk *chunk, *next;
  771. mutex_lock(&pcpu_alloc_mutex);
  772. spin_lock_irq(&pcpu_lock);
  773. list_for_each_entry_safe(chunk, next, head, list) {
  774. WARN_ON(chunk->immutable);
  775. /* spare the first one */
  776. if (chunk == list_first_entry(head, struct pcpu_chunk, list))
  777. continue;
  778. list_move(&chunk->list, &todo);
  779. }
  780. spin_unlock_irq(&pcpu_lock);
  781. list_for_each_entry_safe(chunk, next, &todo, list) {
  782. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size);
  783. pcpu_destroy_chunk(chunk);
  784. }
  785. mutex_unlock(&pcpu_alloc_mutex);
  786. }
  787. /**
  788. * free_percpu - free percpu area
  789. * @ptr: pointer to area to free
  790. *
  791. * Free percpu area @ptr.
  792. *
  793. * CONTEXT:
  794. * Can be called from atomic context.
  795. */
  796. void free_percpu(void __percpu *ptr)
  797. {
  798. void *addr;
  799. struct pcpu_chunk *chunk;
  800. unsigned long flags;
  801. int off;
  802. if (!ptr)
  803. return;
  804. addr = __pcpu_ptr_to_addr(ptr);
  805. spin_lock_irqsave(&pcpu_lock, flags);
  806. chunk = pcpu_chunk_addr_search(addr);
  807. off = addr - chunk->base_addr;
  808. pcpu_free_area(chunk, off);
  809. /* if there are more than one fully free chunks, wake up grim reaper */
  810. if (chunk->free_size == pcpu_unit_size) {
  811. struct pcpu_chunk *pos;
  812. list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
  813. if (pos != chunk) {
  814. schedule_work(&pcpu_reclaim_work);
  815. break;
  816. }
  817. }
  818. spin_unlock_irqrestore(&pcpu_lock, flags);
  819. }
  820. EXPORT_SYMBOL_GPL(free_percpu);
  821. /**
  822. * is_kernel_percpu_address - test whether address is from static percpu area
  823. * @addr: address to test
  824. *
  825. * Test whether @addr belongs to in-kernel static percpu area. Module
  826. * static percpu areas are not considered. For those, use
  827. * is_module_percpu_address().
  828. *
  829. * RETURNS:
  830. * %true if @addr is from in-kernel static percpu area, %false otherwise.
  831. */
  832. bool is_kernel_percpu_address(unsigned long addr)
  833. {
  834. #ifdef CONFIG_SMP
  835. const size_t static_size = __per_cpu_end - __per_cpu_start;
  836. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  837. unsigned int cpu;
  838. for_each_possible_cpu(cpu) {
  839. void *start = per_cpu_ptr(base, cpu);
  840. if ((void *)addr >= start && (void *)addr < start + static_size)
  841. return true;
  842. }
  843. #endif
  844. /* on UP, can't distinguish from other static vars, always false */
  845. return false;
  846. }
  847. /**
  848. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  849. * @addr: the address to be converted to physical address
  850. *
  851. * Given @addr which is dereferenceable address obtained via one of
  852. * percpu access macros, this function translates it into its physical
  853. * address. The caller is responsible for ensuring @addr stays valid
  854. * until this function finishes.
  855. *
  856. * RETURNS:
  857. * The physical address for @addr.
  858. */
  859. phys_addr_t per_cpu_ptr_to_phys(void *addr)
  860. {
  861. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  862. bool in_first_chunk = false;
  863. unsigned long first_start, first_end;
  864. unsigned int cpu;
  865. /*
  866. * The following test on first_start/end isn't strictly
  867. * necessary but will speed up lookups of addresses which
  868. * aren't in the first chunk.
  869. */
  870. first_start = pcpu_chunk_addr(pcpu_first_chunk, pcpu_first_unit_cpu, 0);
  871. first_end = pcpu_chunk_addr(pcpu_first_chunk, pcpu_last_unit_cpu,
  872. pcpu_unit_pages);
  873. if ((unsigned long)addr >= first_start &&
  874. (unsigned long)addr < first_end) {
  875. for_each_possible_cpu(cpu) {
  876. void *start = per_cpu_ptr(base, cpu);
  877. if (addr >= start && addr < start + pcpu_unit_size) {
  878. in_first_chunk = true;
  879. break;
  880. }
  881. }
  882. }
  883. if (in_first_chunk) {
  884. if (!is_vmalloc_addr(addr))
  885. return __pa(addr);
  886. else
  887. return page_to_phys(vmalloc_to_page(addr));
  888. } else
  889. return page_to_phys(pcpu_addr_to_page(addr));
  890. }
  891. /**
  892. * pcpu_alloc_alloc_info - allocate percpu allocation info
  893. * @nr_groups: the number of groups
  894. * @nr_units: the number of units
  895. *
  896. * Allocate ai which is large enough for @nr_groups groups containing
  897. * @nr_units units. The returned ai's groups[0].cpu_map points to the
  898. * cpu_map array which is long enough for @nr_units and filled with
  899. * NR_CPUS. It's the caller's responsibility to initialize cpu_map
  900. * pointer of other groups.
  901. *
  902. * RETURNS:
  903. * Pointer to the allocated pcpu_alloc_info on success, NULL on
  904. * failure.
  905. */
  906. struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  907. int nr_units)
  908. {
  909. struct pcpu_alloc_info *ai;
  910. size_t base_size, ai_size;
  911. void *ptr;
  912. int unit;
  913. base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]),
  914. __alignof__(ai->groups[0].cpu_map[0]));
  915. ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]);
  916. ptr = alloc_bootmem_nopanic(PFN_ALIGN(ai_size));
  917. if (!ptr)
  918. return NULL;
  919. ai = ptr;
  920. ptr += base_size;
  921. ai->groups[0].cpu_map = ptr;
  922. for (unit = 0; unit < nr_units; unit++)
  923. ai->groups[0].cpu_map[unit] = NR_CPUS;
  924. ai->nr_groups = nr_groups;
  925. ai->__ai_size = PFN_ALIGN(ai_size);
  926. return ai;
  927. }
  928. /**
  929. * pcpu_free_alloc_info - free percpu allocation info
  930. * @ai: pcpu_alloc_info to free
  931. *
  932. * Free @ai which was allocated by pcpu_alloc_alloc_info().
  933. */
  934. void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
  935. {
  936. free_bootmem(__pa(ai), ai->__ai_size);
  937. }
  938. /**
  939. * pcpu_dump_alloc_info - print out information about pcpu_alloc_info
  940. * @lvl: loglevel
  941. * @ai: allocation info to dump
  942. *
  943. * Print out information about @ai using loglevel @lvl.
  944. */
  945. static void pcpu_dump_alloc_info(const char *lvl,
  946. const struct pcpu_alloc_info *ai)
  947. {
  948. int group_width = 1, cpu_width = 1, width;
  949. char empty_str[] = "--------";
  950. int alloc = 0, alloc_end = 0;
  951. int group, v;
  952. int upa, apl; /* units per alloc, allocs per line */
  953. v = ai->nr_groups;
  954. while (v /= 10)
  955. group_width++;
  956. v = num_possible_cpus();
  957. while (v /= 10)
  958. cpu_width++;
  959. empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0';
  960. upa = ai->alloc_size / ai->unit_size;
  961. width = upa * (cpu_width + 1) + group_width + 3;
  962. apl = rounddown_pow_of_two(max(60 / width, 1));
  963. printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu",
  964. lvl, ai->static_size, ai->reserved_size, ai->dyn_size,
  965. ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size);
  966. for (group = 0; group < ai->nr_groups; group++) {
  967. const struct pcpu_group_info *gi = &ai->groups[group];
  968. int unit = 0, unit_end = 0;
  969. BUG_ON(gi->nr_units % upa);
  970. for (alloc_end += gi->nr_units / upa;
  971. alloc < alloc_end; alloc++) {
  972. if (!(alloc % apl)) {
  973. printk("\n");
  974. printk("%spcpu-alloc: ", lvl);
  975. }
  976. printk("[%0*d] ", group_width, group);
  977. for (unit_end += upa; unit < unit_end; unit++)
  978. if (gi->cpu_map[unit] != NR_CPUS)
  979. printk("%0*d ", cpu_width,
  980. gi->cpu_map[unit]);
  981. else
  982. printk("%s ", empty_str);
  983. }
  984. }
  985. printk("\n");
  986. }
  987. /**
  988. * pcpu_setup_first_chunk - initialize the first percpu chunk
  989. * @ai: pcpu_alloc_info describing how to percpu area is shaped
  990. * @base_addr: mapped address
  991. *
  992. * Initialize the first percpu chunk which contains the kernel static
  993. * perpcu area. This function is to be called from arch percpu area
  994. * setup path.
  995. *
  996. * @ai contains all information necessary to initialize the first
  997. * chunk and prime the dynamic percpu allocator.
  998. *
  999. * @ai->static_size is the size of static percpu area.
  1000. *
  1001. * @ai->reserved_size, if non-zero, specifies the amount of bytes to
  1002. * reserve after the static area in the first chunk. This reserves
  1003. * the first chunk such that it's available only through reserved
  1004. * percpu allocation. This is primarily used to serve module percpu
  1005. * static areas on architectures where the addressing model has
  1006. * limited offset range for symbol relocations to guarantee module
  1007. * percpu symbols fall inside the relocatable range.
  1008. *
  1009. * @ai->dyn_size determines the number of bytes available for dynamic
  1010. * allocation in the first chunk. The area between @ai->static_size +
  1011. * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused.
  1012. *
  1013. * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE
  1014. * and equal to or larger than @ai->static_size + @ai->reserved_size +
  1015. * @ai->dyn_size.
  1016. *
  1017. * @ai->atom_size is the allocation atom size and used as alignment
  1018. * for vm areas.
  1019. *
  1020. * @ai->alloc_size is the allocation size and always multiple of
  1021. * @ai->atom_size. This is larger than @ai->atom_size if
  1022. * @ai->unit_size is larger than @ai->atom_size.
  1023. *
  1024. * @ai->nr_groups and @ai->groups describe virtual memory layout of
  1025. * percpu areas. Units which should be colocated are put into the
  1026. * same group. Dynamic VM areas will be allocated according to these
  1027. * groupings. If @ai->nr_groups is zero, a single group containing
  1028. * all units is assumed.
  1029. *
  1030. * The caller should have mapped the first chunk at @base_addr and
  1031. * copied static data to each unit.
  1032. *
  1033. * If the first chunk ends up with both reserved and dynamic areas, it
  1034. * is served by two chunks - one to serve the core static and reserved
  1035. * areas and the other for the dynamic area. They share the same vm
  1036. * and page map but uses different area allocation map to stay away
  1037. * from each other. The latter chunk is circulated in the chunk slots
  1038. * and available for dynamic allocation like any other chunks.
  1039. *
  1040. * RETURNS:
  1041. * 0 on success, -errno on failure.
  1042. */
  1043. int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  1044. void *base_addr)
  1045. {
  1046. static char cpus_buf[4096] __initdata;
  1047. static int smap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
  1048. static int dmap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
  1049. size_t dyn_size = ai->dyn_size;
  1050. size_t size_sum = ai->static_size + ai->reserved_size + dyn_size;
  1051. struct pcpu_chunk *schunk, *dchunk = NULL;
  1052. unsigned long *group_offsets;
  1053. size_t *group_sizes;
  1054. unsigned long *unit_off;
  1055. unsigned int cpu;
  1056. int *unit_map;
  1057. int group, unit, i;
  1058. cpumask_scnprintf(cpus_buf, sizeof(cpus_buf), cpu_possible_mask);
  1059. #define PCPU_SETUP_BUG_ON(cond) do { \
  1060. if (unlikely(cond)) { \
  1061. pr_emerg("PERCPU: failed to initialize, %s", #cond); \
  1062. pr_emerg("PERCPU: cpu_possible_mask=%s\n", cpus_buf); \
  1063. pcpu_dump_alloc_info(KERN_EMERG, ai); \
  1064. BUG(); \
  1065. } \
  1066. } while (0)
  1067. /* sanity checks */
  1068. PCPU_SETUP_BUG_ON(ai->nr_groups <= 0);
  1069. #ifdef CONFIG_SMP
  1070. PCPU_SETUP_BUG_ON(!ai->static_size);
  1071. #endif
  1072. PCPU_SETUP_BUG_ON(!base_addr);
  1073. PCPU_SETUP_BUG_ON(ai->unit_size < size_sum);
  1074. PCPU_SETUP_BUG_ON(ai->unit_size & ~PAGE_MASK);
  1075. PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE);
  1076. PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE);
  1077. PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
  1078. /* process group information and build config tables accordingly */
  1079. group_offsets = alloc_bootmem(ai->nr_groups * sizeof(group_offsets[0]));
  1080. group_sizes = alloc_bootmem(ai->nr_groups * sizeof(group_sizes[0]));
  1081. unit_map = alloc_bootmem(nr_cpu_ids * sizeof(unit_map[0]));
  1082. unit_off = alloc_bootmem(nr_cpu_ids * sizeof(unit_off[0]));
  1083. for (cpu = 0; cpu < nr_cpu_ids; cpu++)
  1084. unit_map[cpu] = UINT_MAX;
  1085. pcpu_first_unit_cpu = NR_CPUS;
  1086. for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) {
  1087. const struct pcpu_group_info *gi = &ai->groups[group];
  1088. group_offsets[group] = gi->base_offset;
  1089. group_sizes[group] = gi->nr_units * ai->unit_size;
  1090. for (i = 0; i < gi->nr_units; i++) {
  1091. cpu = gi->cpu_map[i];
  1092. if (cpu == NR_CPUS)
  1093. continue;
  1094. PCPU_SETUP_BUG_ON(cpu > nr_cpu_ids);
  1095. PCPU_SETUP_BUG_ON(!cpu_possible(cpu));
  1096. PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX);
  1097. unit_map[cpu] = unit + i;
  1098. unit_off[cpu] = gi->base_offset + i * ai->unit_size;
  1099. if (pcpu_first_unit_cpu == NR_CPUS)
  1100. pcpu_first_unit_cpu = cpu;
  1101. pcpu_last_unit_cpu = cpu;
  1102. }
  1103. }
  1104. pcpu_nr_units = unit;
  1105. for_each_possible_cpu(cpu)
  1106. PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX);
  1107. /* we're done parsing the input, undefine BUG macro and dump config */
  1108. #undef PCPU_SETUP_BUG_ON
  1109. pcpu_dump_alloc_info(KERN_DEBUG, ai);
  1110. pcpu_nr_groups = ai->nr_groups;
  1111. pcpu_group_offsets = group_offsets;
  1112. pcpu_group_sizes = group_sizes;
  1113. pcpu_unit_map = unit_map;
  1114. pcpu_unit_offsets = unit_off;
  1115. /* determine basic parameters */
  1116. pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
  1117. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  1118. pcpu_atom_size = ai->atom_size;
  1119. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
  1120. BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
  1121. /*
  1122. * Allocate chunk slots. The additional last slot is for
  1123. * empty chunks.
  1124. */
  1125. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  1126. pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
  1127. for (i = 0; i < pcpu_nr_slots; i++)
  1128. INIT_LIST_HEAD(&pcpu_slot[i]);
  1129. /*
  1130. * Initialize static chunk. If reserved_size is zero, the
  1131. * static chunk covers static area + dynamic allocation area
  1132. * in the first chunk. If reserved_size is not zero, it
  1133. * covers static area + reserved area (mostly used for module
  1134. * static percpu allocation).
  1135. */
  1136. schunk = alloc_bootmem(pcpu_chunk_struct_size);
  1137. INIT_LIST_HEAD(&schunk->list);
  1138. schunk->base_addr = base_addr;
  1139. schunk->map = smap;
  1140. schunk->map_alloc = ARRAY_SIZE(smap);
  1141. schunk->immutable = true;
  1142. bitmap_fill(schunk->populated, pcpu_unit_pages);
  1143. if (ai->reserved_size) {
  1144. schunk->free_size = ai->reserved_size;
  1145. pcpu_reserved_chunk = schunk;
  1146. pcpu_reserved_chunk_limit = ai->static_size + ai->reserved_size;
  1147. } else {
  1148. schunk->free_size = dyn_size;
  1149. dyn_size = 0; /* dynamic area covered */
  1150. }
  1151. schunk->contig_hint = schunk->free_size;
  1152. schunk->map[schunk->map_used++] = -ai->static_size;
  1153. if (schunk->free_size)
  1154. schunk->map[schunk->map_used++] = schunk->free_size;
  1155. /* init dynamic chunk if necessary */
  1156. if (dyn_size) {
  1157. dchunk = alloc_bootmem(pcpu_chunk_struct_size);
  1158. INIT_LIST_HEAD(&dchunk->list);
  1159. dchunk->base_addr = base_addr;
  1160. dchunk->map = dmap;
  1161. dchunk->map_alloc = ARRAY_SIZE(dmap);
  1162. dchunk->immutable = true;
  1163. bitmap_fill(dchunk->populated, pcpu_unit_pages);
  1164. dchunk->contig_hint = dchunk->free_size = dyn_size;
  1165. dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
  1166. dchunk->map[dchunk->map_used++] = dchunk->free_size;
  1167. }
  1168. /* link the first chunk in */
  1169. pcpu_first_chunk = dchunk ?: schunk;
  1170. pcpu_chunk_relocate(pcpu_first_chunk, -1);
  1171. /* we're done */
  1172. pcpu_base_addr = base_addr;
  1173. return 0;
  1174. }
  1175. #ifdef CONFIG_SMP
  1176. const char *pcpu_fc_names[PCPU_FC_NR] __initdata = {
  1177. [PCPU_FC_AUTO] = "auto",
  1178. [PCPU_FC_EMBED] = "embed",
  1179. [PCPU_FC_PAGE] = "page",
  1180. };
  1181. enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO;
  1182. static int __init percpu_alloc_setup(char *str)
  1183. {
  1184. if (0)
  1185. /* nada */;
  1186. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  1187. else if (!strcmp(str, "embed"))
  1188. pcpu_chosen_fc = PCPU_FC_EMBED;
  1189. #endif
  1190. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  1191. else if (!strcmp(str, "page"))
  1192. pcpu_chosen_fc = PCPU_FC_PAGE;
  1193. #endif
  1194. else
  1195. pr_warning("PERCPU: unknown allocator %s specified\n", str);
  1196. return 0;
  1197. }
  1198. early_param("percpu_alloc", percpu_alloc_setup);
  1199. /*
  1200. * pcpu_embed_first_chunk() is used by the generic percpu setup.
  1201. * Build it if needed by the arch config or the generic setup is going
  1202. * to be used.
  1203. */
  1204. #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \
  1205. !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  1206. #define BUILD_EMBED_FIRST_CHUNK
  1207. #endif
  1208. /* build pcpu_page_first_chunk() iff needed by the arch config */
  1209. #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK)
  1210. #define BUILD_PAGE_FIRST_CHUNK
  1211. #endif
  1212. /* pcpu_build_alloc_info() is used by both embed and page first chunk */
  1213. #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK)
  1214. /**
  1215. * pcpu_build_alloc_info - build alloc_info considering distances between CPUs
  1216. * @reserved_size: the size of reserved percpu area in bytes
  1217. * @dyn_size: minimum free size for dynamic allocation in bytes
  1218. * @atom_size: allocation atom size
  1219. * @cpu_distance_fn: callback to determine distance between cpus, optional
  1220. *
  1221. * This function determines grouping of units, their mappings to cpus
  1222. * and other parameters considering needed percpu size, allocation
  1223. * atom size and distances between CPUs.
  1224. *
  1225. * Groups are always mutliples of atom size and CPUs which are of
  1226. * LOCAL_DISTANCE both ways are grouped together and share space for
  1227. * units in the same group. The returned configuration is guaranteed
  1228. * to have CPUs on different nodes on different groups and >=75% usage
  1229. * of allocated virtual address space.
  1230. *
  1231. * RETURNS:
  1232. * On success, pointer to the new allocation_info is returned. On
  1233. * failure, ERR_PTR value is returned.
  1234. */
  1235. static struct pcpu_alloc_info * __init pcpu_build_alloc_info(
  1236. size_t reserved_size, size_t dyn_size,
  1237. size_t atom_size,
  1238. pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
  1239. {
  1240. static int group_map[NR_CPUS] __initdata;
  1241. static int group_cnt[NR_CPUS] __initdata;
  1242. const size_t static_size = __per_cpu_end - __per_cpu_start;
  1243. int nr_groups = 1, nr_units = 0;
  1244. size_t size_sum, min_unit_size, alloc_size;
  1245. int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */
  1246. int last_allocs, group, unit;
  1247. unsigned int cpu, tcpu;
  1248. struct pcpu_alloc_info *ai;
  1249. unsigned int *cpu_map;
  1250. /* this function may be called multiple times */
  1251. memset(group_map, 0, sizeof(group_map));
  1252. memset(group_cnt, 0, sizeof(group_cnt));
  1253. /* calculate size_sum and ensure dyn_size is enough for early alloc */
  1254. size_sum = PFN_ALIGN(static_size + reserved_size +
  1255. max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE));
  1256. dyn_size = size_sum - static_size - reserved_size;
  1257. /*
  1258. * Determine min_unit_size, alloc_size and max_upa such that
  1259. * alloc_size is multiple of atom_size and is the smallest
  1260. * which can accommodate 4k aligned segments which are equal to
  1261. * or larger than min_unit_size.
  1262. */
  1263. min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
  1264. alloc_size = roundup(min_unit_size, atom_size);
  1265. upa = alloc_size / min_unit_size;
  1266. while (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK))
  1267. upa--;
  1268. max_upa = upa;
  1269. /* group cpus according to their proximity */
  1270. for_each_possible_cpu(cpu) {
  1271. group = 0;
  1272. next_group:
  1273. for_each_possible_cpu(tcpu) {
  1274. if (cpu == tcpu)
  1275. break;
  1276. if (group_map[tcpu] == group && cpu_distance_fn &&
  1277. (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE ||
  1278. cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) {
  1279. group++;
  1280. nr_groups = max(nr_groups, group + 1);
  1281. goto next_group;
  1282. }
  1283. }
  1284. group_map[cpu] = group;
  1285. group_cnt[group]++;
  1286. }
  1287. /*
  1288. * Expand unit size until address space usage goes over 75%
  1289. * and then as much as possible without using more address
  1290. * space.
  1291. */
  1292. last_allocs = INT_MAX;
  1293. for (upa = max_upa; upa; upa--) {
  1294. int allocs = 0, wasted = 0;
  1295. if (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK))
  1296. continue;
  1297. for (group = 0; group < nr_groups; group++) {
  1298. int this_allocs = DIV_ROUND_UP(group_cnt[group], upa);
  1299. allocs += this_allocs;
  1300. wasted += this_allocs * upa - group_cnt[group];
  1301. }
  1302. /*
  1303. * Don't accept if wastage is over 1/3. The
  1304. * greater-than comparison ensures upa==1 always
  1305. * passes the following check.
  1306. */
  1307. if (wasted > num_possible_cpus() / 3)
  1308. continue;
  1309. /* and then don't consume more memory */
  1310. if (allocs > last_allocs)
  1311. break;
  1312. last_allocs = allocs;
  1313. best_upa = upa;
  1314. }
  1315. upa = best_upa;
  1316. /* allocate and fill alloc_info */
  1317. for (group = 0; group < nr_groups; group++)
  1318. nr_units += roundup(group_cnt[group], upa);
  1319. ai = pcpu_alloc_alloc_info(nr_groups, nr_units);
  1320. if (!ai)
  1321. return ERR_PTR(-ENOMEM);
  1322. cpu_map = ai->groups[0].cpu_map;
  1323. for (group = 0; group < nr_groups; group++) {
  1324. ai->groups[group].cpu_map = cpu_map;
  1325. cpu_map += roundup(group_cnt[group], upa);
  1326. }
  1327. ai->static_size = static_size;
  1328. ai->reserved_size = reserved_size;
  1329. ai->dyn_size = dyn_size;
  1330. ai->unit_size = alloc_size / upa;
  1331. ai->atom_size = atom_size;
  1332. ai->alloc_size = alloc_size;
  1333. for (group = 0, unit = 0; group_cnt[group]; group++) {
  1334. struct pcpu_group_info *gi = &ai->groups[group];
  1335. /*
  1336. * Initialize base_offset as if all groups are located
  1337. * back-to-back. The caller should update this to
  1338. * reflect actual allocation.
  1339. */
  1340. gi->base_offset = unit * ai->unit_size;
  1341. for_each_possible_cpu(cpu)
  1342. if (group_map[cpu] == group)
  1343. gi->cpu_map[gi->nr_units++] = cpu;
  1344. gi->nr_units = roundup(gi->nr_units, upa);
  1345. unit += gi->nr_units;
  1346. }
  1347. BUG_ON(unit != nr_units);
  1348. return ai;
  1349. }
  1350. #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */
  1351. #if defined(BUILD_EMBED_FIRST_CHUNK)
  1352. /**
  1353. * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
  1354. * @reserved_size: the size of reserved percpu area in bytes
  1355. * @dyn_size: minimum free size for dynamic allocation in bytes
  1356. * @atom_size: allocation atom size
  1357. * @cpu_distance_fn: callback to determine distance between cpus, optional
  1358. * @alloc_fn: function to allocate percpu page
  1359. * @free_fn: function to free percpu page
  1360. *
  1361. * This is a helper to ease setting up embedded first percpu chunk and
  1362. * can be called where pcpu_setup_first_chunk() is expected.
  1363. *
  1364. * If this function is used to setup the first chunk, it is allocated
  1365. * by calling @alloc_fn and used as-is without being mapped into
  1366. * vmalloc area. Allocations are always whole multiples of @atom_size
  1367. * aligned to @atom_size.
  1368. *
  1369. * This enables the first chunk to piggy back on the linear physical
  1370. * mapping which often uses larger page size. Please note that this
  1371. * can result in very sparse cpu->unit mapping on NUMA machines thus
  1372. * requiring large vmalloc address space. Don't use this allocator if
  1373. * vmalloc space is not orders of magnitude larger than distances
  1374. * between node memory addresses (ie. 32bit NUMA machines).
  1375. *
  1376. * @dyn_size specifies the minimum dynamic area size.
  1377. *
  1378. * If the needed size is smaller than the minimum or specified unit
  1379. * size, the leftover is returned using @free_fn.
  1380. *
  1381. * RETURNS:
  1382. * 0 on success, -errno on failure.
  1383. */
  1384. int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  1385. size_t atom_size,
  1386. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  1387. pcpu_fc_alloc_fn_t alloc_fn,
  1388. pcpu_fc_free_fn_t free_fn)
  1389. {
  1390. void *base = (void *)ULONG_MAX;
  1391. void **areas = NULL;
  1392. struct pcpu_alloc_info *ai;
  1393. size_t size_sum, areas_size, max_distance;
  1394. int group, i, rc;
  1395. ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size,
  1396. cpu_distance_fn);
  1397. if (IS_ERR(ai))
  1398. return PTR_ERR(ai);
  1399. size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
  1400. areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *));
  1401. areas = alloc_bootmem_nopanic(areas_size);
  1402. if (!areas) {
  1403. rc = -ENOMEM;
  1404. goto out_free;
  1405. }
  1406. /* allocate, copy and determine base address */
  1407. for (group = 0; group < ai->nr_groups; group++) {
  1408. struct pcpu_group_info *gi = &ai->groups[group];
  1409. unsigned int cpu = NR_CPUS;
  1410. void *ptr;
  1411. for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++)
  1412. cpu = gi->cpu_map[i];
  1413. BUG_ON(cpu == NR_CPUS);
  1414. /* allocate space for the whole group */
  1415. ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size);
  1416. if (!ptr) {
  1417. rc = -ENOMEM;
  1418. goto out_free_areas;
  1419. }
  1420. areas[group] = ptr;
  1421. base = min(ptr, base);
  1422. for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) {
  1423. if (gi->cpu_map[i] == NR_CPUS) {
  1424. /* unused unit, free whole */
  1425. free_fn(ptr, ai->unit_size);
  1426. continue;
  1427. }
  1428. /* copy and return the unused part */
  1429. memcpy(ptr, __per_cpu_load, ai->static_size);
  1430. free_fn(ptr + size_sum, ai->unit_size - size_sum);
  1431. }
  1432. }
  1433. /* base address is now known, determine group base offsets */
  1434. max_distance = 0;
  1435. for (group = 0; group < ai->nr_groups; group++) {
  1436. ai->groups[group].base_offset = areas[group] - base;
  1437. max_distance = max_t(size_t, max_distance,
  1438. ai->groups[group].base_offset);
  1439. }
  1440. max_distance += ai->unit_size;
  1441. /* warn if maximum distance is further than 75% of vmalloc space */
  1442. if (max_distance > (VMALLOC_END - VMALLOC_START) * 3 / 4) {
  1443. pr_warning("PERCPU: max_distance=0x%zx too large for vmalloc "
  1444. "space 0x%lx\n",
  1445. max_distance, VMALLOC_END - VMALLOC_START);
  1446. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  1447. /* and fail if we have fallback */
  1448. rc = -EINVAL;
  1449. goto out_free;
  1450. #endif
  1451. }
  1452. pr_info("PERCPU: Embedded %zu pages/cpu @%p s%zu r%zu d%zu u%zu\n",
  1453. PFN_DOWN(size_sum), base, ai->static_size, ai->reserved_size,
  1454. ai->dyn_size, ai->unit_size);
  1455. rc = pcpu_setup_first_chunk(ai, base);
  1456. goto out_free;
  1457. out_free_areas:
  1458. for (group = 0; group < ai->nr_groups; group++)
  1459. free_fn(areas[group],
  1460. ai->groups[group].nr_units * ai->unit_size);
  1461. out_free:
  1462. pcpu_free_alloc_info(ai);
  1463. if (areas)
  1464. free_bootmem(__pa(areas), areas_size);
  1465. return rc;
  1466. }
  1467. #endif /* BUILD_EMBED_FIRST_CHUNK */
  1468. #ifdef BUILD_PAGE_FIRST_CHUNK
  1469. /**
  1470. * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages
  1471. * @reserved_size: the size of reserved percpu area in bytes
  1472. * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE
  1473. * @free_fn: function to free percpu page, always called with PAGE_SIZE
  1474. * @populate_pte_fn: function to populate pte
  1475. *
  1476. * This is a helper to ease setting up page-remapped first percpu
  1477. * chunk and can be called where pcpu_setup_first_chunk() is expected.
  1478. *
  1479. * This is the basic allocator. Static percpu area is allocated
  1480. * page-by-page into vmalloc area.
  1481. *
  1482. * RETURNS:
  1483. * 0 on success, -errno on failure.
  1484. */
  1485. int __init pcpu_page_first_chunk(size_t reserved_size,
  1486. pcpu_fc_alloc_fn_t alloc_fn,
  1487. pcpu_fc_free_fn_t free_fn,
  1488. pcpu_fc_populate_pte_fn_t populate_pte_fn)
  1489. {
  1490. static struct vm_struct vm;
  1491. struct pcpu_alloc_info *ai;
  1492. char psize_str[16];
  1493. int unit_pages;
  1494. size_t pages_size;
  1495. struct page **pages;
  1496. int unit, i, j, rc;
  1497. snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10);
  1498. ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL);
  1499. if (IS_ERR(ai))
  1500. return PTR_ERR(ai);
  1501. BUG_ON(ai->nr_groups != 1);
  1502. BUG_ON(ai->groups[0].nr_units != num_possible_cpus());
  1503. unit_pages = ai->unit_size >> PAGE_SHIFT;
  1504. /* unaligned allocations can't be freed, round up to page size */
  1505. pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
  1506. sizeof(pages[0]));
  1507. pages = alloc_bootmem(pages_size);
  1508. /* allocate pages */
  1509. j = 0;
  1510. for (unit = 0; unit < num_possible_cpus(); unit++)
  1511. for (i = 0; i < unit_pages; i++) {
  1512. unsigned int cpu = ai->groups[0].cpu_map[unit];
  1513. void *ptr;
  1514. ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE);
  1515. if (!ptr) {
  1516. pr_warning("PERCPU: failed to allocate %s page "
  1517. "for cpu%u\n", psize_str, cpu);
  1518. goto enomem;
  1519. }
  1520. pages[j++] = virt_to_page(ptr);
  1521. }
  1522. /* allocate vm area, map the pages and copy static data */
  1523. vm.flags = VM_ALLOC;
  1524. vm.size = num_possible_cpus() * ai->unit_size;
  1525. vm_area_register_early(&vm, PAGE_SIZE);
  1526. for (unit = 0; unit < num_possible_cpus(); unit++) {
  1527. unsigned long unit_addr =
  1528. (unsigned long)vm.addr + unit * ai->unit_size;
  1529. for (i = 0; i < unit_pages; i++)
  1530. populate_pte_fn(unit_addr + (i << PAGE_SHIFT));
  1531. /* pte already populated, the following shouldn't fail */
  1532. rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages],
  1533. unit_pages);
  1534. if (rc < 0)
  1535. panic("failed to map percpu area, err=%d\n", rc);
  1536. /*
  1537. * FIXME: Archs with virtual cache should flush local
  1538. * cache for the linear mapping here - something
  1539. * equivalent to flush_cache_vmap() on the local cpu.
  1540. * flush_cache_vmap() can't be used as most supporting
  1541. * data structures are not set up yet.
  1542. */
  1543. /* copy static data */
  1544. memcpy((void *)unit_addr, __per_cpu_load, ai->static_size);
  1545. }
  1546. /* we're ready, commit */
  1547. pr_info("PERCPU: %d %s pages/cpu @%p s%zu r%zu d%zu\n",
  1548. unit_pages, psize_str, vm.addr, ai->static_size,
  1549. ai->reserved_size, ai->dyn_size);
  1550. rc = pcpu_setup_first_chunk(ai, vm.addr);
  1551. goto out_free_ar;
  1552. enomem:
  1553. while (--j >= 0)
  1554. free_fn(page_address(pages[j]), PAGE_SIZE);
  1555. rc = -ENOMEM;
  1556. out_free_ar:
  1557. free_bootmem(__pa(pages), pages_size);
  1558. pcpu_free_alloc_info(ai);
  1559. return rc;
  1560. }
  1561. #endif /* BUILD_PAGE_FIRST_CHUNK */
  1562. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  1563. /*
  1564. * Generic SMP percpu area setup.
  1565. *
  1566. * The embedding helper is used because its behavior closely resembles
  1567. * the original non-dynamic generic percpu area setup. This is
  1568. * important because many archs have addressing restrictions and might
  1569. * fail if the percpu area is located far away from the previous
  1570. * location. As an added bonus, in non-NUMA cases, embedding is
  1571. * generally a good idea TLB-wise because percpu area can piggy back
  1572. * on the physical linear memory mapping which uses large page
  1573. * mappings on applicable archs.
  1574. */
  1575. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  1576. EXPORT_SYMBOL(__per_cpu_offset);
  1577. static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size,
  1578. size_t align)
  1579. {
  1580. return __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
  1581. }
  1582. static void __init pcpu_dfl_fc_free(void *ptr, size_t size)
  1583. {
  1584. free_bootmem(__pa(ptr), size);
  1585. }
  1586. void __init setup_per_cpu_areas(void)
  1587. {
  1588. unsigned long delta;
  1589. unsigned int cpu;
  1590. int rc;
  1591. /*
  1592. * Always reserve area for module percpu variables. That's
  1593. * what the legacy allocator did.
  1594. */
  1595. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  1596. PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL,
  1597. pcpu_dfl_fc_alloc, pcpu_dfl_fc_free);
  1598. if (rc < 0)
  1599. panic("Failed to initialize percpu areas.");
  1600. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  1601. for_each_possible_cpu(cpu)
  1602. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  1603. }
  1604. #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
  1605. #else /* CONFIG_SMP */
  1606. /*
  1607. * UP percpu area setup.
  1608. *
  1609. * UP always uses km-based percpu allocator with identity mapping.
  1610. * Static percpu variables are indistinguishable from the usual static
  1611. * variables and don't require any special preparation.
  1612. */
  1613. void __init setup_per_cpu_areas(void)
  1614. {
  1615. const size_t unit_size =
  1616. roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE,
  1617. PERCPU_DYNAMIC_RESERVE));
  1618. struct pcpu_alloc_info *ai;
  1619. void *fc;
  1620. ai = pcpu_alloc_alloc_info(1, 1);
  1621. fc = __alloc_bootmem(unit_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  1622. if (!ai || !fc)
  1623. panic("Failed to allocate memory for percpu areas.");
  1624. ai->dyn_size = unit_size;
  1625. ai->unit_size = unit_size;
  1626. ai->atom_size = unit_size;
  1627. ai->alloc_size = unit_size;
  1628. ai->groups[0].nr_units = 1;
  1629. ai->groups[0].cpu_map[0] = 0;
  1630. if (pcpu_setup_first_chunk(ai, fc) < 0)
  1631. panic("Failed to initialize percpu areas.");
  1632. }
  1633. #endif /* CONFIG_SMP */
  1634. /*
  1635. * First and reserved chunks are initialized with temporary allocation
  1636. * map in initdata so that they can be used before slab is online.
  1637. * This function is called after slab is brought up and replaces those
  1638. * with properly allocated maps.
  1639. */
  1640. void __init percpu_init_late(void)
  1641. {
  1642. struct pcpu_chunk *target_chunks[] =
  1643. { pcpu_first_chunk, pcpu_reserved_chunk, NULL };
  1644. struct pcpu_chunk *chunk;
  1645. unsigned long flags;
  1646. int i;
  1647. for (i = 0; (chunk = target_chunks[i]); i++) {
  1648. int *map;
  1649. const size_t size = PERCPU_DYNAMIC_EARLY_SLOTS * sizeof(map[0]);
  1650. BUILD_BUG_ON(size > PAGE_SIZE);
  1651. map = pcpu_mem_alloc(size);
  1652. BUG_ON(!map);
  1653. spin_lock_irqsave(&pcpu_lock, flags);
  1654. memcpy(map, chunk->map, size);
  1655. chunk->map = map;
  1656. spin_unlock_irqrestore(&pcpu_lock, flags);
  1657. }
  1658. }