percpu.c 67 KB

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