percpu.c 54 KB

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