percpu.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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. size = ALIGN(size, 2);
  637. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
  638. WARN(true, "illegal size (%zu) or align (%zu) for "
  639. "percpu allocation\n", size, align);
  640. return NULL;
  641. }
  642. mutex_lock(&pcpu_alloc_mutex);
  643. spin_lock_irqsave(&pcpu_lock, flags);
  644. /* serve reserved allocations from the reserved chunk if available */
  645. if (reserved && pcpu_reserved_chunk) {
  646. chunk = pcpu_reserved_chunk;
  647. if (size > chunk->contig_hint) {
  648. err = "alloc from reserved chunk failed";
  649. goto fail_unlock;
  650. }
  651. while ((new_alloc = pcpu_need_to_extend(chunk))) {
  652. spin_unlock_irqrestore(&pcpu_lock, flags);
  653. if (pcpu_extend_area_map(chunk, new_alloc) < 0) {
  654. err = "failed to extend area map of reserved chunk";
  655. goto fail_unlock_mutex;
  656. }
  657. spin_lock_irqsave(&pcpu_lock, flags);
  658. }
  659. off = pcpu_alloc_area(chunk, size, align);
  660. if (off >= 0)
  661. goto area_found;
  662. err = "alloc from reserved chunk failed";
  663. goto fail_unlock;
  664. }
  665. restart:
  666. /* search through normal chunks */
  667. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  668. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  669. if (size > chunk->contig_hint)
  670. continue;
  671. new_alloc = pcpu_need_to_extend(chunk);
  672. if (new_alloc) {
  673. spin_unlock_irqrestore(&pcpu_lock, flags);
  674. if (pcpu_extend_area_map(chunk,
  675. new_alloc) < 0) {
  676. err = "failed to extend area map";
  677. goto fail_unlock_mutex;
  678. }
  679. spin_lock_irqsave(&pcpu_lock, flags);
  680. /*
  681. * pcpu_lock has been dropped, need to
  682. * restart cpu_slot list walking.
  683. */
  684. goto restart;
  685. }
  686. off = pcpu_alloc_area(chunk, size, align);
  687. if (off >= 0)
  688. goto area_found;
  689. }
  690. }
  691. /* hmmm... no space left, create a new chunk */
  692. spin_unlock_irqrestore(&pcpu_lock, flags);
  693. chunk = pcpu_create_chunk();
  694. if (!chunk) {
  695. err = "failed to allocate new chunk";
  696. goto fail_unlock_mutex;
  697. }
  698. spin_lock_irqsave(&pcpu_lock, flags);
  699. pcpu_chunk_relocate(chunk, -1);
  700. goto restart;
  701. area_found:
  702. spin_unlock_irqrestore(&pcpu_lock, flags);
  703. /* populate, map and clear the area */
  704. if (pcpu_populate_chunk(chunk, off, size)) {
  705. spin_lock_irqsave(&pcpu_lock, flags);
  706. pcpu_free_area(chunk, off);
  707. err = "failed to populate";
  708. goto fail_unlock;
  709. }
  710. mutex_unlock(&pcpu_alloc_mutex);
  711. /* return address relative to base address */
  712. ptr = __addr_to_pcpu_ptr(chunk->base_addr + off);
  713. kmemleak_alloc_percpu(ptr, size);
  714. return ptr;
  715. fail_unlock:
  716. spin_unlock_irqrestore(&pcpu_lock, flags);
  717. fail_unlock_mutex:
  718. mutex_unlock(&pcpu_alloc_mutex);
  719. if (warn_limit) {
  720. pr_warning("PERCPU: allocation failed, size=%zu align=%zu, "
  721. "%s\n", size, align, err);
  722. dump_stack();
  723. if (!--warn_limit)
  724. pr_info("PERCPU: limit reached, disable warning\n");
  725. }
  726. return NULL;
  727. }
  728. /**
  729. * __alloc_percpu - allocate dynamic percpu area
  730. * @size: size of area to allocate in bytes
  731. * @align: alignment of area (max PAGE_SIZE)
  732. *
  733. * Allocate zero-filled percpu area of @size bytes aligned at @align.
  734. * Might sleep. Might trigger writeouts.
  735. *
  736. * CONTEXT:
  737. * Does GFP_KERNEL allocation.
  738. *
  739. * RETURNS:
  740. * Percpu pointer to the allocated area on success, NULL on failure.
  741. */
  742. void __percpu *__alloc_percpu(size_t size, size_t align)
  743. {
  744. return pcpu_alloc(size, align, false);
  745. }
  746. EXPORT_SYMBOL_GPL(__alloc_percpu);
  747. /**
  748. * __alloc_reserved_percpu - allocate reserved percpu area
  749. * @size: size of area to allocate in bytes
  750. * @align: alignment of area (max PAGE_SIZE)
  751. *
  752. * Allocate zero-filled percpu area of @size bytes aligned at @align
  753. * from reserved percpu area if arch has set it up; otherwise,
  754. * allocation is served from the same dynamic area. Might sleep.
  755. * Might trigger writeouts.
  756. *
  757. * CONTEXT:
  758. * Does GFP_KERNEL allocation.
  759. *
  760. * RETURNS:
  761. * Percpu pointer to the allocated area on success, NULL on failure.
  762. */
  763. void __percpu *__alloc_reserved_percpu(size_t size, size_t align)
  764. {
  765. return pcpu_alloc(size, align, true);
  766. }
  767. /**
  768. * pcpu_reclaim - reclaim fully free chunks, workqueue function
  769. * @work: unused
  770. *
  771. * Reclaim all fully free chunks except for the first one.
  772. *
  773. * CONTEXT:
  774. * workqueue context.
  775. */
  776. static void pcpu_reclaim(struct work_struct *work)
  777. {
  778. LIST_HEAD(todo);
  779. struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
  780. struct pcpu_chunk *chunk, *next;
  781. mutex_lock(&pcpu_alloc_mutex);
  782. spin_lock_irq(&pcpu_lock);
  783. list_for_each_entry_safe(chunk, next, head, list) {
  784. WARN_ON(chunk->immutable);
  785. /* spare the first one */
  786. if (chunk == list_first_entry(head, struct pcpu_chunk, list))
  787. continue;
  788. list_move(&chunk->list, &todo);
  789. }
  790. spin_unlock_irq(&pcpu_lock);
  791. list_for_each_entry_safe(chunk, next, &todo, list) {
  792. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size);
  793. pcpu_destroy_chunk(chunk);
  794. }
  795. mutex_unlock(&pcpu_alloc_mutex);
  796. }
  797. /**
  798. * free_percpu - free percpu area
  799. * @ptr: pointer to area to free
  800. *
  801. * Free percpu area @ptr.
  802. *
  803. * CONTEXT:
  804. * Can be called from atomic context.
  805. */
  806. void free_percpu(void __percpu *ptr)
  807. {
  808. void *addr;
  809. struct pcpu_chunk *chunk;
  810. unsigned long flags;
  811. int off;
  812. if (!ptr)
  813. return;
  814. kmemleak_free_percpu(ptr);
  815. addr = __pcpu_ptr_to_addr(ptr);
  816. spin_lock_irqsave(&pcpu_lock, flags);
  817. chunk = pcpu_chunk_addr_search(addr);
  818. off = addr - chunk->base_addr;
  819. pcpu_free_area(chunk, off);
  820. /* if there are more than one fully free chunks, wake up grim reaper */
  821. if (chunk->free_size == pcpu_unit_size) {
  822. struct pcpu_chunk *pos;
  823. list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
  824. if (pos != chunk) {
  825. schedule_work(&pcpu_reclaim_work);
  826. break;
  827. }
  828. }
  829. spin_unlock_irqrestore(&pcpu_lock, flags);
  830. }
  831. EXPORT_SYMBOL_GPL(free_percpu);
  832. /**
  833. * is_kernel_percpu_address - test whether address is from static percpu area
  834. * @addr: address to test
  835. *
  836. * Test whether @addr belongs to in-kernel static percpu area. Module
  837. * static percpu areas are not considered. For those, use
  838. * is_module_percpu_address().
  839. *
  840. * RETURNS:
  841. * %true if @addr is from in-kernel static percpu area, %false otherwise.
  842. */
  843. bool is_kernel_percpu_address(unsigned long addr)
  844. {
  845. #ifdef CONFIG_SMP
  846. const size_t static_size = __per_cpu_end - __per_cpu_start;
  847. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  848. unsigned int cpu;
  849. for_each_possible_cpu(cpu) {
  850. void *start = per_cpu_ptr(base, cpu);
  851. if ((void *)addr >= start && (void *)addr < start + static_size)
  852. return true;
  853. }
  854. #endif
  855. /* on UP, can't distinguish from other static vars, always false */
  856. return false;
  857. }
  858. /**
  859. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  860. * @addr: the address to be converted to physical address
  861. *
  862. * Given @addr which is dereferenceable address obtained via one of
  863. * percpu access macros, this function translates it into its physical
  864. * address. The caller is responsible for ensuring @addr stays valid
  865. * until this function finishes.
  866. *
  867. * percpu allocator has special setup for the first chunk, which currently
  868. * supports either embedding in linear address space or vmalloc mapping,
  869. * and, from the second one, the backing allocator (currently either vm or
  870. * km) provides translation.
  871. *
  872. * The addr can be tranlated simply without checking if it falls into the
  873. * first chunk. But the current code reflects better how percpu allocator
  874. * actually works, and the verification can discover both bugs in percpu
  875. * allocator itself and per_cpu_ptr_to_phys() callers. So we keep current
  876. * code.
  877. *
  878. * RETURNS:
  879. * The physical address for @addr.
  880. */
  881. phys_addr_t per_cpu_ptr_to_phys(void *addr)
  882. {
  883. void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
  884. bool in_first_chunk = false;
  885. unsigned long first_low, first_high;
  886. unsigned int cpu;
  887. /*
  888. * The following test on unit_low/high isn't strictly
  889. * necessary but will speed up lookups of addresses which
  890. * aren't in the first chunk.
  891. */
  892. first_low = pcpu_chunk_addr(pcpu_first_chunk, pcpu_low_unit_cpu, 0);
  893. first_high = pcpu_chunk_addr(pcpu_first_chunk, pcpu_high_unit_cpu,
  894. pcpu_unit_pages);
  895. if ((unsigned long)addr >= first_low &&
  896. (unsigned long)addr < first_high) {
  897. for_each_possible_cpu(cpu) {
  898. void *start = per_cpu_ptr(base, cpu);
  899. if (addr >= start && addr < start + pcpu_unit_size) {
  900. in_first_chunk = true;
  901. break;
  902. }
  903. }
  904. }
  905. if (in_first_chunk) {
  906. if (!is_vmalloc_addr(addr))
  907. return __pa(addr);
  908. else
  909. return page_to_phys(vmalloc_to_page(addr)) +
  910. offset_in_page(addr);
  911. } else
  912. return page_to_phys(pcpu_addr_to_page(addr)) +
  913. offset_in_page(addr);
  914. }
  915. /**
  916. * pcpu_alloc_alloc_info - allocate percpu allocation info
  917. * @nr_groups: the number of groups
  918. * @nr_units: the number of units
  919. *
  920. * Allocate ai which is large enough for @nr_groups groups containing
  921. * @nr_units units. The returned ai's groups[0].cpu_map points to the
  922. * cpu_map array which is long enough for @nr_units and filled with
  923. * NR_CPUS. It's the caller's responsibility to initialize cpu_map
  924. * pointer of other groups.
  925. *
  926. * RETURNS:
  927. * Pointer to the allocated pcpu_alloc_info on success, NULL on
  928. * failure.
  929. */
  930. struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  931. int nr_units)
  932. {
  933. struct pcpu_alloc_info *ai;
  934. size_t base_size, ai_size;
  935. void *ptr;
  936. int unit;
  937. base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]),
  938. __alignof__(ai->groups[0].cpu_map[0]));
  939. ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]);
  940. ptr = memblock_virt_alloc_nopanic(PFN_ALIGN(ai_size), 0);
  941. if (!ptr)
  942. return NULL;
  943. ai = ptr;
  944. ptr += base_size;
  945. ai->groups[0].cpu_map = ptr;
  946. for (unit = 0; unit < nr_units; unit++)
  947. ai->groups[0].cpu_map[unit] = NR_CPUS;
  948. ai->nr_groups = nr_groups;
  949. ai->__ai_size = PFN_ALIGN(ai_size);
  950. return ai;
  951. }
  952. /**
  953. * pcpu_free_alloc_info - free percpu allocation info
  954. * @ai: pcpu_alloc_info to free
  955. *
  956. * Free @ai which was allocated by pcpu_alloc_alloc_info().
  957. */
  958. void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
  959. {
  960. memblock_free_early(__pa(ai), ai->__ai_size);
  961. }
  962. /**
  963. * pcpu_dump_alloc_info - print out information about pcpu_alloc_info
  964. * @lvl: loglevel
  965. * @ai: allocation info to dump
  966. *
  967. * Print out information about @ai using loglevel @lvl.
  968. */
  969. static void pcpu_dump_alloc_info(const char *lvl,
  970. const struct pcpu_alloc_info *ai)
  971. {
  972. int group_width = 1, cpu_width = 1, width;
  973. char empty_str[] = "--------";
  974. int alloc = 0, alloc_end = 0;
  975. int group, v;
  976. int upa, apl; /* units per alloc, allocs per line */
  977. v = ai->nr_groups;
  978. while (v /= 10)
  979. group_width++;
  980. v = num_possible_cpus();
  981. while (v /= 10)
  982. cpu_width++;
  983. empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0';
  984. upa = ai->alloc_size / ai->unit_size;
  985. width = upa * (cpu_width + 1) + group_width + 3;
  986. apl = rounddown_pow_of_two(max(60 / width, 1));
  987. printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu",
  988. lvl, ai->static_size, ai->reserved_size, ai->dyn_size,
  989. ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size);
  990. for (group = 0; group < ai->nr_groups; group++) {
  991. const struct pcpu_group_info *gi = &ai->groups[group];
  992. int unit = 0, unit_end = 0;
  993. BUG_ON(gi->nr_units % upa);
  994. for (alloc_end += gi->nr_units / upa;
  995. alloc < alloc_end; alloc++) {
  996. if (!(alloc % apl)) {
  997. printk(KERN_CONT "\n");
  998. printk("%spcpu-alloc: ", lvl);
  999. }
  1000. printk(KERN_CONT "[%0*d] ", group_width, group);
  1001. for (unit_end += upa; unit < unit_end; unit++)
  1002. if (gi->cpu_map[unit] != NR_CPUS)
  1003. printk(KERN_CONT "%0*d ", cpu_width,
  1004. gi->cpu_map[unit]);
  1005. else
  1006. printk(KERN_CONT "%s ", empty_str);
  1007. }
  1008. }
  1009. printk(KERN_CONT "\n");
  1010. }
  1011. /**
  1012. * pcpu_setup_first_chunk - initialize the first percpu chunk
  1013. * @ai: pcpu_alloc_info describing how to percpu area is shaped
  1014. * @base_addr: mapped address
  1015. *
  1016. * Initialize the first percpu chunk which contains the kernel static
  1017. * perpcu area. This function is to be called from arch percpu area
  1018. * setup path.
  1019. *
  1020. * @ai contains all information necessary to initialize the first
  1021. * chunk and prime the dynamic percpu allocator.
  1022. *
  1023. * @ai->static_size is the size of static percpu area.
  1024. *
  1025. * @ai->reserved_size, if non-zero, specifies the amount of bytes to
  1026. * reserve after the static area in the first chunk. This reserves
  1027. * the first chunk such that it's available only through reserved
  1028. * percpu allocation. This is primarily used to serve module percpu
  1029. * static areas on architectures where the addressing model has
  1030. * limited offset range for symbol relocations to guarantee module
  1031. * percpu symbols fall inside the relocatable range.
  1032. *
  1033. * @ai->dyn_size determines the number of bytes available for dynamic
  1034. * allocation in the first chunk. The area between @ai->static_size +
  1035. * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused.
  1036. *
  1037. * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE
  1038. * and equal to or larger than @ai->static_size + @ai->reserved_size +
  1039. * @ai->dyn_size.
  1040. *
  1041. * @ai->atom_size is the allocation atom size and used as alignment
  1042. * for vm areas.
  1043. *
  1044. * @ai->alloc_size is the allocation size and always multiple of
  1045. * @ai->atom_size. This is larger than @ai->atom_size if
  1046. * @ai->unit_size is larger than @ai->atom_size.
  1047. *
  1048. * @ai->nr_groups and @ai->groups describe virtual memory layout of
  1049. * percpu areas. Units which should be colocated are put into the
  1050. * same group. Dynamic VM areas will be allocated according to these
  1051. * groupings. If @ai->nr_groups is zero, a single group containing
  1052. * all units is assumed.
  1053. *
  1054. * The caller should have mapped the first chunk at @base_addr and
  1055. * copied static data to each unit.
  1056. *
  1057. * If the first chunk ends up with both reserved and dynamic areas, it
  1058. * is served by two chunks - one to serve the core static and reserved
  1059. * areas and the other for the dynamic area. They share the same vm
  1060. * and page map but uses different area allocation map to stay away
  1061. * from each other. The latter chunk is circulated in the chunk slots
  1062. * and available for dynamic allocation like any other chunks.
  1063. *
  1064. * RETURNS:
  1065. * 0 on success, -errno on failure.
  1066. */
  1067. int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  1068. void *base_addr)
  1069. {
  1070. static char cpus_buf[4096] __initdata;
  1071. static int smap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
  1072. static int dmap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
  1073. size_t dyn_size = ai->dyn_size;
  1074. size_t size_sum = ai->static_size + ai->reserved_size + dyn_size;
  1075. struct pcpu_chunk *schunk, *dchunk = NULL;
  1076. unsigned long *group_offsets;
  1077. size_t *group_sizes;
  1078. unsigned long *unit_off;
  1079. unsigned int cpu;
  1080. int *unit_map;
  1081. int group, unit, i;
  1082. cpumask_scnprintf(cpus_buf, sizeof(cpus_buf), cpu_possible_mask);
  1083. #define PCPU_SETUP_BUG_ON(cond) do { \
  1084. if (unlikely(cond)) { \
  1085. pr_emerg("PERCPU: failed to initialize, %s", #cond); \
  1086. pr_emerg("PERCPU: cpu_possible_mask=%s\n", cpus_buf); \
  1087. pcpu_dump_alloc_info(KERN_EMERG, ai); \
  1088. BUG(); \
  1089. } \
  1090. } while (0)
  1091. /* sanity checks */
  1092. PCPU_SETUP_BUG_ON(ai->nr_groups <= 0);
  1093. #ifdef CONFIG_SMP
  1094. PCPU_SETUP_BUG_ON(!ai->static_size);
  1095. PCPU_SETUP_BUG_ON((unsigned long)__per_cpu_start & ~PAGE_MASK);
  1096. #endif
  1097. PCPU_SETUP_BUG_ON(!base_addr);
  1098. PCPU_SETUP_BUG_ON((unsigned long)base_addr & ~PAGE_MASK);
  1099. PCPU_SETUP_BUG_ON(ai->unit_size < size_sum);
  1100. PCPU_SETUP_BUG_ON(ai->unit_size & ~PAGE_MASK);
  1101. PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE);
  1102. PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE);
  1103. PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
  1104. /* process group information and build config tables accordingly */
  1105. group_offsets = memblock_virt_alloc(ai->nr_groups *
  1106. sizeof(group_offsets[0]), 0);
  1107. group_sizes = memblock_virt_alloc(ai->nr_groups *
  1108. sizeof(group_sizes[0]), 0);
  1109. unit_map = memblock_virt_alloc(nr_cpu_ids * sizeof(unit_map[0]), 0);
  1110. unit_off = memblock_virt_alloc(nr_cpu_ids * sizeof(unit_off[0]), 0);
  1111. for (cpu = 0; cpu < nr_cpu_ids; cpu++)
  1112. unit_map[cpu] = UINT_MAX;
  1113. pcpu_low_unit_cpu = NR_CPUS;
  1114. pcpu_high_unit_cpu = NR_CPUS;
  1115. for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) {
  1116. const struct pcpu_group_info *gi = &ai->groups[group];
  1117. group_offsets[group] = gi->base_offset;
  1118. group_sizes[group] = gi->nr_units * ai->unit_size;
  1119. for (i = 0; i < gi->nr_units; i++) {
  1120. cpu = gi->cpu_map[i];
  1121. if (cpu == NR_CPUS)
  1122. continue;
  1123. PCPU_SETUP_BUG_ON(cpu > nr_cpu_ids);
  1124. PCPU_SETUP_BUG_ON(!cpu_possible(cpu));
  1125. PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX);
  1126. unit_map[cpu] = unit + i;
  1127. unit_off[cpu] = gi->base_offset + i * ai->unit_size;
  1128. /* determine low/high unit_cpu */
  1129. if (pcpu_low_unit_cpu == NR_CPUS ||
  1130. unit_off[cpu] < unit_off[pcpu_low_unit_cpu])
  1131. pcpu_low_unit_cpu = cpu;
  1132. if (pcpu_high_unit_cpu == NR_CPUS ||
  1133. unit_off[cpu] > unit_off[pcpu_high_unit_cpu])
  1134. pcpu_high_unit_cpu = cpu;
  1135. }
  1136. }
  1137. pcpu_nr_units = unit;
  1138. for_each_possible_cpu(cpu)
  1139. PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX);
  1140. /* we're done parsing the input, undefine BUG macro and dump config */
  1141. #undef PCPU_SETUP_BUG_ON
  1142. pcpu_dump_alloc_info(KERN_DEBUG, ai);
  1143. pcpu_nr_groups = ai->nr_groups;
  1144. pcpu_group_offsets = group_offsets;
  1145. pcpu_group_sizes = group_sizes;
  1146. pcpu_unit_map = unit_map;
  1147. pcpu_unit_offsets = unit_off;
  1148. /* determine basic parameters */
  1149. pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
  1150. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  1151. pcpu_atom_size = ai->atom_size;
  1152. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
  1153. BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
  1154. /*
  1155. * Allocate chunk slots. The additional last slot is for
  1156. * empty chunks.
  1157. */
  1158. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  1159. pcpu_slot = memblock_virt_alloc(
  1160. pcpu_nr_slots * sizeof(pcpu_slot[0]), 0);
  1161. for (i = 0; i < pcpu_nr_slots; i++)
  1162. INIT_LIST_HEAD(&pcpu_slot[i]);
  1163. /*
  1164. * Initialize static chunk. If reserved_size is zero, the
  1165. * static chunk covers static area + dynamic allocation area
  1166. * in the first chunk. If reserved_size is not zero, it
  1167. * covers static area + reserved area (mostly used for module
  1168. * static percpu allocation).
  1169. */
  1170. schunk = memblock_virt_alloc(pcpu_chunk_struct_size, 0);
  1171. INIT_LIST_HEAD(&schunk->list);
  1172. schunk->base_addr = base_addr;
  1173. schunk->map = smap;
  1174. schunk->map_alloc = ARRAY_SIZE(smap);
  1175. schunk->immutable = true;
  1176. bitmap_fill(schunk->populated, pcpu_unit_pages);
  1177. if (ai->reserved_size) {
  1178. schunk->free_size = ai->reserved_size;
  1179. pcpu_reserved_chunk = schunk;
  1180. pcpu_reserved_chunk_limit = ai->static_size + ai->reserved_size;
  1181. } else {
  1182. schunk->free_size = dyn_size;
  1183. dyn_size = 0; /* dynamic area covered */
  1184. }
  1185. schunk->contig_hint = schunk->free_size;
  1186. schunk->map[0] = 1;
  1187. schunk->map[1] = ai->static_size;
  1188. schunk->map_used = 1;
  1189. if (schunk->free_size)
  1190. schunk->map[++schunk->map_used] = 1 | (ai->static_size + schunk->free_size);
  1191. else
  1192. schunk->map[1] |= 1;
  1193. /* init dynamic chunk if necessary */
  1194. if (dyn_size) {
  1195. dchunk = memblock_virt_alloc(pcpu_chunk_struct_size, 0);
  1196. INIT_LIST_HEAD(&dchunk->list);
  1197. dchunk->base_addr = base_addr;
  1198. dchunk->map = dmap;
  1199. dchunk->map_alloc = ARRAY_SIZE(dmap);
  1200. dchunk->immutable = true;
  1201. bitmap_fill(dchunk->populated, pcpu_unit_pages);
  1202. dchunk->contig_hint = dchunk->free_size = dyn_size;
  1203. dchunk->map[0] = 1;
  1204. dchunk->map[1] = pcpu_reserved_chunk_limit;
  1205. dchunk->map[2] = (pcpu_reserved_chunk_limit + dchunk->free_size) | 1;
  1206. dchunk->map_used = 2;
  1207. }
  1208. /* link the first chunk in */
  1209. pcpu_first_chunk = dchunk ?: schunk;
  1210. pcpu_chunk_relocate(pcpu_first_chunk, -1);
  1211. /* we're done */
  1212. pcpu_base_addr = base_addr;
  1213. return 0;
  1214. }
  1215. #ifdef CONFIG_SMP
  1216. const char * const pcpu_fc_names[PCPU_FC_NR] __initconst = {
  1217. [PCPU_FC_AUTO] = "auto",
  1218. [PCPU_FC_EMBED] = "embed",
  1219. [PCPU_FC_PAGE] = "page",
  1220. };
  1221. enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO;
  1222. static int __init percpu_alloc_setup(char *str)
  1223. {
  1224. if (!str)
  1225. return -EINVAL;
  1226. if (0)
  1227. /* nada */;
  1228. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  1229. else if (!strcmp(str, "embed"))
  1230. pcpu_chosen_fc = PCPU_FC_EMBED;
  1231. #endif
  1232. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  1233. else if (!strcmp(str, "page"))
  1234. pcpu_chosen_fc = PCPU_FC_PAGE;
  1235. #endif
  1236. else
  1237. pr_warning("PERCPU: unknown allocator %s specified\n", str);
  1238. return 0;
  1239. }
  1240. early_param("percpu_alloc", percpu_alloc_setup);
  1241. /*
  1242. * pcpu_embed_first_chunk() is used by the generic percpu setup.
  1243. * Build it if needed by the arch config or the generic setup is going
  1244. * to be used.
  1245. */
  1246. #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \
  1247. !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  1248. #define BUILD_EMBED_FIRST_CHUNK
  1249. #endif
  1250. /* build pcpu_page_first_chunk() iff needed by the arch config */
  1251. #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK)
  1252. #define BUILD_PAGE_FIRST_CHUNK
  1253. #endif
  1254. /* pcpu_build_alloc_info() is used by both embed and page first chunk */
  1255. #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK)
  1256. /**
  1257. * pcpu_build_alloc_info - build alloc_info considering distances between CPUs
  1258. * @reserved_size: the size of reserved percpu area in bytes
  1259. * @dyn_size: minimum free size for dynamic allocation in bytes
  1260. * @atom_size: allocation atom size
  1261. * @cpu_distance_fn: callback to determine distance between cpus, optional
  1262. *
  1263. * This function determines grouping of units, their mappings to cpus
  1264. * and other parameters considering needed percpu size, allocation
  1265. * atom size and distances between CPUs.
  1266. *
  1267. * Groups are always mutliples of atom size and CPUs which are of
  1268. * LOCAL_DISTANCE both ways are grouped together and share space for
  1269. * units in the same group. The returned configuration is guaranteed
  1270. * to have CPUs on different nodes on different groups and >=75% usage
  1271. * of allocated virtual address space.
  1272. *
  1273. * RETURNS:
  1274. * On success, pointer to the new allocation_info is returned. On
  1275. * failure, ERR_PTR value is returned.
  1276. */
  1277. static struct pcpu_alloc_info * __init pcpu_build_alloc_info(
  1278. size_t reserved_size, size_t dyn_size,
  1279. size_t atom_size,
  1280. pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
  1281. {
  1282. static int group_map[NR_CPUS] __initdata;
  1283. static int group_cnt[NR_CPUS] __initdata;
  1284. const size_t static_size = __per_cpu_end - __per_cpu_start;
  1285. int nr_groups = 1, nr_units = 0;
  1286. size_t size_sum, min_unit_size, alloc_size;
  1287. int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */
  1288. int last_allocs, group, unit;
  1289. unsigned int cpu, tcpu;
  1290. struct pcpu_alloc_info *ai;
  1291. unsigned int *cpu_map;
  1292. /* this function may be called multiple times */
  1293. memset(group_map, 0, sizeof(group_map));
  1294. memset(group_cnt, 0, sizeof(group_cnt));
  1295. /* calculate size_sum and ensure dyn_size is enough for early alloc */
  1296. size_sum = PFN_ALIGN(static_size + reserved_size +
  1297. max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE));
  1298. dyn_size = size_sum - static_size - reserved_size;
  1299. /*
  1300. * Determine min_unit_size, alloc_size and max_upa such that
  1301. * alloc_size is multiple of atom_size and is the smallest
  1302. * which can accommodate 4k aligned segments which are equal to
  1303. * or larger than min_unit_size.
  1304. */
  1305. min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
  1306. alloc_size = roundup(min_unit_size, atom_size);
  1307. upa = alloc_size / min_unit_size;
  1308. while (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK))
  1309. upa--;
  1310. max_upa = upa;
  1311. /* group cpus according to their proximity */
  1312. for_each_possible_cpu(cpu) {
  1313. group = 0;
  1314. next_group:
  1315. for_each_possible_cpu(tcpu) {
  1316. if (cpu == tcpu)
  1317. break;
  1318. if (group_map[tcpu] == group && cpu_distance_fn &&
  1319. (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE ||
  1320. cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) {
  1321. group++;
  1322. nr_groups = max(nr_groups, group + 1);
  1323. goto next_group;
  1324. }
  1325. }
  1326. group_map[cpu] = group;
  1327. group_cnt[group]++;
  1328. }
  1329. /*
  1330. * Expand unit size until address space usage goes over 75%
  1331. * and then as much as possible without using more address
  1332. * space.
  1333. */
  1334. last_allocs = INT_MAX;
  1335. for (upa = max_upa; upa; upa--) {
  1336. int allocs = 0, wasted = 0;
  1337. if (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK))
  1338. continue;
  1339. for (group = 0; group < nr_groups; group++) {
  1340. int this_allocs = DIV_ROUND_UP(group_cnt[group], upa);
  1341. allocs += this_allocs;
  1342. wasted += this_allocs * upa - group_cnt[group];
  1343. }
  1344. /*
  1345. * Don't accept if wastage is over 1/3. The
  1346. * greater-than comparison ensures upa==1 always
  1347. * passes the following check.
  1348. */
  1349. if (wasted > num_possible_cpus() / 3)
  1350. continue;
  1351. /* and then don't consume more memory */
  1352. if (allocs > last_allocs)
  1353. break;
  1354. last_allocs = allocs;
  1355. best_upa = upa;
  1356. }
  1357. upa = best_upa;
  1358. /* allocate and fill alloc_info */
  1359. for (group = 0; group < nr_groups; group++)
  1360. nr_units += roundup(group_cnt[group], upa);
  1361. ai = pcpu_alloc_alloc_info(nr_groups, nr_units);
  1362. if (!ai)
  1363. return ERR_PTR(-ENOMEM);
  1364. cpu_map = ai->groups[0].cpu_map;
  1365. for (group = 0; group < nr_groups; group++) {
  1366. ai->groups[group].cpu_map = cpu_map;
  1367. cpu_map += roundup(group_cnt[group], upa);
  1368. }
  1369. ai->static_size = static_size;
  1370. ai->reserved_size = reserved_size;
  1371. ai->dyn_size = dyn_size;
  1372. ai->unit_size = alloc_size / upa;
  1373. ai->atom_size = atom_size;
  1374. ai->alloc_size = alloc_size;
  1375. for (group = 0, unit = 0; group_cnt[group]; group++) {
  1376. struct pcpu_group_info *gi = &ai->groups[group];
  1377. /*
  1378. * Initialize base_offset as if all groups are located
  1379. * back-to-back. The caller should update this to
  1380. * reflect actual allocation.
  1381. */
  1382. gi->base_offset = unit * ai->unit_size;
  1383. for_each_possible_cpu(cpu)
  1384. if (group_map[cpu] == group)
  1385. gi->cpu_map[gi->nr_units++] = cpu;
  1386. gi->nr_units = roundup(gi->nr_units, upa);
  1387. unit += gi->nr_units;
  1388. }
  1389. BUG_ON(unit != nr_units);
  1390. return ai;
  1391. }
  1392. #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */
  1393. #if defined(BUILD_EMBED_FIRST_CHUNK)
  1394. /**
  1395. * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
  1396. * @reserved_size: the size of reserved percpu area in bytes
  1397. * @dyn_size: minimum free size for dynamic allocation in bytes
  1398. * @atom_size: allocation atom size
  1399. * @cpu_distance_fn: callback to determine distance between cpus, optional
  1400. * @alloc_fn: function to allocate percpu page
  1401. * @free_fn: function to free percpu page
  1402. *
  1403. * This is a helper to ease setting up embedded first percpu chunk and
  1404. * can be called where pcpu_setup_first_chunk() is expected.
  1405. *
  1406. * If this function is used to setup the first chunk, it is allocated
  1407. * by calling @alloc_fn and used as-is without being mapped into
  1408. * vmalloc area. Allocations are always whole multiples of @atom_size
  1409. * aligned to @atom_size.
  1410. *
  1411. * This enables the first chunk to piggy back on the linear physical
  1412. * mapping which often uses larger page size. Please note that this
  1413. * can result in very sparse cpu->unit mapping on NUMA machines thus
  1414. * requiring large vmalloc address space. Don't use this allocator if
  1415. * vmalloc space is not orders of magnitude larger than distances
  1416. * between node memory addresses (ie. 32bit NUMA machines).
  1417. *
  1418. * @dyn_size specifies the minimum dynamic area size.
  1419. *
  1420. * If the needed size is smaller than the minimum or specified unit
  1421. * size, the leftover is returned using @free_fn.
  1422. *
  1423. * RETURNS:
  1424. * 0 on success, -errno on failure.
  1425. */
  1426. int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  1427. size_t atom_size,
  1428. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  1429. pcpu_fc_alloc_fn_t alloc_fn,
  1430. pcpu_fc_free_fn_t free_fn)
  1431. {
  1432. void *base = (void *)ULONG_MAX;
  1433. void **areas = NULL;
  1434. struct pcpu_alloc_info *ai;
  1435. size_t size_sum, areas_size, max_distance;
  1436. int group, i, rc;
  1437. ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size,
  1438. cpu_distance_fn);
  1439. if (IS_ERR(ai))
  1440. return PTR_ERR(ai);
  1441. size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
  1442. areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *));
  1443. areas = memblock_virt_alloc_nopanic(areas_size, 0);
  1444. if (!areas) {
  1445. rc = -ENOMEM;
  1446. goto out_free;
  1447. }
  1448. /* allocate, copy and determine base address */
  1449. for (group = 0; group < ai->nr_groups; group++) {
  1450. struct pcpu_group_info *gi = &ai->groups[group];
  1451. unsigned int cpu = NR_CPUS;
  1452. void *ptr;
  1453. for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++)
  1454. cpu = gi->cpu_map[i];
  1455. BUG_ON(cpu == NR_CPUS);
  1456. /* allocate space for the whole group */
  1457. ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size);
  1458. if (!ptr) {
  1459. rc = -ENOMEM;
  1460. goto out_free_areas;
  1461. }
  1462. /* kmemleak tracks the percpu allocations separately */
  1463. kmemleak_free(ptr);
  1464. areas[group] = ptr;
  1465. base = min(ptr, base);
  1466. }
  1467. /*
  1468. * Copy data and free unused parts. This should happen after all
  1469. * allocations are complete; otherwise, we may end up with
  1470. * overlapping groups.
  1471. */
  1472. for (group = 0; group < ai->nr_groups; group++) {
  1473. struct pcpu_group_info *gi = &ai->groups[group];
  1474. void *ptr = areas[group];
  1475. for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) {
  1476. if (gi->cpu_map[i] == NR_CPUS) {
  1477. /* unused unit, free whole */
  1478. free_fn(ptr, ai->unit_size);
  1479. continue;
  1480. }
  1481. /* copy and return the unused part */
  1482. memcpy(ptr, __per_cpu_load, ai->static_size);
  1483. free_fn(ptr + size_sum, ai->unit_size - size_sum);
  1484. }
  1485. }
  1486. /* base address is now known, determine group base offsets */
  1487. max_distance = 0;
  1488. for (group = 0; group < ai->nr_groups; group++) {
  1489. ai->groups[group].base_offset = areas[group] - base;
  1490. max_distance = max_t(size_t, max_distance,
  1491. ai->groups[group].base_offset);
  1492. }
  1493. max_distance += ai->unit_size;
  1494. /* warn if maximum distance is further than 75% of vmalloc space */
  1495. if (max_distance > VMALLOC_TOTAL * 3 / 4) {
  1496. pr_warning("PERCPU: max_distance=0x%zx too large for vmalloc "
  1497. "space 0x%lx\n", max_distance,
  1498. VMALLOC_TOTAL);
  1499. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  1500. /* and fail if we have fallback */
  1501. rc = -EINVAL;
  1502. goto out_free;
  1503. #endif
  1504. }
  1505. pr_info("PERCPU: Embedded %zu pages/cpu @%p s%zu r%zu d%zu u%zu\n",
  1506. PFN_DOWN(size_sum), base, ai->static_size, ai->reserved_size,
  1507. ai->dyn_size, ai->unit_size);
  1508. rc = pcpu_setup_first_chunk(ai, base);
  1509. goto out_free;
  1510. out_free_areas:
  1511. for (group = 0; group < ai->nr_groups; group++)
  1512. if (areas[group])
  1513. free_fn(areas[group],
  1514. ai->groups[group].nr_units * ai->unit_size);
  1515. out_free:
  1516. pcpu_free_alloc_info(ai);
  1517. if (areas)
  1518. memblock_free_early(__pa(areas), areas_size);
  1519. return rc;
  1520. }
  1521. #endif /* BUILD_EMBED_FIRST_CHUNK */
  1522. #ifdef BUILD_PAGE_FIRST_CHUNK
  1523. /**
  1524. * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages
  1525. * @reserved_size: the size of reserved percpu area in bytes
  1526. * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE
  1527. * @free_fn: function to free percpu page, always called with PAGE_SIZE
  1528. * @populate_pte_fn: function to populate pte
  1529. *
  1530. * This is a helper to ease setting up page-remapped first percpu
  1531. * chunk and can be called where pcpu_setup_first_chunk() is expected.
  1532. *
  1533. * This is the basic allocator. Static percpu area is allocated
  1534. * page-by-page into vmalloc area.
  1535. *
  1536. * RETURNS:
  1537. * 0 on success, -errno on failure.
  1538. */
  1539. int __init pcpu_page_first_chunk(size_t reserved_size,
  1540. pcpu_fc_alloc_fn_t alloc_fn,
  1541. pcpu_fc_free_fn_t free_fn,
  1542. pcpu_fc_populate_pte_fn_t populate_pte_fn)
  1543. {
  1544. static struct vm_struct vm;
  1545. struct pcpu_alloc_info *ai;
  1546. char psize_str[16];
  1547. int unit_pages;
  1548. size_t pages_size;
  1549. struct page **pages;
  1550. int unit, i, j, rc;
  1551. snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10);
  1552. ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL);
  1553. if (IS_ERR(ai))
  1554. return PTR_ERR(ai);
  1555. BUG_ON(ai->nr_groups != 1);
  1556. BUG_ON(ai->groups[0].nr_units != num_possible_cpus());
  1557. unit_pages = ai->unit_size >> PAGE_SHIFT;
  1558. /* unaligned allocations can't be freed, round up to page size */
  1559. pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
  1560. sizeof(pages[0]));
  1561. pages = memblock_virt_alloc(pages_size, 0);
  1562. /* allocate pages */
  1563. j = 0;
  1564. for (unit = 0; unit < num_possible_cpus(); unit++)
  1565. for (i = 0; i < unit_pages; i++) {
  1566. unsigned int cpu = ai->groups[0].cpu_map[unit];
  1567. void *ptr;
  1568. ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE);
  1569. if (!ptr) {
  1570. pr_warning("PERCPU: failed to allocate %s page "
  1571. "for cpu%u\n", psize_str, cpu);
  1572. goto enomem;
  1573. }
  1574. /* kmemleak tracks the percpu allocations separately */
  1575. kmemleak_free(ptr);
  1576. pages[j++] = virt_to_page(ptr);
  1577. }
  1578. /* allocate vm area, map the pages and copy static data */
  1579. vm.flags = VM_ALLOC;
  1580. vm.size = num_possible_cpus() * ai->unit_size;
  1581. vm_area_register_early(&vm, PAGE_SIZE);
  1582. for (unit = 0; unit < num_possible_cpus(); unit++) {
  1583. unsigned long unit_addr =
  1584. (unsigned long)vm.addr + unit * ai->unit_size;
  1585. for (i = 0; i < unit_pages; i++)
  1586. populate_pte_fn(unit_addr + (i << PAGE_SHIFT));
  1587. /* pte already populated, the following shouldn't fail */
  1588. rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages],
  1589. unit_pages);
  1590. if (rc < 0)
  1591. panic("failed to map percpu area, err=%d\n", rc);
  1592. /*
  1593. * FIXME: Archs with virtual cache should flush local
  1594. * cache for the linear mapping here - something
  1595. * equivalent to flush_cache_vmap() on the local cpu.
  1596. * flush_cache_vmap() can't be used as most supporting
  1597. * data structures are not set up yet.
  1598. */
  1599. /* copy static data */
  1600. memcpy((void *)unit_addr, __per_cpu_load, ai->static_size);
  1601. }
  1602. /* we're ready, commit */
  1603. pr_info("PERCPU: %d %s pages/cpu @%p s%zu r%zu d%zu\n",
  1604. unit_pages, psize_str, vm.addr, ai->static_size,
  1605. ai->reserved_size, ai->dyn_size);
  1606. rc = pcpu_setup_first_chunk(ai, vm.addr);
  1607. goto out_free_ar;
  1608. enomem:
  1609. while (--j >= 0)
  1610. free_fn(page_address(pages[j]), PAGE_SIZE);
  1611. rc = -ENOMEM;
  1612. out_free_ar:
  1613. memblock_free_early(__pa(pages), pages_size);
  1614. pcpu_free_alloc_info(ai);
  1615. return rc;
  1616. }
  1617. #endif /* BUILD_PAGE_FIRST_CHUNK */
  1618. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  1619. /*
  1620. * Generic SMP percpu area setup.
  1621. *
  1622. * The embedding helper is used because its behavior closely resembles
  1623. * the original non-dynamic generic percpu area setup. This is
  1624. * important because many archs have addressing restrictions and might
  1625. * fail if the percpu area is located far away from the previous
  1626. * location. As an added bonus, in non-NUMA cases, embedding is
  1627. * generally a good idea TLB-wise because percpu area can piggy back
  1628. * on the physical linear memory mapping which uses large page
  1629. * mappings on applicable archs.
  1630. */
  1631. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  1632. EXPORT_SYMBOL(__per_cpu_offset);
  1633. static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size,
  1634. size_t align)
  1635. {
  1636. return memblock_virt_alloc_from_nopanic(
  1637. size, align, __pa(MAX_DMA_ADDRESS));
  1638. }
  1639. static void __init pcpu_dfl_fc_free(void *ptr, size_t size)
  1640. {
  1641. memblock_free_early(__pa(ptr), size);
  1642. }
  1643. void __init setup_per_cpu_areas(void)
  1644. {
  1645. unsigned long delta;
  1646. unsigned int cpu;
  1647. int rc;
  1648. /*
  1649. * Always reserve area for module percpu variables. That's
  1650. * what the legacy allocator did.
  1651. */
  1652. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  1653. PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL,
  1654. pcpu_dfl_fc_alloc, pcpu_dfl_fc_free);
  1655. if (rc < 0)
  1656. panic("Failed to initialize percpu areas.");
  1657. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  1658. for_each_possible_cpu(cpu)
  1659. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  1660. }
  1661. #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
  1662. #else /* CONFIG_SMP */
  1663. /*
  1664. * UP percpu area setup.
  1665. *
  1666. * UP always uses km-based percpu allocator with identity mapping.
  1667. * Static percpu variables are indistinguishable from the usual static
  1668. * variables and don't require any special preparation.
  1669. */
  1670. void __init setup_per_cpu_areas(void)
  1671. {
  1672. const size_t unit_size =
  1673. roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE,
  1674. PERCPU_DYNAMIC_RESERVE));
  1675. struct pcpu_alloc_info *ai;
  1676. void *fc;
  1677. ai = pcpu_alloc_alloc_info(1, 1);
  1678. fc = memblock_virt_alloc_from_nopanic(unit_size,
  1679. PAGE_SIZE,
  1680. __pa(MAX_DMA_ADDRESS));
  1681. if (!ai || !fc)
  1682. panic("Failed to allocate memory for percpu areas.");
  1683. /* kmemleak tracks the percpu allocations separately */
  1684. kmemleak_free(fc);
  1685. ai->dyn_size = unit_size;
  1686. ai->unit_size = unit_size;
  1687. ai->atom_size = unit_size;
  1688. ai->alloc_size = unit_size;
  1689. ai->groups[0].nr_units = 1;
  1690. ai->groups[0].cpu_map[0] = 0;
  1691. if (pcpu_setup_first_chunk(ai, fc) < 0)
  1692. panic("Failed to initialize percpu areas.");
  1693. pcpu_free_alloc_info(ai);
  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. }