percpu.c 54 KB

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