percpu.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794
  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. * Copyright (C) 2017 Facebook Inc.
  8. * Copyright (C) 2017 Dennis Zhou <dennisszhou@gmail.com>
  9. *
  10. * This file is released under the GPLv2 license.
  11. *
  12. * The percpu allocator handles both static and dynamic areas. Percpu
  13. * areas are allocated in chunks which are divided into units. There is
  14. * a 1-to-1 mapping for units to possible cpus. These units are grouped
  15. * based on NUMA properties of the machine.
  16. *
  17. * c0 c1 c2
  18. * ------------------- ------------------- ------------
  19. * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u
  20. * ------------------- ...... ------------------- .... ------------
  21. *
  22. * Allocation is done by offsets into a unit's address space. Ie., an
  23. * area of 512 bytes at 6k in c1 occupies 512 bytes at 6k in c1:u0,
  24. * c1:u1, c1:u2, etc. On NUMA machines, the mapping may be non-linear
  25. * and even sparse. Access is handled by configuring percpu base
  26. * registers according to the cpu to unit mappings and offsetting the
  27. * base address using pcpu_unit_size.
  28. *
  29. * There is special consideration for the first chunk which must handle
  30. * the static percpu variables in the kernel image as allocation services
  31. * are not online yet. In short, the first chunk is structured like so:
  32. *
  33. * <Static | [Reserved] | Dynamic>
  34. *
  35. * The static data is copied from the original section managed by the
  36. * linker. The reserved section, if non-zero, primarily manages static
  37. * percpu variables from kernel modules. Finally, the dynamic section
  38. * takes care of normal allocations.
  39. *
  40. * The allocator organizes chunks into lists according to free size and
  41. * tries to allocate from the fullest chunk first. Each chunk is managed
  42. * by a bitmap with metadata blocks. The allocation map is updated on
  43. * every allocation and free to reflect the current state while the boundary
  44. * map is only updated on allocation. Each metadata block contains
  45. * information to help mitigate the need to iterate over large portions
  46. * of the bitmap. The reverse mapping from page to chunk is stored in
  47. * the page's index. Lastly, units are lazily backed and grow in unison.
  48. *
  49. * There is a unique conversion that goes on here between bytes and bits.
  50. * Each bit represents a fragment of size PCPU_MIN_ALLOC_SIZE. The chunk
  51. * tracks the number of pages it is responsible for in nr_pages. Helper
  52. * functions are used to convert from between the bytes, bits, and blocks.
  53. * All hints are managed in bits unless explicitly stated.
  54. *
  55. * To use this allocator, arch code should do the following:
  56. *
  57. * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
  58. * regular address to percpu pointer and back if they need to be
  59. * different from the default
  60. *
  61. * - use pcpu_setup_first_chunk() during percpu area initialization to
  62. * setup the first chunk containing the kernel static percpu area
  63. */
  64. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  65. #include <linux/bitmap.h>
  66. #include <linux/bootmem.h>
  67. #include <linux/err.h>
  68. #include <linux/lcm.h>
  69. #include <linux/list.h>
  70. #include <linux/log2.h>
  71. #include <linux/mm.h>
  72. #include <linux/module.h>
  73. #include <linux/mutex.h>
  74. #include <linux/percpu.h>
  75. #include <linux/pfn.h>
  76. #include <linux/slab.h>
  77. #include <linux/spinlock.h>
  78. #include <linux/vmalloc.h>
  79. #include <linux/workqueue.h>
  80. #include <linux/kmemleak.h>
  81. #include <linux/sched.h>
  82. #include <asm/cacheflush.h>
  83. #include <asm/sections.h>
  84. #include <asm/tlbflush.h>
  85. #include <asm/io.h>
  86. #define CREATE_TRACE_POINTS
  87. #include <trace/events/percpu.h>
  88. #include "percpu-internal.h"
  89. /* the slots are sorted by free bytes left, 1-31 bytes share the same slot */
  90. #define PCPU_SLOT_BASE_SHIFT 5
  91. #define PCPU_EMPTY_POP_PAGES_LOW 2
  92. #define PCPU_EMPTY_POP_PAGES_HIGH 4
  93. #ifdef CONFIG_SMP
  94. /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */
  95. #ifndef __addr_to_pcpu_ptr
  96. #define __addr_to_pcpu_ptr(addr) \
  97. (void __percpu *)((unsigned long)(addr) - \
  98. (unsigned long)pcpu_base_addr + \
  99. (unsigned long)__per_cpu_start)
  100. #endif
  101. #ifndef __pcpu_ptr_to_addr
  102. #define __pcpu_ptr_to_addr(ptr) \
  103. (void __force *)((unsigned long)(ptr) + \
  104. (unsigned long)pcpu_base_addr - \
  105. (unsigned long)__per_cpu_start)
  106. #endif
  107. #else /* CONFIG_SMP */
  108. /* on UP, it's always identity mapped */
  109. #define __addr_to_pcpu_ptr(addr) (void __percpu *)(addr)
  110. #define __pcpu_ptr_to_addr(ptr) (void __force *)(ptr)
  111. #endif /* CONFIG_SMP */
  112. static int pcpu_unit_pages __ro_after_init;
  113. static int pcpu_unit_size __ro_after_init;
  114. static int pcpu_nr_units __ro_after_init;
  115. static int pcpu_atom_size __ro_after_init;
  116. int pcpu_nr_slots __ro_after_init;
  117. static size_t pcpu_chunk_struct_size __ro_after_init;
  118. /* cpus with the lowest and highest unit addresses */
  119. static unsigned int pcpu_low_unit_cpu __ro_after_init;
  120. static unsigned int pcpu_high_unit_cpu __ro_after_init;
  121. /* the address of the first chunk which starts with the kernel static area */
  122. void *pcpu_base_addr __ro_after_init;
  123. EXPORT_SYMBOL_GPL(pcpu_base_addr);
  124. static const int *pcpu_unit_map __ro_after_init; /* cpu -> unit */
  125. const unsigned long *pcpu_unit_offsets __ro_after_init; /* cpu -> unit offset */
  126. /* group information, used for vm allocation */
  127. static int pcpu_nr_groups __ro_after_init;
  128. static const unsigned long *pcpu_group_offsets __ro_after_init;
  129. static const size_t *pcpu_group_sizes __ro_after_init;
  130. /*
  131. * The first chunk which always exists. Note that unlike other
  132. * chunks, this one can be allocated and mapped in several different
  133. * ways and thus often doesn't live in the vmalloc area.
  134. */
  135. struct pcpu_chunk *pcpu_first_chunk __ro_after_init;
  136. /*
  137. * Optional reserved chunk. This chunk reserves part of the first
  138. * chunk and serves it for reserved allocations. When the reserved
  139. * region doesn't exist, the following variable is NULL.
  140. */
  141. struct pcpu_chunk *pcpu_reserved_chunk __ro_after_init;
  142. DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */
  143. static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */
  144. struct list_head *pcpu_slot __ro_after_init; /* chunk list slots */
  145. /* chunks which need their map areas extended, protected by pcpu_lock */
  146. static LIST_HEAD(pcpu_map_extend_chunks);
  147. /*
  148. * The number of empty populated pages, protected by pcpu_lock. The
  149. * reserved chunk doesn't contribute to the count.
  150. */
  151. int pcpu_nr_empty_pop_pages;
  152. /*
  153. * The number of populated pages in use by the allocator, protected by
  154. * pcpu_lock. This number is kept per a unit per chunk (i.e. when a page gets
  155. * allocated/deallocated, it is allocated/deallocated in all units of a chunk
  156. * and increments/decrements this count by 1).
  157. */
  158. static unsigned long pcpu_nr_populated;
  159. /*
  160. * Balance work is used to populate or destroy chunks asynchronously. We
  161. * try to keep the number of populated free pages between
  162. * PCPU_EMPTY_POP_PAGES_LOW and HIGH for atomic allocations and at most one
  163. * empty chunk.
  164. */
  165. static void pcpu_balance_workfn(struct work_struct *work);
  166. static DECLARE_WORK(pcpu_balance_work, pcpu_balance_workfn);
  167. static bool pcpu_async_enabled __read_mostly;
  168. static bool pcpu_atomic_alloc_failed;
  169. static void pcpu_schedule_balance_work(void)
  170. {
  171. if (pcpu_async_enabled)
  172. schedule_work(&pcpu_balance_work);
  173. }
  174. /**
  175. * pcpu_addr_in_chunk - check if the address is served from this chunk
  176. * @chunk: chunk of interest
  177. * @addr: percpu address
  178. *
  179. * RETURNS:
  180. * True if the address is served from this chunk.
  181. */
  182. static bool pcpu_addr_in_chunk(struct pcpu_chunk *chunk, void *addr)
  183. {
  184. void *start_addr, *end_addr;
  185. if (!chunk)
  186. return false;
  187. start_addr = chunk->base_addr + chunk->start_offset;
  188. end_addr = chunk->base_addr + chunk->nr_pages * PAGE_SIZE -
  189. chunk->end_offset;
  190. return addr >= start_addr && addr < end_addr;
  191. }
  192. static int __pcpu_size_to_slot(int size)
  193. {
  194. int highbit = fls(size); /* size is in bytes */
  195. return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
  196. }
  197. static int pcpu_size_to_slot(int size)
  198. {
  199. if (size == pcpu_unit_size)
  200. return pcpu_nr_slots - 1;
  201. return __pcpu_size_to_slot(size);
  202. }
  203. static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
  204. {
  205. if (chunk->free_bytes < PCPU_MIN_ALLOC_SIZE || chunk->contig_bits == 0)
  206. return 0;
  207. return pcpu_size_to_slot(chunk->free_bytes);
  208. }
  209. /* set the pointer to a chunk in a page struct */
  210. static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu)
  211. {
  212. page->index = (unsigned long)pcpu;
  213. }
  214. /* obtain pointer to a chunk from a page struct */
  215. static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page)
  216. {
  217. return (struct pcpu_chunk *)page->index;
  218. }
  219. static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx)
  220. {
  221. return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx;
  222. }
  223. static unsigned long pcpu_unit_page_offset(unsigned int cpu, int page_idx)
  224. {
  225. return pcpu_unit_offsets[cpu] + (page_idx << PAGE_SHIFT);
  226. }
  227. static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
  228. unsigned int cpu, int page_idx)
  229. {
  230. return (unsigned long)chunk->base_addr +
  231. pcpu_unit_page_offset(cpu, page_idx);
  232. }
  233. static void pcpu_next_unpop(unsigned long *bitmap, int *rs, int *re, int end)
  234. {
  235. *rs = find_next_zero_bit(bitmap, end, *rs);
  236. *re = find_next_bit(bitmap, end, *rs + 1);
  237. }
  238. static void pcpu_next_pop(unsigned long *bitmap, int *rs, int *re, int end)
  239. {
  240. *rs = find_next_bit(bitmap, end, *rs);
  241. *re = find_next_zero_bit(bitmap, end, *rs + 1);
  242. }
  243. /*
  244. * Bitmap region iterators. Iterates over the bitmap between
  245. * [@start, @end) in @chunk. @rs and @re should be integer variables
  246. * and will be set to start and end index of the current free region.
  247. */
  248. #define pcpu_for_each_unpop_region(bitmap, rs, re, start, end) \
  249. for ((rs) = (start), pcpu_next_unpop((bitmap), &(rs), &(re), (end)); \
  250. (rs) < (re); \
  251. (rs) = (re) + 1, pcpu_next_unpop((bitmap), &(rs), &(re), (end)))
  252. #define pcpu_for_each_pop_region(bitmap, rs, re, start, end) \
  253. for ((rs) = (start), pcpu_next_pop((bitmap), &(rs), &(re), (end)); \
  254. (rs) < (re); \
  255. (rs) = (re) + 1, pcpu_next_pop((bitmap), &(rs), &(re), (end)))
  256. /*
  257. * The following are helper functions to help access bitmaps and convert
  258. * between bitmap offsets to address offsets.
  259. */
  260. static unsigned long *pcpu_index_alloc_map(struct pcpu_chunk *chunk, int index)
  261. {
  262. return chunk->alloc_map +
  263. (index * PCPU_BITMAP_BLOCK_BITS / BITS_PER_LONG);
  264. }
  265. static unsigned long pcpu_off_to_block_index(int off)
  266. {
  267. return off / PCPU_BITMAP_BLOCK_BITS;
  268. }
  269. static unsigned long pcpu_off_to_block_off(int off)
  270. {
  271. return off & (PCPU_BITMAP_BLOCK_BITS - 1);
  272. }
  273. static unsigned long pcpu_block_off_to_off(int index, int off)
  274. {
  275. return index * PCPU_BITMAP_BLOCK_BITS + off;
  276. }
  277. /**
  278. * pcpu_next_md_free_region - finds the next hint free area
  279. * @chunk: chunk of interest
  280. * @bit_off: chunk offset
  281. * @bits: size of free area
  282. *
  283. * Helper function for pcpu_for_each_md_free_region. It checks
  284. * block->contig_hint and performs aggregation across blocks to find the
  285. * next hint. It modifies bit_off and bits in-place to be consumed in the
  286. * loop.
  287. */
  288. static void pcpu_next_md_free_region(struct pcpu_chunk *chunk, int *bit_off,
  289. int *bits)
  290. {
  291. int i = pcpu_off_to_block_index(*bit_off);
  292. int block_off = pcpu_off_to_block_off(*bit_off);
  293. struct pcpu_block_md *block;
  294. *bits = 0;
  295. for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk);
  296. block++, i++) {
  297. /* handles contig area across blocks */
  298. if (*bits) {
  299. *bits += block->left_free;
  300. if (block->left_free == PCPU_BITMAP_BLOCK_BITS)
  301. continue;
  302. return;
  303. }
  304. /*
  305. * This checks three things. First is there a contig_hint to
  306. * check. Second, have we checked this hint before by
  307. * comparing the block_off. Third, is this the same as the
  308. * right contig hint. In the last case, it spills over into
  309. * the next block and should be handled by the contig area
  310. * across blocks code.
  311. */
  312. *bits = block->contig_hint;
  313. if (*bits && block->contig_hint_start >= block_off &&
  314. *bits + block->contig_hint_start < PCPU_BITMAP_BLOCK_BITS) {
  315. *bit_off = pcpu_block_off_to_off(i,
  316. block->contig_hint_start);
  317. return;
  318. }
  319. /* reset to satisfy the second predicate above */
  320. block_off = 0;
  321. *bits = block->right_free;
  322. *bit_off = (i + 1) * PCPU_BITMAP_BLOCK_BITS - block->right_free;
  323. }
  324. }
  325. /**
  326. * pcpu_next_fit_region - finds fit areas for a given allocation request
  327. * @chunk: chunk of interest
  328. * @alloc_bits: size of allocation
  329. * @align: alignment of area (max PAGE_SIZE)
  330. * @bit_off: chunk offset
  331. * @bits: size of free area
  332. *
  333. * Finds the next free region that is viable for use with a given size and
  334. * alignment. This only returns if there is a valid area to be used for this
  335. * allocation. block->first_free is returned if the allocation request fits
  336. * within the block to see if the request can be fulfilled prior to the contig
  337. * hint.
  338. */
  339. static void pcpu_next_fit_region(struct pcpu_chunk *chunk, int alloc_bits,
  340. int align, int *bit_off, int *bits)
  341. {
  342. int i = pcpu_off_to_block_index(*bit_off);
  343. int block_off = pcpu_off_to_block_off(*bit_off);
  344. struct pcpu_block_md *block;
  345. *bits = 0;
  346. for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk);
  347. block++, i++) {
  348. /* handles contig area across blocks */
  349. if (*bits) {
  350. *bits += block->left_free;
  351. if (*bits >= alloc_bits)
  352. return;
  353. if (block->left_free == PCPU_BITMAP_BLOCK_BITS)
  354. continue;
  355. }
  356. /* check block->contig_hint */
  357. *bits = ALIGN(block->contig_hint_start, align) -
  358. block->contig_hint_start;
  359. /*
  360. * This uses the block offset to determine if this has been
  361. * checked in the prior iteration.
  362. */
  363. if (block->contig_hint &&
  364. block->contig_hint_start >= block_off &&
  365. block->contig_hint >= *bits + alloc_bits) {
  366. *bits += alloc_bits + block->contig_hint_start -
  367. block->first_free;
  368. *bit_off = pcpu_block_off_to_off(i, block->first_free);
  369. return;
  370. }
  371. /* reset to satisfy the second predicate above */
  372. block_off = 0;
  373. *bit_off = ALIGN(PCPU_BITMAP_BLOCK_BITS - block->right_free,
  374. align);
  375. *bits = PCPU_BITMAP_BLOCK_BITS - *bit_off;
  376. *bit_off = pcpu_block_off_to_off(i, *bit_off);
  377. if (*bits >= alloc_bits)
  378. return;
  379. }
  380. /* no valid offsets were found - fail condition */
  381. *bit_off = pcpu_chunk_map_bits(chunk);
  382. }
  383. /*
  384. * Metadata free area iterators. These perform aggregation of free areas
  385. * based on the metadata blocks and return the offset @bit_off and size in
  386. * bits of the free area @bits. pcpu_for_each_fit_region only returns when
  387. * a fit is found for the allocation request.
  388. */
  389. #define pcpu_for_each_md_free_region(chunk, bit_off, bits) \
  390. for (pcpu_next_md_free_region((chunk), &(bit_off), &(bits)); \
  391. (bit_off) < pcpu_chunk_map_bits((chunk)); \
  392. (bit_off) += (bits) + 1, \
  393. pcpu_next_md_free_region((chunk), &(bit_off), &(bits)))
  394. #define pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) \
  395. for (pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \
  396. &(bits)); \
  397. (bit_off) < pcpu_chunk_map_bits((chunk)); \
  398. (bit_off) += (bits), \
  399. pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \
  400. &(bits)))
  401. /**
  402. * pcpu_mem_zalloc - allocate memory
  403. * @size: bytes to allocate
  404. * @gfp: allocation flags
  405. *
  406. * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
  407. * kzalloc() is used; otherwise, the equivalent of vzalloc() is used.
  408. * This is to facilitate passing through whitelisted flags. The
  409. * returned memory is always zeroed.
  410. *
  411. * RETURNS:
  412. * Pointer to the allocated area on success, NULL on failure.
  413. */
  414. static void *pcpu_mem_zalloc(size_t size, gfp_t gfp)
  415. {
  416. if (WARN_ON_ONCE(!slab_is_available()))
  417. return NULL;
  418. if (size <= PAGE_SIZE)
  419. return kzalloc(size, gfp);
  420. else
  421. return __vmalloc(size, gfp | __GFP_ZERO, PAGE_KERNEL);
  422. }
  423. /**
  424. * pcpu_mem_free - free memory
  425. * @ptr: memory to free
  426. *
  427. * Free @ptr. @ptr should have been allocated using pcpu_mem_zalloc().
  428. */
  429. static void pcpu_mem_free(void *ptr)
  430. {
  431. kvfree(ptr);
  432. }
  433. /**
  434. * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
  435. * @chunk: chunk of interest
  436. * @oslot: the previous slot it was on
  437. *
  438. * This function is called after an allocation or free changed @chunk.
  439. * New slot according to the changed state is determined and @chunk is
  440. * moved to the slot. Note that the reserved chunk is never put on
  441. * chunk slots.
  442. *
  443. * CONTEXT:
  444. * pcpu_lock.
  445. */
  446. static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
  447. {
  448. int nslot = pcpu_chunk_slot(chunk);
  449. if (chunk != pcpu_reserved_chunk && oslot != nslot) {
  450. if (oslot < nslot)
  451. list_move(&chunk->list, &pcpu_slot[nslot]);
  452. else
  453. list_move_tail(&chunk->list, &pcpu_slot[nslot]);
  454. }
  455. }
  456. /**
  457. * pcpu_cnt_pop_pages- counts populated backing pages in range
  458. * @chunk: chunk of interest
  459. * @bit_off: start offset
  460. * @bits: size of area to check
  461. *
  462. * Calculates the number of populated pages in the region
  463. * [page_start, page_end). This keeps track of how many empty populated
  464. * pages are available and decide if async work should be scheduled.
  465. *
  466. * RETURNS:
  467. * The nr of populated pages.
  468. */
  469. static inline int pcpu_cnt_pop_pages(struct pcpu_chunk *chunk, int bit_off,
  470. int bits)
  471. {
  472. int page_start = PFN_UP(bit_off * PCPU_MIN_ALLOC_SIZE);
  473. int page_end = PFN_DOWN((bit_off + bits) * PCPU_MIN_ALLOC_SIZE);
  474. if (page_start >= page_end)
  475. return 0;
  476. /*
  477. * bitmap_weight counts the number of bits set in a bitmap up to
  478. * the specified number of bits. This is counting the populated
  479. * pages up to page_end and then subtracting the populated pages
  480. * up to page_start to count the populated pages in
  481. * [page_start, page_end).
  482. */
  483. return bitmap_weight(chunk->populated, page_end) -
  484. bitmap_weight(chunk->populated, page_start);
  485. }
  486. /**
  487. * pcpu_chunk_update - updates the chunk metadata given a free area
  488. * @chunk: chunk of interest
  489. * @bit_off: chunk offset
  490. * @bits: size of free area
  491. *
  492. * This updates the chunk's contig hint and starting offset given a free area.
  493. * Choose the best starting offset if the contig hint is equal.
  494. */
  495. static void pcpu_chunk_update(struct pcpu_chunk *chunk, int bit_off, int bits)
  496. {
  497. if (bits > chunk->contig_bits) {
  498. chunk->contig_bits_start = bit_off;
  499. chunk->contig_bits = bits;
  500. } else if (bits == chunk->contig_bits && chunk->contig_bits_start &&
  501. (!bit_off ||
  502. __ffs(bit_off) > __ffs(chunk->contig_bits_start))) {
  503. /* use the start with the best alignment */
  504. chunk->contig_bits_start = bit_off;
  505. }
  506. }
  507. /**
  508. * pcpu_chunk_refresh_hint - updates metadata about a chunk
  509. * @chunk: chunk of interest
  510. *
  511. * Iterates over the metadata blocks to find the largest contig area.
  512. * It also counts the populated pages and uses the delta to update the
  513. * global count.
  514. *
  515. * Updates:
  516. * chunk->contig_bits
  517. * chunk->contig_bits_start
  518. * nr_empty_pop_pages (chunk and global)
  519. */
  520. static void pcpu_chunk_refresh_hint(struct pcpu_chunk *chunk)
  521. {
  522. int bit_off, bits, nr_empty_pop_pages;
  523. /* clear metadata */
  524. chunk->contig_bits = 0;
  525. bit_off = chunk->first_bit;
  526. bits = nr_empty_pop_pages = 0;
  527. pcpu_for_each_md_free_region(chunk, bit_off, bits) {
  528. pcpu_chunk_update(chunk, bit_off, bits);
  529. nr_empty_pop_pages += pcpu_cnt_pop_pages(chunk, bit_off, bits);
  530. }
  531. /*
  532. * Keep track of nr_empty_pop_pages.
  533. *
  534. * The chunk maintains the previous number of free pages it held,
  535. * so the delta is used to update the global counter. The reserved
  536. * chunk is not part of the free page count as they are populated
  537. * at init and are special to serving reserved allocations.
  538. */
  539. if (chunk != pcpu_reserved_chunk)
  540. pcpu_nr_empty_pop_pages +=
  541. (nr_empty_pop_pages - chunk->nr_empty_pop_pages);
  542. chunk->nr_empty_pop_pages = nr_empty_pop_pages;
  543. }
  544. /**
  545. * pcpu_block_update - updates a block given a free area
  546. * @block: block of interest
  547. * @start: start offset in block
  548. * @end: end offset in block
  549. *
  550. * Updates a block given a known free area. The region [start, end) is
  551. * expected to be the entirety of the free area within a block. Chooses
  552. * the best starting offset if the contig hints are equal.
  553. */
  554. static void pcpu_block_update(struct pcpu_block_md *block, int start, int end)
  555. {
  556. int contig = end - start;
  557. block->first_free = min(block->first_free, start);
  558. if (start == 0)
  559. block->left_free = contig;
  560. if (end == PCPU_BITMAP_BLOCK_BITS)
  561. block->right_free = contig;
  562. if (contig > block->contig_hint) {
  563. block->contig_hint_start = start;
  564. block->contig_hint = contig;
  565. } else if (block->contig_hint_start && contig == block->contig_hint &&
  566. (!start || __ffs(start) > __ffs(block->contig_hint_start))) {
  567. /* use the start with the best alignment */
  568. block->contig_hint_start = start;
  569. }
  570. }
  571. /**
  572. * pcpu_block_refresh_hint
  573. * @chunk: chunk of interest
  574. * @index: index of the metadata block
  575. *
  576. * Scans over the block beginning at first_free and updates the block
  577. * metadata accordingly.
  578. */
  579. static void pcpu_block_refresh_hint(struct pcpu_chunk *chunk, int index)
  580. {
  581. struct pcpu_block_md *block = chunk->md_blocks + index;
  582. unsigned long *alloc_map = pcpu_index_alloc_map(chunk, index);
  583. int rs, re; /* region start, region end */
  584. /* clear hints */
  585. block->contig_hint = 0;
  586. block->left_free = block->right_free = 0;
  587. /* iterate over free areas and update the contig hints */
  588. pcpu_for_each_unpop_region(alloc_map, rs, re, block->first_free,
  589. PCPU_BITMAP_BLOCK_BITS) {
  590. pcpu_block_update(block, rs, re);
  591. }
  592. }
  593. /**
  594. * pcpu_block_update_hint_alloc - update hint on allocation path
  595. * @chunk: chunk of interest
  596. * @bit_off: chunk offset
  597. * @bits: size of request
  598. *
  599. * Updates metadata for the allocation path. The metadata only has to be
  600. * refreshed by a full scan iff the chunk's contig hint is broken. Block level
  601. * scans are required if the block's contig hint is broken.
  602. */
  603. static void pcpu_block_update_hint_alloc(struct pcpu_chunk *chunk, int bit_off,
  604. int bits)
  605. {
  606. struct pcpu_block_md *s_block, *e_block, *block;
  607. int s_index, e_index; /* block indexes of the freed allocation */
  608. int s_off, e_off; /* block offsets of the freed allocation */
  609. /*
  610. * Calculate per block offsets.
  611. * The calculation uses an inclusive range, but the resulting offsets
  612. * are [start, end). e_index always points to the last block in the
  613. * range.
  614. */
  615. s_index = pcpu_off_to_block_index(bit_off);
  616. e_index = pcpu_off_to_block_index(bit_off + bits - 1);
  617. s_off = pcpu_off_to_block_off(bit_off);
  618. e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1;
  619. s_block = chunk->md_blocks + s_index;
  620. e_block = chunk->md_blocks + e_index;
  621. /*
  622. * Update s_block.
  623. * block->first_free must be updated if the allocation takes its place.
  624. * If the allocation breaks the contig_hint, a scan is required to
  625. * restore this hint.
  626. */
  627. if (s_off == s_block->first_free)
  628. s_block->first_free = find_next_zero_bit(
  629. pcpu_index_alloc_map(chunk, s_index),
  630. PCPU_BITMAP_BLOCK_BITS,
  631. s_off + bits);
  632. if (s_off >= s_block->contig_hint_start &&
  633. s_off < s_block->contig_hint_start + s_block->contig_hint) {
  634. /* block contig hint is broken - scan to fix it */
  635. pcpu_block_refresh_hint(chunk, s_index);
  636. } else {
  637. /* update left and right contig manually */
  638. s_block->left_free = min(s_block->left_free, s_off);
  639. if (s_index == e_index)
  640. s_block->right_free = min_t(int, s_block->right_free,
  641. PCPU_BITMAP_BLOCK_BITS - e_off);
  642. else
  643. s_block->right_free = 0;
  644. }
  645. /*
  646. * Update e_block.
  647. */
  648. if (s_index != e_index) {
  649. /*
  650. * When the allocation is across blocks, the end is along
  651. * the left part of the e_block.
  652. */
  653. e_block->first_free = find_next_zero_bit(
  654. pcpu_index_alloc_map(chunk, e_index),
  655. PCPU_BITMAP_BLOCK_BITS, e_off);
  656. if (e_off == PCPU_BITMAP_BLOCK_BITS) {
  657. /* reset the block */
  658. e_block++;
  659. } else {
  660. if (e_off > e_block->contig_hint_start) {
  661. /* contig hint is broken - scan to fix it */
  662. pcpu_block_refresh_hint(chunk, e_index);
  663. } else {
  664. e_block->left_free = 0;
  665. e_block->right_free =
  666. min_t(int, e_block->right_free,
  667. PCPU_BITMAP_BLOCK_BITS - e_off);
  668. }
  669. }
  670. /* update in-between md_blocks */
  671. for (block = s_block + 1; block < e_block; block++) {
  672. block->contig_hint = 0;
  673. block->left_free = 0;
  674. block->right_free = 0;
  675. }
  676. }
  677. /*
  678. * The only time a full chunk scan is required is if the chunk
  679. * contig hint is broken. Otherwise, it means a smaller space
  680. * was used and therefore the chunk contig hint is still correct.
  681. */
  682. if (bit_off >= chunk->contig_bits_start &&
  683. bit_off < chunk->contig_bits_start + chunk->contig_bits)
  684. pcpu_chunk_refresh_hint(chunk);
  685. }
  686. /**
  687. * pcpu_block_update_hint_free - updates the block hints on the free path
  688. * @chunk: chunk of interest
  689. * @bit_off: chunk offset
  690. * @bits: size of request
  691. *
  692. * Updates metadata for the allocation path. This avoids a blind block
  693. * refresh by making use of the block contig hints. If this fails, it scans
  694. * forward and backward to determine the extent of the free area. This is
  695. * capped at the boundary of blocks.
  696. *
  697. * A chunk update is triggered if a page becomes free, a block becomes free,
  698. * or the free spans across blocks. This tradeoff is to minimize iterating
  699. * over the block metadata to update chunk->contig_bits. chunk->contig_bits
  700. * may be off by up to a page, but it will never be more than the available
  701. * space. If the contig hint is contained in one block, it will be accurate.
  702. */
  703. static void pcpu_block_update_hint_free(struct pcpu_chunk *chunk, int bit_off,
  704. int bits)
  705. {
  706. struct pcpu_block_md *s_block, *e_block, *block;
  707. int s_index, e_index; /* block indexes of the freed allocation */
  708. int s_off, e_off; /* block offsets of the freed allocation */
  709. int start, end; /* start and end of the whole free area */
  710. /*
  711. * Calculate per block offsets.
  712. * The calculation uses an inclusive range, but the resulting offsets
  713. * are [start, end). e_index always points to the last block in the
  714. * range.
  715. */
  716. s_index = pcpu_off_to_block_index(bit_off);
  717. e_index = pcpu_off_to_block_index(bit_off + bits - 1);
  718. s_off = pcpu_off_to_block_off(bit_off);
  719. e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1;
  720. s_block = chunk->md_blocks + s_index;
  721. e_block = chunk->md_blocks + e_index;
  722. /*
  723. * Check if the freed area aligns with the block->contig_hint.
  724. * If it does, then the scan to find the beginning/end of the
  725. * larger free area can be avoided.
  726. *
  727. * start and end refer to beginning and end of the free area
  728. * within each their respective blocks. This is not necessarily
  729. * the entire free area as it may span blocks past the beginning
  730. * or end of the block.
  731. */
  732. start = s_off;
  733. if (s_off == s_block->contig_hint + s_block->contig_hint_start) {
  734. start = s_block->contig_hint_start;
  735. } else {
  736. /*
  737. * Scan backwards to find the extent of the free area.
  738. * find_last_bit returns the starting bit, so if the start bit
  739. * is returned, that means there was no last bit and the
  740. * remainder of the chunk is free.
  741. */
  742. int l_bit = find_last_bit(pcpu_index_alloc_map(chunk, s_index),
  743. start);
  744. start = (start == l_bit) ? 0 : l_bit + 1;
  745. }
  746. end = e_off;
  747. if (e_off == e_block->contig_hint_start)
  748. end = e_block->contig_hint_start + e_block->contig_hint;
  749. else
  750. end = find_next_bit(pcpu_index_alloc_map(chunk, e_index),
  751. PCPU_BITMAP_BLOCK_BITS, end);
  752. /* update s_block */
  753. e_off = (s_index == e_index) ? end : PCPU_BITMAP_BLOCK_BITS;
  754. pcpu_block_update(s_block, start, e_off);
  755. /* freeing in the same block */
  756. if (s_index != e_index) {
  757. /* update e_block */
  758. pcpu_block_update(e_block, 0, end);
  759. /* reset md_blocks in the middle */
  760. for (block = s_block + 1; block < e_block; block++) {
  761. block->first_free = 0;
  762. block->contig_hint_start = 0;
  763. block->contig_hint = PCPU_BITMAP_BLOCK_BITS;
  764. block->left_free = PCPU_BITMAP_BLOCK_BITS;
  765. block->right_free = PCPU_BITMAP_BLOCK_BITS;
  766. }
  767. }
  768. /*
  769. * Refresh chunk metadata when the free makes a page free, a block
  770. * free, or spans across blocks. The contig hint may be off by up to
  771. * a page, but if the hint is contained in a block, it will be accurate
  772. * with the else condition below.
  773. */
  774. if ((ALIGN_DOWN(end, min(PCPU_BITS_PER_PAGE, PCPU_BITMAP_BLOCK_BITS)) >
  775. ALIGN(start, min(PCPU_BITS_PER_PAGE, PCPU_BITMAP_BLOCK_BITS))) ||
  776. s_index != e_index)
  777. pcpu_chunk_refresh_hint(chunk);
  778. else
  779. pcpu_chunk_update(chunk, pcpu_block_off_to_off(s_index, start),
  780. s_block->contig_hint);
  781. }
  782. /**
  783. * pcpu_is_populated - determines if the region is populated
  784. * @chunk: chunk of interest
  785. * @bit_off: chunk offset
  786. * @bits: size of area
  787. * @next_off: return value for the next offset to start searching
  788. *
  789. * For atomic allocations, check if the backing pages are populated.
  790. *
  791. * RETURNS:
  792. * Bool if the backing pages are populated.
  793. * next_index is to skip over unpopulated blocks in pcpu_find_block_fit.
  794. */
  795. static bool pcpu_is_populated(struct pcpu_chunk *chunk, int bit_off, int bits,
  796. int *next_off)
  797. {
  798. int page_start, page_end, rs, re;
  799. page_start = PFN_DOWN(bit_off * PCPU_MIN_ALLOC_SIZE);
  800. page_end = PFN_UP((bit_off + bits) * PCPU_MIN_ALLOC_SIZE);
  801. rs = page_start;
  802. pcpu_next_unpop(chunk->populated, &rs, &re, page_end);
  803. if (rs >= page_end)
  804. return true;
  805. *next_off = re * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE;
  806. return false;
  807. }
  808. /**
  809. * pcpu_find_block_fit - finds the block index to start searching
  810. * @chunk: chunk of interest
  811. * @alloc_bits: size of request in allocation units
  812. * @align: alignment of area (max PAGE_SIZE bytes)
  813. * @pop_only: use populated regions only
  814. *
  815. * Given a chunk and an allocation spec, find the offset to begin searching
  816. * for a free region. This iterates over the bitmap metadata blocks to
  817. * find an offset that will be guaranteed to fit the requirements. It is
  818. * not quite first fit as if the allocation does not fit in the contig hint
  819. * of a block or chunk, it is skipped. This errs on the side of caution
  820. * to prevent excess iteration. Poor alignment can cause the allocator to
  821. * skip over blocks and chunks that have valid free areas.
  822. *
  823. * RETURNS:
  824. * The offset in the bitmap to begin searching.
  825. * -1 if no offset is found.
  826. */
  827. static int pcpu_find_block_fit(struct pcpu_chunk *chunk, int alloc_bits,
  828. size_t align, bool pop_only)
  829. {
  830. int bit_off, bits, next_off;
  831. /*
  832. * Check to see if the allocation can fit in the chunk's contig hint.
  833. * This is an optimization to prevent scanning by assuming if it
  834. * cannot fit in the global hint, there is memory pressure and creating
  835. * a new chunk would happen soon.
  836. */
  837. bit_off = ALIGN(chunk->contig_bits_start, align) -
  838. chunk->contig_bits_start;
  839. if (bit_off + alloc_bits > chunk->contig_bits)
  840. return -1;
  841. bit_off = chunk->first_bit;
  842. bits = 0;
  843. pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) {
  844. if (!pop_only || pcpu_is_populated(chunk, bit_off, bits,
  845. &next_off))
  846. break;
  847. bit_off = next_off;
  848. bits = 0;
  849. }
  850. if (bit_off == pcpu_chunk_map_bits(chunk))
  851. return -1;
  852. return bit_off;
  853. }
  854. /**
  855. * pcpu_alloc_area - allocates an area from a pcpu_chunk
  856. * @chunk: chunk of interest
  857. * @alloc_bits: size of request in allocation units
  858. * @align: alignment of area (max PAGE_SIZE)
  859. * @start: bit_off to start searching
  860. *
  861. * This function takes in a @start offset to begin searching to fit an
  862. * allocation of @alloc_bits with alignment @align. It needs to scan
  863. * the allocation map because if it fits within the block's contig hint,
  864. * @start will be block->first_free. This is an attempt to fill the
  865. * allocation prior to breaking the contig hint. The allocation and
  866. * boundary maps are updated accordingly if it confirms a valid
  867. * free area.
  868. *
  869. * RETURNS:
  870. * Allocated addr offset in @chunk on success.
  871. * -1 if no matching area is found.
  872. */
  873. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int alloc_bits,
  874. size_t align, int start)
  875. {
  876. size_t align_mask = (align) ? (align - 1) : 0;
  877. int bit_off, end, oslot;
  878. lockdep_assert_held(&pcpu_lock);
  879. oslot = pcpu_chunk_slot(chunk);
  880. /*
  881. * Search to find a fit.
  882. */
  883. end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS,
  884. pcpu_chunk_map_bits(chunk));
  885. bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end, start,
  886. alloc_bits, align_mask);
  887. if (bit_off >= end)
  888. return -1;
  889. /* update alloc map */
  890. bitmap_set(chunk->alloc_map, bit_off, alloc_bits);
  891. /* update boundary map */
  892. set_bit(bit_off, chunk->bound_map);
  893. bitmap_clear(chunk->bound_map, bit_off + 1, alloc_bits - 1);
  894. set_bit(bit_off + alloc_bits, chunk->bound_map);
  895. chunk->free_bytes -= alloc_bits * PCPU_MIN_ALLOC_SIZE;
  896. /* update first free bit */
  897. if (bit_off == chunk->first_bit)
  898. chunk->first_bit = find_next_zero_bit(
  899. chunk->alloc_map,
  900. pcpu_chunk_map_bits(chunk),
  901. bit_off + alloc_bits);
  902. pcpu_block_update_hint_alloc(chunk, bit_off, alloc_bits);
  903. pcpu_chunk_relocate(chunk, oslot);
  904. return bit_off * PCPU_MIN_ALLOC_SIZE;
  905. }
  906. /**
  907. * pcpu_free_area - frees the corresponding offset
  908. * @chunk: chunk of interest
  909. * @off: addr offset into chunk
  910. *
  911. * This function determines the size of an allocation to free using
  912. * the boundary bitmap and clears the allocation map.
  913. */
  914. static void pcpu_free_area(struct pcpu_chunk *chunk, int off)
  915. {
  916. int bit_off, bits, end, oslot;
  917. lockdep_assert_held(&pcpu_lock);
  918. pcpu_stats_area_dealloc(chunk);
  919. oslot = pcpu_chunk_slot(chunk);
  920. bit_off = off / PCPU_MIN_ALLOC_SIZE;
  921. /* find end index */
  922. end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk),
  923. bit_off + 1);
  924. bits = end - bit_off;
  925. bitmap_clear(chunk->alloc_map, bit_off, bits);
  926. /* update metadata */
  927. chunk->free_bytes += bits * PCPU_MIN_ALLOC_SIZE;
  928. /* update first free bit */
  929. chunk->first_bit = min(chunk->first_bit, bit_off);
  930. pcpu_block_update_hint_free(chunk, bit_off, bits);
  931. pcpu_chunk_relocate(chunk, oslot);
  932. }
  933. static void pcpu_init_md_blocks(struct pcpu_chunk *chunk)
  934. {
  935. struct pcpu_block_md *md_block;
  936. for (md_block = chunk->md_blocks;
  937. md_block != chunk->md_blocks + pcpu_chunk_nr_blocks(chunk);
  938. md_block++) {
  939. md_block->contig_hint = PCPU_BITMAP_BLOCK_BITS;
  940. md_block->left_free = PCPU_BITMAP_BLOCK_BITS;
  941. md_block->right_free = PCPU_BITMAP_BLOCK_BITS;
  942. }
  943. }
  944. /**
  945. * pcpu_alloc_first_chunk - creates chunks that serve the first chunk
  946. * @tmp_addr: the start of the region served
  947. * @map_size: size of the region served
  948. *
  949. * This is responsible for creating the chunks that serve the first chunk. The
  950. * base_addr is page aligned down of @tmp_addr while the region end is page
  951. * aligned up. Offsets are kept track of to determine the region served. All
  952. * this is done to appease the bitmap allocator in avoiding partial blocks.
  953. *
  954. * RETURNS:
  955. * Chunk serving the region at @tmp_addr of @map_size.
  956. */
  957. static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
  958. int map_size)
  959. {
  960. struct pcpu_chunk *chunk;
  961. unsigned long aligned_addr, lcm_align;
  962. int start_offset, offset_bits, region_size, region_bits;
  963. /* region calculations */
  964. aligned_addr = tmp_addr & PAGE_MASK;
  965. start_offset = tmp_addr - aligned_addr;
  966. /*
  967. * Align the end of the region with the LCM of PAGE_SIZE and
  968. * PCPU_BITMAP_BLOCK_SIZE. One of these constants is a multiple of
  969. * the other.
  970. */
  971. lcm_align = lcm(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE);
  972. region_size = ALIGN(start_offset + map_size, lcm_align);
  973. /* allocate chunk */
  974. chunk = memblock_virt_alloc(sizeof(struct pcpu_chunk) +
  975. BITS_TO_LONGS(region_size >> PAGE_SHIFT),
  976. 0);
  977. INIT_LIST_HEAD(&chunk->list);
  978. chunk->base_addr = (void *)aligned_addr;
  979. chunk->start_offset = start_offset;
  980. chunk->end_offset = region_size - chunk->start_offset - map_size;
  981. chunk->nr_pages = region_size >> PAGE_SHIFT;
  982. region_bits = pcpu_chunk_map_bits(chunk);
  983. chunk->alloc_map = memblock_virt_alloc(BITS_TO_LONGS(region_bits) *
  984. sizeof(chunk->alloc_map[0]), 0);
  985. chunk->bound_map = memblock_virt_alloc(BITS_TO_LONGS(region_bits + 1) *
  986. sizeof(chunk->bound_map[0]), 0);
  987. chunk->md_blocks = memblock_virt_alloc(pcpu_chunk_nr_blocks(chunk) *
  988. sizeof(chunk->md_blocks[0]), 0);
  989. pcpu_init_md_blocks(chunk);
  990. /* manage populated page bitmap */
  991. chunk->immutable = true;
  992. bitmap_fill(chunk->populated, chunk->nr_pages);
  993. chunk->nr_populated = chunk->nr_pages;
  994. chunk->nr_empty_pop_pages =
  995. pcpu_cnt_pop_pages(chunk, start_offset / PCPU_MIN_ALLOC_SIZE,
  996. map_size / PCPU_MIN_ALLOC_SIZE);
  997. chunk->contig_bits = map_size / PCPU_MIN_ALLOC_SIZE;
  998. chunk->free_bytes = map_size;
  999. if (chunk->start_offset) {
  1000. /* hide the beginning of the bitmap */
  1001. offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
  1002. bitmap_set(chunk->alloc_map, 0, offset_bits);
  1003. set_bit(0, chunk->bound_map);
  1004. set_bit(offset_bits, chunk->bound_map);
  1005. chunk->first_bit = offset_bits;
  1006. pcpu_block_update_hint_alloc(chunk, 0, offset_bits);
  1007. }
  1008. if (chunk->end_offset) {
  1009. /* hide the end of the bitmap */
  1010. offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE;
  1011. bitmap_set(chunk->alloc_map,
  1012. pcpu_chunk_map_bits(chunk) - offset_bits,
  1013. offset_bits);
  1014. set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE,
  1015. chunk->bound_map);
  1016. set_bit(region_bits, chunk->bound_map);
  1017. pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk)
  1018. - offset_bits, offset_bits);
  1019. }
  1020. return chunk;
  1021. }
  1022. static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp)
  1023. {
  1024. struct pcpu_chunk *chunk;
  1025. int region_bits;
  1026. chunk = pcpu_mem_zalloc(pcpu_chunk_struct_size, gfp);
  1027. if (!chunk)
  1028. return NULL;
  1029. INIT_LIST_HEAD(&chunk->list);
  1030. chunk->nr_pages = pcpu_unit_pages;
  1031. region_bits = pcpu_chunk_map_bits(chunk);
  1032. chunk->alloc_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits) *
  1033. sizeof(chunk->alloc_map[0]), gfp);
  1034. if (!chunk->alloc_map)
  1035. goto alloc_map_fail;
  1036. chunk->bound_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits + 1) *
  1037. sizeof(chunk->bound_map[0]), gfp);
  1038. if (!chunk->bound_map)
  1039. goto bound_map_fail;
  1040. chunk->md_blocks = pcpu_mem_zalloc(pcpu_chunk_nr_blocks(chunk) *
  1041. sizeof(chunk->md_blocks[0]), gfp);
  1042. if (!chunk->md_blocks)
  1043. goto md_blocks_fail;
  1044. pcpu_init_md_blocks(chunk);
  1045. /* init metadata */
  1046. chunk->contig_bits = region_bits;
  1047. chunk->free_bytes = chunk->nr_pages * PAGE_SIZE;
  1048. return chunk;
  1049. md_blocks_fail:
  1050. pcpu_mem_free(chunk->bound_map);
  1051. bound_map_fail:
  1052. pcpu_mem_free(chunk->alloc_map);
  1053. alloc_map_fail:
  1054. pcpu_mem_free(chunk);
  1055. return NULL;
  1056. }
  1057. static void pcpu_free_chunk(struct pcpu_chunk *chunk)
  1058. {
  1059. if (!chunk)
  1060. return;
  1061. pcpu_mem_free(chunk->md_blocks);
  1062. pcpu_mem_free(chunk->bound_map);
  1063. pcpu_mem_free(chunk->alloc_map);
  1064. pcpu_mem_free(chunk);
  1065. }
  1066. /**
  1067. * pcpu_chunk_populated - post-population bookkeeping
  1068. * @chunk: pcpu_chunk which got populated
  1069. * @page_start: the start page
  1070. * @page_end: the end page
  1071. * @for_alloc: if this is to populate for allocation
  1072. *
  1073. * Pages in [@page_start,@page_end) have been populated to @chunk. Update
  1074. * the bookkeeping information accordingly. Must be called after each
  1075. * successful population.
  1076. *
  1077. * If this is @for_alloc, do not increment pcpu_nr_empty_pop_pages because it
  1078. * is to serve an allocation in that area.
  1079. */
  1080. static void pcpu_chunk_populated(struct pcpu_chunk *chunk, int page_start,
  1081. int page_end, bool for_alloc)
  1082. {
  1083. int nr = page_end - page_start;
  1084. lockdep_assert_held(&pcpu_lock);
  1085. bitmap_set(chunk->populated, page_start, nr);
  1086. chunk->nr_populated += nr;
  1087. pcpu_nr_populated += nr;
  1088. if (!for_alloc) {
  1089. chunk->nr_empty_pop_pages += nr;
  1090. pcpu_nr_empty_pop_pages += nr;
  1091. }
  1092. }
  1093. /**
  1094. * pcpu_chunk_depopulated - post-depopulation bookkeeping
  1095. * @chunk: pcpu_chunk which got depopulated
  1096. * @page_start: the start page
  1097. * @page_end: the end page
  1098. *
  1099. * Pages in [@page_start,@page_end) have been depopulated from @chunk.
  1100. * Update the bookkeeping information accordingly. Must be called after
  1101. * each successful depopulation.
  1102. */
  1103. static void pcpu_chunk_depopulated(struct pcpu_chunk *chunk,
  1104. int page_start, int page_end)
  1105. {
  1106. int nr = page_end - page_start;
  1107. lockdep_assert_held(&pcpu_lock);
  1108. bitmap_clear(chunk->populated, page_start, nr);
  1109. chunk->nr_populated -= nr;
  1110. chunk->nr_empty_pop_pages -= nr;
  1111. pcpu_nr_empty_pop_pages -= nr;
  1112. pcpu_nr_populated -= nr;
  1113. }
  1114. /*
  1115. * Chunk management implementation.
  1116. *
  1117. * To allow different implementations, chunk alloc/free and
  1118. * [de]population are implemented in a separate file which is pulled
  1119. * into this file and compiled together. The following functions
  1120. * should be implemented.
  1121. *
  1122. * pcpu_populate_chunk - populate the specified range of a chunk
  1123. * pcpu_depopulate_chunk - depopulate the specified range of a chunk
  1124. * pcpu_create_chunk - create a new chunk
  1125. * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop
  1126. * pcpu_addr_to_page - translate address to physical address
  1127. * pcpu_verify_alloc_info - check alloc_info is acceptable during init
  1128. */
  1129. static int pcpu_populate_chunk(struct pcpu_chunk *chunk,
  1130. int page_start, int page_end, gfp_t gfp);
  1131. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
  1132. int page_start, int page_end);
  1133. static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp);
  1134. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk);
  1135. static struct page *pcpu_addr_to_page(void *addr);
  1136. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai);
  1137. #ifdef CONFIG_NEED_PER_CPU_KM
  1138. #include "percpu-km.c"
  1139. #else
  1140. #include "percpu-vm.c"
  1141. #endif
  1142. /**
  1143. * pcpu_chunk_addr_search - determine chunk containing specified address
  1144. * @addr: address for which the chunk needs to be determined.
  1145. *
  1146. * This is an internal function that handles all but static allocations.
  1147. * Static percpu address values should never be passed into the allocator.
  1148. *
  1149. * RETURNS:
  1150. * The address of the found chunk.
  1151. */
  1152. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  1153. {
  1154. /* is it in the dynamic region (first chunk)? */
  1155. if (pcpu_addr_in_chunk(pcpu_first_chunk, addr))
  1156. return pcpu_first_chunk;
  1157. /* is it in the reserved region? */
  1158. if (pcpu_addr_in_chunk(pcpu_reserved_chunk, addr))
  1159. return pcpu_reserved_chunk;
  1160. /*
  1161. * The address is relative to unit0 which might be unused and
  1162. * thus unmapped. Offset the address to the unit space of the
  1163. * current processor before looking it up in the vmalloc
  1164. * space. Note that any possible cpu id can be used here, so
  1165. * there's no need to worry about preemption or cpu hotplug.
  1166. */
  1167. addr += pcpu_unit_offsets[raw_smp_processor_id()];
  1168. return pcpu_get_page_chunk(pcpu_addr_to_page(addr));
  1169. }
  1170. /**
  1171. * pcpu_alloc - the percpu allocator
  1172. * @size: size of area to allocate in bytes
  1173. * @align: alignment of area (max PAGE_SIZE)
  1174. * @reserved: allocate from the reserved chunk if available
  1175. * @gfp: allocation flags
  1176. *
  1177. * Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't
  1178. * contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN
  1179. * then no warning will be triggered on invalid or failed allocation
  1180. * requests.
  1181. *
  1182. * RETURNS:
  1183. * Percpu pointer to the allocated area on success, NULL on failure.
  1184. */
  1185. static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
  1186. gfp_t gfp)
  1187. {
  1188. /* whitelisted flags that can be passed to the backing allocators */
  1189. gfp_t pcpu_gfp = gfp & (GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
  1190. bool is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL;
  1191. bool do_warn = !(gfp & __GFP_NOWARN);
  1192. static int warn_limit = 10;
  1193. struct pcpu_chunk *chunk;
  1194. const char *err;
  1195. int slot, off, cpu, ret;
  1196. unsigned long flags;
  1197. void __percpu *ptr;
  1198. size_t bits, bit_align;
  1199. /*
  1200. * There is now a minimum allocation size of PCPU_MIN_ALLOC_SIZE,
  1201. * therefore alignment must be a minimum of that many bytes.
  1202. * An allocation may have internal fragmentation from rounding up
  1203. * of up to PCPU_MIN_ALLOC_SIZE - 1 bytes.
  1204. */
  1205. if (unlikely(align < PCPU_MIN_ALLOC_SIZE))
  1206. align = PCPU_MIN_ALLOC_SIZE;
  1207. size = ALIGN(size, PCPU_MIN_ALLOC_SIZE);
  1208. bits = size >> PCPU_MIN_ALLOC_SHIFT;
  1209. bit_align = align >> PCPU_MIN_ALLOC_SHIFT;
  1210. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE ||
  1211. !is_power_of_2(align))) {
  1212. WARN(do_warn, "illegal size (%zu) or align (%zu) for percpu allocation\n",
  1213. size, align);
  1214. return NULL;
  1215. }
  1216. if (!is_atomic) {
  1217. /*
  1218. * pcpu_balance_workfn() allocates memory under this mutex,
  1219. * and it may wait for memory reclaim. Allow current task
  1220. * to become OOM victim, in case of memory pressure.
  1221. */
  1222. if (gfp & __GFP_NOFAIL)
  1223. mutex_lock(&pcpu_alloc_mutex);
  1224. else if (mutex_lock_killable(&pcpu_alloc_mutex))
  1225. return NULL;
  1226. }
  1227. spin_lock_irqsave(&pcpu_lock, flags);
  1228. /* serve reserved allocations from the reserved chunk if available */
  1229. if (reserved && pcpu_reserved_chunk) {
  1230. chunk = pcpu_reserved_chunk;
  1231. off = pcpu_find_block_fit(chunk, bits, bit_align, is_atomic);
  1232. if (off < 0) {
  1233. err = "alloc from reserved chunk failed";
  1234. goto fail_unlock;
  1235. }
  1236. off = pcpu_alloc_area(chunk, bits, bit_align, off);
  1237. if (off >= 0)
  1238. goto area_found;
  1239. err = "alloc from reserved chunk failed";
  1240. goto fail_unlock;
  1241. }
  1242. restart:
  1243. /* search through normal chunks */
  1244. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  1245. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  1246. off = pcpu_find_block_fit(chunk, bits, bit_align,
  1247. is_atomic);
  1248. if (off < 0)
  1249. continue;
  1250. off = pcpu_alloc_area(chunk, bits, bit_align, off);
  1251. if (off >= 0)
  1252. goto area_found;
  1253. }
  1254. }
  1255. spin_unlock_irqrestore(&pcpu_lock, flags);
  1256. /*
  1257. * No space left. Create a new chunk. We don't want multiple
  1258. * tasks to create chunks simultaneously. Serialize and create iff
  1259. * there's still no empty chunk after grabbing the mutex.
  1260. */
  1261. if (is_atomic) {
  1262. err = "atomic alloc failed, no space left";
  1263. goto fail;
  1264. }
  1265. if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) {
  1266. chunk = pcpu_create_chunk(pcpu_gfp);
  1267. if (!chunk) {
  1268. err = "failed to allocate new chunk";
  1269. goto fail;
  1270. }
  1271. spin_lock_irqsave(&pcpu_lock, flags);
  1272. pcpu_chunk_relocate(chunk, -1);
  1273. } else {
  1274. spin_lock_irqsave(&pcpu_lock, flags);
  1275. }
  1276. goto restart;
  1277. area_found:
  1278. pcpu_stats_area_alloc(chunk, size);
  1279. spin_unlock_irqrestore(&pcpu_lock, flags);
  1280. /* populate if not all pages are already there */
  1281. if (!is_atomic) {
  1282. int page_start, page_end, rs, re;
  1283. page_start = PFN_DOWN(off);
  1284. page_end = PFN_UP(off + size);
  1285. pcpu_for_each_unpop_region(chunk->populated, rs, re,
  1286. page_start, page_end) {
  1287. WARN_ON(chunk->immutable);
  1288. ret = pcpu_populate_chunk(chunk, rs, re, pcpu_gfp);
  1289. spin_lock_irqsave(&pcpu_lock, flags);
  1290. if (ret) {
  1291. pcpu_free_area(chunk, off);
  1292. err = "failed to populate";
  1293. goto fail_unlock;
  1294. }
  1295. pcpu_chunk_populated(chunk, rs, re, true);
  1296. spin_unlock_irqrestore(&pcpu_lock, flags);
  1297. }
  1298. mutex_unlock(&pcpu_alloc_mutex);
  1299. }
  1300. if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_LOW)
  1301. pcpu_schedule_balance_work();
  1302. /* clear the areas and return address relative to base address */
  1303. for_each_possible_cpu(cpu)
  1304. memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
  1305. ptr = __addr_to_pcpu_ptr(chunk->base_addr + off);
  1306. kmemleak_alloc_percpu(ptr, size, gfp);
  1307. trace_percpu_alloc_percpu(reserved, is_atomic, size, align,
  1308. chunk->base_addr, off, ptr);
  1309. return ptr;
  1310. fail_unlock:
  1311. spin_unlock_irqrestore(&pcpu_lock, flags);
  1312. fail:
  1313. trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align);
  1314. if (!is_atomic && do_warn && warn_limit) {
  1315. pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
  1316. size, align, is_atomic, err);
  1317. dump_stack();
  1318. if (!--warn_limit)
  1319. pr_info("limit reached, disable warning\n");
  1320. }
  1321. if (is_atomic) {
  1322. /* see the flag handling in pcpu_blance_workfn() */
  1323. pcpu_atomic_alloc_failed = true;
  1324. pcpu_schedule_balance_work();
  1325. } else {
  1326. mutex_unlock(&pcpu_alloc_mutex);
  1327. }
  1328. return NULL;
  1329. }
  1330. /**
  1331. * __alloc_percpu_gfp - allocate dynamic percpu area
  1332. * @size: size of area to allocate in bytes
  1333. * @align: alignment of area (max PAGE_SIZE)
  1334. * @gfp: allocation flags
  1335. *
  1336. * Allocate zero-filled percpu area of @size bytes aligned at @align. If
  1337. * @gfp doesn't contain %GFP_KERNEL, the allocation doesn't block and can
  1338. * be called from any context but is a lot more likely to fail. If @gfp
  1339. * has __GFP_NOWARN then no warning will be triggered on invalid or failed
  1340. * allocation requests.
  1341. *
  1342. * RETURNS:
  1343. * Percpu pointer to the allocated area on success, NULL on failure.
  1344. */
  1345. void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp)
  1346. {
  1347. return pcpu_alloc(size, align, false, gfp);
  1348. }
  1349. EXPORT_SYMBOL_GPL(__alloc_percpu_gfp);
  1350. /**
  1351. * __alloc_percpu - allocate dynamic percpu area
  1352. * @size: size of area to allocate in bytes
  1353. * @align: alignment of area (max PAGE_SIZE)
  1354. *
  1355. * Equivalent to __alloc_percpu_gfp(size, align, %GFP_KERNEL).
  1356. */
  1357. void __percpu *__alloc_percpu(size_t size, size_t align)
  1358. {
  1359. return pcpu_alloc(size, align, false, GFP_KERNEL);
  1360. }
  1361. EXPORT_SYMBOL_GPL(__alloc_percpu);
  1362. /**
  1363. * __alloc_reserved_percpu - allocate reserved percpu area
  1364. * @size: size of area to allocate in bytes
  1365. * @align: alignment of area (max PAGE_SIZE)
  1366. *
  1367. * Allocate zero-filled percpu area of @size bytes aligned at @align
  1368. * from reserved percpu area if arch has set it up; otherwise,
  1369. * allocation is served from the same dynamic area. Might sleep.
  1370. * Might trigger writeouts.
  1371. *
  1372. * CONTEXT:
  1373. * Does GFP_KERNEL allocation.
  1374. *
  1375. * RETURNS:
  1376. * Percpu pointer to the allocated area on success, NULL on failure.
  1377. */
  1378. void __percpu *__alloc_reserved_percpu(size_t size, size_t align)
  1379. {
  1380. return pcpu_alloc(size, align, true, GFP_KERNEL);
  1381. }
  1382. /**
  1383. * pcpu_balance_workfn - manage the amount of free chunks and populated pages
  1384. * @work: unused
  1385. *
  1386. * Reclaim all fully free chunks except for the first one. This is also
  1387. * responsible for maintaining the pool of empty populated pages. However,
  1388. * it is possible that this is called when physical memory is scarce causing
  1389. * OOM killer to be triggered. We should avoid doing so until an actual
  1390. * allocation causes the failure as it is possible that requests can be
  1391. * serviced from already backed regions.
  1392. */
  1393. static void pcpu_balance_workfn(struct work_struct *work)
  1394. {
  1395. /* gfp flags passed to underlying allocators */
  1396. const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
  1397. LIST_HEAD(to_free);
  1398. struct list_head *free_head = &pcpu_slot[pcpu_nr_slots - 1];
  1399. struct pcpu_chunk *chunk, *next;
  1400. int slot, nr_to_pop, ret;
  1401. /*
  1402. * There's no reason to keep around multiple unused chunks and VM
  1403. * areas can be scarce. Destroy all free chunks except for one.
  1404. */
  1405. mutex_lock(&pcpu_alloc_mutex);
  1406. spin_lock_irq(&pcpu_lock);
  1407. list_for_each_entry_safe(chunk, next, free_head, list) {
  1408. WARN_ON(chunk->immutable);
  1409. /* spare the first one */
  1410. if (chunk == list_first_entry(free_head, struct pcpu_chunk, list))
  1411. continue;
  1412. list_move(&chunk->list, &to_free);
  1413. }
  1414. spin_unlock_irq(&pcpu_lock);
  1415. list_for_each_entry_safe(chunk, next, &to_free, list) {
  1416. int rs, re;
  1417. pcpu_for_each_pop_region(chunk->populated, rs, re, 0,
  1418. chunk->nr_pages) {
  1419. pcpu_depopulate_chunk(chunk, rs, re);
  1420. spin_lock_irq(&pcpu_lock);
  1421. pcpu_chunk_depopulated(chunk, rs, re);
  1422. spin_unlock_irq(&pcpu_lock);
  1423. }
  1424. pcpu_destroy_chunk(chunk);
  1425. cond_resched();
  1426. }
  1427. /*
  1428. * Ensure there are certain number of free populated pages for
  1429. * atomic allocs. Fill up from the most packed so that atomic
  1430. * allocs don't increase fragmentation. If atomic allocation
  1431. * failed previously, always populate the maximum amount. This
  1432. * should prevent atomic allocs larger than PAGE_SIZE from keeping
  1433. * failing indefinitely; however, large atomic allocs are not
  1434. * something we support properly and can be highly unreliable and
  1435. * inefficient.
  1436. */
  1437. retry_pop:
  1438. if (pcpu_atomic_alloc_failed) {
  1439. nr_to_pop = PCPU_EMPTY_POP_PAGES_HIGH;
  1440. /* best effort anyway, don't worry about synchronization */
  1441. pcpu_atomic_alloc_failed = false;
  1442. } else {
  1443. nr_to_pop = clamp(PCPU_EMPTY_POP_PAGES_HIGH -
  1444. pcpu_nr_empty_pop_pages,
  1445. 0, PCPU_EMPTY_POP_PAGES_HIGH);
  1446. }
  1447. for (slot = pcpu_size_to_slot(PAGE_SIZE); slot < pcpu_nr_slots; slot++) {
  1448. int nr_unpop = 0, rs, re;
  1449. if (!nr_to_pop)
  1450. break;
  1451. spin_lock_irq(&pcpu_lock);
  1452. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  1453. nr_unpop = chunk->nr_pages - chunk->nr_populated;
  1454. if (nr_unpop)
  1455. break;
  1456. }
  1457. spin_unlock_irq(&pcpu_lock);
  1458. if (!nr_unpop)
  1459. continue;
  1460. /* @chunk can't go away while pcpu_alloc_mutex is held */
  1461. pcpu_for_each_unpop_region(chunk->populated, rs, re, 0,
  1462. chunk->nr_pages) {
  1463. int nr = min(re - rs, nr_to_pop);
  1464. ret = pcpu_populate_chunk(chunk, rs, rs + nr, gfp);
  1465. if (!ret) {
  1466. nr_to_pop -= nr;
  1467. spin_lock_irq(&pcpu_lock);
  1468. pcpu_chunk_populated(chunk, rs, rs + nr, false);
  1469. spin_unlock_irq(&pcpu_lock);
  1470. } else {
  1471. nr_to_pop = 0;
  1472. }
  1473. if (!nr_to_pop)
  1474. break;
  1475. }
  1476. }
  1477. if (nr_to_pop) {
  1478. /* ran out of chunks to populate, create a new one and retry */
  1479. chunk = pcpu_create_chunk(gfp);
  1480. if (chunk) {
  1481. spin_lock_irq(&pcpu_lock);
  1482. pcpu_chunk_relocate(chunk, -1);
  1483. spin_unlock_irq(&pcpu_lock);
  1484. goto retry_pop;
  1485. }
  1486. }
  1487. mutex_unlock(&pcpu_alloc_mutex);
  1488. }
  1489. /**
  1490. * free_percpu - free percpu area
  1491. * @ptr: pointer to area to free
  1492. *
  1493. * Free percpu area @ptr.
  1494. *
  1495. * CONTEXT:
  1496. * Can be called from atomic context.
  1497. */
  1498. void free_percpu(void __percpu *ptr)
  1499. {
  1500. void *addr;
  1501. struct pcpu_chunk *chunk;
  1502. unsigned long flags;
  1503. int off;
  1504. bool need_balance = false;
  1505. if (!ptr)
  1506. return;
  1507. kmemleak_free_percpu(ptr);
  1508. addr = __pcpu_ptr_to_addr(ptr);
  1509. spin_lock_irqsave(&pcpu_lock, flags);
  1510. chunk = pcpu_chunk_addr_search(addr);
  1511. off = addr - chunk->base_addr;
  1512. pcpu_free_area(chunk, off);
  1513. /* if there are more than one fully free chunks, wake up grim reaper */
  1514. if (chunk->free_bytes == pcpu_unit_size) {
  1515. struct pcpu_chunk *pos;
  1516. list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
  1517. if (pos != chunk) {
  1518. need_balance = true;
  1519. break;
  1520. }
  1521. }
  1522. trace_percpu_free_percpu(chunk->base_addr, off, ptr);
  1523. spin_unlock_irqrestore(&pcpu_lock, flags);
  1524. if (need_balance)
  1525. pcpu_schedule_balance_work();
  1526. }
  1527. EXPORT_SYMBOL_GPL(free_percpu);
  1528. bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr)
  1529. {
  1530. #ifdef CONFIG_SMP
  1531. const size_t static_size = __per_cpu_end - __per_cpu_start;
  1532. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  1533. unsigned int cpu;
  1534. for_each_possible_cpu(cpu) {
  1535. void *start = per_cpu_ptr(base, cpu);
  1536. void *va = (void *)addr;
  1537. if (va >= start && va < start + static_size) {
  1538. if (can_addr) {
  1539. *can_addr = (unsigned long) (va - start);
  1540. *can_addr += (unsigned long)
  1541. per_cpu_ptr(base, get_boot_cpu_id());
  1542. }
  1543. return true;
  1544. }
  1545. }
  1546. #endif
  1547. /* on UP, can't distinguish from other static vars, always false */
  1548. return false;
  1549. }
  1550. /**
  1551. * is_kernel_percpu_address - test whether address is from static percpu area
  1552. * @addr: address to test
  1553. *
  1554. * Test whether @addr belongs to in-kernel static percpu area. Module
  1555. * static percpu areas are not considered. For those, use
  1556. * is_module_percpu_address().
  1557. *
  1558. * RETURNS:
  1559. * %true if @addr is from in-kernel static percpu area, %false otherwise.
  1560. */
  1561. bool is_kernel_percpu_address(unsigned long addr)
  1562. {
  1563. return __is_kernel_percpu_address(addr, NULL);
  1564. }
  1565. /**
  1566. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  1567. * @addr: the address to be converted to physical address
  1568. *
  1569. * Given @addr which is dereferenceable address obtained via one of
  1570. * percpu access macros, this function translates it into its physical
  1571. * address. The caller is responsible for ensuring @addr stays valid
  1572. * until this function finishes.
  1573. *
  1574. * percpu allocator has special setup for the first chunk, which currently
  1575. * supports either embedding in linear address space or vmalloc mapping,
  1576. * and, from the second one, the backing allocator (currently either vm or
  1577. * km) provides translation.
  1578. *
  1579. * The addr can be translated simply without checking if it falls into the
  1580. * first chunk. But the current code reflects better how percpu allocator
  1581. * actually works, and the verification can discover both bugs in percpu
  1582. * allocator itself and per_cpu_ptr_to_phys() callers. So we keep current
  1583. * code.
  1584. *
  1585. * RETURNS:
  1586. * The physical address for @addr.
  1587. */
  1588. phys_addr_t per_cpu_ptr_to_phys(void *addr)
  1589. {
  1590. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  1591. bool in_first_chunk = false;
  1592. unsigned long first_low, first_high;
  1593. unsigned int cpu;
  1594. /*
  1595. * The following test on unit_low/high isn't strictly
  1596. * necessary but will speed up lookups of addresses which
  1597. * aren't in the first chunk.
  1598. *
  1599. * The address check is against full chunk sizes. pcpu_base_addr
  1600. * points to the beginning of the first chunk including the
  1601. * static region. Assumes good intent as the first chunk may
  1602. * not be full (ie. < pcpu_unit_pages in size).
  1603. */
  1604. first_low = (unsigned long)pcpu_base_addr +
  1605. pcpu_unit_page_offset(pcpu_low_unit_cpu, 0);
  1606. first_high = (unsigned long)pcpu_base_addr +
  1607. pcpu_unit_page_offset(pcpu_high_unit_cpu, pcpu_unit_pages);
  1608. if ((unsigned long)addr >= first_low &&
  1609. (unsigned long)addr < first_high) {
  1610. for_each_possible_cpu(cpu) {
  1611. void *start = per_cpu_ptr(base, cpu);
  1612. if (addr >= start && addr < start + pcpu_unit_size) {
  1613. in_first_chunk = true;
  1614. break;
  1615. }
  1616. }
  1617. }
  1618. if (in_first_chunk) {
  1619. if (!is_vmalloc_addr(addr))
  1620. return __pa(addr);
  1621. else
  1622. return page_to_phys(vmalloc_to_page(addr)) +
  1623. offset_in_page(addr);
  1624. } else
  1625. return page_to_phys(pcpu_addr_to_page(addr)) +
  1626. offset_in_page(addr);
  1627. }
  1628. /**
  1629. * pcpu_alloc_alloc_info - allocate percpu allocation info
  1630. * @nr_groups: the number of groups
  1631. * @nr_units: the number of units
  1632. *
  1633. * Allocate ai which is large enough for @nr_groups groups containing
  1634. * @nr_units units. The returned ai's groups[0].cpu_map points to the
  1635. * cpu_map array which is long enough for @nr_units and filled with
  1636. * NR_CPUS. It's the caller's responsibility to initialize cpu_map
  1637. * pointer of other groups.
  1638. *
  1639. * RETURNS:
  1640. * Pointer to the allocated pcpu_alloc_info on success, NULL on
  1641. * failure.
  1642. */
  1643. struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  1644. int nr_units)
  1645. {
  1646. struct pcpu_alloc_info *ai;
  1647. size_t base_size, ai_size;
  1648. void *ptr;
  1649. int unit;
  1650. base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]),
  1651. __alignof__(ai->groups[0].cpu_map[0]));
  1652. ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]);
  1653. ptr = memblock_virt_alloc_nopanic(PFN_ALIGN(ai_size), PAGE_SIZE);
  1654. if (!ptr)
  1655. return NULL;
  1656. ai = ptr;
  1657. ptr += base_size;
  1658. ai->groups[0].cpu_map = ptr;
  1659. for (unit = 0; unit < nr_units; unit++)
  1660. ai->groups[0].cpu_map[unit] = NR_CPUS;
  1661. ai->nr_groups = nr_groups;
  1662. ai->__ai_size = PFN_ALIGN(ai_size);
  1663. return ai;
  1664. }
  1665. /**
  1666. * pcpu_free_alloc_info - free percpu allocation info
  1667. * @ai: pcpu_alloc_info to free
  1668. *
  1669. * Free @ai which was allocated by pcpu_alloc_alloc_info().
  1670. */
  1671. void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
  1672. {
  1673. memblock_free_early(__pa(ai), ai->__ai_size);
  1674. }
  1675. /**
  1676. * pcpu_dump_alloc_info - print out information about pcpu_alloc_info
  1677. * @lvl: loglevel
  1678. * @ai: allocation info to dump
  1679. *
  1680. * Print out information about @ai using loglevel @lvl.
  1681. */
  1682. static void pcpu_dump_alloc_info(const char *lvl,
  1683. const struct pcpu_alloc_info *ai)
  1684. {
  1685. int group_width = 1, cpu_width = 1, width;
  1686. char empty_str[] = "--------";
  1687. int alloc = 0, alloc_end = 0;
  1688. int group, v;
  1689. int upa, apl; /* units per alloc, allocs per line */
  1690. v = ai->nr_groups;
  1691. while (v /= 10)
  1692. group_width++;
  1693. v = num_possible_cpus();
  1694. while (v /= 10)
  1695. cpu_width++;
  1696. empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0';
  1697. upa = ai->alloc_size / ai->unit_size;
  1698. width = upa * (cpu_width + 1) + group_width + 3;
  1699. apl = rounddown_pow_of_two(max(60 / width, 1));
  1700. printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu",
  1701. lvl, ai->static_size, ai->reserved_size, ai->dyn_size,
  1702. ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size);
  1703. for (group = 0; group < ai->nr_groups; group++) {
  1704. const struct pcpu_group_info *gi = &ai->groups[group];
  1705. int unit = 0, unit_end = 0;
  1706. BUG_ON(gi->nr_units % upa);
  1707. for (alloc_end += gi->nr_units / upa;
  1708. alloc < alloc_end; alloc++) {
  1709. if (!(alloc % apl)) {
  1710. pr_cont("\n");
  1711. printk("%spcpu-alloc: ", lvl);
  1712. }
  1713. pr_cont("[%0*d] ", group_width, group);
  1714. for (unit_end += upa; unit < unit_end; unit++)
  1715. if (gi->cpu_map[unit] != NR_CPUS)
  1716. pr_cont("%0*d ",
  1717. cpu_width, gi->cpu_map[unit]);
  1718. else
  1719. pr_cont("%s ", empty_str);
  1720. }
  1721. }
  1722. pr_cont("\n");
  1723. }
  1724. /**
  1725. * pcpu_setup_first_chunk - initialize the first percpu chunk
  1726. * @ai: pcpu_alloc_info describing how to percpu area is shaped
  1727. * @base_addr: mapped address
  1728. *
  1729. * Initialize the first percpu chunk which contains the kernel static
  1730. * perpcu area. This function is to be called from arch percpu area
  1731. * setup path.
  1732. *
  1733. * @ai contains all information necessary to initialize the first
  1734. * chunk and prime the dynamic percpu allocator.
  1735. *
  1736. * @ai->static_size is the size of static percpu area.
  1737. *
  1738. * @ai->reserved_size, if non-zero, specifies the amount of bytes to
  1739. * reserve after the static area in the first chunk. This reserves
  1740. * the first chunk such that it's available only through reserved
  1741. * percpu allocation. This is primarily used to serve module percpu
  1742. * static areas on architectures where the addressing model has
  1743. * limited offset range for symbol relocations to guarantee module
  1744. * percpu symbols fall inside the relocatable range.
  1745. *
  1746. * @ai->dyn_size determines the number of bytes available for dynamic
  1747. * allocation in the first chunk. The area between @ai->static_size +
  1748. * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused.
  1749. *
  1750. * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE
  1751. * and equal to or larger than @ai->static_size + @ai->reserved_size +
  1752. * @ai->dyn_size.
  1753. *
  1754. * @ai->atom_size is the allocation atom size and used as alignment
  1755. * for vm areas.
  1756. *
  1757. * @ai->alloc_size is the allocation size and always multiple of
  1758. * @ai->atom_size. This is larger than @ai->atom_size if
  1759. * @ai->unit_size is larger than @ai->atom_size.
  1760. *
  1761. * @ai->nr_groups and @ai->groups describe virtual memory layout of
  1762. * percpu areas. Units which should be colocated are put into the
  1763. * same group. Dynamic VM areas will be allocated according to these
  1764. * groupings. If @ai->nr_groups is zero, a single group containing
  1765. * all units is assumed.
  1766. *
  1767. * The caller should have mapped the first chunk at @base_addr and
  1768. * copied static data to each unit.
  1769. *
  1770. * The first chunk will always contain a static and a dynamic region.
  1771. * However, the static region is not managed by any chunk. If the first
  1772. * chunk also contains a reserved region, it is served by two chunks -
  1773. * one for the reserved region and one for the dynamic region. They
  1774. * share the same vm, but use offset regions in the area allocation map.
  1775. * The chunk serving the dynamic region is circulated in the chunk slots
  1776. * and available for dynamic allocation like any other chunk.
  1777. *
  1778. * RETURNS:
  1779. * 0 on success, -errno on failure.
  1780. */
  1781. int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  1782. void *base_addr)
  1783. {
  1784. size_t size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
  1785. size_t static_size, dyn_size;
  1786. struct pcpu_chunk *chunk;
  1787. unsigned long *group_offsets;
  1788. size_t *group_sizes;
  1789. unsigned long *unit_off;
  1790. unsigned int cpu;
  1791. int *unit_map;
  1792. int group, unit, i;
  1793. int map_size;
  1794. unsigned long tmp_addr;
  1795. #define PCPU_SETUP_BUG_ON(cond) do { \
  1796. if (unlikely(cond)) { \
  1797. pr_emerg("failed to initialize, %s\n", #cond); \
  1798. pr_emerg("cpu_possible_mask=%*pb\n", \
  1799. cpumask_pr_args(cpu_possible_mask)); \
  1800. pcpu_dump_alloc_info(KERN_EMERG, ai); \
  1801. BUG(); \
  1802. } \
  1803. } while (0)
  1804. /* sanity checks */
  1805. PCPU_SETUP_BUG_ON(ai->nr_groups <= 0);
  1806. #ifdef CONFIG_SMP
  1807. PCPU_SETUP_BUG_ON(!ai->static_size);
  1808. PCPU_SETUP_BUG_ON(offset_in_page(__per_cpu_start));
  1809. #endif
  1810. PCPU_SETUP_BUG_ON(!base_addr);
  1811. PCPU_SETUP_BUG_ON(offset_in_page(base_addr));
  1812. PCPU_SETUP_BUG_ON(ai->unit_size < size_sum);
  1813. PCPU_SETUP_BUG_ON(offset_in_page(ai->unit_size));
  1814. PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE);
  1815. PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->unit_size, PCPU_BITMAP_BLOCK_SIZE));
  1816. PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE);
  1817. PCPU_SETUP_BUG_ON(!ai->dyn_size);
  1818. PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->reserved_size, PCPU_MIN_ALLOC_SIZE));
  1819. PCPU_SETUP_BUG_ON(!(IS_ALIGNED(PCPU_BITMAP_BLOCK_SIZE, PAGE_SIZE) ||
  1820. IS_ALIGNED(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE)));
  1821. PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
  1822. /* process group information and build config tables accordingly */
  1823. group_offsets = memblock_virt_alloc(ai->nr_groups *
  1824. sizeof(group_offsets[0]), 0);
  1825. group_sizes = memblock_virt_alloc(ai->nr_groups *
  1826. sizeof(group_sizes[0]), 0);
  1827. unit_map = memblock_virt_alloc(nr_cpu_ids * sizeof(unit_map[0]), 0);
  1828. unit_off = memblock_virt_alloc(nr_cpu_ids * sizeof(unit_off[0]), 0);
  1829. for (cpu = 0; cpu < nr_cpu_ids; cpu++)
  1830. unit_map[cpu] = UINT_MAX;
  1831. pcpu_low_unit_cpu = NR_CPUS;
  1832. pcpu_high_unit_cpu = NR_CPUS;
  1833. for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) {
  1834. const struct pcpu_group_info *gi = &ai->groups[group];
  1835. group_offsets[group] = gi->base_offset;
  1836. group_sizes[group] = gi->nr_units * ai->unit_size;
  1837. for (i = 0; i < gi->nr_units; i++) {
  1838. cpu = gi->cpu_map[i];
  1839. if (cpu == NR_CPUS)
  1840. continue;
  1841. PCPU_SETUP_BUG_ON(cpu >= nr_cpu_ids);
  1842. PCPU_SETUP_BUG_ON(!cpu_possible(cpu));
  1843. PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX);
  1844. unit_map[cpu] = unit + i;
  1845. unit_off[cpu] = gi->base_offset + i * ai->unit_size;
  1846. /* determine low/high unit_cpu */
  1847. if (pcpu_low_unit_cpu == NR_CPUS ||
  1848. unit_off[cpu] < unit_off[pcpu_low_unit_cpu])
  1849. pcpu_low_unit_cpu = cpu;
  1850. if (pcpu_high_unit_cpu == NR_CPUS ||
  1851. unit_off[cpu] > unit_off[pcpu_high_unit_cpu])
  1852. pcpu_high_unit_cpu = cpu;
  1853. }
  1854. }
  1855. pcpu_nr_units = unit;
  1856. for_each_possible_cpu(cpu)
  1857. PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX);
  1858. /* we're done parsing the input, undefine BUG macro and dump config */
  1859. #undef PCPU_SETUP_BUG_ON
  1860. pcpu_dump_alloc_info(KERN_DEBUG, ai);
  1861. pcpu_nr_groups = ai->nr_groups;
  1862. pcpu_group_offsets = group_offsets;
  1863. pcpu_group_sizes = group_sizes;
  1864. pcpu_unit_map = unit_map;
  1865. pcpu_unit_offsets = unit_off;
  1866. /* determine basic parameters */
  1867. pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
  1868. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  1869. pcpu_atom_size = ai->atom_size;
  1870. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
  1871. BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
  1872. pcpu_stats_save_ai(ai);
  1873. /*
  1874. * Allocate chunk slots. The additional last slot is for
  1875. * empty chunks.
  1876. */
  1877. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  1878. pcpu_slot = memblock_virt_alloc(
  1879. pcpu_nr_slots * sizeof(pcpu_slot[0]), 0);
  1880. for (i = 0; i < pcpu_nr_slots; i++)
  1881. INIT_LIST_HEAD(&pcpu_slot[i]);
  1882. /*
  1883. * The end of the static region needs to be aligned with the
  1884. * minimum allocation size as this offsets the reserved and
  1885. * dynamic region. The first chunk ends page aligned by
  1886. * expanding the dynamic region, therefore the dynamic region
  1887. * can be shrunk to compensate while still staying above the
  1888. * configured sizes.
  1889. */
  1890. static_size = ALIGN(ai->static_size, PCPU_MIN_ALLOC_SIZE);
  1891. dyn_size = ai->dyn_size - (static_size - ai->static_size);
  1892. /*
  1893. * Initialize first chunk.
  1894. * If the reserved_size is non-zero, this initializes the reserved
  1895. * chunk. If the reserved_size is zero, the reserved chunk is NULL
  1896. * and the dynamic region is initialized here. The first chunk,
  1897. * pcpu_first_chunk, will always point to the chunk that serves
  1898. * the dynamic region.
  1899. */
  1900. tmp_addr = (unsigned long)base_addr + static_size;
  1901. map_size = ai->reserved_size ?: dyn_size;
  1902. chunk = pcpu_alloc_first_chunk(tmp_addr, map_size);
  1903. /* init dynamic chunk if necessary */
  1904. if (ai->reserved_size) {
  1905. pcpu_reserved_chunk = chunk;
  1906. tmp_addr = (unsigned long)base_addr + static_size +
  1907. ai->reserved_size;
  1908. map_size = dyn_size;
  1909. chunk = pcpu_alloc_first_chunk(tmp_addr, map_size);
  1910. }
  1911. /* link the first chunk in */
  1912. pcpu_first_chunk = chunk;
  1913. pcpu_nr_empty_pop_pages = pcpu_first_chunk->nr_empty_pop_pages;
  1914. pcpu_chunk_relocate(pcpu_first_chunk, -1);
  1915. /* include all regions of the first chunk */
  1916. pcpu_nr_populated += PFN_DOWN(size_sum);
  1917. pcpu_stats_chunk_alloc();
  1918. trace_percpu_create_chunk(base_addr);
  1919. /* we're done */
  1920. pcpu_base_addr = base_addr;
  1921. return 0;
  1922. }
  1923. #ifdef CONFIG_SMP
  1924. const char * const pcpu_fc_names[PCPU_FC_NR] __initconst = {
  1925. [PCPU_FC_AUTO] = "auto",
  1926. [PCPU_FC_EMBED] = "embed",
  1927. [PCPU_FC_PAGE] = "page",
  1928. };
  1929. enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO;
  1930. static int __init percpu_alloc_setup(char *str)
  1931. {
  1932. if (!str)
  1933. return -EINVAL;
  1934. if (0)
  1935. /* nada */;
  1936. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  1937. else if (!strcmp(str, "embed"))
  1938. pcpu_chosen_fc = PCPU_FC_EMBED;
  1939. #endif
  1940. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  1941. else if (!strcmp(str, "page"))
  1942. pcpu_chosen_fc = PCPU_FC_PAGE;
  1943. #endif
  1944. else
  1945. pr_warn("unknown allocator %s specified\n", str);
  1946. return 0;
  1947. }
  1948. early_param("percpu_alloc", percpu_alloc_setup);
  1949. /*
  1950. * pcpu_embed_first_chunk() is used by the generic percpu setup.
  1951. * Build it if needed by the arch config or the generic setup is going
  1952. * to be used.
  1953. */
  1954. #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \
  1955. !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  1956. #define BUILD_EMBED_FIRST_CHUNK
  1957. #endif
  1958. /* build pcpu_page_first_chunk() iff needed by the arch config */
  1959. #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK)
  1960. #define BUILD_PAGE_FIRST_CHUNK
  1961. #endif
  1962. /* pcpu_build_alloc_info() is used by both embed and page first chunk */
  1963. #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK)
  1964. /**
  1965. * pcpu_build_alloc_info - build alloc_info considering distances between CPUs
  1966. * @reserved_size: the size of reserved percpu area in bytes
  1967. * @dyn_size: minimum free size for dynamic allocation in bytes
  1968. * @atom_size: allocation atom size
  1969. * @cpu_distance_fn: callback to determine distance between cpus, optional
  1970. *
  1971. * This function determines grouping of units, their mappings to cpus
  1972. * and other parameters considering needed percpu size, allocation
  1973. * atom size and distances between CPUs.
  1974. *
  1975. * Groups are always multiples of atom size and CPUs which are of
  1976. * LOCAL_DISTANCE both ways are grouped together and share space for
  1977. * units in the same group. The returned configuration is guaranteed
  1978. * to have CPUs on different nodes on different groups and >=75% usage
  1979. * of allocated virtual address space.
  1980. *
  1981. * RETURNS:
  1982. * On success, pointer to the new allocation_info is returned. On
  1983. * failure, ERR_PTR value is returned.
  1984. */
  1985. static struct pcpu_alloc_info * __init pcpu_build_alloc_info(
  1986. size_t reserved_size, size_t dyn_size,
  1987. size_t atom_size,
  1988. pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
  1989. {
  1990. static int group_map[NR_CPUS] __initdata;
  1991. static int group_cnt[NR_CPUS] __initdata;
  1992. const size_t static_size = __per_cpu_end - __per_cpu_start;
  1993. int nr_groups = 1, nr_units = 0;
  1994. size_t size_sum, min_unit_size, alloc_size;
  1995. int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */
  1996. int last_allocs, group, unit;
  1997. unsigned int cpu, tcpu;
  1998. struct pcpu_alloc_info *ai;
  1999. unsigned int *cpu_map;
  2000. /* this function may be called multiple times */
  2001. memset(group_map, 0, sizeof(group_map));
  2002. memset(group_cnt, 0, sizeof(group_cnt));
  2003. /* calculate size_sum and ensure dyn_size is enough for early alloc */
  2004. size_sum = PFN_ALIGN(static_size + reserved_size +
  2005. max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE));
  2006. dyn_size = size_sum - static_size - reserved_size;
  2007. /*
  2008. * Determine min_unit_size, alloc_size and max_upa such that
  2009. * alloc_size is multiple of atom_size and is the smallest
  2010. * which can accommodate 4k aligned segments which are equal to
  2011. * or larger than min_unit_size.
  2012. */
  2013. min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
  2014. /* determine the maximum # of units that can fit in an allocation */
  2015. alloc_size = roundup(min_unit_size, atom_size);
  2016. upa = alloc_size / min_unit_size;
  2017. while (alloc_size % upa || (offset_in_page(alloc_size / upa)))
  2018. upa--;
  2019. max_upa = upa;
  2020. /* group cpus according to their proximity */
  2021. for_each_possible_cpu(cpu) {
  2022. group = 0;
  2023. next_group:
  2024. for_each_possible_cpu(tcpu) {
  2025. if (cpu == tcpu)
  2026. break;
  2027. if (group_map[tcpu] == group && cpu_distance_fn &&
  2028. (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE ||
  2029. cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) {
  2030. group++;
  2031. nr_groups = max(nr_groups, group + 1);
  2032. goto next_group;
  2033. }
  2034. }
  2035. group_map[cpu] = group;
  2036. group_cnt[group]++;
  2037. }
  2038. /*
  2039. * Wasted space is caused by a ratio imbalance of upa to group_cnt.
  2040. * Expand the unit_size until we use >= 75% of the units allocated.
  2041. * Related to atom_size, which could be much larger than the unit_size.
  2042. */
  2043. last_allocs = INT_MAX;
  2044. for (upa = max_upa; upa; upa--) {
  2045. int allocs = 0, wasted = 0;
  2046. if (alloc_size % upa || (offset_in_page(alloc_size / upa)))
  2047. continue;
  2048. for (group = 0; group < nr_groups; group++) {
  2049. int this_allocs = DIV_ROUND_UP(group_cnt[group], upa);
  2050. allocs += this_allocs;
  2051. wasted += this_allocs * upa - group_cnt[group];
  2052. }
  2053. /*
  2054. * Don't accept if wastage is over 1/3. The
  2055. * greater-than comparison ensures upa==1 always
  2056. * passes the following check.
  2057. */
  2058. if (wasted > num_possible_cpus() / 3)
  2059. continue;
  2060. /* and then don't consume more memory */
  2061. if (allocs > last_allocs)
  2062. break;
  2063. last_allocs = allocs;
  2064. best_upa = upa;
  2065. }
  2066. upa = best_upa;
  2067. /* allocate and fill alloc_info */
  2068. for (group = 0; group < nr_groups; group++)
  2069. nr_units += roundup(group_cnt[group], upa);
  2070. ai = pcpu_alloc_alloc_info(nr_groups, nr_units);
  2071. if (!ai)
  2072. return ERR_PTR(-ENOMEM);
  2073. cpu_map = ai->groups[0].cpu_map;
  2074. for (group = 0; group < nr_groups; group++) {
  2075. ai->groups[group].cpu_map = cpu_map;
  2076. cpu_map += roundup(group_cnt[group], upa);
  2077. }
  2078. ai->static_size = static_size;
  2079. ai->reserved_size = reserved_size;
  2080. ai->dyn_size = dyn_size;
  2081. ai->unit_size = alloc_size / upa;
  2082. ai->atom_size = atom_size;
  2083. ai->alloc_size = alloc_size;
  2084. for (group = 0, unit = 0; group_cnt[group]; group++) {
  2085. struct pcpu_group_info *gi = &ai->groups[group];
  2086. /*
  2087. * Initialize base_offset as if all groups are located
  2088. * back-to-back. The caller should update this to
  2089. * reflect actual allocation.
  2090. */
  2091. gi->base_offset = unit * ai->unit_size;
  2092. for_each_possible_cpu(cpu)
  2093. if (group_map[cpu] == group)
  2094. gi->cpu_map[gi->nr_units++] = cpu;
  2095. gi->nr_units = roundup(gi->nr_units, upa);
  2096. unit += gi->nr_units;
  2097. }
  2098. BUG_ON(unit != nr_units);
  2099. return ai;
  2100. }
  2101. #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */
  2102. #if defined(BUILD_EMBED_FIRST_CHUNK)
  2103. /**
  2104. * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
  2105. * @reserved_size: the size of reserved percpu area in bytes
  2106. * @dyn_size: minimum free size for dynamic allocation in bytes
  2107. * @atom_size: allocation atom size
  2108. * @cpu_distance_fn: callback to determine distance between cpus, optional
  2109. * @alloc_fn: function to allocate percpu page
  2110. * @free_fn: function to free percpu page
  2111. *
  2112. * This is a helper to ease setting up embedded first percpu chunk and
  2113. * can be called where pcpu_setup_first_chunk() is expected.
  2114. *
  2115. * If this function is used to setup the first chunk, it is allocated
  2116. * by calling @alloc_fn and used as-is without being mapped into
  2117. * vmalloc area. Allocations are always whole multiples of @atom_size
  2118. * aligned to @atom_size.
  2119. *
  2120. * This enables the first chunk to piggy back on the linear physical
  2121. * mapping which often uses larger page size. Please note that this
  2122. * can result in very sparse cpu->unit mapping on NUMA machines thus
  2123. * requiring large vmalloc address space. Don't use this allocator if
  2124. * vmalloc space is not orders of magnitude larger than distances
  2125. * between node memory addresses (ie. 32bit NUMA machines).
  2126. *
  2127. * @dyn_size specifies the minimum dynamic area size.
  2128. *
  2129. * If the needed size is smaller than the minimum or specified unit
  2130. * size, the leftover is returned using @free_fn.
  2131. *
  2132. * RETURNS:
  2133. * 0 on success, -errno on failure.
  2134. */
  2135. int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  2136. size_t atom_size,
  2137. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  2138. pcpu_fc_alloc_fn_t alloc_fn,
  2139. pcpu_fc_free_fn_t free_fn)
  2140. {
  2141. void *base = (void *)ULONG_MAX;
  2142. void **areas = NULL;
  2143. struct pcpu_alloc_info *ai;
  2144. size_t size_sum, areas_size;
  2145. unsigned long max_distance;
  2146. int group, i, highest_group, rc;
  2147. ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size,
  2148. cpu_distance_fn);
  2149. if (IS_ERR(ai))
  2150. return PTR_ERR(ai);
  2151. size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
  2152. areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *));
  2153. areas = memblock_virt_alloc_nopanic(areas_size, 0);
  2154. if (!areas) {
  2155. rc = -ENOMEM;
  2156. goto out_free;
  2157. }
  2158. /* allocate, copy and determine base address & max_distance */
  2159. highest_group = 0;
  2160. for (group = 0; group < ai->nr_groups; group++) {
  2161. struct pcpu_group_info *gi = &ai->groups[group];
  2162. unsigned int cpu = NR_CPUS;
  2163. void *ptr;
  2164. for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++)
  2165. cpu = gi->cpu_map[i];
  2166. BUG_ON(cpu == NR_CPUS);
  2167. /* allocate space for the whole group */
  2168. ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size);
  2169. if (!ptr) {
  2170. rc = -ENOMEM;
  2171. goto out_free_areas;
  2172. }
  2173. /* kmemleak tracks the percpu allocations separately */
  2174. kmemleak_free(ptr);
  2175. areas[group] = ptr;
  2176. base = min(ptr, base);
  2177. if (ptr > areas[highest_group])
  2178. highest_group = group;
  2179. }
  2180. max_distance = areas[highest_group] - base;
  2181. max_distance += ai->unit_size * ai->groups[highest_group].nr_units;
  2182. /* warn if maximum distance is further than 75% of vmalloc space */
  2183. if (max_distance > VMALLOC_TOTAL * 3 / 4) {
  2184. pr_warn("max_distance=0x%lx too large for vmalloc space 0x%lx\n",
  2185. max_distance, VMALLOC_TOTAL);
  2186. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  2187. /* and fail if we have fallback */
  2188. rc = -EINVAL;
  2189. goto out_free_areas;
  2190. #endif
  2191. }
  2192. /*
  2193. * Copy data and free unused parts. This should happen after all
  2194. * allocations are complete; otherwise, we may end up with
  2195. * overlapping groups.
  2196. */
  2197. for (group = 0; group < ai->nr_groups; group++) {
  2198. struct pcpu_group_info *gi = &ai->groups[group];
  2199. void *ptr = areas[group];
  2200. for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) {
  2201. if (gi->cpu_map[i] == NR_CPUS) {
  2202. /* unused unit, free whole */
  2203. free_fn(ptr, ai->unit_size);
  2204. continue;
  2205. }
  2206. /* copy and return the unused part */
  2207. memcpy(ptr, __per_cpu_load, ai->static_size);
  2208. free_fn(ptr + size_sum, ai->unit_size - size_sum);
  2209. }
  2210. }
  2211. /* base address is now known, determine group base offsets */
  2212. for (group = 0; group < ai->nr_groups; group++) {
  2213. ai->groups[group].base_offset = areas[group] - base;
  2214. }
  2215. pr_info("Embedded %zu pages/cpu s%zu r%zu d%zu u%zu\n",
  2216. PFN_DOWN(size_sum), ai->static_size, ai->reserved_size,
  2217. ai->dyn_size, ai->unit_size);
  2218. rc = pcpu_setup_first_chunk(ai, base);
  2219. goto out_free;
  2220. out_free_areas:
  2221. for (group = 0; group < ai->nr_groups; group++)
  2222. if (areas[group])
  2223. free_fn(areas[group],
  2224. ai->groups[group].nr_units * ai->unit_size);
  2225. out_free:
  2226. pcpu_free_alloc_info(ai);
  2227. if (areas)
  2228. memblock_free_early(__pa(areas), areas_size);
  2229. return rc;
  2230. }
  2231. #endif /* BUILD_EMBED_FIRST_CHUNK */
  2232. #ifdef BUILD_PAGE_FIRST_CHUNK
  2233. /**
  2234. * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages
  2235. * @reserved_size: the size of reserved percpu area in bytes
  2236. * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE
  2237. * @free_fn: function to free percpu page, always called with PAGE_SIZE
  2238. * @populate_pte_fn: function to populate pte
  2239. *
  2240. * This is a helper to ease setting up page-remapped first percpu
  2241. * chunk and can be called where pcpu_setup_first_chunk() is expected.
  2242. *
  2243. * This is the basic allocator. Static percpu area is allocated
  2244. * page-by-page into vmalloc area.
  2245. *
  2246. * RETURNS:
  2247. * 0 on success, -errno on failure.
  2248. */
  2249. int __init pcpu_page_first_chunk(size_t reserved_size,
  2250. pcpu_fc_alloc_fn_t alloc_fn,
  2251. pcpu_fc_free_fn_t free_fn,
  2252. pcpu_fc_populate_pte_fn_t populate_pte_fn)
  2253. {
  2254. static struct vm_struct vm;
  2255. struct pcpu_alloc_info *ai;
  2256. char psize_str[16];
  2257. int unit_pages;
  2258. size_t pages_size;
  2259. struct page **pages;
  2260. int unit, i, j, rc;
  2261. int upa;
  2262. int nr_g0_units;
  2263. snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10);
  2264. ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL);
  2265. if (IS_ERR(ai))
  2266. return PTR_ERR(ai);
  2267. BUG_ON(ai->nr_groups != 1);
  2268. upa = ai->alloc_size/ai->unit_size;
  2269. nr_g0_units = roundup(num_possible_cpus(), upa);
  2270. if (unlikely(WARN_ON(ai->groups[0].nr_units != nr_g0_units))) {
  2271. pcpu_free_alloc_info(ai);
  2272. return -EINVAL;
  2273. }
  2274. unit_pages = ai->unit_size >> PAGE_SHIFT;
  2275. /* unaligned allocations can't be freed, round up to page size */
  2276. pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
  2277. sizeof(pages[0]));
  2278. pages = memblock_virt_alloc(pages_size, 0);
  2279. /* allocate pages */
  2280. j = 0;
  2281. for (unit = 0; unit < num_possible_cpus(); unit++) {
  2282. unsigned int cpu = ai->groups[0].cpu_map[unit];
  2283. for (i = 0; i < unit_pages; i++) {
  2284. void *ptr;
  2285. ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE);
  2286. if (!ptr) {
  2287. pr_warn("failed to allocate %s page for cpu%u\n",
  2288. psize_str, cpu);
  2289. goto enomem;
  2290. }
  2291. /* kmemleak tracks the percpu allocations separately */
  2292. kmemleak_free(ptr);
  2293. pages[j++] = virt_to_page(ptr);
  2294. }
  2295. }
  2296. /* allocate vm area, map the pages and copy static data */
  2297. vm.flags = VM_ALLOC;
  2298. vm.size = num_possible_cpus() * ai->unit_size;
  2299. vm_area_register_early(&vm, PAGE_SIZE);
  2300. for (unit = 0; unit < num_possible_cpus(); unit++) {
  2301. unsigned long unit_addr =
  2302. (unsigned long)vm.addr + unit * ai->unit_size;
  2303. for (i = 0; i < unit_pages; i++)
  2304. populate_pte_fn(unit_addr + (i << PAGE_SHIFT));
  2305. /* pte already populated, the following shouldn't fail */
  2306. rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages],
  2307. unit_pages);
  2308. if (rc < 0)
  2309. panic("failed to map percpu area, err=%d\n", rc);
  2310. /*
  2311. * FIXME: Archs with virtual cache should flush local
  2312. * cache for the linear mapping here - something
  2313. * equivalent to flush_cache_vmap() on the local cpu.
  2314. * flush_cache_vmap() can't be used as most supporting
  2315. * data structures are not set up yet.
  2316. */
  2317. /* copy static data */
  2318. memcpy((void *)unit_addr, __per_cpu_load, ai->static_size);
  2319. }
  2320. /* we're ready, commit */
  2321. pr_info("%d %s pages/cpu s%zu r%zu d%zu\n",
  2322. unit_pages, psize_str, ai->static_size,
  2323. ai->reserved_size, ai->dyn_size);
  2324. rc = pcpu_setup_first_chunk(ai, vm.addr);
  2325. goto out_free_ar;
  2326. enomem:
  2327. while (--j >= 0)
  2328. free_fn(page_address(pages[j]), PAGE_SIZE);
  2329. rc = -ENOMEM;
  2330. out_free_ar:
  2331. memblock_free_early(__pa(pages), pages_size);
  2332. pcpu_free_alloc_info(ai);
  2333. return rc;
  2334. }
  2335. #endif /* BUILD_PAGE_FIRST_CHUNK */
  2336. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  2337. /*
  2338. * Generic SMP percpu area setup.
  2339. *
  2340. * The embedding helper is used because its behavior closely resembles
  2341. * the original non-dynamic generic percpu area setup. This is
  2342. * important because many archs have addressing restrictions and might
  2343. * fail if the percpu area is located far away from the previous
  2344. * location. As an added bonus, in non-NUMA cases, embedding is
  2345. * generally a good idea TLB-wise because percpu area can piggy back
  2346. * on the physical linear memory mapping which uses large page
  2347. * mappings on applicable archs.
  2348. */
  2349. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  2350. EXPORT_SYMBOL(__per_cpu_offset);
  2351. static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size,
  2352. size_t align)
  2353. {
  2354. return memblock_virt_alloc_from_nopanic(
  2355. size, align, __pa(MAX_DMA_ADDRESS));
  2356. }
  2357. static void __init pcpu_dfl_fc_free(void *ptr, size_t size)
  2358. {
  2359. memblock_free_early(__pa(ptr), size);
  2360. }
  2361. void __init setup_per_cpu_areas(void)
  2362. {
  2363. unsigned long delta;
  2364. unsigned int cpu;
  2365. int rc;
  2366. /*
  2367. * Always reserve area for module percpu variables. That's
  2368. * what the legacy allocator did.
  2369. */
  2370. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  2371. PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL,
  2372. pcpu_dfl_fc_alloc, pcpu_dfl_fc_free);
  2373. if (rc < 0)
  2374. panic("Failed to initialize percpu areas.");
  2375. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  2376. for_each_possible_cpu(cpu)
  2377. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  2378. }
  2379. #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
  2380. #else /* CONFIG_SMP */
  2381. /*
  2382. * UP percpu area setup.
  2383. *
  2384. * UP always uses km-based percpu allocator with identity mapping.
  2385. * Static percpu variables are indistinguishable from the usual static
  2386. * variables and don't require any special preparation.
  2387. */
  2388. void __init setup_per_cpu_areas(void)
  2389. {
  2390. const size_t unit_size =
  2391. roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE,
  2392. PERCPU_DYNAMIC_RESERVE));
  2393. struct pcpu_alloc_info *ai;
  2394. void *fc;
  2395. ai = pcpu_alloc_alloc_info(1, 1);
  2396. fc = memblock_virt_alloc_from_nopanic(unit_size,
  2397. PAGE_SIZE,
  2398. __pa(MAX_DMA_ADDRESS));
  2399. if (!ai || !fc)
  2400. panic("Failed to allocate memory for percpu areas.");
  2401. /* kmemleak tracks the percpu allocations separately */
  2402. kmemleak_free(fc);
  2403. ai->dyn_size = unit_size;
  2404. ai->unit_size = unit_size;
  2405. ai->atom_size = unit_size;
  2406. ai->alloc_size = unit_size;
  2407. ai->groups[0].nr_units = 1;
  2408. ai->groups[0].cpu_map[0] = 0;
  2409. if (pcpu_setup_first_chunk(ai, fc) < 0)
  2410. panic("Failed to initialize percpu areas.");
  2411. pcpu_free_alloc_info(ai);
  2412. }
  2413. #endif /* CONFIG_SMP */
  2414. /*
  2415. * pcpu_nr_pages - calculate total number of populated backing pages
  2416. *
  2417. * This reflects the number of pages populated to back chunks. Metadata is
  2418. * excluded in the number exposed in meminfo as the number of backing pages
  2419. * scales with the number of cpus and can quickly outweigh the memory used for
  2420. * metadata. It also keeps this calculation nice and simple.
  2421. *
  2422. * RETURNS:
  2423. * Total number of populated backing pages in use by the allocator.
  2424. */
  2425. unsigned long pcpu_nr_pages(void)
  2426. {
  2427. return pcpu_nr_populated * pcpu_nr_units;
  2428. }
  2429. /*
  2430. * Percpu allocator is initialized early during boot when neither slab or
  2431. * workqueue is available. Plug async management until everything is up
  2432. * and running.
  2433. */
  2434. static int __init percpu_enable_async(void)
  2435. {
  2436. pcpu_async_enabled = true;
  2437. return 0;
  2438. }
  2439. subsys_initcall(percpu_enable_async);