percpu.c 57 KB

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