percpu.c 53 KB

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