percpu.c 66 KB

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