percpu.c 66 KB

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