percpu.c 37 KB

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