percpu.c 84 KB

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