percpu.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  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/memblock.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 = start + alloc_bits + PCPU_BITMAP_BLOCK_BITS;
  884. bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end, start,
  885. alloc_bits, align_mask);
  886. if (bit_off >= end)
  887. return -1;
  888. /* update alloc map */
  889. bitmap_set(chunk->alloc_map, bit_off, alloc_bits);
  890. /* update boundary map */
  891. set_bit(bit_off, chunk->bound_map);
  892. bitmap_clear(chunk->bound_map, bit_off + 1, alloc_bits - 1);
  893. set_bit(bit_off + alloc_bits, chunk->bound_map);
  894. chunk->free_bytes -= alloc_bits * PCPU_MIN_ALLOC_SIZE;
  895. /* update first free bit */
  896. if (bit_off == chunk->first_bit)
  897. chunk->first_bit = find_next_zero_bit(
  898. chunk->alloc_map,
  899. pcpu_chunk_map_bits(chunk),
  900. bit_off + alloc_bits);
  901. pcpu_block_update_hint_alloc(chunk, bit_off, alloc_bits);
  902. pcpu_chunk_relocate(chunk, oslot);
  903. return bit_off * PCPU_MIN_ALLOC_SIZE;
  904. }
  905. /**
  906. * pcpu_free_area - frees the corresponding offset
  907. * @chunk: chunk of interest
  908. * @off: addr offset into chunk
  909. *
  910. * This function determines the size of an allocation to free using
  911. * the boundary bitmap and clears the allocation map.
  912. */
  913. static void pcpu_free_area(struct pcpu_chunk *chunk, int off)
  914. {
  915. int bit_off, bits, end, oslot;
  916. lockdep_assert_held(&pcpu_lock);
  917. pcpu_stats_area_dealloc(chunk);
  918. oslot = pcpu_chunk_slot(chunk);
  919. bit_off = off / PCPU_MIN_ALLOC_SIZE;
  920. /* find end index */
  921. end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk),
  922. bit_off + 1);
  923. bits = end - bit_off;
  924. bitmap_clear(chunk->alloc_map, bit_off, bits);
  925. /* update metadata */
  926. chunk->free_bytes += bits * PCPU_MIN_ALLOC_SIZE;
  927. /* update first free bit */
  928. chunk->first_bit = min(chunk->first_bit, bit_off);
  929. pcpu_block_update_hint_free(chunk, bit_off, bits);
  930. pcpu_chunk_relocate(chunk, oslot);
  931. }
  932. static void pcpu_init_md_blocks(struct pcpu_chunk *chunk)
  933. {
  934. struct pcpu_block_md *md_block;
  935. for (md_block = chunk->md_blocks;
  936. md_block != chunk->md_blocks + pcpu_chunk_nr_blocks(chunk);
  937. md_block++) {
  938. md_block->contig_hint = PCPU_BITMAP_BLOCK_BITS;
  939. md_block->left_free = PCPU_BITMAP_BLOCK_BITS;
  940. md_block->right_free = PCPU_BITMAP_BLOCK_BITS;
  941. }
  942. }
  943. /**
  944. * pcpu_alloc_first_chunk - creates chunks that serve the first chunk
  945. * @tmp_addr: the start of the region served
  946. * @map_size: size of the region served
  947. *
  948. * This is responsible for creating the chunks that serve the first chunk. The
  949. * base_addr is page aligned down of @tmp_addr while the region end is page
  950. * aligned up. Offsets are kept track of to determine the region served. All
  951. * this is done to appease the bitmap allocator in avoiding partial blocks.
  952. *
  953. * RETURNS:
  954. * Chunk serving the region at @tmp_addr of @map_size.
  955. */
  956. static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
  957. int map_size)
  958. {
  959. struct pcpu_chunk *chunk;
  960. unsigned long aligned_addr, lcm_align;
  961. int start_offset, offset_bits, region_size, region_bits;
  962. /* region calculations */
  963. aligned_addr = tmp_addr & PAGE_MASK;
  964. start_offset = tmp_addr - aligned_addr;
  965. /*
  966. * Align the end of the region with the LCM of PAGE_SIZE and
  967. * PCPU_BITMAP_BLOCK_SIZE. One of these constants is a multiple of
  968. * the other.
  969. */
  970. lcm_align = lcm(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE);
  971. region_size = ALIGN(start_offset + map_size, lcm_align);
  972. /* allocate chunk */
  973. chunk = memblock_alloc(sizeof(struct pcpu_chunk) +
  974. BITS_TO_LONGS(region_size >> PAGE_SHIFT),
  975. SMP_CACHE_BYTES);
  976. INIT_LIST_HEAD(&chunk->list);
  977. chunk->base_addr = (void *)aligned_addr;
  978. chunk->start_offset = start_offset;
  979. chunk->end_offset = region_size - chunk->start_offset - map_size;
  980. chunk->nr_pages = region_size >> PAGE_SHIFT;
  981. region_bits = pcpu_chunk_map_bits(chunk);
  982. chunk->alloc_map = memblock_alloc(BITS_TO_LONGS(region_bits) * sizeof(chunk->alloc_map[0]),
  983. SMP_CACHE_BYTES);
  984. chunk->bound_map = memblock_alloc(BITS_TO_LONGS(region_bits + 1) * sizeof(chunk->bound_map[0]),
  985. SMP_CACHE_BYTES);
  986. chunk->md_blocks = memblock_alloc(pcpu_chunk_nr_blocks(chunk) * sizeof(chunk->md_blocks[0]),
  987. SMP_CACHE_BYTES);
  988. pcpu_init_md_blocks(chunk);
  989. /* manage populated page bitmap */
  990. chunk->immutable = true;
  991. bitmap_fill(chunk->populated, chunk->nr_pages);
  992. chunk->nr_populated = chunk->nr_pages;
  993. chunk->nr_empty_pop_pages =
  994. pcpu_cnt_pop_pages(chunk, start_offset / PCPU_MIN_ALLOC_SIZE,
  995. map_size / PCPU_MIN_ALLOC_SIZE);
  996. chunk->contig_bits = map_size / PCPU_MIN_ALLOC_SIZE;
  997. chunk->free_bytes = map_size;
  998. if (chunk->start_offset) {
  999. /* hide the beginning of the bitmap */
  1000. offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
  1001. bitmap_set(chunk->alloc_map, 0, offset_bits);
  1002. set_bit(0, chunk->bound_map);
  1003. set_bit(offset_bits, chunk->bound_map);
  1004. chunk->first_bit = offset_bits;
  1005. pcpu_block_update_hint_alloc(chunk, 0, offset_bits);
  1006. }
  1007. if (chunk->end_offset) {
  1008. /* hide the end of the bitmap */
  1009. offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE;
  1010. bitmap_set(chunk->alloc_map,
  1011. pcpu_chunk_map_bits(chunk) - offset_bits,
  1012. offset_bits);
  1013. set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE,
  1014. chunk->bound_map);
  1015. set_bit(region_bits, chunk->bound_map);
  1016. pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk)
  1017. - offset_bits, offset_bits);
  1018. }
  1019. return chunk;
  1020. }
  1021. static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp)
  1022. {
  1023. struct pcpu_chunk *chunk;
  1024. int region_bits;
  1025. chunk = pcpu_mem_zalloc(pcpu_chunk_struct_size, gfp);
  1026. if (!chunk)
  1027. return NULL;
  1028. INIT_LIST_HEAD(&chunk->list);
  1029. chunk->nr_pages = pcpu_unit_pages;
  1030. region_bits = pcpu_chunk_map_bits(chunk);
  1031. chunk->alloc_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits) *
  1032. sizeof(chunk->alloc_map[0]), gfp);
  1033. if (!chunk->alloc_map)
  1034. goto alloc_map_fail;
  1035. chunk->bound_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits + 1) *
  1036. sizeof(chunk->bound_map[0]), gfp);
  1037. if (!chunk->bound_map)
  1038. goto bound_map_fail;
  1039. chunk->md_blocks = pcpu_mem_zalloc(pcpu_chunk_nr_blocks(chunk) *
  1040. sizeof(chunk->md_blocks[0]), gfp);
  1041. if (!chunk->md_blocks)
  1042. goto md_blocks_fail;
  1043. pcpu_init_md_blocks(chunk);
  1044. /* init metadata */
  1045. chunk->contig_bits = region_bits;
  1046. chunk->free_bytes = chunk->nr_pages * PAGE_SIZE;
  1047. return chunk;
  1048. md_blocks_fail:
  1049. pcpu_mem_free(chunk->bound_map);
  1050. bound_map_fail:
  1051. pcpu_mem_free(chunk->alloc_map);
  1052. alloc_map_fail:
  1053. pcpu_mem_free(chunk);
  1054. return NULL;
  1055. }
  1056. static void pcpu_free_chunk(struct pcpu_chunk *chunk)
  1057. {
  1058. if (!chunk)
  1059. return;
  1060. pcpu_mem_free(chunk->md_blocks);
  1061. pcpu_mem_free(chunk->bound_map);
  1062. pcpu_mem_free(chunk->alloc_map);
  1063. pcpu_mem_free(chunk);
  1064. }
  1065. /**
  1066. * pcpu_chunk_populated - post-population bookkeeping
  1067. * @chunk: pcpu_chunk which got populated
  1068. * @page_start: the start page
  1069. * @page_end: the end page
  1070. * @for_alloc: if this is to populate for allocation
  1071. *
  1072. * Pages in [@page_start,@page_end) have been populated to @chunk. Update
  1073. * the bookkeeping information accordingly. Must be called after each
  1074. * successful population.
  1075. *
  1076. * If this is @for_alloc, do not increment pcpu_nr_empty_pop_pages because it
  1077. * is to serve an allocation in that area.
  1078. */
  1079. static void pcpu_chunk_populated(struct pcpu_chunk *chunk, int page_start,
  1080. int page_end, bool for_alloc)
  1081. {
  1082. int nr = page_end - page_start;
  1083. lockdep_assert_held(&pcpu_lock);
  1084. bitmap_set(chunk->populated, page_start, nr);
  1085. chunk->nr_populated += nr;
  1086. pcpu_nr_populated += nr;
  1087. if (!for_alloc) {
  1088. chunk->nr_empty_pop_pages += nr;
  1089. pcpu_nr_empty_pop_pages += nr;
  1090. }
  1091. }
  1092. /**
  1093. * pcpu_chunk_depopulated - post-depopulation bookkeeping
  1094. * @chunk: pcpu_chunk which got depopulated
  1095. * @page_start: the start page
  1096. * @page_end: the end page
  1097. *
  1098. * Pages in [@page_start,@page_end) have been depopulated from @chunk.
  1099. * Update the bookkeeping information accordingly. Must be called after
  1100. * each successful depopulation.
  1101. */
  1102. static void pcpu_chunk_depopulated(struct pcpu_chunk *chunk,
  1103. int page_start, int page_end)
  1104. {
  1105. int nr = page_end - page_start;
  1106. lockdep_assert_held(&pcpu_lock);
  1107. bitmap_clear(chunk->populated, page_start, nr);
  1108. chunk->nr_populated -= nr;
  1109. chunk->nr_empty_pop_pages -= nr;
  1110. pcpu_nr_empty_pop_pages -= nr;
  1111. pcpu_nr_populated -= nr;
  1112. }
  1113. /*
  1114. * Chunk management implementation.
  1115. *
  1116. * To allow different implementations, chunk alloc/free and
  1117. * [de]population are implemented in a separate file which is pulled
  1118. * into this file and compiled together. The following functions
  1119. * should be implemented.
  1120. *
  1121. * pcpu_populate_chunk - populate the specified range of a chunk
  1122. * pcpu_depopulate_chunk - depopulate the specified range of a chunk
  1123. * pcpu_create_chunk - create a new chunk
  1124. * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop
  1125. * pcpu_addr_to_page - translate address to physical address
  1126. * pcpu_verify_alloc_info - check alloc_info is acceptable during init
  1127. */
  1128. static int pcpu_populate_chunk(struct pcpu_chunk *chunk,
  1129. int page_start, int page_end, gfp_t gfp);
  1130. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,
  1131. int page_start, int page_end);
  1132. static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp);
  1133. static void pcpu_destroy_chunk(struct pcpu_chunk *chunk);
  1134. static struct page *pcpu_addr_to_page(void *addr);
  1135. static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai);
  1136. #ifdef CONFIG_NEED_PER_CPU_KM
  1137. #include "percpu-km.c"
  1138. #else
  1139. #include "percpu-vm.c"
  1140. #endif
  1141. /**
  1142. * pcpu_chunk_addr_search - determine chunk containing specified address
  1143. * @addr: address for which the chunk needs to be determined.
  1144. *
  1145. * This is an internal function that handles all but static allocations.
  1146. * Static percpu address values should never be passed into the allocator.
  1147. *
  1148. * RETURNS:
  1149. * The address of the found chunk.
  1150. */
  1151. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  1152. {
  1153. /* is it in the dynamic region (first chunk)? */
  1154. if (pcpu_addr_in_chunk(pcpu_first_chunk, addr))
  1155. return pcpu_first_chunk;
  1156. /* is it in the reserved region? */
  1157. if (pcpu_addr_in_chunk(pcpu_reserved_chunk, addr))
  1158. return pcpu_reserved_chunk;
  1159. /*
  1160. * The address is relative to unit0 which might be unused and
  1161. * thus unmapped. Offset the address to the unit space of the
  1162. * current processor before looking it up in the vmalloc
  1163. * space. Note that any possible cpu id can be used here, so
  1164. * there's no need to worry about preemption or cpu hotplug.
  1165. */
  1166. addr += pcpu_unit_offsets[raw_smp_processor_id()];
  1167. return pcpu_get_page_chunk(pcpu_addr_to_page(addr));
  1168. }
  1169. /**
  1170. * pcpu_alloc - the percpu allocator
  1171. * @size: size of area to allocate in bytes
  1172. * @align: alignment of area (max PAGE_SIZE)
  1173. * @reserved: allocate from the reserved chunk if available
  1174. * @gfp: allocation flags
  1175. *
  1176. * Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't
  1177. * contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN
  1178. * then no warning will be triggered on invalid or failed allocation
  1179. * requests.
  1180. *
  1181. * RETURNS:
  1182. * Percpu pointer to the allocated area on success, NULL on failure.
  1183. */
  1184. static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
  1185. gfp_t gfp)
  1186. {
  1187. /* whitelisted flags that can be passed to the backing allocators */
  1188. gfp_t pcpu_gfp = gfp & (GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
  1189. bool is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL;
  1190. bool do_warn = !(gfp & __GFP_NOWARN);
  1191. static int warn_limit = 10;
  1192. struct pcpu_chunk *chunk;
  1193. const char *err;
  1194. int slot, off, cpu, ret;
  1195. unsigned long flags;
  1196. void __percpu *ptr;
  1197. size_t bits, bit_align;
  1198. /*
  1199. * There is now a minimum allocation size of PCPU_MIN_ALLOC_SIZE,
  1200. * therefore alignment must be a minimum of that many bytes.
  1201. * An allocation may have internal fragmentation from rounding up
  1202. * of up to PCPU_MIN_ALLOC_SIZE - 1 bytes.
  1203. */
  1204. if (unlikely(align < PCPU_MIN_ALLOC_SIZE))
  1205. align = PCPU_MIN_ALLOC_SIZE;
  1206. size = ALIGN(size, PCPU_MIN_ALLOC_SIZE);
  1207. bits = size >> PCPU_MIN_ALLOC_SHIFT;
  1208. bit_align = align >> PCPU_MIN_ALLOC_SHIFT;
  1209. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE ||
  1210. !is_power_of_2(align))) {
  1211. WARN(do_warn, "illegal size (%zu) or align (%zu) for percpu allocation\n",
  1212. size, align);
  1213. return NULL;
  1214. }
  1215. if (!is_atomic) {
  1216. /*
  1217. * pcpu_balance_workfn() allocates memory under this mutex,
  1218. * and it may wait for memory reclaim. Allow current task
  1219. * to become OOM victim, in case of memory pressure.
  1220. */
  1221. if (gfp & __GFP_NOFAIL)
  1222. mutex_lock(&pcpu_alloc_mutex);
  1223. else if (mutex_lock_killable(&pcpu_alloc_mutex))
  1224. return NULL;
  1225. }
  1226. spin_lock_irqsave(&pcpu_lock, flags);
  1227. /* serve reserved allocations from the reserved chunk if available */
  1228. if (reserved && pcpu_reserved_chunk) {
  1229. chunk = pcpu_reserved_chunk;
  1230. off = pcpu_find_block_fit(chunk, bits, bit_align, is_atomic);
  1231. if (off < 0) {
  1232. err = "alloc from reserved chunk failed";
  1233. goto fail_unlock;
  1234. }
  1235. off = pcpu_alloc_area(chunk, bits, bit_align, off);
  1236. if (off >= 0)
  1237. goto area_found;
  1238. err = "alloc from reserved chunk failed";
  1239. goto fail_unlock;
  1240. }
  1241. restart:
  1242. /* search through normal chunks */
  1243. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  1244. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  1245. off = pcpu_find_block_fit(chunk, bits, bit_align,
  1246. is_atomic);
  1247. if (off < 0)
  1248. continue;
  1249. off = pcpu_alloc_area(chunk, bits, bit_align, off);
  1250. if (off >= 0)
  1251. goto area_found;
  1252. }
  1253. }
  1254. spin_unlock_irqrestore(&pcpu_lock, flags);
  1255. /*
  1256. * No space left. Create a new chunk. We don't want multiple
  1257. * tasks to create chunks simultaneously. Serialize and create iff
  1258. * there's still no empty chunk after grabbing the mutex.
  1259. */
  1260. if (is_atomic) {
  1261. err = "atomic alloc failed, no space left";
  1262. goto fail;
  1263. }
  1264. if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) {
  1265. chunk = pcpu_create_chunk(pcpu_gfp);
  1266. if (!chunk) {
  1267. err = "failed to allocate new chunk";
  1268. goto fail;
  1269. }
  1270. spin_lock_irqsave(&pcpu_lock, flags);
  1271. pcpu_chunk_relocate(chunk, -1);
  1272. } else {
  1273. spin_lock_irqsave(&pcpu_lock, flags);
  1274. }
  1275. goto restart;
  1276. area_found:
  1277. pcpu_stats_area_alloc(chunk, size);
  1278. spin_unlock_irqrestore(&pcpu_lock, flags);
  1279. /* populate if not all pages are already there */
  1280. if (!is_atomic) {
  1281. int page_start, page_end, rs, re;
  1282. page_start = PFN_DOWN(off);
  1283. page_end = PFN_UP(off + size);
  1284. pcpu_for_each_unpop_region(chunk->populated, rs, re,
  1285. page_start, page_end) {
  1286. WARN_ON(chunk->immutable);
  1287. ret = pcpu_populate_chunk(chunk, rs, re, pcpu_gfp);
  1288. spin_lock_irqsave(&pcpu_lock, flags);
  1289. if (ret) {
  1290. pcpu_free_area(chunk, off);
  1291. err = "failed to populate";
  1292. goto fail_unlock;
  1293. }
  1294. pcpu_chunk_populated(chunk, rs, re, true);
  1295. spin_unlock_irqrestore(&pcpu_lock, flags);
  1296. }
  1297. mutex_unlock(&pcpu_alloc_mutex);
  1298. }
  1299. if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_LOW)
  1300. pcpu_schedule_balance_work();
  1301. /* clear the areas and return address relative to base address */
  1302. for_each_possible_cpu(cpu)
  1303. memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
  1304. ptr = __addr_to_pcpu_ptr(chunk->base_addr + off);
  1305. kmemleak_alloc_percpu(ptr, size, gfp);
  1306. trace_percpu_alloc_percpu(reserved, is_atomic, size, align,
  1307. chunk->base_addr, off, ptr);
  1308. return ptr;
  1309. fail_unlock:
  1310. spin_unlock_irqrestore(&pcpu_lock, flags);
  1311. fail:
  1312. trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align);
  1313. if (!is_atomic && do_warn && warn_limit) {
  1314. pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
  1315. size, align, is_atomic, err);
  1316. dump_stack();
  1317. if (!--warn_limit)
  1318. pr_info("limit reached, disable warning\n");
  1319. }
  1320. if (is_atomic) {
  1321. /* see the flag handling in pcpu_blance_workfn() */
  1322. pcpu_atomic_alloc_failed = true;
  1323. pcpu_schedule_balance_work();
  1324. } else {
  1325. mutex_unlock(&pcpu_alloc_mutex);
  1326. }
  1327. return NULL;
  1328. }
  1329. /**
  1330. * __alloc_percpu_gfp - allocate dynamic percpu area
  1331. * @size: size of area to allocate in bytes
  1332. * @align: alignment of area (max PAGE_SIZE)
  1333. * @gfp: allocation flags
  1334. *
  1335. * Allocate zero-filled percpu area of @size bytes aligned at @align. If
  1336. * @gfp doesn't contain %GFP_KERNEL, the allocation doesn't block and can
  1337. * be called from any context but is a lot more likely to fail. If @gfp
  1338. * has __GFP_NOWARN then no warning will be triggered on invalid or failed
  1339. * allocation requests.
  1340. *
  1341. * RETURNS:
  1342. * Percpu pointer to the allocated area on success, NULL on failure.
  1343. */
  1344. void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp)
  1345. {
  1346. return pcpu_alloc(size, align, false, gfp);
  1347. }
  1348. EXPORT_SYMBOL_GPL(__alloc_percpu_gfp);
  1349. /**
  1350. * __alloc_percpu - allocate dynamic percpu area
  1351. * @size: size of area to allocate in bytes
  1352. * @align: alignment of area (max PAGE_SIZE)
  1353. *
  1354. * Equivalent to __alloc_percpu_gfp(size, align, %GFP_KERNEL).
  1355. */
  1356. void __percpu *__alloc_percpu(size_t size, size_t align)
  1357. {
  1358. return pcpu_alloc(size, align, false, GFP_KERNEL);
  1359. }
  1360. EXPORT_SYMBOL_GPL(__alloc_percpu);
  1361. /**
  1362. * __alloc_reserved_percpu - allocate reserved percpu area
  1363. * @size: size of area to allocate in bytes
  1364. * @align: alignment of area (max PAGE_SIZE)
  1365. *
  1366. * Allocate zero-filled percpu area of @size bytes aligned at @align
  1367. * from reserved percpu area if arch has set it up; otherwise,
  1368. * allocation is served from the same dynamic area. Might sleep.
  1369. * Might trigger writeouts.
  1370. *
  1371. * CONTEXT:
  1372. * Does GFP_KERNEL allocation.
  1373. *
  1374. * RETURNS:
  1375. * Percpu pointer to the allocated area on success, NULL on failure.
  1376. */
  1377. void __percpu *__alloc_reserved_percpu(size_t size, size_t align)
  1378. {
  1379. return pcpu_alloc(size, align, true, GFP_KERNEL);
  1380. }
  1381. /**
  1382. * pcpu_balance_workfn - manage the amount of free chunks and populated pages
  1383. * @work: unused
  1384. *
  1385. * Reclaim all fully free chunks except for the first one. This is also
  1386. * responsible for maintaining the pool of empty populated pages. However,
  1387. * it is possible that this is called when physical memory is scarce causing
  1388. * OOM killer to be triggered. We should avoid doing so until an actual
  1389. * allocation causes the failure as it is possible that requests can be
  1390. * serviced from already backed regions.
  1391. */
  1392. static void pcpu_balance_workfn(struct work_struct *work)
  1393. {
  1394. /* gfp flags passed to underlying allocators */
  1395. const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
  1396. LIST_HEAD(to_free);
  1397. struct list_head *free_head = &pcpu_slot[pcpu_nr_slots - 1];
  1398. struct pcpu_chunk *chunk, *next;
  1399. int slot, nr_to_pop, ret;
  1400. /*
  1401. * There's no reason to keep around multiple unused chunks and VM
  1402. * areas can be scarce. Destroy all free chunks except for one.
  1403. */
  1404. mutex_lock(&pcpu_alloc_mutex);
  1405. spin_lock_irq(&pcpu_lock);
  1406. list_for_each_entry_safe(chunk, next, free_head, list) {
  1407. WARN_ON(chunk->immutable);
  1408. /* spare the first one */
  1409. if (chunk == list_first_entry(free_head, struct pcpu_chunk, list))
  1410. continue;
  1411. list_move(&chunk->list, &to_free);
  1412. }
  1413. spin_unlock_irq(&pcpu_lock);
  1414. list_for_each_entry_safe(chunk, next, &to_free, list) {
  1415. int rs, re;
  1416. pcpu_for_each_pop_region(chunk->populated, rs, re, 0,
  1417. chunk->nr_pages) {
  1418. pcpu_depopulate_chunk(chunk, rs, re);
  1419. spin_lock_irq(&pcpu_lock);
  1420. pcpu_chunk_depopulated(chunk, rs, re);
  1421. spin_unlock_irq(&pcpu_lock);
  1422. }
  1423. pcpu_destroy_chunk(chunk);
  1424. cond_resched();
  1425. }
  1426. /*
  1427. * Ensure there are certain number of free populated pages for
  1428. * atomic allocs. Fill up from the most packed so that atomic
  1429. * allocs don't increase fragmentation. If atomic allocation
  1430. * failed previously, always populate the maximum amount. This
  1431. * should prevent atomic allocs larger than PAGE_SIZE from keeping
  1432. * failing indefinitely; however, large atomic allocs are not
  1433. * something we support properly and can be highly unreliable and
  1434. * inefficient.
  1435. */
  1436. retry_pop:
  1437. if (pcpu_atomic_alloc_failed) {
  1438. nr_to_pop = PCPU_EMPTY_POP_PAGES_HIGH;
  1439. /* best effort anyway, don't worry about synchronization */
  1440. pcpu_atomic_alloc_failed = false;
  1441. } else {
  1442. nr_to_pop = clamp(PCPU_EMPTY_POP_PAGES_HIGH -
  1443. pcpu_nr_empty_pop_pages,
  1444. 0, PCPU_EMPTY_POP_PAGES_HIGH);
  1445. }
  1446. for (slot = pcpu_size_to_slot(PAGE_SIZE); slot < pcpu_nr_slots; slot++) {
  1447. int nr_unpop = 0, rs, re;
  1448. if (!nr_to_pop)
  1449. break;
  1450. spin_lock_irq(&pcpu_lock);
  1451. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  1452. nr_unpop = chunk->nr_pages - chunk->nr_populated;
  1453. if (nr_unpop)
  1454. break;
  1455. }
  1456. spin_unlock_irq(&pcpu_lock);
  1457. if (!nr_unpop)
  1458. continue;
  1459. /* @chunk can't go away while pcpu_alloc_mutex is held */
  1460. pcpu_for_each_unpop_region(chunk->populated, rs, re, 0,
  1461. chunk->nr_pages) {
  1462. int nr = min(re - rs, nr_to_pop);
  1463. ret = pcpu_populate_chunk(chunk, rs, rs + nr, gfp);
  1464. if (!ret) {
  1465. nr_to_pop -= nr;
  1466. spin_lock_irq(&pcpu_lock);
  1467. pcpu_chunk_populated(chunk, rs, rs + nr, false);
  1468. spin_unlock_irq(&pcpu_lock);
  1469. } else {
  1470. nr_to_pop = 0;
  1471. }
  1472. if (!nr_to_pop)
  1473. break;
  1474. }
  1475. }
  1476. if (nr_to_pop) {
  1477. /* ran out of chunks to populate, create a new one and retry */
  1478. chunk = pcpu_create_chunk(gfp);
  1479. if (chunk) {
  1480. spin_lock_irq(&pcpu_lock);
  1481. pcpu_chunk_relocate(chunk, -1);
  1482. spin_unlock_irq(&pcpu_lock);
  1483. goto retry_pop;
  1484. }
  1485. }
  1486. mutex_unlock(&pcpu_alloc_mutex);
  1487. }
  1488. /**
  1489. * free_percpu - free percpu area
  1490. * @ptr: pointer to area to free
  1491. *
  1492. * Free percpu area @ptr.
  1493. *
  1494. * CONTEXT:
  1495. * Can be called from atomic context.
  1496. */
  1497. void free_percpu(void __percpu *ptr)
  1498. {
  1499. void *addr;
  1500. struct pcpu_chunk *chunk;
  1501. unsigned long flags;
  1502. int off;
  1503. if (!ptr)
  1504. return;
  1505. kmemleak_free_percpu(ptr);
  1506. addr = __pcpu_ptr_to_addr(ptr);
  1507. spin_lock_irqsave(&pcpu_lock, flags);
  1508. chunk = pcpu_chunk_addr_search(addr);
  1509. off = addr - chunk->base_addr;
  1510. pcpu_free_area(chunk, off);
  1511. /* if there are more than one fully free chunks, wake up grim reaper */
  1512. if (chunk->free_bytes == pcpu_unit_size) {
  1513. struct pcpu_chunk *pos;
  1514. list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
  1515. if (pos != chunk) {
  1516. pcpu_schedule_balance_work();
  1517. break;
  1518. }
  1519. }
  1520. trace_percpu_free_percpu(chunk->base_addr, off, ptr);
  1521. spin_unlock_irqrestore(&pcpu_lock, flags);
  1522. }
  1523. EXPORT_SYMBOL_GPL(free_percpu);
  1524. bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr)
  1525. {
  1526. #ifdef CONFIG_SMP
  1527. const size_t static_size = __per_cpu_end - __per_cpu_start;
  1528. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  1529. unsigned int cpu;
  1530. for_each_possible_cpu(cpu) {
  1531. void *start = per_cpu_ptr(base, cpu);
  1532. void *va = (void *)addr;
  1533. if (va >= start && va < start + static_size) {
  1534. if (can_addr) {
  1535. *can_addr = (unsigned long) (va - start);
  1536. *can_addr += (unsigned long)
  1537. per_cpu_ptr(base, get_boot_cpu_id());
  1538. }
  1539. return true;
  1540. }
  1541. }
  1542. #endif
  1543. /* on UP, can't distinguish from other static vars, always false */
  1544. return false;
  1545. }
  1546. /**
  1547. * is_kernel_percpu_address - test whether address is from static percpu area
  1548. * @addr: address to test
  1549. *
  1550. * Test whether @addr belongs to in-kernel static percpu area. Module
  1551. * static percpu areas are not considered. For those, use
  1552. * is_module_percpu_address().
  1553. *
  1554. * RETURNS:
  1555. * %true if @addr is from in-kernel static percpu area, %false otherwise.
  1556. */
  1557. bool is_kernel_percpu_address(unsigned long addr)
  1558. {
  1559. return __is_kernel_percpu_address(addr, NULL);
  1560. }
  1561. /**
  1562. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  1563. * @addr: the address to be converted to physical address
  1564. *
  1565. * Given @addr which is dereferenceable address obtained via one of
  1566. * percpu access macros, this function translates it into its physical
  1567. * address. The caller is responsible for ensuring @addr stays valid
  1568. * until this function finishes.
  1569. *
  1570. * percpu allocator has special setup for the first chunk, which currently
  1571. * supports either embedding in linear address space or vmalloc mapping,
  1572. * and, from the second one, the backing allocator (currently either vm or
  1573. * km) provides translation.
  1574. *
  1575. * The addr can be translated simply without checking if it falls into the
  1576. * first chunk. But the current code reflects better how percpu allocator
  1577. * actually works, and the verification can discover both bugs in percpu
  1578. * allocator itself and per_cpu_ptr_to_phys() callers. So we keep current
  1579. * code.
  1580. *
  1581. * RETURNS:
  1582. * The physical address for @addr.
  1583. */
  1584. phys_addr_t per_cpu_ptr_to_phys(void *addr)
  1585. {
  1586. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  1587. bool in_first_chunk = false;
  1588. unsigned long first_low, first_high;
  1589. unsigned int cpu;
  1590. /*
  1591. * The following test on unit_low/high isn't strictly
  1592. * necessary but will speed up lookups of addresses which
  1593. * aren't in the first chunk.
  1594. *
  1595. * The address check is against full chunk sizes. pcpu_base_addr
  1596. * points to the beginning of the first chunk including the
  1597. * static region. Assumes good intent as the first chunk may
  1598. * not be full (ie. < pcpu_unit_pages in size).
  1599. */
  1600. first_low = (unsigned long)pcpu_base_addr +
  1601. pcpu_unit_page_offset(pcpu_low_unit_cpu, 0);
  1602. first_high = (unsigned long)pcpu_base_addr +
  1603. pcpu_unit_page_offset(pcpu_high_unit_cpu, pcpu_unit_pages);
  1604. if ((unsigned long)addr >= first_low &&
  1605. (unsigned long)addr < first_high) {
  1606. for_each_possible_cpu(cpu) {
  1607. void *start = per_cpu_ptr(base, cpu);
  1608. if (addr >= start && addr < start + pcpu_unit_size) {
  1609. in_first_chunk = true;
  1610. break;
  1611. }
  1612. }
  1613. }
  1614. if (in_first_chunk) {
  1615. if (!is_vmalloc_addr(addr))
  1616. return __pa(addr);
  1617. else
  1618. return page_to_phys(vmalloc_to_page(addr)) +
  1619. offset_in_page(addr);
  1620. } else
  1621. return page_to_phys(pcpu_addr_to_page(addr)) +
  1622. offset_in_page(addr);
  1623. }
  1624. /**
  1625. * pcpu_alloc_alloc_info - allocate percpu allocation info
  1626. * @nr_groups: the number of groups
  1627. * @nr_units: the number of units
  1628. *
  1629. * Allocate ai which is large enough for @nr_groups groups containing
  1630. * @nr_units units. The returned ai's groups[0].cpu_map points to the
  1631. * cpu_map array which is long enough for @nr_units and filled with
  1632. * NR_CPUS. It's the caller's responsibility to initialize cpu_map
  1633. * pointer of other groups.
  1634. *
  1635. * RETURNS:
  1636. * Pointer to the allocated pcpu_alloc_info on success, NULL on
  1637. * failure.
  1638. */
  1639. struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  1640. int nr_units)
  1641. {
  1642. struct pcpu_alloc_info *ai;
  1643. size_t base_size, ai_size;
  1644. void *ptr;
  1645. int unit;
  1646. base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]),
  1647. __alignof__(ai->groups[0].cpu_map[0]));
  1648. ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]);
  1649. ptr = memblock_alloc_nopanic(PFN_ALIGN(ai_size), PAGE_SIZE);
  1650. if (!ptr)
  1651. return NULL;
  1652. ai = ptr;
  1653. ptr += base_size;
  1654. ai->groups[0].cpu_map = ptr;
  1655. for (unit = 0; unit < nr_units; unit++)
  1656. ai->groups[0].cpu_map[unit] = NR_CPUS;
  1657. ai->nr_groups = nr_groups;
  1658. ai->__ai_size = PFN_ALIGN(ai_size);
  1659. return ai;
  1660. }
  1661. /**
  1662. * pcpu_free_alloc_info - free percpu allocation info
  1663. * @ai: pcpu_alloc_info to free
  1664. *
  1665. * Free @ai which was allocated by pcpu_alloc_alloc_info().
  1666. */
  1667. void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
  1668. {
  1669. memblock_free_early(__pa(ai), ai->__ai_size);
  1670. }
  1671. /**
  1672. * pcpu_dump_alloc_info - print out information about pcpu_alloc_info
  1673. * @lvl: loglevel
  1674. * @ai: allocation info to dump
  1675. *
  1676. * Print out information about @ai using loglevel @lvl.
  1677. */
  1678. static void pcpu_dump_alloc_info(const char *lvl,
  1679. const struct pcpu_alloc_info *ai)
  1680. {
  1681. int group_width = 1, cpu_width = 1, width;
  1682. char empty_str[] = "--------";
  1683. int alloc = 0, alloc_end = 0;
  1684. int group, v;
  1685. int upa, apl; /* units per alloc, allocs per line */
  1686. v = ai->nr_groups;
  1687. while (v /= 10)
  1688. group_width++;
  1689. v = num_possible_cpus();
  1690. while (v /= 10)
  1691. cpu_width++;
  1692. empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0';
  1693. upa = ai->alloc_size / ai->unit_size;
  1694. width = upa * (cpu_width + 1) + group_width + 3;
  1695. apl = rounddown_pow_of_two(max(60 / width, 1));
  1696. printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu",
  1697. lvl, ai->static_size, ai->reserved_size, ai->dyn_size,
  1698. ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size);
  1699. for (group = 0; group < ai->nr_groups; group++) {
  1700. const struct pcpu_group_info *gi = &ai->groups[group];
  1701. int unit = 0, unit_end = 0;
  1702. BUG_ON(gi->nr_units % upa);
  1703. for (alloc_end += gi->nr_units / upa;
  1704. alloc < alloc_end; alloc++) {
  1705. if (!(alloc % apl)) {
  1706. pr_cont("\n");
  1707. printk("%spcpu-alloc: ", lvl);
  1708. }
  1709. pr_cont("[%0*d] ", group_width, group);
  1710. for (unit_end += upa; unit < unit_end; unit++)
  1711. if (gi->cpu_map[unit] != NR_CPUS)
  1712. pr_cont("%0*d ",
  1713. cpu_width, gi->cpu_map[unit]);
  1714. else
  1715. pr_cont("%s ", empty_str);
  1716. }
  1717. }
  1718. pr_cont("\n");
  1719. }
  1720. /**
  1721. * pcpu_setup_first_chunk - initialize the first percpu chunk
  1722. * @ai: pcpu_alloc_info describing how to percpu area is shaped
  1723. * @base_addr: mapped address
  1724. *
  1725. * Initialize the first percpu chunk which contains the kernel static
  1726. * perpcu area. This function is to be called from arch percpu area
  1727. * setup path.
  1728. *
  1729. * @ai contains all information necessary to initialize the first
  1730. * chunk and prime the dynamic percpu allocator.
  1731. *
  1732. * @ai->static_size is the size of static percpu area.
  1733. *
  1734. * @ai->reserved_size, if non-zero, specifies the amount of bytes to
  1735. * reserve after the static area in the first chunk. This reserves
  1736. * the first chunk such that it's available only through reserved
  1737. * percpu allocation. This is primarily used to serve module percpu
  1738. * static areas on architectures where the addressing model has
  1739. * limited offset range for symbol relocations to guarantee module
  1740. * percpu symbols fall inside the relocatable range.
  1741. *
  1742. * @ai->dyn_size determines the number of bytes available for dynamic
  1743. * allocation in the first chunk. The area between @ai->static_size +
  1744. * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused.
  1745. *
  1746. * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE
  1747. * and equal to or larger than @ai->static_size + @ai->reserved_size +
  1748. * @ai->dyn_size.
  1749. *
  1750. * @ai->atom_size is the allocation atom size and used as alignment
  1751. * for vm areas.
  1752. *
  1753. * @ai->alloc_size is the allocation size and always multiple of
  1754. * @ai->atom_size. This is larger than @ai->atom_size if
  1755. * @ai->unit_size is larger than @ai->atom_size.
  1756. *
  1757. * @ai->nr_groups and @ai->groups describe virtual memory layout of
  1758. * percpu areas. Units which should be colocated are put into the
  1759. * same group. Dynamic VM areas will be allocated according to these
  1760. * groupings. If @ai->nr_groups is zero, a single group containing
  1761. * all units is assumed.
  1762. *
  1763. * The caller should have mapped the first chunk at @base_addr and
  1764. * copied static data to each unit.
  1765. *
  1766. * The first chunk will always contain a static and a dynamic region.
  1767. * However, the static region is not managed by any chunk. If the first
  1768. * chunk also contains a reserved region, it is served by two chunks -
  1769. * one for the reserved region and one for the dynamic region. They
  1770. * share the same vm, but use offset regions in the area allocation map.
  1771. * The chunk serving the dynamic region is circulated in the chunk slots
  1772. * and available for dynamic allocation like any other chunk.
  1773. *
  1774. * RETURNS:
  1775. * 0 on success, -errno on failure.
  1776. */
  1777. int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  1778. void *base_addr)
  1779. {
  1780. size_t size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
  1781. size_t static_size, dyn_size;
  1782. struct pcpu_chunk *chunk;
  1783. unsigned long *group_offsets;
  1784. size_t *group_sizes;
  1785. unsigned long *unit_off;
  1786. unsigned int cpu;
  1787. int *unit_map;
  1788. int group, unit, i;
  1789. int map_size;
  1790. unsigned long tmp_addr;
  1791. #define PCPU_SETUP_BUG_ON(cond) do { \
  1792. if (unlikely(cond)) { \
  1793. pr_emerg("failed to initialize, %s\n", #cond); \
  1794. pr_emerg("cpu_possible_mask=%*pb\n", \
  1795. cpumask_pr_args(cpu_possible_mask)); \
  1796. pcpu_dump_alloc_info(KERN_EMERG, ai); \
  1797. BUG(); \
  1798. } \
  1799. } while (0)
  1800. /* sanity checks */
  1801. PCPU_SETUP_BUG_ON(ai->nr_groups <= 0);
  1802. #ifdef CONFIG_SMP
  1803. PCPU_SETUP_BUG_ON(!ai->static_size);
  1804. PCPU_SETUP_BUG_ON(offset_in_page(__per_cpu_start));
  1805. #endif
  1806. PCPU_SETUP_BUG_ON(!base_addr);
  1807. PCPU_SETUP_BUG_ON(offset_in_page(base_addr));
  1808. PCPU_SETUP_BUG_ON(ai->unit_size < size_sum);
  1809. PCPU_SETUP_BUG_ON(offset_in_page(ai->unit_size));
  1810. PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE);
  1811. PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->unit_size, PCPU_BITMAP_BLOCK_SIZE));
  1812. PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE);
  1813. PCPU_SETUP_BUG_ON(!ai->dyn_size);
  1814. PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->reserved_size, PCPU_MIN_ALLOC_SIZE));
  1815. PCPU_SETUP_BUG_ON(!(IS_ALIGNED(PCPU_BITMAP_BLOCK_SIZE, PAGE_SIZE) ||
  1816. IS_ALIGNED(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE)));
  1817. PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
  1818. /* process group information and build config tables accordingly */
  1819. group_offsets = memblock_alloc(ai->nr_groups * sizeof(group_offsets[0]),
  1820. SMP_CACHE_BYTES);
  1821. group_sizes = memblock_alloc(ai->nr_groups * sizeof(group_sizes[0]),
  1822. SMP_CACHE_BYTES);
  1823. unit_map = memblock_alloc(nr_cpu_ids * sizeof(unit_map[0]),
  1824. SMP_CACHE_BYTES);
  1825. unit_off = memblock_alloc(nr_cpu_ids * sizeof(unit_off[0]),
  1826. SMP_CACHE_BYTES);
  1827. for (cpu = 0; cpu < nr_cpu_ids; cpu++)
  1828. unit_map[cpu] = UINT_MAX;
  1829. pcpu_low_unit_cpu = NR_CPUS;
  1830. pcpu_high_unit_cpu = NR_CPUS;
  1831. for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) {
  1832. const struct pcpu_group_info *gi = &ai->groups[group];
  1833. group_offsets[group] = gi->base_offset;
  1834. group_sizes[group] = gi->nr_units * ai->unit_size;
  1835. for (i = 0; i < gi->nr_units; i++) {
  1836. cpu = gi->cpu_map[i];
  1837. if (cpu == NR_CPUS)
  1838. continue;
  1839. PCPU_SETUP_BUG_ON(cpu >= nr_cpu_ids);
  1840. PCPU_SETUP_BUG_ON(!cpu_possible(cpu));
  1841. PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX);
  1842. unit_map[cpu] = unit + i;
  1843. unit_off[cpu] = gi->base_offset + i * ai->unit_size;
  1844. /* determine low/high unit_cpu */
  1845. if (pcpu_low_unit_cpu == NR_CPUS ||
  1846. unit_off[cpu] < unit_off[pcpu_low_unit_cpu])
  1847. pcpu_low_unit_cpu = cpu;
  1848. if (pcpu_high_unit_cpu == NR_CPUS ||
  1849. unit_off[cpu] > unit_off[pcpu_high_unit_cpu])
  1850. pcpu_high_unit_cpu = cpu;
  1851. }
  1852. }
  1853. pcpu_nr_units = unit;
  1854. for_each_possible_cpu(cpu)
  1855. PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX);
  1856. /* we're done parsing the input, undefine BUG macro and dump config */
  1857. #undef PCPU_SETUP_BUG_ON
  1858. pcpu_dump_alloc_info(KERN_DEBUG, ai);
  1859. pcpu_nr_groups = ai->nr_groups;
  1860. pcpu_group_offsets = group_offsets;
  1861. pcpu_group_sizes = group_sizes;
  1862. pcpu_unit_map = unit_map;
  1863. pcpu_unit_offsets = unit_off;
  1864. /* determine basic parameters */
  1865. pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
  1866. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  1867. pcpu_atom_size = ai->atom_size;
  1868. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
  1869. BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
  1870. pcpu_stats_save_ai(ai);
  1871. /*
  1872. * Allocate chunk slots. The additional last slot is for
  1873. * empty chunks.
  1874. */
  1875. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  1876. pcpu_slot = memblock_alloc(pcpu_nr_slots * sizeof(pcpu_slot[0]),
  1877. SMP_CACHE_BYTES);
  1878. for (i = 0; i < pcpu_nr_slots; i++)
  1879. INIT_LIST_HEAD(&pcpu_slot[i]);
  1880. /*
  1881. * The end of the static region needs to be aligned with the
  1882. * minimum allocation size as this offsets the reserved and
  1883. * dynamic region. The first chunk ends page aligned by
  1884. * expanding the dynamic region, therefore the dynamic region
  1885. * can be shrunk to compensate while still staying above the
  1886. * configured sizes.
  1887. */
  1888. static_size = ALIGN(ai->static_size, PCPU_MIN_ALLOC_SIZE);
  1889. dyn_size = ai->dyn_size - (static_size - ai->static_size);
  1890. /*
  1891. * Initialize first chunk.
  1892. * If the reserved_size is non-zero, this initializes the reserved
  1893. * chunk. If the reserved_size is zero, the reserved chunk is NULL
  1894. * and the dynamic region is initialized here. The first chunk,
  1895. * pcpu_first_chunk, will always point to the chunk that serves
  1896. * the dynamic region.
  1897. */
  1898. tmp_addr = (unsigned long)base_addr + static_size;
  1899. map_size = ai->reserved_size ?: dyn_size;
  1900. chunk = pcpu_alloc_first_chunk(tmp_addr, map_size);
  1901. /* init dynamic chunk if necessary */
  1902. if (ai->reserved_size) {
  1903. pcpu_reserved_chunk = chunk;
  1904. tmp_addr = (unsigned long)base_addr + static_size +
  1905. ai->reserved_size;
  1906. map_size = dyn_size;
  1907. chunk = pcpu_alloc_first_chunk(tmp_addr, map_size);
  1908. }
  1909. /* link the first chunk in */
  1910. pcpu_first_chunk = chunk;
  1911. pcpu_nr_empty_pop_pages = pcpu_first_chunk->nr_empty_pop_pages;
  1912. pcpu_chunk_relocate(pcpu_first_chunk, -1);
  1913. /* include all regions of the first chunk */
  1914. pcpu_nr_populated += PFN_DOWN(size_sum);
  1915. pcpu_stats_chunk_alloc();
  1916. trace_percpu_create_chunk(base_addr);
  1917. /* we're done */
  1918. pcpu_base_addr = base_addr;
  1919. return 0;
  1920. }
  1921. #ifdef CONFIG_SMP
  1922. const char * const pcpu_fc_names[PCPU_FC_NR] __initconst = {
  1923. [PCPU_FC_AUTO] = "auto",
  1924. [PCPU_FC_EMBED] = "embed",
  1925. [PCPU_FC_PAGE] = "page",
  1926. };
  1927. enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO;
  1928. static int __init percpu_alloc_setup(char *str)
  1929. {
  1930. if (!str)
  1931. return -EINVAL;
  1932. if (0)
  1933. /* nada */;
  1934. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  1935. else if (!strcmp(str, "embed"))
  1936. pcpu_chosen_fc = PCPU_FC_EMBED;
  1937. #endif
  1938. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  1939. else if (!strcmp(str, "page"))
  1940. pcpu_chosen_fc = PCPU_FC_PAGE;
  1941. #endif
  1942. else
  1943. pr_warn("unknown allocator %s specified\n", str);
  1944. return 0;
  1945. }
  1946. early_param("percpu_alloc", percpu_alloc_setup);
  1947. /*
  1948. * pcpu_embed_first_chunk() is used by the generic percpu setup.
  1949. * Build it if needed by the arch config or the generic setup is going
  1950. * to be used.
  1951. */
  1952. #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \
  1953. !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  1954. #define BUILD_EMBED_FIRST_CHUNK
  1955. #endif
  1956. /* build pcpu_page_first_chunk() iff needed by the arch config */
  1957. #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK)
  1958. #define BUILD_PAGE_FIRST_CHUNK
  1959. #endif
  1960. /* pcpu_build_alloc_info() is used by both embed and page first chunk */
  1961. #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK)
  1962. /**
  1963. * pcpu_build_alloc_info - build alloc_info considering distances between CPUs
  1964. * @reserved_size: the size of reserved percpu area in bytes
  1965. * @dyn_size: minimum free size for dynamic allocation in bytes
  1966. * @atom_size: allocation atom size
  1967. * @cpu_distance_fn: callback to determine distance between cpus, optional
  1968. *
  1969. * This function determines grouping of units, their mappings to cpus
  1970. * and other parameters considering needed percpu size, allocation
  1971. * atom size and distances between CPUs.
  1972. *
  1973. * Groups are always multiples of atom size and CPUs which are of
  1974. * LOCAL_DISTANCE both ways are grouped together and share space for
  1975. * units in the same group. The returned configuration is guaranteed
  1976. * to have CPUs on different nodes on different groups and >=75% usage
  1977. * of allocated virtual address space.
  1978. *
  1979. * RETURNS:
  1980. * On success, pointer to the new allocation_info is returned. On
  1981. * failure, ERR_PTR value is returned.
  1982. */
  1983. static struct pcpu_alloc_info * __init pcpu_build_alloc_info(
  1984. size_t reserved_size, size_t dyn_size,
  1985. size_t atom_size,
  1986. pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
  1987. {
  1988. static int group_map[NR_CPUS] __initdata;
  1989. static int group_cnt[NR_CPUS] __initdata;
  1990. const size_t static_size = __per_cpu_end - __per_cpu_start;
  1991. int nr_groups = 1, nr_units = 0;
  1992. size_t size_sum, min_unit_size, alloc_size;
  1993. int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */
  1994. int last_allocs, group, unit;
  1995. unsigned int cpu, tcpu;
  1996. struct pcpu_alloc_info *ai;
  1997. unsigned int *cpu_map;
  1998. /* this function may be called multiple times */
  1999. memset(group_map, 0, sizeof(group_map));
  2000. memset(group_cnt, 0, sizeof(group_cnt));
  2001. /* calculate size_sum and ensure dyn_size is enough for early alloc */
  2002. size_sum = PFN_ALIGN(static_size + reserved_size +
  2003. max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE));
  2004. dyn_size = size_sum - static_size - reserved_size;
  2005. /*
  2006. * Determine min_unit_size, alloc_size and max_upa such that
  2007. * alloc_size is multiple of atom_size and is the smallest
  2008. * which can accommodate 4k aligned segments which are equal to
  2009. * or larger than min_unit_size.
  2010. */
  2011. min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
  2012. /* determine the maximum # of units that can fit in an allocation */
  2013. alloc_size = roundup(min_unit_size, atom_size);
  2014. upa = alloc_size / min_unit_size;
  2015. while (alloc_size % upa || (offset_in_page(alloc_size / upa)))
  2016. upa--;
  2017. max_upa = upa;
  2018. /* group cpus according to their proximity */
  2019. for_each_possible_cpu(cpu) {
  2020. group = 0;
  2021. next_group:
  2022. for_each_possible_cpu(tcpu) {
  2023. if (cpu == tcpu)
  2024. break;
  2025. if (group_map[tcpu] == group && cpu_distance_fn &&
  2026. (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE ||
  2027. cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) {
  2028. group++;
  2029. nr_groups = max(nr_groups, group + 1);
  2030. goto next_group;
  2031. }
  2032. }
  2033. group_map[cpu] = group;
  2034. group_cnt[group]++;
  2035. }
  2036. /*
  2037. * Wasted space is caused by a ratio imbalance of upa to group_cnt.
  2038. * Expand the unit_size until we use >= 75% of the units allocated.
  2039. * Related to atom_size, which could be much larger than the unit_size.
  2040. */
  2041. last_allocs = INT_MAX;
  2042. for (upa = max_upa; upa; upa--) {
  2043. int allocs = 0, wasted = 0;
  2044. if (alloc_size % upa || (offset_in_page(alloc_size / upa)))
  2045. continue;
  2046. for (group = 0; group < nr_groups; group++) {
  2047. int this_allocs = DIV_ROUND_UP(group_cnt[group], upa);
  2048. allocs += this_allocs;
  2049. wasted += this_allocs * upa - group_cnt[group];
  2050. }
  2051. /*
  2052. * Don't accept if wastage is over 1/3. The
  2053. * greater-than comparison ensures upa==1 always
  2054. * passes the following check.
  2055. */
  2056. if (wasted > num_possible_cpus() / 3)
  2057. continue;
  2058. /* and then don't consume more memory */
  2059. if (allocs > last_allocs)
  2060. break;
  2061. last_allocs = allocs;
  2062. best_upa = upa;
  2063. }
  2064. upa = best_upa;
  2065. /* allocate and fill alloc_info */
  2066. for (group = 0; group < nr_groups; group++)
  2067. nr_units += roundup(group_cnt[group], upa);
  2068. ai = pcpu_alloc_alloc_info(nr_groups, nr_units);
  2069. if (!ai)
  2070. return ERR_PTR(-ENOMEM);
  2071. cpu_map = ai->groups[0].cpu_map;
  2072. for (group = 0; group < nr_groups; group++) {
  2073. ai->groups[group].cpu_map = cpu_map;
  2074. cpu_map += roundup(group_cnt[group], upa);
  2075. }
  2076. ai->static_size = static_size;
  2077. ai->reserved_size = reserved_size;
  2078. ai->dyn_size = dyn_size;
  2079. ai->unit_size = alloc_size / upa;
  2080. ai->atom_size = atom_size;
  2081. ai->alloc_size = alloc_size;
  2082. for (group = 0, unit = 0; group_cnt[group]; group++) {
  2083. struct pcpu_group_info *gi = &ai->groups[group];
  2084. /*
  2085. * Initialize base_offset as if all groups are located
  2086. * back-to-back. The caller should update this to
  2087. * reflect actual allocation.
  2088. */
  2089. gi->base_offset = unit * ai->unit_size;
  2090. for_each_possible_cpu(cpu)
  2091. if (group_map[cpu] == group)
  2092. gi->cpu_map[gi->nr_units++] = cpu;
  2093. gi->nr_units = roundup(gi->nr_units, upa);
  2094. unit += gi->nr_units;
  2095. }
  2096. BUG_ON(unit != nr_units);
  2097. return ai;
  2098. }
  2099. #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */
  2100. #if defined(BUILD_EMBED_FIRST_CHUNK)
  2101. /**
  2102. * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
  2103. * @reserved_size: the size of reserved percpu area in bytes
  2104. * @dyn_size: minimum free size for dynamic allocation in bytes
  2105. * @atom_size: allocation atom size
  2106. * @cpu_distance_fn: callback to determine distance between cpus, optional
  2107. * @alloc_fn: function to allocate percpu page
  2108. * @free_fn: function to free percpu page
  2109. *
  2110. * This is a helper to ease setting up embedded first percpu chunk and
  2111. * can be called where pcpu_setup_first_chunk() is expected.
  2112. *
  2113. * If this function is used to setup the first chunk, it is allocated
  2114. * by calling @alloc_fn and used as-is without being mapped into
  2115. * vmalloc area. Allocations are always whole multiples of @atom_size
  2116. * aligned to @atom_size.
  2117. *
  2118. * This enables the first chunk to piggy back on the linear physical
  2119. * mapping which often uses larger page size. Please note that this
  2120. * can result in very sparse cpu->unit mapping on NUMA machines thus
  2121. * requiring large vmalloc address space. Don't use this allocator if
  2122. * vmalloc space is not orders of magnitude larger than distances
  2123. * between node memory addresses (ie. 32bit NUMA machines).
  2124. *
  2125. * @dyn_size specifies the minimum dynamic area size.
  2126. *
  2127. * If the needed size is smaller than the minimum or specified unit
  2128. * size, the leftover is returned using @free_fn.
  2129. *
  2130. * RETURNS:
  2131. * 0 on success, -errno on failure.
  2132. */
  2133. int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  2134. size_t atom_size,
  2135. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  2136. pcpu_fc_alloc_fn_t alloc_fn,
  2137. pcpu_fc_free_fn_t free_fn)
  2138. {
  2139. void *base = (void *)ULONG_MAX;
  2140. void **areas = NULL;
  2141. struct pcpu_alloc_info *ai;
  2142. size_t size_sum, areas_size;
  2143. unsigned long max_distance;
  2144. int group, i, highest_group, rc;
  2145. ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size,
  2146. cpu_distance_fn);
  2147. if (IS_ERR(ai))
  2148. return PTR_ERR(ai);
  2149. size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
  2150. areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *));
  2151. areas = memblock_alloc_nopanic(areas_size, SMP_CACHE_BYTES);
  2152. if (!areas) {
  2153. rc = -ENOMEM;
  2154. goto out_free;
  2155. }
  2156. /* allocate, copy and determine base address & max_distance */
  2157. highest_group = 0;
  2158. for (group = 0; group < ai->nr_groups; group++) {
  2159. struct pcpu_group_info *gi = &ai->groups[group];
  2160. unsigned int cpu = NR_CPUS;
  2161. void *ptr;
  2162. for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++)
  2163. cpu = gi->cpu_map[i];
  2164. BUG_ON(cpu == NR_CPUS);
  2165. /* allocate space for the whole group */
  2166. ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size);
  2167. if (!ptr) {
  2168. rc = -ENOMEM;
  2169. goto out_free_areas;
  2170. }
  2171. /* kmemleak tracks the percpu allocations separately */
  2172. kmemleak_free(ptr);
  2173. areas[group] = ptr;
  2174. base = min(ptr, base);
  2175. if (ptr > areas[highest_group])
  2176. highest_group = group;
  2177. }
  2178. max_distance = areas[highest_group] - base;
  2179. max_distance += ai->unit_size * ai->groups[highest_group].nr_units;
  2180. /* warn if maximum distance is further than 75% of vmalloc space */
  2181. if (max_distance > VMALLOC_TOTAL * 3 / 4) {
  2182. pr_warn("max_distance=0x%lx too large for vmalloc space 0x%lx\n",
  2183. max_distance, VMALLOC_TOTAL);
  2184. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  2185. /* and fail if we have fallback */
  2186. rc = -EINVAL;
  2187. goto out_free_areas;
  2188. #endif
  2189. }
  2190. /*
  2191. * Copy data and free unused parts. This should happen after all
  2192. * allocations are complete; otherwise, we may end up with
  2193. * overlapping groups.
  2194. */
  2195. for (group = 0; group < ai->nr_groups; group++) {
  2196. struct pcpu_group_info *gi = &ai->groups[group];
  2197. void *ptr = areas[group];
  2198. for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) {
  2199. if (gi->cpu_map[i] == NR_CPUS) {
  2200. /* unused unit, free whole */
  2201. free_fn(ptr, ai->unit_size);
  2202. continue;
  2203. }
  2204. /* copy and return the unused part */
  2205. memcpy(ptr, __per_cpu_load, ai->static_size);
  2206. free_fn(ptr + size_sum, ai->unit_size - size_sum);
  2207. }
  2208. }
  2209. /* base address is now known, determine group base offsets */
  2210. for (group = 0; group < ai->nr_groups; group++) {
  2211. ai->groups[group].base_offset = areas[group] - base;
  2212. }
  2213. pr_info("Embedded %zu pages/cpu @%p s%zu r%zu d%zu u%zu\n",
  2214. PFN_DOWN(size_sum), base, ai->static_size, ai->reserved_size,
  2215. ai->dyn_size, ai->unit_size);
  2216. rc = pcpu_setup_first_chunk(ai, base);
  2217. goto out_free;
  2218. out_free_areas:
  2219. for (group = 0; group < ai->nr_groups; group++)
  2220. if (areas[group])
  2221. free_fn(areas[group],
  2222. ai->groups[group].nr_units * ai->unit_size);
  2223. out_free:
  2224. pcpu_free_alloc_info(ai);
  2225. if (areas)
  2226. memblock_free_early(__pa(areas), areas_size);
  2227. return rc;
  2228. }
  2229. #endif /* BUILD_EMBED_FIRST_CHUNK */
  2230. #ifdef BUILD_PAGE_FIRST_CHUNK
  2231. /**
  2232. * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages
  2233. * @reserved_size: the size of reserved percpu area in bytes
  2234. * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE
  2235. * @free_fn: function to free percpu page, always called with PAGE_SIZE
  2236. * @populate_pte_fn: function to populate pte
  2237. *
  2238. * This is a helper to ease setting up page-remapped first percpu
  2239. * chunk and can be called where pcpu_setup_first_chunk() is expected.
  2240. *
  2241. * This is the basic allocator. Static percpu area is allocated
  2242. * page-by-page into vmalloc area.
  2243. *
  2244. * RETURNS:
  2245. * 0 on success, -errno on failure.
  2246. */
  2247. int __init pcpu_page_first_chunk(size_t reserved_size,
  2248. pcpu_fc_alloc_fn_t alloc_fn,
  2249. pcpu_fc_free_fn_t free_fn,
  2250. pcpu_fc_populate_pte_fn_t populate_pte_fn)
  2251. {
  2252. static struct vm_struct vm;
  2253. struct pcpu_alloc_info *ai;
  2254. char psize_str[16];
  2255. int unit_pages;
  2256. size_t pages_size;
  2257. struct page **pages;
  2258. int unit, i, j, rc;
  2259. int upa;
  2260. int nr_g0_units;
  2261. snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10);
  2262. ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL);
  2263. if (IS_ERR(ai))
  2264. return PTR_ERR(ai);
  2265. BUG_ON(ai->nr_groups != 1);
  2266. upa = ai->alloc_size/ai->unit_size;
  2267. nr_g0_units = roundup(num_possible_cpus(), upa);
  2268. if (unlikely(WARN_ON(ai->groups[0].nr_units != nr_g0_units))) {
  2269. pcpu_free_alloc_info(ai);
  2270. return -EINVAL;
  2271. }
  2272. unit_pages = ai->unit_size >> PAGE_SHIFT;
  2273. /* unaligned allocations can't be freed, round up to page size */
  2274. pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
  2275. sizeof(pages[0]));
  2276. pages = memblock_alloc(pages_size, SMP_CACHE_BYTES);
  2277. /* allocate pages */
  2278. j = 0;
  2279. for (unit = 0; unit < num_possible_cpus(); unit++) {
  2280. unsigned int cpu = ai->groups[0].cpu_map[unit];
  2281. for (i = 0; i < unit_pages; i++) {
  2282. void *ptr;
  2283. ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE);
  2284. if (!ptr) {
  2285. pr_warn("failed to allocate %s page for cpu%u\n",
  2286. psize_str, cpu);
  2287. goto enomem;
  2288. }
  2289. /* kmemleak tracks the percpu allocations separately */
  2290. kmemleak_free(ptr);
  2291. pages[j++] = virt_to_page(ptr);
  2292. }
  2293. }
  2294. /* allocate vm area, map the pages and copy static data */
  2295. vm.flags = VM_ALLOC;
  2296. vm.size = num_possible_cpus() * ai->unit_size;
  2297. vm_area_register_early(&vm, PAGE_SIZE);
  2298. for (unit = 0; unit < num_possible_cpus(); unit++) {
  2299. unsigned long unit_addr =
  2300. (unsigned long)vm.addr + unit * ai->unit_size;
  2301. for (i = 0; i < unit_pages; i++)
  2302. populate_pte_fn(unit_addr + (i << PAGE_SHIFT));
  2303. /* pte already populated, the following shouldn't fail */
  2304. rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages],
  2305. unit_pages);
  2306. if (rc < 0)
  2307. panic("failed to map percpu area, err=%d\n", rc);
  2308. /*
  2309. * FIXME: Archs with virtual cache should flush local
  2310. * cache for the linear mapping here - something
  2311. * equivalent to flush_cache_vmap() on the local cpu.
  2312. * flush_cache_vmap() can't be used as most supporting
  2313. * data structures are not set up yet.
  2314. */
  2315. /* copy static data */
  2316. memcpy((void *)unit_addr, __per_cpu_load, ai->static_size);
  2317. }
  2318. /* we're ready, commit */
  2319. pr_info("%d %s pages/cpu @%p s%zu r%zu d%zu\n",
  2320. unit_pages, psize_str, vm.addr, ai->static_size,
  2321. ai->reserved_size, ai->dyn_size);
  2322. rc = pcpu_setup_first_chunk(ai, vm.addr);
  2323. goto out_free_ar;
  2324. enomem:
  2325. while (--j >= 0)
  2326. free_fn(page_address(pages[j]), PAGE_SIZE);
  2327. rc = -ENOMEM;
  2328. out_free_ar:
  2329. memblock_free_early(__pa(pages), pages_size);
  2330. pcpu_free_alloc_info(ai);
  2331. return rc;
  2332. }
  2333. #endif /* BUILD_PAGE_FIRST_CHUNK */
  2334. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  2335. /*
  2336. * Generic SMP percpu area setup.
  2337. *
  2338. * The embedding helper is used because its behavior closely resembles
  2339. * the original non-dynamic generic percpu area setup. This is
  2340. * important because many archs have addressing restrictions and might
  2341. * fail if the percpu area is located far away from the previous
  2342. * location. As an added bonus, in non-NUMA cases, embedding is
  2343. * generally a good idea TLB-wise because percpu area can piggy back
  2344. * on the physical linear memory mapping which uses large page
  2345. * mappings on applicable archs.
  2346. */
  2347. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  2348. EXPORT_SYMBOL(__per_cpu_offset);
  2349. static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size,
  2350. size_t align)
  2351. {
  2352. return memblock_alloc_from_nopanic(
  2353. size, align, __pa(MAX_DMA_ADDRESS));
  2354. }
  2355. static void __init pcpu_dfl_fc_free(void *ptr, size_t size)
  2356. {
  2357. memblock_free_early(__pa(ptr), size);
  2358. }
  2359. void __init setup_per_cpu_areas(void)
  2360. {
  2361. unsigned long delta;
  2362. unsigned int cpu;
  2363. int rc;
  2364. /*
  2365. * Always reserve area for module percpu variables. That's
  2366. * what the legacy allocator did.
  2367. */
  2368. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  2369. PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL,
  2370. pcpu_dfl_fc_alloc, pcpu_dfl_fc_free);
  2371. if (rc < 0)
  2372. panic("Failed to initialize percpu areas.");
  2373. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  2374. for_each_possible_cpu(cpu)
  2375. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  2376. }
  2377. #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
  2378. #else /* CONFIG_SMP */
  2379. /*
  2380. * UP percpu area setup.
  2381. *
  2382. * UP always uses km-based percpu allocator with identity mapping.
  2383. * Static percpu variables are indistinguishable from the usual static
  2384. * variables and don't require any special preparation.
  2385. */
  2386. void __init setup_per_cpu_areas(void)
  2387. {
  2388. const size_t unit_size =
  2389. roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE,
  2390. PERCPU_DYNAMIC_RESERVE));
  2391. struct pcpu_alloc_info *ai;
  2392. void *fc;
  2393. ai = pcpu_alloc_alloc_info(1, 1);
  2394. fc = memblock_alloc_from_nopanic(unit_size,
  2395. PAGE_SIZE,
  2396. __pa(MAX_DMA_ADDRESS));
  2397. if (!ai || !fc)
  2398. panic("Failed to allocate memory for percpu areas.");
  2399. /* kmemleak tracks the percpu allocations separately */
  2400. kmemleak_free(fc);
  2401. ai->dyn_size = unit_size;
  2402. ai->unit_size = unit_size;
  2403. ai->atom_size = unit_size;
  2404. ai->alloc_size = unit_size;
  2405. ai->groups[0].nr_units = 1;
  2406. ai->groups[0].cpu_map[0] = 0;
  2407. if (pcpu_setup_first_chunk(ai, fc) < 0)
  2408. panic("Failed to initialize percpu areas.");
  2409. pcpu_free_alloc_info(ai);
  2410. }
  2411. #endif /* CONFIG_SMP */
  2412. /*
  2413. * pcpu_nr_pages - calculate total number of populated backing pages
  2414. *
  2415. * This reflects the number of pages populated to back chunks. Metadata is
  2416. * excluded in the number exposed in meminfo as the number of backing pages
  2417. * scales with the number of cpus and can quickly outweigh the memory used for
  2418. * metadata. It also keeps this calculation nice and simple.
  2419. *
  2420. * RETURNS:
  2421. * Total number of populated backing pages in use by the allocator.
  2422. */
  2423. unsigned long pcpu_nr_pages(void)
  2424. {
  2425. return pcpu_nr_populated * pcpu_nr_units;
  2426. }
  2427. /*
  2428. * Percpu allocator is initialized early during boot when neither slab or
  2429. * workqueue is available. Plug async management until everything is up
  2430. * and running.
  2431. */
  2432. static int __init percpu_enable_async(void)
  2433. {
  2434. pcpu_async_enabled = true;
  2435. return 0;
  2436. }
  2437. subsys_initcall(percpu_enable_async);