percpu.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * linux/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 in vmalloc area. Each
  11. * chunk is consisted of num_possible_cpus() units and the first chunk
  12. * is used for static percpu variables in the kernel image (special
  13. * boot time alloc/init handling necessary as these areas need to be
  14. * brought up before allocation services are running). Unit grows as
  15. * necessary and all units grow or shrink in unison. When a chunk is
  16. * filled up, another chunk is allocated. ie. in vmalloc area
  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. Percpu access can be done by configuring
  26. * percpu base registers pcpu_unit_size apart.
  27. *
  28. * There are usually many small percpu allocations many of them as
  29. * small as 4 bytes. The allocator organizes chunks into lists
  30. * according to free size and tries to allocate from the fullest one.
  31. * Each chunk keeps the maximum contiguous area size hint which is
  32. * guaranteed to be eqaul to or larger than the maximum contiguous
  33. * area in the chunk. This helps the allocator not to iterate the
  34. * chunk maps unnecessarily.
  35. *
  36. * Allocation state in each chunk is kept using an array of integers
  37. * on chunk->map. A positive value in the map represents a free
  38. * region and negative allocated. Allocation inside a chunk is done
  39. * by scanning this map sequentially and serving the first matching
  40. * entry. This is mostly copied from the percpu_modalloc() allocator.
  41. * Chunks can be determined from the address using the index field
  42. * in the page struct. The index field contains a pointer to the chunk.
  43. *
  44. * To use this allocator, arch code should do the followings.
  45. *
  46. * - define CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
  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/list.h>
  58. #include <linux/mm.h>
  59. #include <linux/module.h>
  60. #include <linux/mutex.h>
  61. #include <linux/percpu.h>
  62. #include <linux/pfn.h>
  63. #include <linux/slab.h>
  64. #include <linux/spinlock.h>
  65. #include <linux/vmalloc.h>
  66. #include <linux/workqueue.h>
  67. #include <asm/cacheflush.h>
  68. #include <asm/sections.h>
  69. #include <asm/tlbflush.h>
  70. #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
  71. #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
  72. /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */
  73. #ifndef __addr_to_pcpu_ptr
  74. #define __addr_to_pcpu_ptr(addr) \
  75. (void *)((unsigned long)(addr) - (unsigned long)pcpu_base_addr \
  76. + (unsigned long)__per_cpu_start)
  77. #endif
  78. #ifndef __pcpu_ptr_to_addr
  79. #define __pcpu_ptr_to_addr(ptr) \
  80. (void *)((unsigned long)(ptr) + (unsigned long)pcpu_base_addr \
  81. - (unsigned long)__per_cpu_start)
  82. #endif
  83. struct pcpu_chunk {
  84. struct list_head list; /* linked to pcpu_slot lists */
  85. int free_size; /* free bytes in the chunk */
  86. int contig_hint; /* max contiguous size hint */
  87. struct vm_struct *vm; /* mapped vmalloc region */
  88. int map_used; /* # of map entries used */
  89. int map_alloc; /* # of map entries allocated */
  90. int *map; /* allocation map */
  91. bool immutable; /* no [de]population allowed */
  92. struct page **page; /* points to page array */
  93. struct page *page_ar[]; /* #cpus * UNIT_PAGES */
  94. };
  95. static int pcpu_unit_pages __read_mostly;
  96. static int pcpu_unit_size __read_mostly;
  97. static int pcpu_chunk_size __read_mostly;
  98. static int pcpu_nr_slots __read_mostly;
  99. static size_t pcpu_chunk_struct_size __read_mostly;
  100. /* the address of the first chunk which starts with the kernel static area */
  101. void *pcpu_base_addr __read_mostly;
  102. EXPORT_SYMBOL_GPL(pcpu_base_addr);
  103. /*
  104. * The first chunk which always exists. Note that unlike other
  105. * chunks, this one can be allocated and mapped in several different
  106. * ways and thus often doesn't live in the vmalloc area.
  107. */
  108. static struct pcpu_chunk *pcpu_first_chunk;
  109. /*
  110. * Optional reserved chunk. This chunk reserves part of the first
  111. * chunk and serves it for reserved allocations. The amount of
  112. * reserved offset is in pcpu_reserved_chunk_limit. When reserved
  113. * area doesn't exist, the following variables contain NULL and 0
  114. * respectively.
  115. */
  116. static struct pcpu_chunk *pcpu_reserved_chunk;
  117. static int pcpu_reserved_chunk_limit;
  118. /*
  119. * Synchronization rules.
  120. *
  121. * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former
  122. * protects allocation/reclaim paths, chunks and chunk->page arrays.
  123. * The latter is a spinlock and protects the index data structures -
  124. * chunk slots, chunks and area maps in chunks.
  125. *
  126. * During allocation, pcpu_alloc_mutex is kept locked all the time and
  127. * pcpu_lock is grabbed and released as necessary. All actual memory
  128. * allocations are done using GFP_KERNEL with pcpu_lock released.
  129. *
  130. * Free path accesses and alters only the index data structures, so it
  131. * can be safely called from atomic context. When memory needs to be
  132. * returned to the system, free path schedules reclaim_work which
  133. * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be
  134. * reclaimed, release both locks and frees the chunks. Note that it's
  135. * necessary to grab both locks to remove a chunk from circulation as
  136. * allocation path might be referencing the chunk with only
  137. * pcpu_alloc_mutex locked.
  138. */
  139. static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */
  140. static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */
  141. static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
  142. /* reclaim work to release fully free chunks, scheduled from free path */
  143. static void pcpu_reclaim(struct work_struct *work);
  144. static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
  145. static int __pcpu_size_to_slot(int size)
  146. {
  147. int highbit = fls(size); /* size is in bytes */
  148. return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
  149. }
  150. static int pcpu_size_to_slot(int size)
  151. {
  152. if (size == pcpu_unit_size)
  153. return pcpu_nr_slots - 1;
  154. return __pcpu_size_to_slot(size);
  155. }
  156. static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
  157. {
  158. if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
  159. return 0;
  160. return pcpu_size_to_slot(chunk->free_size);
  161. }
  162. static int pcpu_page_idx(unsigned int cpu, int page_idx)
  163. {
  164. return cpu * pcpu_unit_pages + page_idx;
  165. }
  166. static struct page **pcpu_chunk_pagep(struct pcpu_chunk *chunk,
  167. unsigned int cpu, int page_idx)
  168. {
  169. return &chunk->page[pcpu_page_idx(cpu, page_idx)];
  170. }
  171. static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
  172. unsigned int cpu, int page_idx)
  173. {
  174. return (unsigned long)chunk->vm->addr +
  175. (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
  176. }
  177. static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
  178. int page_idx)
  179. {
  180. return *pcpu_chunk_pagep(chunk, 0, page_idx) != NULL;
  181. }
  182. /* set the pointer to a chunk in a page struct */
  183. static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu)
  184. {
  185. page->index = (unsigned long)pcpu;
  186. }
  187. /* obtain pointer to a chunk from a page struct */
  188. static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page)
  189. {
  190. return (struct pcpu_chunk *)page->index;
  191. }
  192. /**
  193. * pcpu_mem_alloc - allocate memory
  194. * @size: bytes to allocate
  195. *
  196. * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
  197. * kzalloc() is used; otherwise, vmalloc() is used. The returned
  198. * memory is always zeroed.
  199. *
  200. * CONTEXT:
  201. * Does GFP_KERNEL allocation.
  202. *
  203. * RETURNS:
  204. * Pointer to the allocated area on success, NULL on failure.
  205. */
  206. static void *pcpu_mem_alloc(size_t size)
  207. {
  208. if (size <= PAGE_SIZE)
  209. return kzalloc(size, GFP_KERNEL);
  210. else {
  211. void *ptr = vmalloc(size);
  212. if (ptr)
  213. memset(ptr, 0, size);
  214. return ptr;
  215. }
  216. }
  217. /**
  218. * pcpu_mem_free - free memory
  219. * @ptr: memory to free
  220. * @size: size of the area
  221. *
  222. * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc().
  223. */
  224. static void pcpu_mem_free(void *ptr, size_t size)
  225. {
  226. if (size <= PAGE_SIZE)
  227. kfree(ptr);
  228. else
  229. vfree(ptr);
  230. }
  231. /**
  232. * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
  233. * @chunk: chunk of interest
  234. * @oslot: the previous slot it was on
  235. *
  236. * This function is called after an allocation or free changed @chunk.
  237. * New slot according to the changed state is determined and @chunk is
  238. * moved to the slot. Note that the reserved chunk is never put on
  239. * chunk slots.
  240. *
  241. * CONTEXT:
  242. * pcpu_lock.
  243. */
  244. static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
  245. {
  246. int nslot = pcpu_chunk_slot(chunk);
  247. if (chunk != pcpu_reserved_chunk && oslot != nslot) {
  248. if (oslot < nslot)
  249. list_move(&chunk->list, &pcpu_slot[nslot]);
  250. else
  251. list_move_tail(&chunk->list, &pcpu_slot[nslot]);
  252. }
  253. }
  254. /**
  255. * pcpu_chunk_addr_search - determine chunk containing specified address
  256. * @addr: address for which the chunk needs to be determined.
  257. *
  258. * RETURNS:
  259. * The address of the found chunk.
  260. */
  261. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  262. {
  263. void *first_start = pcpu_first_chunk->vm->addr;
  264. /* is it in the first chunk? */
  265. if (addr >= first_start && addr < first_start + pcpu_chunk_size) {
  266. /* is it in the reserved area? */
  267. if (addr < first_start + pcpu_reserved_chunk_limit)
  268. return pcpu_reserved_chunk;
  269. return pcpu_first_chunk;
  270. }
  271. return pcpu_get_page_chunk(vmalloc_to_page(addr));
  272. }
  273. /**
  274. * pcpu_extend_area_map - extend area map for allocation
  275. * @chunk: target chunk
  276. *
  277. * Extend area map of @chunk so that it can accomodate an allocation.
  278. * A single allocation can split an area into three areas, so this
  279. * function makes sure that @chunk->map has at least two extra slots.
  280. *
  281. * CONTEXT:
  282. * pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired
  283. * if area map is extended.
  284. *
  285. * RETURNS:
  286. * 0 if noop, 1 if successfully extended, -errno on failure.
  287. */
  288. static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
  289. {
  290. int new_alloc;
  291. int *new;
  292. size_t size;
  293. /* has enough? */
  294. if (chunk->map_alloc >= chunk->map_used + 2)
  295. return 0;
  296. spin_unlock_irq(&pcpu_lock);
  297. new_alloc = PCPU_DFL_MAP_ALLOC;
  298. while (new_alloc < chunk->map_used + 2)
  299. new_alloc *= 2;
  300. new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
  301. if (!new) {
  302. spin_lock_irq(&pcpu_lock);
  303. return -ENOMEM;
  304. }
  305. /*
  306. * Acquire pcpu_lock and switch to new area map. Only free
  307. * could have happened inbetween, so map_used couldn't have
  308. * grown.
  309. */
  310. spin_lock_irq(&pcpu_lock);
  311. BUG_ON(new_alloc < chunk->map_used + 2);
  312. size = chunk->map_alloc * sizeof(chunk->map[0]);
  313. memcpy(new, chunk->map, size);
  314. /*
  315. * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
  316. * one of the first chunks and still using static map.
  317. */
  318. if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
  319. pcpu_mem_free(chunk->map, size);
  320. chunk->map_alloc = new_alloc;
  321. chunk->map = new;
  322. return 0;
  323. }
  324. /**
  325. * pcpu_split_block - split a map block
  326. * @chunk: chunk of interest
  327. * @i: index of map block to split
  328. * @head: head size in bytes (can be 0)
  329. * @tail: tail size in bytes (can be 0)
  330. *
  331. * Split the @i'th map block into two or three blocks. If @head is
  332. * non-zero, @head bytes block is inserted before block @i moving it
  333. * to @i+1 and reducing its size by @head bytes.
  334. *
  335. * If @tail is non-zero, the target block, which can be @i or @i+1
  336. * depending on @head, is reduced by @tail bytes and @tail byte block
  337. * is inserted after the target block.
  338. *
  339. * @chunk->map must have enough free slots to accomodate the split.
  340. *
  341. * CONTEXT:
  342. * pcpu_lock.
  343. */
  344. static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
  345. int head, int tail)
  346. {
  347. int nr_extra = !!head + !!tail;
  348. BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
  349. /* insert new subblocks */
  350. memmove(&chunk->map[i + nr_extra], &chunk->map[i],
  351. sizeof(chunk->map[0]) * (chunk->map_used - i));
  352. chunk->map_used += nr_extra;
  353. if (head) {
  354. chunk->map[i + 1] = chunk->map[i] - head;
  355. chunk->map[i++] = head;
  356. }
  357. if (tail) {
  358. chunk->map[i++] -= tail;
  359. chunk->map[i] = tail;
  360. }
  361. }
  362. /**
  363. * pcpu_alloc_area - allocate area from a pcpu_chunk
  364. * @chunk: chunk of interest
  365. * @size: wanted size in bytes
  366. * @align: wanted align
  367. *
  368. * Try to allocate @size bytes area aligned at @align from @chunk.
  369. * Note that this function only allocates the offset. It doesn't
  370. * populate or map the area.
  371. *
  372. * @chunk->map must have at least two free slots.
  373. *
  374. * CONTEXT:
  375. * pcpu_lock.
  376. *
  377. * RETURNS:
  378. * Allocated offset in @chunk on success, -1 if no matching area is
  379. * found.
  380. */
  381. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
  382. {
  383. int oslot = pcpu_chunk_slot(chunk);
  384. int max_contig = 0;
  385. int i, off;
  386. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
  387. bool is_last = i + 1 == chunk->map_used;
  388. int head, tail;
  389. /* extra for alignment requirement */
  390. head = ALIGN(off, align) - off;
  391. BUG_ON(i == 0 && head != 0);
  392. if (chunk->map[i] < 0)
  393. continue;
  394. if (chunk->map[i] < head + size) {
  395. max_contig = max(chunk->map[i], max_contig);
  396. continue;
  397. }
  398. /*
  399. * If head is small or the previous block is free,
  400. * merge'em. Note that 'small' is defined as smaller
  401. * than sizeof(int), which is very small but isn't too
  402. * uncommon for percpu allocations.
  403. */
  404. if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
  405. if (chunk->map[i - 1] > 0)
  406. chunk->map[i - 1] += head;
  407. else {
  408. chunk->map[i - 1] -= head;
  409. chunk->free_size -= head;
  410. }
  411. chunk->map[i] -= head;
  412. off += head;
  413. head = 0;
  414. }
  415. /* if tail is small, just keep it around */
  416. tail = chunk->map[i] - head - size;
  417. if (tail < sizeof(int))
  418. tail = 0;
  419. /* split if warranted */
  420. if (head || tail) {
  421. pcpu_split_block(chunk, i, head, tail);
  422. if (head) {
  423. i++;
  424. off += head;
  425. max_contig = max(chunk->map[i - 1], max_contig);
  426. }
  427. if (tail)
  428. max_contig = max(chunk->map[i + 1], max_contig);
  429. }
  430. /* update hint and mark allocated */
  431. if (is_last)
  432. chunk->contig_hint = max_contig; /* fully scanned */
  433. else
  434. chunk->contig_hint = max(chunk->contig_hint,
  435. max_contig);
  436. chunk->free_size -= chunk->map[i];
  437. chunk->map[i] = -chunk->map[i];
  438. pcpu_chunk_relocate(chunk, oslot);
  439. return off;
  440. }
  441. chunk->contig_hint = max_contig; /* fully scanned */
  442. pcpu_chunk_relocate(chunk, oslot);
  443. /* tell the upper layer that this chunk has no matching area */
  444. return -1;
  445. }
  446. /**
  447. * pcpu_free_area - free area to a pcpu_chunk
  448. * @chunk: chunk of interest
  449. * @freeme: offset of area to free
  450. *
  451. * Free area starting from @freeme to @chunk. Note that this function
  452. * only modifies the allocation map. It doesn't depopulate or unmap
  453. * the area.
  454. *
  455. * CONTEXT:
  456. * pcpu_lock.
  457. */
  458. static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
  459. {
  460. int oslot = pcpu_chunk_slot(chunk);
  461. int i, off;
  462. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
  463. if (off == freeme)
  464. break;
  465. BUG_ON(off != freeme);
  466. BUG_ON(chunk->map[i] > 0);
  467. chunk->map[i] = -chunk->map[i];
  468. chunk->free_size += chunk->map[i];
  469. /* merge with previous? */
  470. if (i > 0 && chunk->map[i - 1] >= 0) {
  471. chunk->map[i - 1] += chunk->map[i];
  472. chunk->map_used--;
  473. memmove(&chunk->map[i], &chunk->map[i + 1],
  474. (chunk->map_used - i) * sizeof(chunk->map[0]));
  475. i--;
  476. }
  477. /* merge with next? */
  478. if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
  479. chunk->map[i] += chunk->map[i + 1];
  480. chunk->map_used--;
  481. memmove(&chunk->map[i + 1], &chunk->map[i + 2],
  482. (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
  483. }
  484. chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
  485. pcpu_chunk_relocate(chunk, oslot);
  486. }
  487. /**
  488. * pcpu_unmap - unmap pages out of a pcpu_chunk
  489. * @chunk: chunk of interest
  490. * @page_start: page index of the first page to unmap
  491. * @page_end: page index of the last page to unmap + 1
  492. * @flush: whether to flush cache and tlb or not
  493. *
  494. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  495. * If @flush is true, vcache is flushed before unmapping and tlb
  496. * after.
  497. */
  498. static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
  499. bool flush)
  500. {
  501. unsigned int last = num_possible_cpus() - 1;
  502. unsigned int cpu;
  503. /* unmap must not be done on immutable chunk */
  504. WARN_ON(chunk->immutable);
  505. /*
  506. * Each flushing trial can be very expensive, issue flush on
  507. * the whole region at once rather than doing it for each cpu.
  508. * This could be an overkill but is more scalable.
  509. */
  510. if (flush)
  511. flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
  512. pcpu_chunk_addr(chunk, last, page_end));
  513. for_each_possible_cpu(cpu)
  514. unmap_kernel_range_noflush(
  515. pcpu_chunk_addr(chunk, cpu, page_start),
  516. (page_end - page_start) << PAGE_SHIFT);
  517. /* ditto as flush_cache_vunmap() */
  518. if (flush)
  519. flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
  520. pcpu_chunk_addr(chunk, last, page_end));
  521. }
  522. /**
  523. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  524. * @chunk: chunk to depopulate
  525. * @off: offset to the area to depopulate
  526. * @size: size of the area to depopulate in bytes
  527. * @flush: whether to flush cache and tlb or not
  528. *
  529. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  530. * from @chunk. If @flush is true, vcache is flushed before unmapping
  531. * and tlb after.
  532. *
  533. * CONTEXT:
  534. * pcpu_alloc_mutex.
  535. */
  536. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
  537. bool flush)
  538. {
  539. int page_start = PFN_DOWN(off);
  540. int page_end = PFN_UP(off + size);
  541. int unmap_start = -1;
  542. int uninitialized_var(unmap_end);
  543. unsigned int cpu;
  544. int i;
  545. for (i = page_start; i < page_end; i++) {
  546. for_each_possible_cpu(cpu) {
  547. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  548. if (!*pagep)
  549. continue;
  550. __free_page(*pagep);
  551. /*
  552. * If it's partial depopulation, it might get
  553. * populated or depopulated again. Mark the
  554. * page gone.
  555. */
  556. *pagep = NULL;
  557. unmap_start = unmap_start < 0 ? i : unmap_start;
  558. unmap_end = i + 1;
  559. }
  560. }
  561. if (unmap_start >= 0)
  562. pcpu_unmap(chunk, unmap_start, unmap_end, flush);
  563. }
  564. /**
  565. * pcpu_map - map pages into a pcpu_chunk
  566. * @chunk: chunk of interest
  567. * @page_start: page index of the first page to map
  568. * @page_end: page index of the last page to map + 1
  569. *
  570. * For each cpu, map pages [@page_start,@page_end) into @chunk.
  571. * vcache is flushed afterwards.
  572. */
  573. static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
  574. {
  575. unsigned int last = num_possible_cpus() - 1;
  576. unsigned int cpu;
  577. int err;
  578. /* map must not be done on immutable chunk */
  579. WARN_ON(chunk->immutable);
  580. for_each_possible_cpu(cpu) {
  581. err = map_kernel_range_noflush(
  582. pcpu_chunk_addr(chunk, cpu, page_start),
  583. (page_end - page_start) << PAGE_SHIFT,
  584. PAGE_KERNEL,
  585. pcpu_chunk_pagep(chunk, cpu, page_start));
  586. if (err < 0)
  587. return err;
  588. }
  589. /* flush at once, please read comments in pcpu_unmap() */
  590. flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
  591. pcpu_chunk_addr(chunk, last, page_end));
  592. return 0;
  593. }
  594. /**
  595. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  596. * @chunk: chunk of interest
  597. * @off: offset to the area to populate
  598. * @size: size of the area to populate in bytes
  599. *
  600. * For each cpu, populate and map pages [@page_start,@page_end) into
  601. * @chunk. The area is cleared on return.
  602. *
  603. * CONTEXT:
  604. * pcpu_alloc_mutex, does GFP_KERNEL allocation.
  605. */
  606. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
  607. {
  608. const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  609. int page_start = PFN_DOWN(off);
  610. int page_end = PFN_UP(off + size);
  611. int map_start = -1;
  612. int uninitialized_var(map_end);
  613. unsigned int cpu;
  614. int i;
  615. for (i = page_start; i < page_end; i++) {
  616. if (pcpu_chunk_page_occupied(chunk, i)) {
  617. if (map_start >= 0) {
  618. if (pcpu_map(chunk, map_start, map_end))
  619. goto err;
  620. map_start = -1;
  621. }
  622. continue;
  623. }
  624. map_start = map_start < 0 ? i : map_start;
  625. map_end = i + 1;
  626. for_each_possible_cpu(cpu) {
  627. struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
  628. *pagep = alloc_pages_node(cpu_to_node(cpu),
  629. alloc_mask, 0);
  630. if (!*pagep)
  631. goto err;
  632. pcpu_set_page_chunk(*pagep, chunk);
  633. }
  634. }
  635. if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
  636. goto err;
  637. for_each_possible_cpu(cpu)
  638. memset(chunk->vm->addr + cpu * pcpu_unit_size + off, 0,
  639. size);
  640. return 0;
  641. err:
  642. /* likely under heavy memory pressure, give memory back */
  643. pcpu_depopulate_chunk(chunk, off, size, true);
  644. return -ENOMEM;
  645. }
  646. static void free_pcpu_chunk(struct pcpu_chunk *chunk)
  647. {
  648. if (!chunk)
  649. return;
  650. if (chunk->vm)
  651. free_vm_area(chunk->vm);
  652. pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
  653. kfree(chunk);
  654. }
  655. static struct pcpu_chunk *alloc_pcpu_chunk(void)
  656. {
  657. struct pcpu_chunk *chunk;
  658. chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
  659. if (!chunk)
  660. return NULL;
  661. chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  662. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  663. chunk->map[chunk->map_used++] = pcpu_unit_size;
  664. chunk->page = chunk->page_ar;
  665. chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
  666. if (!chunk->vm) {
  667. free_pcpu_chunk(chunk);
  668. return NULL;
  669. }
  670. INIT_LIST_HEAD(&chunk->list);
  671. chunk->free_size = pcpu_unit_size;
  672. chunk->contig_hint = pcpu_unit_size;
  673. return chunk;
  674. }
  675. /**
  676. * pcpu_alloc - the percpu allocator
  677. * @size: size of area to allocate in bytes
  678. * @align: alignment of area (max PAGE_SIZE)
  679. * @reserved: allocate from the reserved chunk if available
  680. *
  681. * Allocate percpu area of @size bytes aligned at @align.
  682. *
  683. * CONTEXT:
  684. * Does GFP_KERNEL allocation.
  685. *
  686. * RETURNS:
  687. * Percpu pointer to the allocated area on success, NULL on failure.
  688. */
  689. static void *pcpu_alloc(size_t size, size_t align, bool reserved)
  690. {
  691. struct pcpu_chunk *chunk;
  692. int slot, off;
  693. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
  694. WARN(true, "illegal size (%zu) or align (%zu) for "
  695. "percpu allocation\n", size, align);
  696. return NULL;
  697. }
  698. mutex_lock(&pcpu_alloc_mutex);
  699. spin_lock_irq(&pcpu_lock);
  700. /* serve reserved allocations from the reserved chunk if available */
  701. if (reserved && pcpu_reserved_chunk) {
  702. chunk = pcpu_reserved_chunk;
  703. if (size > chunk->contig_hint ||
  704. pcpu_extend_area_map(chunk) < 0)
  705. goto fail_unlock;
  706. off = pcpu_alloc_area(chunk, size, align);
  707. if (off >= 0)
  708. goto area_found;
  709. goto fail_unlock;
  710. }
  711. restart:
  712. /* search through normal chunks */
  713. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  714. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  715. if (size > chunk->contig_hint)
  716. continue;
  717. switch (pcpu_extend_area_map(chunk)) {
  718. case 0:
  719. break;
  720. case 1:
  721. goto restart; /* pcpu_lock dropped, restart */
  722. default:
  723. goto fail_unlock;
  724. }
  725. off = pcpu_alloc_area(chunk, size, align);
  726. if (off >= 0)
  727. goto area_found;
  728. }
  729. }
  730. /* hmmm... no space left, create a new chunk */
  731. spin_unlock_irq(&pcpu_lock);
  732. chunk = alloc_pcpu_chunk();
  733. if (!chunk)
  734. goto fail_unlock_mutex;
  735. spin_lock_irq(&pcpu_lock);
  736. pcpu_chunk_relocate(chunk, -1);
  737. goto restart;
  738. area_found:
  739. spin_unlock_irq(&pcpu_lock);
  740. /* populate, map and clear the area */
  741. if (pcpu_populate_chunk(chunk, off, size)) {
  742. spin_lock_irq(&pcpu_lock);
  743. pcpu_free_area(chunk, off);
  744. goto fail_unlock;
  745. }
  746. mutex_unlock(&pcpu_alloc_mutex);
  747. return __addr_to_pcpu_ptr(chunk->vm->addr + off);
  748. fail_unlock:
  749. spin_unlock_irq(&pcpu_lock);
  750. fail_unlock_mutex:
  751. mutex_unlock(&pcpu_alloc_mutex);
  752. return NULL;
  753. }
  754. /**
  755. * __alloc_percpu - allocate dynamic percpu area
  756. * @size: size of area to allocate in bytes
  757. * @align: alignment of area (max PAGE_SIZE)
  758. *
  759. * Allocate percpu area of @size bytes aligned at @align. Might
  760. * sleep. Might trigger writeouts.
  761. *
  762. * CONTEXT:
  763. * Does GFP_KERNEL allocation.
  764. *
  765. * RETURNS:
  766. * Percpu pointer to the allocated area on success, NULL on failure.
  767. */
  768. void *__alloc_percpu(size_t size, size_t align)
  769. {
  770. return pcpu_alloc(size, align, false);
  771. }
  772. EXPORT_SYMBOL_GPL(__alloc_percpu);
  773. /**
  774. * __alloc_reserved_percpu - allocate reserved percpu area
  775. * @size: size of area to allocate in bytes
  776. * @align: alignment of area (max PAGE_SIZE)
  777. *
  778. * Allocate percpu area of @size bytes aligned at @align from reserved
  779. * percpu area if arch has set it up; otherwise, allocation is served
  780. * from the same dynamic area. Might sleep. Might trigger writeouts.
  781. *
  782. * CONTEXT:
  783. * Does GFP_KERNEL allocation.
  784. *
  785. * RETURNS:
  786. * Percpu pointer to the allocated area on success, NULL on failure.
  787. */
  788. void *__alloc_reserved_percpu(size_t size, size_t align)
  789. {
  790. return pcpu_alloc(size, align, true);
  791. }
  792. /**
  793. * pcpu_reclaim - reclaim fully free chunks, workqueue function
  794. * @work: unused
  795. *
  796. * Reclaim all fully free chunks except for the first one.
  797. *
  798. * CONTEXT:
  799. * workqueue context.
  800. */
  801. static void pcpu_reclaim(struct work_struct *work)
  802. {
  803. LIST_HEAD(todo);
  804. struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
  805. struct pcpu_chunk *chunk, *next;
  806. mutex_lock(&pcpu_alloc_mutex);
  807. spin_lock_irq(&pcpu_lock);
  808. list_for_each_entry_safe(chunk, next, head, list) {
  809. WARN_ON(chunk->immutable);
  810. /* spare the first one */
  811. if (chunk == list_first_entry(head, struct pcpu_chunk, list))
  812. continue;
  813. list_move(&chunk->list, &todo);
  814. }
  815. spin_unlock_irq(&pcpu_lock);
  816. mutex_unlock(&pcpu_alloc_mutex);
  817. list_for_each_entry_safe(chunk, next, &todo, list) {
  818. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
  819. free_pcpu_chunk(chunk);
  820. }
  821. }
  822. /**
  823. * free_percpu - free percpu area
  824. * @ptr: pointer to area to free
  825. *
  826. * Free percpu area @ptr.
  827. *
  828. * CONTEXT:
  829. * Can be called from atomic context.
  830. */
  831. void free_percpu(void *ptr)
  832. {
  833. void *addr = __pcpu_ptr_to_addr(ptr);
  834. struct pcpu_chunk *chunk;
  835. unsigned long flags;
  836. int off;
  837. if (!ptr)
  838. return;
  839. spin_lock_irqsave(&pcpu_lock, flags);
  840. chunk = pcpu_chunk_addr_search(addr);
  841. off = addr - chunk->vm->addr;
  842. pcpu_free_area(chunk, off);
  843. /* if there are more than one fully free chunks, wake up grim reaper */
  844. if (chunk->free_size == pcpu_unit_size) {
  845. struct pcpu_chunk *pos;
  846. list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
  847. if (pos != chunk) {
  848. schedule_work(&pcpu_reclaim_work);
  849. break;
  850. }
  851. }
  852. spin_unlock_irqrestore(&pcpu_lock, flags);
  853. }
  854. EXPORT_SYMBOL_GPL(free_percpu);
  855. /**
  856. * pcpu_setup_first_chunk - initialize the first percpu chunk
  857. * @get_page_fn: callback to fetch page pointer
  858. * @static_size: the size of static percpu area in bytes
  859. * @reserved_size: the size of reserved percpu area in bytes
  860. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  861. * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
  862. * @base_addr: mapped address, NULL for auto
  863. * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
  864. *
  865. * Initialize the first percpu chunk which contains the kernel static
  866. * perpcu area. This function is to be called from arch percpu area
  867. * setup path. The first two parameters are mandatory. The rest are
  868. * optional.
  869. *
  870. * @get_page_fn() should return pointer to percpu page given cpu
  871. * number and page number. It should at least return enough pages to
  872. * cover the static area. The returned pages for static area should
  873. * have been initialized with valid data. If @unit_size is specified,
  874. * it can also return pages after the static area. NULL return
  875. * indicates end of pages for the cpu. Note that @get_page_fn() must
  876. * return the same number of pages for all cpus.
  877. *
  878. * @reserved_size, if non-zero, specifies the amount of bytes to
  879. * reserve after the static area in the first chunk. This reserves
  880. * the first chunk such that it's available only through reserved
  881. * percpu allocation. This is primarily used to serve module percpu
  882. * static areas on architectures where the addressing model has
  883. * limited offset range for symbol relocations to guarantee module
  884. * percpu symbols fall inside the relocatable range.
  885. *
  886. * @dyn_size, if non-negative, determines the number of bytes
  887. * available for dynamic allocation in the first chunk. Specifying
  888. * non-negative value makes percpu leave alone the area beyond
  889. * @static_size + @reserved_size + @dyn_size.
  890. *
  891. * @unit_size, if non-negative, specifies unit size and must be
  892. * aligned to PAGE_SIZE and equal to or larger than @static_size +
  893. * @reserved_size + if non-negative, @dyn_size.
  894. *
  895. * Non-null @base_addr means that the caller already allocated virtual
  896. * region for the first chunk and mapped it. percpu must not mess
  897. * with the chunk. Note that @base_addr with 0 @unit_size or non-NULL
  898. * @populate_pte_fn doesn't make any sense.
  899. *
  900. * @populate_pte_fn is used to populate the pagetable. NULL means the
  901. * caller already populated the pagetable.
  902. *
  903. * If the first chunk ends up with both reserved and dynamic areas, it
  904. * is served by two chunks - one to serve the core static and reserved
  905. * areas and the other for the dynamic area. They share the same vm
  906. * and page map but uses different area allocation map to stay away
  907. * from each other. The latter chunk is circulated in the chunk slots
  908. * and available for dynamic allocation like any other chunks.
  909. *
  910. * RETURNS:
  911. * The determined pcpu_unit_size which can be used to initialize
  912. * percpu access.
  913. */
  914. size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
  915. size_t static_size, size_t reserved_size,
  916. ssize_t dyn_size, ssize_t unit_size,
  917. void *base_addr,
  918. pcpu_populate_pte_fn_t populate_pte_fn)
  919. {
  920. static struct vm_struct first_vm;
  921. static int smap[2], dmap[2];
  922. size_t size_sum = static_size + reserved_size +
  923. (dyn_size >= 0 ? dyn_size : 0);
  924. struct pcpu_chunk *schunk, *dchunk = NULL;
  925. unsigned int cpu;
  926. int nr_pages;
  927. int err, i;
  928. /* santiy checks */
  929. BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
  930. ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
  931. BUG_ON(!static_size);
  932. if (unit_size >= 0) {
  933. BUG_ON(unit_size < size_sum);
  934. BUG_ON(unit_size & ~PAGE_MASK);
  935. BUG_ON(unit_size < PCPU_MIN_UNIT_SIZE);
  936. } else
  937. BUG_ON(base_addr);
  938. BUG_ON(base_addr && populate_pte_fn);
  939. if (unit_size >= 0)
  940. pcpu_unit_pages = unit_size >> PAGE_SHIFT;
  941. else
  942. pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
  943. PFN_UP(size_sum));
  944. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  945. pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
  946. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
  947. + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
  948. if (dyn_size < 0)
  949. dyn_size = pcpu_unit_size - static_size - reserved_size;
  950. /*
  951. * Allocate chunk slots. The additional last slot is for
  952. * empty chunks.
  953. */
  954. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  955. pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
  956. for (i = 0; i < pcpu_nr_slots; i++)
  957. INIT_LIST_HEAD(&pcpu_slot[i]);
  958. /*
  959. * Initialize static chunk. If reserved_size is zero, the
  960. * static chunk covers static area + dynamic allocation area
  961. * in the first chunk. If reserved_size is not zero, it
  962. * covers static area + reserved area (mostly used for module
  963. * static percpu allocation).
  964. */
  965. schunk = alloc_bootmem(pcpu_chunk_struct_size);
  966. INIT_LIST_HEAD(&schunk->list);
  967. schunk->vm = &first_vm;
  968. schunk->map = smap;
  969. schunk->map_alloc = ARRAY_SIZE(smap);
  970. schunk->page = schunk->page_ar;
  971. if (reserved_size) {
  972. schunk->free_size = reserved_size;
  973. pcpu_reserved_chunk = schunk;
  974. pcpu_reserved_chunk_limit = static_size + reserved_size;
  975. } else {
  976. schunk->free_size = dyn_size;
  977. dyn_size = 0; /* dynamic area covered */
  978. }
  979. schunk->contig_hint = schunk->free_size;
  980. schunk->map[schunk->map_used++] = -static_size;
  981. if (schunk->free_size)
  982. schunk->map[schunk->map_used++] = schunk->free_size;
  983. /* init dynamic chunk if necessary */
  984. if (dyn_size) {
  985. dchunk = alloc_bootmem(sizeof(struct pcpu_chunk));
  986. INIT_LIST_HEAD(&dchunk->list);
  987. dchunk->vm = &first_vm;
  988. dchunk->map = dmap;
  989. dchunk->map_alloc = ARRAY_SIZE(dmap);
  990. dchunk->page = schunk->page_ar; /* share page map with schunk */
  991. dchunk->contig_hint = dchunk->free_size = dyn_size;
  992. dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
  993. dchunk->map[dchunk->map_used++] = dchunk->free_size;
  994. }
  995. /* allocate vm address */
  996. first_vm.flags = VM_ALLOC;
  997. first_vm.size = pcpu_chunk_size;
  998. if (!base_addr)
  999. vm_area_register_early(&first_vm, PAGE_SIZE);
  1000. else {
  1001. /*
  1002. * Pages already mapped. No need to remap into
  1003. * vmalloc area. In this case the first chunks can't
  1004. * be mapped or unmapped by percpu and are marked
  1005. * immutable.
  1006. */
  1007. first_vm.addr = base_addr;
  1008. schunk->immutable = true;
  1009. if (dchunk)
  1010. dchunk->immutable = true;
  1011. }
  1012. /* assign pages */
  1013. nr_pages = -1;
  1014. for_each_possible_cpu(cpu) {
  1015. for (i = 0; i < pcpu_unit_pages; i++) {
  1016. struct page *page = get_page_fn(cpu, i);
  1017. if (!page)
  1018. break;
  1019. *pcpu_chunk_pagep(schunk, cpu, i) = page;
  1020. }
  1021. BUG_ON(i < PFN_UP(static_size));
  1022. if (nr_pages < 0)
  1023. nr_pages = i;
  1024. else
  1025. BUG_ON(nr_pages != i);
  1026. }
  1027. /* map them */
  1028. if (populate_pte_fn) {
  1029. for_each_possible_cpu(cpu)
  1030. for (i = 0; i < nr_pages; i++)
  1031. populate_pte_fn(pcpu_chunk_addr(schunk,
  1032. cpu, i));
  1033. err = pcpu_map(schunk, 0, nr_pages);
  1034. if (err)
  1035. panic("failed to setup static percpu area, err=%d\n",
  1036. err);
  1037. }
  1038. /* link the first chunk in */
  1039. pcpu_first_chunk = dchunk ?: schunk;
  1040. pcpu_chunk_relocate(pcpu_first_chunk, -1);
  1041. /* we're done */
  1042. pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
  1043. return pcpu_unit_size;
  1044. }
  1045. /*
  1046. * Embedding first chunk setup helper.
  1047. */
  1048. static void *pcpue_ptr __initdata;
  1049. static size_t pcpue_size __initdata;
  1050. static size_t pcpue_unit_size __initdata;
  1051. static struct page * __init pcpue_get_page(unsigned int cpu, int pageno)
  1052. {
  1053. size_t off = (size_t)pageno << PAGE_SHIFT;
  1054. if (off >= pcpue_size)
  1055. return NULL;
  1056. return virt_to_page(pcpue_ptr + cpu * pcpue_unit_size + off);
  1057. }
  1058. /**
  1059. * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
  1060. * @static_size: the size of static percpu area in bytes
  1061. * @reserved_size: the size of reserved percpu area in bytes
  1062. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  1063. * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
  1064. *
  1065. * This is a helper to ease setting up embedded first percpu chunk and
  1066. * can be called where pcpu_setup_first_chunk() is expected.
  1067. *
  1068. * If this function is used to setup the first chunk, it is allocated
  1069. * as a contiguous area using bootmem allocator and used as-is without
  1070. * being mapped into vmalloc area. This enables the first chunk to
  1071. * piggy back on the linear physical mapping which often uses larger
  1072. * page size.
  1073. *
  1074. * When @dyn_size is positive, dynamic area might be larger than
  1075. * specified to fill page alignment. Also, when @dyn_size is auto,
  1076. * @dyn_size does not fill the whole first chunk but only what's
  1077. * necessary for page alignment after static and reserved areas.
  1078. *
  1079. * If the needed size is smaller than the minimum or specified unit
  1080. * size, the leftover is returned to the bootmem allocator.
  1081. *
  1082. * RETURNS:
  1083. * The determined pcpu_unit_size which can be used to initialize
  1084. * percpu access on success, -errno on failure.
  1085. */
  1086. ssize_t __init pcpu_embed_first_chunk(size_t static_size, size_t reserved_size,
  1087. ssize_t dyn_size, ssize_t unit_size)
  1088. {
  1089. unsigned int cpu;
  1090. /* determine parameters and allocate */
  1091. pcpue_size = PFN_ALIGN(static_size + reserved_size +
  1092. (dyn_size >= 0 ? dyn_size : 0));
  1093. if (dyn_size != 0)
  1094. dyn_size = pcpue_size - static_size - reserved_size;
  1095. if (unit_size >= 0) {
  1096. BUG_ON(unit_size < pcpue_size);
  1097. pcpue_unit_size = unit_size;
  1098. } else
  1099. pcpue_unit_size = max_t(size_t, pcpue_size, PCPU_MIN_UNIT_SIZE);
  1100. pcpue_ptr = __alloc_bootmem_nopanic(
  1101. num_possible_cpus() * pcpue_unit_size,
  1102. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  1103. if (!pcpue_ptr)
  1104. return -ENOMEM;
  1105. /* return the leftover and copy */
  1106. for_each_possible_cpu(cpu) {
  1107. void *ptr = pcpue_ptr + cpu * pcpue_unit_size;
  1108. free_bootmem(__pa(ptr + pcpue_size),
  1109. pcpue_unit_size - pcpue_size);
  1110. memcpy(ptr, __per_cpu_load, static_size);
  1111. }
  1112. /* we're ready, commit */
  1113. pr_info("PERCPU: Embedded %zu pages at %p, static data %zu bytes\n",
  1114. pcpue_size >> PAGE_SHIFT, pcpue_ptr, static_size);
  1115. return pcpu_setup_first_chunk(pcpue_get_page, static_size,
  1116. reserved_size, dyn_size,
  1117. pcpue_unit_size, pcpue_ptr, NULL);
  1118. }