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