percpu.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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 boot-time determined number of units and the
  12. * first chunk is used for static percpu variables in the kernel image
  13. * (special boot time alloc/init handling necessary as these areas
  14. * need to be brought up before allocation services are running).
  15. * Unit grows as necessary and all units grow or shrink in unison.
  16. * When a chunk is filled up, another chunk is allocated. ie. in
  17. * vmalloc area
  18. *
  19. * c0 c1 c2
  20. * ------------------- ------------------- ------------
  21. * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u
  22. * ------------------- ...... ------------------- .... ------------
  23. *
  24. * Allocation is done in offset-size areas of single unit space. Ie,
  25. * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0,
  26. * c1:u1, c1:u2 and c1:u3. On UMA, units corresponds directly to
  27. * cpus. On NUMA, the mapping can be non-linear and even sparse.
  28. * Percpu access can be done by configuring percpu base registers
  29. * according to cpu to unit mapping and pcpu_unit_size.
  30. *
  31. * There are usually many small percpu allocations many of them being
  32. * as small as 4 bytes. The allocator organizes chunks into lists
  33. * according to free size and tries to allocate from the fullest one.
  34. * Each chunk keeps the maximum contiguous area size hint which is
  35. * guaranteed to be eqaul to or larger than the maximum contiguous
  36. * area in the chunk. This helps the allocator not to iterate the
  37. * chunk maps unnecessarily.
  38. *
  39. * Allocation state in each chunk is kept using an array of integers
  40. * on chunk->map. A positive value in the map represents a free
  41. * region and negative allocated. Allocation inside a chunk is done
  42. * by scanning this map sequentially and serving the first matching
  43. * entry. This is mostly copied from the percpu_modalloc() allocator.
  44. * Chunks can be determined from the address using the index field
  45. * in the page struct. The index field contains a pointer to the chunk.
  46. *
  47. * To use this allocator, arch code should do the followings.
  48. *
  49. * - drop CONFIG_HAVE_LEGACY_PER_CPU_AREA
  50. *
  51. * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
  52. * regular address to percpu pointer and back if they need to be
  53. * different from the default
  54. *
  55. * - use pcpu_setup_first_chunk() during percpu area initialization to
  56. * setup the first chunk containing the kernel static percpu area
  57. */
  58. #include <linux/bitmap.h>
  59. #include <linux/bootmem.h>
  60. #include <linux/list.h>
  61. #include <linux/mm.h>
  62. #include <linux/module.h>
  63. #include <linux/mutex.h>
  64. #include <linux/percpu.h>
  65. #include <linux/pfn.h>
  66. #include <linux/slab.h>
  67. #include <linux/spinlock.h>
  68. #include <linux/vmalloc.h>
  69. #include <linux/workqueue.h>
  70. #include <asm/cacheflush.h>
  71. #include <asm/sections.h>
  72. #include <asm/tlbflush.h>
  73. #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
  74. #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
  75. /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */
  76. #ifndef __addr_to_pcpu_ptr
  77. #define __addr_to_pcpu_ptr(addr) \
  78. (void *)((unsigned long)(addr) - (unsigned long)pcpu_base_addr \
  79. + (unsigned long)__per_cpu_start)
  80. #endif
  81. #ifndef __pcpu_ptr_to_addr
  82. #define __pcpu_ptr_to_addr(ptr) \
  83. (void *)((unsigned long)(ptr) + (unsigned long)pcpu_base_addr \
  84. - (unsigned long)__per_cpu_start)
  85. #endif
  86. struct pcpu_chunk {
  87. struct list_head list; /* linked to pcpu_slot lists */
  88. int free_size; /* free bytes in the chunk */
  89. int contig_hint; /* max contiguous size hint */
  90. struct vm_struct *vm; /* mapped vmalloc region */
  91. int map_used; /* # of map entries used */
  92. int map_alloc; /* # of map entries allocated */
  93. int *map; /* allocation map */
  94. bool immutable; /* no [de]population allowed */
  95. unsigned long populated[]; /* populated bitmap */
  96. };
  97. static int pcpu_unit_pages __read_mostly;
  98. static int pcpu_unit_size __read_mostly;
  99. static int pcpu_nr_units __read_mostly;
  100. static int pcpu_chunk_size __read_mostly;
  101. static int pcpu_nr_slots __read_mostly;
  102. static size_t pcpu_chunk_struct_size __read_mostly;
  103. /* cpus with the lowest and highest unit numbers */
  104. static unsigned int pcpu_first_unit_cpu __read_mostly;
  105. static unsigned int pcpu_last_unit_cpu __read_mostly;
  106. /* the address of the first chunk which starts with the kernel static area */
  107. void *pcpu_base_addr __read_mostly;
  108. EXPORT_SYMBOL_GPL(pcpu_base_addr);
  109. /* cpu -> unit map */
  110. const int *pcpu_unit_map __read_mostly;
  111. /*
  112. * The first chunk which always exists. Note that unlike other
  113. * chunks, this one can be allocated and mapped in several different
  114. * ways and thus often doesn't live in the vmalloc area.
  115. */
  116. static struct pcpu_chunk *pcpu_first_chunk;
  117. /*
  118. * Optional reserved chunk. This chunk reserves part of the first
  119. * chunk and serves it for reserved allocations. The amount of
  120. * reserved offset is in pcpu_reserved_chunk_limit. When reserved
  121. * area doesn't exist, the following variables contain NULL and 0
  122. * respectively.
  123. */
  124. static struct pcpu_chunk *pcpu_reserved_chunk;
  125. static int pcpu_reserved_chunk_limit;
  126. /*
  127. * Synchronization rules.
  128. *
  129. * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former
  130. * protects allocation/reclaim paths, chunks, populated bitmap and
  131. * vmalloc mapping. The latter is a spinlock and protects the index
  132. * data structures - chunk slots, chunks and area maps in chunks.
  133. *
  134. * During allocation, pcpu_alloc_mutex is kept locked all the time and
  135. * pcpu_lock is grabbed and released as necessary. All actual memory
  136. * allocations are done using GFP_KERNEL with pcpu_lock released.
  137. *
  138. * Free path accesses and alters only the index data structures, so it
  139. * can be safely called from atomic context. When memory needs to be
  140. * returned to the system, free path schedules reclaim_work which
  141. * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be
  142. * reclaimed, release both locks and frees the chunks. Note that it's
  143. * necessary to grab both locks to remove a chunk from circulation as
  144. * allocation path might be referencing the chunk with only
  145. * pcpu_alloc_mutex locked.
  146. */
  147. static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */
  148. static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */
  149. static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
  150. /* reclaim work to release fully free chunks, scheduled from free path */
  151. static void pcpu_reclaim(struct work_struct *work);
  152. static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
  153. static int __pcpu_size_to_slot(int size)
  154. {
  155. int highbit = fls(size); /* size is in bytes */
  156. return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
  157. }
  158. static int pcpu_size_to_slot(int size)
  159. {
  160. if (size == pcpu_unit_size)
  161. return pcpu_nr_slots - 1;
  162. return __pcpu_size_to_slot(size);
  163. }
  164. static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
  165. {
  166. if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
  167. return 0;
  168. return pcpu_size_to_slot(chunk->free_size);
  169. }
  170. static int pcpu_page_idx(unsigned int cpu, int page_idx)
  171. {
  172. return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx;
  173. }
  174. static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
  175. unsigned int cpu, int page_idx)
  176. {
  177. return (unsigned long)chunk->vm->addr +
  178. (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
  179. }
  180. static struct page *pcpu_chunk_page(struct pcpu_chunk *chunk,
  181. unsigned int cpu, int page_idx)
  182. {
  183. /* must not be used on pre-mapped chunk */
  184. WARN_ON(chunk->immutable);
  185. return vmalloc_to_page((void *)pcpu_chunk_addr(chunk, cpu, page_idx));
  186. }
  187. /* set the pointer to a chunk in a page struct */
  188. static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu)
  189. {
  190. page->index = (unsigned long)pcpu;
  191. }
  192. /* obtain pointer to a chunk from a page struct */
  193. static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page)
  194. {
  195. return (struct pcpu_chunk *)page->index;
  196. }
  197. static void pcpu_next_unpop(struct pcpu_chunk *chunk, int *rs, int *re, int end)
  198. {
  199. *rs = find_next_zero_bit(chunk->populated, end, *rs);
  200. *re = find_next_bit(chunk->populated, end, *rs + 1);
  201. }
  202. static void pcpu_next_pop(struct pcpu_chunk *chunk, int *rs, int *re, int end)
  203. {
  204. *rs = find_next_bit(chunk->populated, end, *rs);
  205. *re = find_next_zero_bit(chunk->populated, end, *rs + 1);
  206. }
  207. /*
  208. * (Un)populated page region iterators. Iterate over (un)populated
  209. * page regions betwen @start and @end in @chunk. @rs and @re should
  210. * be integer variables and will be set to start and end page index of
  211. * the current region.
  212. */
  213. #define pcpu_for_each_unpop_region(chunk, rs, re, start, end) \
  214. for ((rs) = (start), pcpu_next_unpop((chunk), &(rs), &(re), (end)); \
  215. (rs) < (re); \
  216. (rs) = (re) + 1, pcpu_next_unpop((chunk), &(rs), &(re), (end)))
  217. #define pcpu_for_each_pop_region(chunk, rs, re, start, end) \
  218. for ((rs) = (start), pcpu_next_pop((chunk), &(rs), &(re), (end)); \
  219. (rs) < (re); \
  220. (rs) = (re) + 1, pcpu_next_pop((chunk), &(rs), &(re), (end)))
  221. /**
  222. * pcpu_mem_alloc - allocate memory
  223. * @size: bytes to allocate
  224. *
  225. * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
  226. * kzalloc() is used; otherwise, vmalloc() is used. The returned
  227. * memory is always zeroed.
  228. *
  229. * CONTEXT:
  230. * Does GFP_KERNEL allocation.
  231. *
  232. * RETURNS:
  233. * Pointer to the allocated area on success, NULL on failure.
  234. */
  235. static void *pcpu_mem_alloc(size_t size)
  236. {
  237. if (size <= PAGE_SIZE)
  238. return kzalloc(size, GFP_KERNEL);
  239. else {
  240. void *ptr = vmalloc(size);
  241. if (ptr)
  242. memset(ptr, 0, size);
  243. return ptr;
  244. }
  245. }
  246. /**
  247. * pcpu_mem_free - free memory
  248. * @ptr: memory to free
  249. * @size: size of the area
  250. *
  251. * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc().
  252. */
  253. static void pcpu_mem_free(void *ptr, size_t size)
  254. {
  255. if (size <= PAGE_SIZE)
  256. kfree(ptr);
  257. else
  258. vfree(ptr);
  259. }
  260. /**
  261. * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
  262. * @chunk: chunk of interest
  263. * @oslot: the previous slot it was on
  264. *
  265. * This function is called after an allocation or free changed @chunk.
  266. * New slot according to the changed state is determined and @chunk is
  267. * moved to the slot. Note that the reserved chunk is never put on
  268. * chunk slots.
  269. *
  270. * CONTEXT:
  271. * pcpu_lock.
  272. */
  273. static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
  274. {
  275. int nslot = pcpu_chunk_slot(chunk);
  276. if (chunk != pcpu_reserved_chunk && oslot != nslot) {
  277. if (oslot < nslot)
  278. list_move(&chunk->list, &pcpu_slot[nslot]);
  279. else
  280. list_move_tail(&chunk->list, &pcpu_slot[nslot]);
  281. }
  282. }
  283. /**
  284. * pcpu_chunk_addr_search - determine chunk containing specified address
  285. * @addr: address for which the chunk needs to be determined.
  286. *
  287. * RETURNS:
  288. * The address of the found chunk.
  289. */
  290. static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
  291. {
  292. void *first_start = pcpu_first_chunk->vm->addr;
  293. /* is it in the first chunk? */
  294. if (addr >= first_start && addr < first_start + pcpu_unit_size) {
  295. /* is it in the reserved area? */
  296. if (addr < first_start + pcpu_reserved_chunk_limit)
  297. return pcpu_reserved_chunk;
  298. return pcpu_first_chunk;
  299. }
  300. /*
  301. * The address is relative to unit0 which might be unused and
  302. * thus unmapped. Offset the address to the unit space of the
  303. * current processor before looking it up in the vmalloc
  304. * space. Note that any possible cpu id can be used here, so
  305. * there's no need to worry about preemption or cpu hotplug.
  306. */
  307. addr += pcpu_unit_map[smp_processor_id()] * pcpu_unit_size;
  308. return pcpu_get_page_chunk(vmalloc_to_page(addr));
  309. }
  310. /**
  311. * pcpu_extend_area_map - extend area map for allocation
  312. * @chunk: target chunk
  313. *
  314. * Extend area map of @chunk so that it can accomodate an allocation.
  315. * A single allocation can split an area into three areas, so this
  316. * function makes sure that @chunk->map has at least two extra slots.
  317. *
  318. * CONTEXT:
  319. * pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired
  320. * if area map is extended.
  321. *
  322. * RETURNS:
  323. * 0 if noop, 1 if successfully extended, -errno on failure.
  324. */
  325. static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
  326. {
  327. int new_alloc;
  328. int *new;
  329. size_t size;
  330. /* has enough? */
  331. if (chunk->map_alloc >= chunk->map_used + 2)
  332. return 0;
  333. spin_unlock_irq(&pcpu_lock);
  334. new_alloc = PCPU_DFL_MAP_ALLOC;
  335. while (new_alloc < chunk->map_used + 2)
  336. new_alloc *= 2;
  337. new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
  338. if (!new) {
  339. spin_lock_irq(&pcpu_lock);
  340. return -ENOMEM;
  341. }
  342. /*
  343. * Acquire pcpu_lock and switch to new area map. Only free
  344. * could have happened inbetween, so map_used couldn't have
  345. * grown.
  346. */
  347. spin_lock_irq(&pcpu_lock);
  348. BUG_ON(new_alloc < chunk->map_used + 2);
  349. size = chunk->map_alloc * sizeof(chunk->map[0]);
  350. memcpy(new, chunk->map, size);
  351. /*
  352. * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
  353. * one of the first chunks and still using static map.
  354. */
  355. if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
  356. pcpu_mem_free(chunk->map, size);
  357. chunk->map_alloc = new_alloc;
  358. chunk->map = new;
  359. return 0;
  360. }
  361. /**
  362. * pcpu_split_block - split a map block
  363. * @chunk: chunk of interest
  364. * @i: index of map block to split
  365. * @head: head size in bytes (can be 0)
  366. * @tail: tail size in bytes (can be 0)
  367. *
  368. * Split the @i'th map block into two or three blocks. If @head is
  369. * non-zero, @head bytes block is inserted before block @i moving it
  370. * to @i+1 and reducing its size by @head bytes.
  371. *
  372. * If @tail is non-zero, the target block, which can be @i or @i+1
  373. * depending on @head, is reduced by @tail bytes and @tail byte block
  374. * is inserted after the target block.
  375. *
  376. * @chunk->map must have enough free slots to accomodate the split.
  377. *
  378. * CONTEXT:
  379. * pcpu_lock.
  380. */
  381. static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
  382. int head, int tail)
  383. {
  384. int nr_extra = !!head + !!tail;
  385. BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
  386. /* insert new subblocks */
  387. memmove(&chunk->map[i + nr_extra], &chunk->map[i],
  388. sizeof(chunk->map[0]) * (chunk->map_used - i));
  389. chunk->map_used += nr_extra;
  390. if (head) {
  391. chunk->map[i + 1] = chunk->map[i] - head;
  392. chunk->map[i++] = head;
  393. }
  394. if (tail) {
  395. chunk->map[i++] -= tail;
  396. chunk->map[i] = tail;
  397. }
  398. }
  399. /**
  400. * pcpu_alloc_area - allocate area from a pcpu_chunk
  401. * @chunk: chunk of interest
  402. * @size: wanted size in bytes
  403. * @align: wanted align
  404. *
  405. * Try to allocate @size bytes area aligned at @align from @chunk.
  406. * Note that this function only allocates the offset. It doesn't
  407. * populate or map the area.
  408. *
  409. * @chunk->map must have at least two free slots.
  410. *
  411. * CONTEXT:
  412. * pcpu_lock.
  413. *
  414. * RETURNS:
  415. * Allocated offset in @chunk on success, -1 if no matching area is
  416. * found.
  417. */
  418. static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
  419. {
  420. int oslot = pcpu_chunk_slot(chunk);
  421. int max_contig = 0;
  422. int i, off;
  423. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
  424. bool is_last = i + 1 == chunk->map_used;
  425. int head, tail;
  426. /* extra for alignment requirement */
  427. head = ALIGN(off, align) - off;
  428. BUG_ON(i == 0 && head != 0);
  429. if (chunk->map[i] < 0)
  430. continue;
  431. if (chunk->map[i] < head + size) {
  432. max_contig = max(chunk->map[i], max_contig);
  433. continue;
  434. }
  435. /*
  436. * If head is small or the previous block is free,
  437. * merge'em. Note that 'small' is defined as smaller
  438. * than sizeof(int), which is very small but isn't too
  439. * uncommon for percpu allocations.
  440. */
  441. if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
  442. if (chunk->map[i - 1] > 0)
  443. chunk->map[i - 1] += head;
  444. else {
  445. chunk->map[i - 1] -= head;
  446. chunk->free_size -= head;
  447. }
  448. chunk->map[i] -= head;
  449. off += head;
  450. head = 0;
  451. }
  452. /* if tail is small, just keep it around */
  453. tail = chunk->map[i] - head - size;
  454. if (tail < sizeof(int))
  455. tail = 0;
  456. /* split if warranted */
  457. if (head || tail) {
  458. pcpu_split_block(chunk, i, head, tail);
  459. if (head) {
  460. i++;
  461. off += head;
  462. max_contig = max(chunk->map[i - 1], max_contig);
  463. }
  464. if (tail)
  465. max_contig = max(chunk->map[i + 1], max_contig);
  466. }
  467. /* update hint and mark allocated */
  468. if (is_last)
  469. chunk->contig_hint = max_contig; /* fully scanned */
  470. else
  471. chunk->contig_hint = max(chunk->contig_hint,
  472. max_contig);
  473. chunk->free_size -= chunk->map[i];
  474. chunk->map[i] = -chunk->map[i];
  475. pcpu_chunk_relocate(chunk, oslot);
  476. return off;
  477. }
  478. chunk->contig_hint = max_contig; /* fully scanned */
  479. pcpu_chunk_relocate(chunk, oslot);
  480. /* tell the upper layer that this chunk has no matching area */
  481. return -1;
  482. }
  483. /**
  484. * pcpu_free_area - free area to a pcpu_chunk
  485. * @chunk: chunk of interest
  486. * @freeme: offset of area to free
  487. *
  488. * Free area starting from @freeme to @chunk. Note that this function
  489. * only modifies the allocation map. It doesn't depopulate or unmap
  490. * the area.
  491. *
  492. * CONTEXT:
  493. * pcpu_lock.
  494. */
  495. static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
  496. {
  497. int oslot = pcpu_chunk_slot(chunk);
  498. int i, off;
  499. for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
  500. if (off == freeme)
  501. break;
  502. BUG_ON(off != freeme);
  503. BUG_ON(chunk->map[i] > 0);
  504. chunk->map[i] = -chunk->map[i];
  505. chunk->free_size += chunk->map[i];
  506. /* merge with previous? */
  507. if (i > 0 && chunk->map[i - 1] >= 0) {
  508. chunk->map[i - 1] += chunk->map[i];
  509. chunk->map_used--;
  510. memmove(&chunk->map[i], &chunk->map[i + 1],
  511. (chunk->map_used - i) * sizeof(chunk->map[0]));
  512. i--;
  513. }
  514. /* merge with next? */
  515. if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
  516. chunk->map[i] += chunk->map[i + 1];
  517. chunk->map_used--;
  518. memmove(&chunk->map[i + 1], &chunk->map[i + 2],
  519. (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
  520. }
  521. chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
  522. pcpu_chunk_relocate(chunk, oslot);
  523. }
  524. /**
  525. * pcpu_get_pages_and_bitmap - get temp pages array and bitmap
  526. * @chunk: chunk of interest
  527. * @bitmapp: output parameter for bitmap
  528. * @may_alloc: may allocate the array
  529. *
  530. * Returns pointer to array of pointers to struct page and bitmap,
  531. * both of which can be indexed with pcpu_page_idx(). The returned
  532. * array is cleared to zero and *@bitmapp is copied from
  533. * @chunk->populated. Note that there is only one array and bitmap
  534. * and access exclusion is the caller's responsibility.
  535. *
  536. * CONTEXT:
  537. * pcpu_alloc_mutex and does GFP_KERNEL allocation if @may_alloc.
  538. * Otherwise, don't care.
  539. *
  540. * RETURNS:
  541. * Pointer to temp pages array on success, NULL on failure.
  542. */
  543. static struct page **pcpu_get_pages_and_bitmap(struct pcpu_chunk *chunk,
  544. unsigned long **bitmapp,
  545. bool may_alloc)
  546. {
  547. static struct page **pages;
  548. static unsigned long *bitmap;
  549. size_t pages_size = pcpu_nr_units * pcpu_unit_pages * sizeof(pages[0]);
  550. size_t bitmap_size = BITS_TO_LONGS(pcpu_unit_pages) *
  551. sizeof(unsigned long);
  552. if (!pages || !bitmap) {
  553. if (may_alloc && !pages)
  554. pages = pcpu_mem_alloc(pages_size);
  555. if (may_alloc && !bitmap)
  556. bitmap = pcpu_mem_alloc(bitmap_size);
  557. if (!pages || !bitmap)
  558. return NULL;
  559. }
  560. memset(pages, 0, pages_size);
  561. bitmap_copy(bitmap, chunk->populated, pcpu_unit_pages);
  562. *bitmapp = bitmap;
  563. return pages;
  564. }
  565. /**
  566. * pcpu_free_pages - free pages which were allocated for @chunk
  567. * @chunk: chunk pages were allocated for
  568. * @pages: array of pages to be freed, indexed by pcpu_page_idx()
  569. * @populated: populated bitmap
  570. * @page_start: page index of the first page to be freed
  571. * @page_end: page index of the last page to be freed + 1
  572. *
  573. * Free pages [@page_start and @page_end) in @pages for all units.
  574. * The pages were allocated for @chunk.
  575. */
  576. static void pcpu_free_pages(struct pcpu_chunk *chunk,
  577. struct page **pages, unsigned long *populated,
  578. int page_start, int page_end)
  579. {
  580. unsigned int cpu;
  581. int i;
  582. for_each_possible_cpu(cpu) {
  583. for (i = page_start; i < page_end; i++) {
  584. struct page *page = pages[pcpu_page_idx(cpu, i)];
  585. if (page)
  586. __free_page(page);
  587. }
  588. }
  589. }
  590. /**
  591. * pcpu_alloc_pages - allocates pages for @chunk
  592. * @chunk: target chunk
  593. * @pages: array to put the allocated pages into, indexed by pcpu_page_idx()
  594. * @populated: populated bitmap
  595. * @page_start: page index of the first page to be allocated
  596. * @page_end: page index of the last page to be allocated + 1
  597. *
  598. * Allocate pages [@page_start,@page_end) into @pages for all units.
  599. * The allocation is for @chunk. Percpu core doesn't care about the
  600. * content of @pages and will pass it verbatim to pcpu_map_pages().
  601. */
  602. static int pcpu_alloc_pages(struct pcpu_chunk *chunk,
  603. struct page **pages, unsigned long *populated,
  604. int page_start, int page_end)
  605. {
  606. const gfp_t gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
  607. unsigned int cpu;
  608. int i;
  609. for_each_possible_cpu(cpu) {
  610. for (i = page_start; i < page_end; i++) {
  611. struct page **pagep = &pages[pcpu_page_idx(cpu, i)];
  612. *pagep = alloc_pages_node(cpu_to_node(cpu), gfp, 0);
  613. if (!*pagep) {
  614. pcpu_free_pages(chunk, pages, populated,
  615. page_start, page_end);
  616. return -ENOMEM;
  617. }
  618. }
  619. }
  620. return 0;
  621. }
  622. /**
  623. * pcpu_pre_unmap_flush - flush cache prior to unmapping
  624. * @chunk: chunk the regions to be flushed belongs to
  625. * @page_start: page index of the first page to be flushed
  626. * @page_end: page index of the last page to be flushed + 1
  627. *
  628. * Pages in [@page_start,@page_end) of @chunk are about to be
  629. * unmapped. Flush cache. As each flushing trial can be very
  630. * expensive, issue flush on the whole region at once rather than
  631. * doing it for each cpu. This could be an overkill but is more
  632. * scalable.
  633. */
  634. static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk,
  635. int page_start, int page_end)
  636. {
  637. flush_cache_vunmap(
  638. pcpu_chunk_addr(chunk, pcpu_first_unit_cpu, page_start),
  639. pcpu_chunk_addr(chunk, pcpu_last_unit_cpu, page_end));
  640. }
  641. static void __pcpu_unmap_pages(unsigned long addr, int nr_pages)
  642. {
  643. unmap_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT);
  644. }
  645. /**
  646. * pcpu_unmap_pages - unmap pages out of a pcpu_chunk
  647. * @chunk: chunk of interest
  648. * @pages: pages array which can be used to pass information to free
  649. * @populated: populated bitmap
  650. * @page_start: page index of the first page to unmap
  651. * @page_end: page index of the last page to unmap + 1
  652. *
  653. * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
  654. * Corresponding elements in @pages were cleared by the caller and can
  655. * be used to carry information to pcpu_free_pages() which will be
  656. * called after all unmaps are finished. The caller should call
  657. * proper pre/post flush functions.
  658. */
  659. static void pcpu_unmap_pages(struct pcpu_chunk *chunk,
  660. struct page **pages, unsigned long *populated,
  661. int page_start, int page_end)
  662. {
  663. unsigned int cpu;
  664. int i;
  665. for_each_possible_cpu(cpu) {
  666. for (i = page_start; i < page_end; i++) {
  667. struct page *page;
  668. page = pcpu_chunk_page(chunk, cpu, i);
  669. WARN_ON(!page);
  670. pages[pcpu_page_idx(cpu, i)] = page;
  671. }
  672. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  673. page_end - page_start);
  674. }
  675. for (i = page_start; i < page_end; i++)
  676. __clear_bit(i, populated);
  677. }
  678. /**
  679. * pcpu_post_unmap_tlb_flush - flush TLB after unmapping
  680. * @chunk: pcpu_chunk the regions to be flushed belong to
  681. * @page_start: page index of the first page to be flushed
  682. * @page_end: page index of the last page to be flushed + 1
  683. *
  684. * Pages [@page_start,@page_end) of @chunk have been unmapped. Flush
  685. * TLB for the regions. This can be skipped if the area is to be
  686. * returned to vmalloc as vmalloc will handle TLB flushing lazily.
  687. *
  688. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  689. * for the whole region.
  690. */
  691. static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk,
  692. int page_start, int page_end)
  693. {
  694. flush_tlb_kernel_range(
  695. pcpu_chunk_addr(chunk, pcpu_first_unit_cpu, page_start),
  696. pcpu_chunk_addr(chunk, pcpu_last_unit_cpu, page_end));
  697. }
  698. static int __pcpu_map_pages(unsigned long addr, struct page **pages,
  699. int nr_pages)
  700. {
  701. return map_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT,
  702. PAGE_KERNEL, pages);
  703. }
  704. /**
  705. * pcpu_map_pages - map pages into a pcpu_chunk
  706. * @chunk: chunk of interest
  707. * @pages: pages array containing pages to be mapped
  708. * @populated: populated bitmap
  709. * @page_start: page index of the first page to map
  710. * @page_end: page index of the last page to map + 1
  711. *
  712. * For each cpu, map pages [@page_start,@page_end) into @chunk. The
  713. * caller is responsible for calling pcpu_post_map_flush() after all
  714. * mappings are complete.
  715. *
  716. * This function is responsible for setting corresponding bits in
  717. * @chunk->populated bitmap and whatever is necessary for reverse
  718. * lookup (addr -> chunk).
  719. */
  720. static int pcpu_map_pages(struct pcpu_chunk *chunk,
  721. struct page **pages, unsigned long *populated,
  722. int page_start, int page_end)
  723. {
  724. unsigned int cpu, tcpu;
  725. int i, err;
  726. for_each_possible_cpu(cpu) {
  727. err = __pcpu_map_pages(pcpu_chunk_addr(chunk, cpu, page_start),
  728. &pages[pcpu_page_idx(cpu, page_start)],
  729. page_end - page_start);
  730. if (err < 0)
  731. goto err;
  732. }
  733. /* mapping successful, link chunk and mark populated */
  734. for (i = page_start; i < page_end; i++) {
  735. for_each_possible_cpu(cpu)
  736. pcpu_set_page_chunk(pages[pcpu_page_idx(cpu, i)],
  737. chunk);
  738. __set_bit(i, populated);
  739. }
  740. return 0;
  741. err:
  742. for_each_possible_cpu(tcpu) {
  743. if (tcpu == cpu)
  744. break;
  745. __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
  746. page_end - page_start);
  747. }
  748. return err;
  749. }
  750. /**
  751. * pcpu_post_map_flush - flush cache after mapping
  752. * @chunk: pcpu_chunk the regions to be flushed belong to
  753. * @page_start: page index of the first page to be flushed
  754. * @page_end: page index of the last page to be flushed + 1
  755. *
  756. * Pages [@page_start,@page_end) of @chunk have been mapped. Flush
  757. * cache.
  758. *
  759. * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once
  760. * for the whole region.
  761. */
  762. static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
  763. int page_start, int page_end)
  764. {
  765. flush_cache_vmap(
  766. pcpu_chunk_addr(chunk, pcpu_first_unit_cpu, page_start),
  767. pcpu_chunk_addr(chunk, pcpu_last_unit_cpu, page_end));
  768. }
  769. /**
  770. * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
  771. * @chunk: chunk to depopulate
  772. * @off: offset to the area to depopulate
  773. * @size: size of the area to depopulate in bytes
  774. * @flush: whether to flush cache and tlb or not
  775. *
  776. * For each cpu, depopulate and unmap pages [@page_start,@page_end)
  777. * from @chunk. If @flush is true, vcache is flushed before unmapping
  778. * and tlb after.
  779. *
  780. * CONTEXT:
  781. * pcpu_alloc_mutex.
  782. */
  783. static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size)
  784. {
  785. int page_start = PFN_DOWN(off);
  786. int page_end = PFN_UP(off + size);
  787. struct page **pages;
  788. unsigned long *populated;
  789. int rs, re;
  790. /* quick path, check whether it's empty already */
  791. pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
  792. if (rs == page_start && re == page_end)
  793. return;
  794. break;
  795. }
  796. /* immutable chunks can't be depopulated */
  797. WARN_ON(chunk->immutable);
  798. /*
  799. * If control reaches here, there must have been at least one
  800. * successful population attempt so the temp pages array must
  801. * be available now.
  802. */
  803. pages = pcpu_get_pages_and_bitmap(chunk, &populated, false);
  804. BUG_ON(!pages);
  805. /* unmap and free */
  806. pcpu_pre_unmap_flush(chunk, page_start, page_end);
  807. pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
  808. pcpu_unmap_pages(chunk, pages, populated, rs, re);
  809. /* no need to flush tlb, vmalloc will handle it lazily */
  810. pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end)
  811. pcpu_free_pages(chunk, pages, populated, rs, re);
  812. /* commit new bitmap */
  813. bitmap_copy(chunk->populated, populated, pcpu_unit_pages);
  814. }
  815. /**
  816. * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
  817. * @chunk: chunk of interest
  818. * @off: offset to the area to populate
  819. * @size: size of the area to populate in bytes
  820. *
  821. * For each cpu, populate and map pages [@page_start,@page_end) into
  822. * @chunk. The area is cleared on return.
  823. *
  824. * CONTEXT:
  825. * pcpu_alloc_mutex, does GFP_KERNEL allocation.
  826. */
  827. static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
  828. {
  829. int page_start = PFN_DOWN(off);
  830. int page_end = PFN_UP(off + size);
  831. int free_end = page_start, unmap_end = page_start;
  832. struct page **pages;
  833. unsigned long *populated;
  834. unsigned int cpu;
  835. int rs, re, rc;
  836. /* quick path, check whether all pages are already there */
  837. pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end) {
  838. if (rs == page_start && re == page_end)
  839. goto clear;
  840. break;
  841. }
  842. /* need to allocate and map pages, this chunk can't be immutable */
  843. WARN_ON(chunk->immutable);
  844. pages = pcpu_get_pages_and_bitmap(chunk, &populated, true);
  845. if (!pages)
  846. return -ENOMEM;
  847. /* alloc and map */
  848. pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
  849. rc = pcpu_alloc_pages(chunk, pages, populated, rs, re);
  850. if (rc)
  851. goto err_free;
  852. free_end = re;
  853. }
  854. pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) {
  855. rc = pcpu_map_pages(chunk, pages, populated, rs, re);
  856. if (rc)
  857. goto err_unmap;
  858. unmap_end = re;
  859. }
  860. pcpu_post_map_flush(chunk, page_start, page_end);
  861. /* commit new bitmap */
  862. bitmap_copy(chunk->populated, populated, pcpu_unit_pages);
  863. clear:
  864. for_each_possible_cpu(cpu)
  865. memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
  866. return 0;
  867. err_unmap:
  868. pcpu_pre_unmap_flush(chunk, page_start, unmap_end);
  869. pcpu_for_each_unpop_region(chunk, rs, re, page_start, unmap_end)
  870. pcpu_unmap_pages(chunk, pages, populated, rs, re);
  871. pcpu_post_unmap_tlb_flush(chunk, page_start, unmap_end);
  872. err_free:
  873. pcpu_for_each_unpop_region(chunk, rs, re, page_start, free_end)
  874. pcpu_free_pages(chunk, pages, populated, rs, re);
  875. return rc;
  876. }
  877. static void free_pcpu_chunk(struct pcpu_chunk *chunk)
  878. {
  879. if (!chunk)
  880. return;
  881. if (chunk->vm)
  882. free_vm_area(chunk->vm);
  883. pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
  884. kfree(chunk);
  885. }
  886. static struct pcpu_chunk *alloc_pcpu_chunk(void)
  887. {
  888. struct pcpu_chunk *chunk;
  889. chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
  890. if (!chunk)
  891. return NULL;
  892. chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
  893. chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
  894. chunk->map[chunk->map_used++] = pcpu_unit_size;
  895. chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
  896. if (!chunk->vm) {
  897. free_pcpu_chunk(chunk);
  898. return NULL;
  899. }
  900. INIT_LIST_HEAD(&chunk->list);
  901. chunk->free_size = pcpu_unit_size;
  902. chunk->contig_hint = pcpu_unit_size;
  903. return chunk;
  904. }
  905. /**
  906. * pcpu_alloc - the percpu allocator
  907. * @size: size of area to allocate in bytes
  908. * @align: alignment of area (max PAGE_SIZE)
  909. * @reserved: allocate from the reserved chunk if available
  910. *
  911. * Allocate percpu area of @size bytes aligned at @align.
  912. *
  913. * CONTEXT:
  914. * Does GFP_KERNEL allocation.
  915. *
  916. * RETURNS:
  917. * Percpu pointer to the allocated area on success, NULL on failure.
  918. */
  919. static void *pcpu_alloc(size_t size, size_t align, bool reserved)
  920. {
  921. struct pcpu_chunk *chunk;
  922. int slot, off;
  923. if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
  924. WARN(true, "illegal size (%zu) or align (%zu) for "
  925. "percpu allocation\n", size, align);
  926. return NULL;
  927. }
  928. mutex_lock(&pcpu_alloc_mutex);
  929. spin_lock_irq(&pcpu_lock);
  930. /* serve reserved allocations from the reserved chunk if available */
  931. if (reserved && pcpu_reserved_chunk) {
  932. chunk = pcpu_reserved_chunk;
  933. if (size > chunk->contig_hint ||
  934. pcpu_extend_area_map(chunk) < 0)
  935. goto fail_unlock;
  936. off = pcpu_alloc_area(chunk, size, align);
  937. if (off >= 0)
  938. goto area_found;
  939. goto fail_unlock;
  940. }
  941. restart:
  942. /* search through normal chunks */
  943. for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
  944. list_for_each_entry(chunk, &pcpu_slot[slot], list) {
  945. if (size > chunk->contig_hint)
  946. continue;
  947. switch (pcpu_extend_area_map(chunk)) {
  948. case 0:
  949. break;
  950. case 1:
  951. goto restart; /* pcpu_lock dropped, restart */
  952. default:
  953. goto fail_unlock;
  954. }
  955. off = pcpu_alloc_area(chunk, size, align);
  956. if (off >= 0)
  957. goto area_found;
  958. }
  959. }
  960. /* hmmm... no space left, create a new chunk */
  961. spin_unlock_irq(&pcpu_lock);
  962. chunk = alloc_pcpu_chunk();
  963. if (!chunk)
  964. goto fail_unlock_mutex;
  965. spin_lock_irq(&pcpu_lock);
  966. pcpu_chunk_relocate(chunk, -1);
  967. goto restart;
  968. area_found:
  969. spin_unlock_irq(&pcpu_lock);
  970. /* populate, map and clear the area */
  971. if (pcpu_populate_chunk(chunk, off, size)) {
  972. spin_lock_irq(&pcpu_lock);
  973. pcpu_free_area(chunk, off);
  974. goto fail_unlock;
  975. }
  976. mutex_unlock(&pcpu_alloc_mutex);
  977. /* return address relative to unit0 */
  978. return __addr_to_pcpu_ptr(chunk->vm->addr + off);
  979. fail_unlock:
  980. spin_unlock_irq(&pcpu_lock);
  981. fail_unlock_mutex:
  982. mutex_unlock(&pcpu_alloc_mutex);
  983. return NULL;
  984. }
  985. /**
  986. * __alloc_percpu - allocate dynamic percpu area
  987. * @size: size of area to allocate in bytes
  988. * @align: alignment of area (max PAGE_SIZE)
  989. *
  990. * Allocate percpu area of @size bytes aligned at @align. Might
  991. * sleep. Might trigger writeouts.
  992. *
  993. * CONTEXT:
  994. * Does GFP_KERNEL allocation.
  995. *
  996. * RETURNS:
  997. * Percpu pointer to the allocated area on success, NULL on failure.
  998. */
  999. void *__alloc_percpu(size_t size, size_t align)
  1000. {
  1001. return pcpu_alloc(size, align, false);
  1002. }
  1003. EXPORT_SYMBOL_GPL(__alloc_percpu);
  1004. /**
  1005. * __alloc_reserved_percpu - allocate reserved percpu area
  1006. * @size: size of area to allocate in bytes
  1007. * @align: alignment of area (max PAGE_SIZE)
  1008. *
  1009. * Allocate percpu area of @size bytes aligned at @align from reserved
  1010. * percpu area if arch has set it up; otherwise, allocation is served
  1011. * from the same dynamic area. Might sleep. Might trigger writeouts.
  1012. *
  1013. * CONTEXT:
  1014. * Does GFP_KERNEL allocation.
  1015. *
  1016. * RETURNS:
  1017. * Percpu pointer to the allocated area on success, NULL on failure.
  1018. */
  1019. void *__alloc_reserved_percpu(size_t size, size_t align)
  1020. {
  1021. return pcpu_alloc(size, align, true);
  1022. }
  1023. /**
  1024. * pcpu_reclaim - reclaim fully free chunks, workqueue function
  1025. * @work: unused
  1026. *
  1027. * Reclaim all fully free chunks except for the first one.
  1028. *
  1029. * CONTEXT:
  1030. * workqueue context.
  1031. */
  1032. static void pcpu_reclaim(struct work_struct *work)
  1033. {
  1034. LIST_HEAD(todo);
  1035. struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
  1036. struct pcpu_chunk *chunk, *next;
  1037. mutex_lock(&pcpu_alloc_mutex);
  1038. spin_lock_irq(&pcpu_lock);
  1039. list_for_each_entry_safe(chunk, next, head, list) {
  1040. WARN_ON(chunk->immutable);
  1041. /* spare the first one */
  1042. if (chunk == list_first_entry(head, struct pcpu_chunk, list))
  1043. continue;
  1044. list_move(&chunk->list, &todo);
  1045. }
  1046. spin_unlock_irq(&pcpu_lock);
  1047. mutex_unlock(&pcpu_alloc_mutex);
  1048. list_for_each_entry_safe(chunk, next, &todo, list) {
  1049. pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size);
  1050. free_pcpu_chunk(chunk);
  1051. }
  1052. }
  1053. /**
  1054. * free_percpu - free percpu area
  1055. * @ptr: pointer to area to free
  1056. *
  1057. * Free percpu area @ptr.
  1058. *
  1059. * CONTEXT:
  1060. * Can be called from atomic context.
  1061. */
  1062. void free_percpu(void *ptr)
  1063. {
  1064. void *addr = __pcpu_ptr_to_addr(ptr);
  1065. struct pcpu_chunk *chunk;
  1066. unsigned long flags;
  1067. int off;
  1068. if (!ptr)
  1069. return;
  1070. spin_lock_irqsave(&pcpu_lock, flags);
  1071. chunk = pcpu_chunk_addr_search(addr);
  1072. off = addr - chunk->vm->addr;
  1073. pcpu_free_area(chunk, off);
  1074. /* if there are more than one fully free chunks, wake up grim reaper */
  1075. if (chunk->free_size == pcpu_unit_size) {
  1076. struct pcpu_chunk *pos;
  1077. list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
  1078. if (pos != chunk) {
  1079. schedule_work(&pcpu_reclaim_work);
  1080. break;
  1081. }
  1082. }
  1083. spin_unlock_irqrestore(&pcpu_lock, flags);
  1084. }
  1085. EXPORT_SYMBOL_GPL(free_percpu);
  1086. /**
  1087. * pcpu_setup_first_chunk - initialize the first percpu chunk
  1088. * @static_size: the size of static percpu area in bytes
  1089. * @reserved_size: the size of reserved percpu area in bytes, 0 for none
  1090. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  1091. * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE
  1092. * @base_addr: mapped address
  1093. * @unit_map: cpu -> unit map, NULL for sequential mapping
  1094. *
  1095. * Initialize the first percpu chunk which contains the kernel static
  1096. * perpcu area. This function is to be called from arch percpu area
  1097. * setup path.
  1098. *
  1099. * @reserved_size, if non-zero, specifies the amount of bytes to
  1100. * reserve after the static area in the first chunk. This reserves
  1101. * the first chunk such that it's available only through reserved
  1102. * percpu allocation. This is primarily used to serve module percpu
  1103. * static areas on architectures where the addressing model has
  1104. * limited offset range for symbol relocations to guarantee module
  1105. * percpu symbols fall inside the relocatable range.
  1106. *
  1107. * @dyn_size, if non-negative, determines the number of bytes
  1108. * available for dynamic allocation in the first chunk. Specifying
  1109. * non-negative value makes percpu leave alone the area beyond
  1110. * @static_size + @reserved_size + @dyn_size.
  1111. *
  1112. * @unit_size specifies unit size and must be aligned to PAGE_SIZE and
  1113. * equal to or larger than @static_size + @reserved_size + if
  1114. * non-negative, @dyn_size.
  1115. *
  1116. * The caller should have mapped the first chunk at @base_addr and
  1117. * copied static data to each unit.
  1118. *
  1119. * If the first chunk ends up with both reserved and dynamic areas, it
  1120. * is served by two chunks - one to serve the core static and reserved
  1121. * areas and the other for the dynamic area. They share the same vm
  1122. * and page map but uses different area allocation map to stay away
  1123. * from each other. The latter chunk is circulated in the chunk slots
  1124. * and available for dynamic allocation like any other chunks.
  1125. *
  1126. * RETURNS:
  1127. * The determined pcpu_unit_size which can be used to initialize
  1128. * percpu access.
  1129. */
  1130. size_t __init pcpu_setup_first_chunk(size_t static_size, size_t reserved_size,
  1131. ssize_t dyn_size, size_t unit_size,
  1132. void *base_addr, const int *unit_map)
  1133. {
  1134. static struct vm_struct first_vm;
  1135. static int smap[2], dmap[2];
  1136. size_t size_sum = static_size + reserved_size +
  1137. (dyn_size >= 0 ? dyn_size : 0);
  1138. struct pcpu_chunk *schunk, *dchunk = NULL;
  1139. unsigned int cpu, tcpu;
  1140. int i;
  1141. /* sanity checks */
  1142. BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
  1143. ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
  1144. BUG_ON(!static_size);
  1145. BUG_ON(!base_addr);
  1146. BUG_ON(unit_size < size_sum);
  1147. BUG_ON(unit_size & ~PAGE_MASK);
  1148. BUG_ON(unit_size < PCPU_MIN_UNIT_SIZE);
  1149. /* determine number of units and verify and initialize pcpu_unit_map */
  1150. if (unit_map) {
  1151. int first_unit = INT_MAX, last_unit = INT_MIN;
  1152. for_each_possible_cpu(cpu) {
  1153. int unit = unit_map[cpu];
  1154. BUG_ON(unit < 0);
  1155. for_each_possible_cpu(tcpu) {
  1156. if (tcpu == cpu)
  1157. break;
  1158. /* the mapping should be one-to-one */
  1159. BUG_ON(unit_map[tcpu] == unit);
  1160. }
  1161. if (unit < first_unit) {
  1162. pcpu_first_unit_cpu = cpu;
  1163. first_unit = unit;
  1164. }
  1165. if (unit > last_unit) {
  1166. pcpu_last_unit_cpu = cpu;
  1167. last_unit = unit;
  1168. }
  1169. }
  1170. pcpu_nr_units = last_unit + 1;
  1171. pcpu_unit_map = unit_map;
  1172. } else {
  1173. int *identity_map;
  1174. /* #units == #cpus, identity mapped */
  1175. identity_map = alloc_bootmem(num_possible_cpus() *
  1176. sizeof(identity_map[0]));
  1177. for_each_possible_cpu(cpu)
  1178. identity_map[cpu] = cpu;
  1179. pcpu_first_unit_cpu = 0;
  1180. pcpu_last_unit_cpu = pcpu_nr_units - 1;
  1181. pcpu_nr_units = num_possible_cpus();
  1182. pcpu_unit_map = identity_map;
  1183. }
  1184. /* determine basic parameters */
  1185. pcpu_unit_pages = unit_size >> PAGE_SHIFT;
  1186. pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
  1187. pcpu_chunk_size = pcpu_nr_units * pcpu_unit_size;
  1188. pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
  1189. BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
  1190. if (dyn_size < 0)
  1191. dyn_size = pcpu_unit_size - static_size - reserved_size;
  1192. first_vm.flags = VM_ALLOC;
  1193. first_vm.size = pcpu_chunk_size;
  1194. first_vm.addr = base_addr;
  1195. /*
  1196. * Allocate chunk slots. The additional last slot is for
  1197. * empty chunks.
  1198. */
  1199. pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
  1200. pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
  1201. for (i = 0; i < pcpu_nr_slots; i++)
  1202. INIT_LIST_HEAD(&pcpu_slot[i]);
  1203. /*
  1204. * Initialize static chunk. If reserved_size is zero, the
  1205. * static chunk covers static area + dynamic allocation area
  1206. * in the first chunk. If reserved_size is not zero, it
  1207. * covers static area + reserved area (mostly used for module
  1208. * static percpu allocation).
  1209. */
  1210. schunk = alloc_bootmem(pcpu_chunk_struct_size);
  1211. INIT_LIST_HEAD(&schunk->list);
  1212. schunk->vm = &first_vm;
  1213. schunk->map = smap;
  1214. schunk->map_alloc = ARRAY_SIZE(smap);
  1215. schunk->immutable = true;
  1216. bitmap_fill(schunk->populated, pcpu_unit_pages);
  1217. if (reserved_size) {
  1218. schunk->free_size = reserved_size;
  1219. pcpu_reserved_chunk = schunk;
  1220. pcpu_reserved_chunk_limit = static_size + reserved_size;
  1221. } else {
  1222. schunk->free_size = dyn_size;
  1223. dyn_size = 0; /* dynamic area covered */
  1224. }
  1225. schunk->contig_hint = schunk->free_size;
  1226. schunk->map[schunk->map_used++] = -static_size;
  1227. if (schunk->free_size)
  1228. schunk->map[schunk->map_used++] = schunk->free_size;
  1229. /* init dynamic chunk if necessary */
  1230. if (dyn_size) {
  1231. dchunk = alloc_bootmem(pcpu_chunk_struct_size);
  1232. INIT_LIST_HEAD(&dchunk->list);
  1233. dchunk->vm = &first_vm;
  1234. dchunk->map = dmap;
  1235. dchunk->map_alloc = ARRAY_SIZE(dmap);
  1236. dchunk->immutable = true;
  1237. bitmap_fill(dchunk->populated, pcpu_unit_pages);
  1238. dchunk->contig_hint = dchunk->free_size = dyn_size;
  1239. dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
  1240. dchunk->map[dchunk->map_used++] = dchunk->free_size;
  1241. }
  1242. /* link the first chunk in */
  1243. pcpu_first_chunk = dchunk ?: schunk;
  1244. pcpu_chunk_relocate(pcpu_first_chunk, -1);
  1245. /* we're done */
  1246. pcpu_base_addr = schunk->vm->addr;
  1247. return pcpu_unit_size;
  1248. }
  1249. static size_t pcpu_calc_fc_sizes(size_t static_size, size_t reserved_size,
  1250. ssize_t *dyn_sizep)
  1251. {
  1252. size_t size_sum;
  1253. size_sum = PFN_ALIGN(static_size + reserved_size +
  1254. (*dyn_sizep >= 0 ? *dyn_sizep : 0));
  1255. if (*dyn_sizep != 0)
  1256. *dyn_sizep = size_sum - static_size - reserved_size;
  1257. return size_sum;
  1258. }
  1259. /**
  1260. * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
  1261. * @static_size: the size of static percpu area in bytes
  1262. * @reserved_size: the size of reserved percpu area in bytes
  1263. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  1264. *
  1265. * This is a helper to ease setting up embedded first percpu chunk and
  1266. * can be called where pcpu_setup_first_chunk() is expected.
  1267. *
  1268. * If this function is used to setup the first chunk, it is allocated
  1269. * as a contiguous area using bootmem allocator and used as-is without
  1270. * being mapped into vmalloc area. This enables the first chunk to
  1271. * piggy back on the linear physical mapping which often uses larger
  1272. * page size.
  1273. *
  1274. * When @dyn_size is positive, dynamic area might be larger than
  1275. * specified to fill page alignment. When @dyn_size is auto,
  1276. * @dyn_size is just big enough to fill page alignment after static
  1277. * and reserved areas.
  1278. *
  1279. * If the needed size is smaller than the minimum or specified unit
  1280. * size, the leftover is returned to the bootmem allocator.
  1281. *
  1282. * RETURNS:
  1283. * The determined pcpu_unit_size which can be used to initialize
  1284. * percpu access on success, -errno on failure.
  1285. */
  1286. ssize_t __init pcpu_embed_first_chunk(size_t static_size, size_t reserved_size,
  1287. ssize_t dyn_size)
  1288. {
  1289. size_t size_sum, unit_size, chunk_size;
  1290. void *base;
  1291. unsigned int cpu;
  1292. /* determine parameters and allocate */
  1293. size_sum = pcpu_calc_fc_sizes(static_size, reserved_size, &dyn_size);
  1294. unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
  1295. chunk_size = unit_size * num_possible_cpus();
  1296. base = __alloc_bootmem_nopanic(chunk_size, PAGE_SIZE,
  1297. __pa(MAX_DMA_ADDRESS));
  1298. if (!base) {
  1299. pr_warning("PERCPU: failed to allocate %zu bytes for "
  1300. "embedding\n", chunk_size);
  1301. return -ENOMEM;
  1302. }
  1303. /* return the leftover and copy */
  1304. for_each_possible_cpu(cpu) {
  1305. void *ptr = base + cpu * unit_size;
  1306. free_bootmem(__pa(ptr + size_sum), unit_size - size_sum);
  1307. memcpy(ptr, __per_cpu_load, static_size);
  1308. }
  1309. /* we're ready, commit */
  1310. pr_info("PERCPU: Embedded %zu pages at %p, static data %zu bytes\n",
  1311. size_sum >> PAGE_SHIFT, base, static_size);
  1312. return pcpu_setup_first_chunk(static_size, reserved_size, dyn_size,
  1313. unit_size, base, NULL);
  1314. }
  1315. /**
  1316. * pcpu_4k_first_chunk - map the first chunk using PAGE_SIZE pages
  1317. * @static_size: the size of static percpu area in bytes
  1318. * @reserved_size: the size of reserved percpu area in bytes
  1319. * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE
  1320. * @free_fn: funtion to free percpu page, always called with PAGE_SIZE
  1321. * @populate_pte_fn: function to populate pte
  1322. *
  1323. * This is a helper to ease setting up embedded first percpu chunk and
  1324. * can be called where pcpu_setup_first_chunk() is expected.
  1325. *
  1326. * This is the basic allocator. Static percpu area is allocated
  1327. * page-by-page into vmalloc area.
  1328. *
  1329. * RETURNS:
  1330. * The determined pcpu_unit_size which can be used to initialize
  1331. * percpu access on success, -errno on failure.
  1332. */
  1333. ssize_t __init pcpu_4k_first_chunk(size_t static_size, size_t reserved_size,
  1334. pcpu_fc_alloc_fn_t alloc_fn,
  1335. pcpu_fc_free_fn_t free_fn,
  1336. pcpu_fc_populate_pte_fn_t populate_pte_fn)
  1337. {
  1338. static struct vm_struct vm;
  1339. int unit_pages;
  1340. size_t pages_size;
  1341. struct page **pages;
  1342. unsigned int cpu;
  1343. int i, j;
  1344. ssize_t ret;
  1345. unit_pages = PFN_UP(max_t(size_t, static_size + reserved_size,
  1346. PCPU_MIN_UNIT_SIZE));
  1347. /* unaligned allocations can't be freed, round up to page size */
  1348. pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
  1349. sizeof(pages[0]));
  1350. pages = alloc_bootmem(pages_size);
  1351. /* allocate pages */
  1352. j = 0;
  1353. for_each_possible_cpu(cpu)
  1354. for (i = 0; i < unit_pages; i++) {
  1355. void *ptr;
  1356. ptr = alloc_fn(cpu, PAGE_SIZE);
  1357. if (!ptr) {
  1358. pr_warning("PERCPU: failed to allocate "
  1359. "4k page for cpu%u\n", cpu);
  1360. goto enomem;
  1361. }
  1362. pages[j++] = virt_to_page(ptr);
  1363. }
  1364. /* allocate vm area, map the pages and copy static data */
  1365. vm.flags = VM_ALLOC;
  1366. vm.size = num_possible_cpus() * unit_pages << PAGE_SHIFT;
  1367. vm_area_register_early(&vm, PAGE_SIZE);
  1368. for_each_possible_cpu(cpu) {
  1369. unsigned long unit_addr = (unsigned long)vm.addr +
  1370. (cpu * unit_pages << PAGE_SHIFT);
  1371. for (i = 0; i < unit_pages; i++)
  1372. populate_pte_fn(unit_addr + (i << PAGE_SHIFT));
  1373. /* pte already populated, the following shouldn't fail */
  1374. ret = __pcpu_map_pages(unit_addr, &pages[cpu * unit_pages],
  1375. unit_pages);
  1376. if (ret < 0)
  1377. panic("failed to map percpu area, err=%zd\n", ret);
  1378. /*
  1379. * FIXME: Archs with virtual cache should flush local
  1380. * cache for the linear mapping here - something
  1381. * equivalent to flush_cache_vmap() on the local cpu.
  1382. * flush_cache_vmap() can't be used as most supporting
  1383. * data structures are not set up yet.
  1384. */
  1385. /* copy static data */
  1386. memcpy((void *)unit_addr, __per_cpu_load, static_size);
  1387. }
  1388. /* we're ready, commit */
  1389. pr_info("PERCPU: %d 4k pages per cpu, static data %zu bytes\n",
  1390. unit_pages, static_size);
  1391. ret = pcpu_setup_first_chunk(static_size, reserved_size, -1,
  1392. unit_pages << PAGE_SHIFT, vm.addr, NULL);
  1393. goto out_free_ar;
  1394. enomem:
  1395. while (--j >= 0)
  1396. free_fn(page_address(pages[j]), PAGE_SIZE);
  1397. ret = -ENOMEM;
  1398. out_free_ar:
  1399. free_bootmem(__pa(pages), pages_size);
  1400. return ret;
  1401. }
  1402. /*
  1403. * Large page remapping first chunk setup helper
  1404. */
  1405. #ifdef CONFIG_NEED_MULTIPLE_NODES
  1406. struct pcpul_ent {
  1407. unsigned int cpu;
  1408. void *ptr;
  1409. };
  1410. static size_t pcpul_size;
  1411. static size_t pcpul_unit_size;
  1412. static struct pcpul_ent *pcpul_map;
  1413. static struct vm_struct pcpul_vm;
  1414. /**
  1415. * pcpu_lpage_first_chunk - remap the first percpu chunk using large page
  1416. * @static_size: the size of static percpu area in bytes
  1417. * @reserved_size: the size of reserved percpu area in bytes
  1418. * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
  1419. * @lpage_size: the size of a large page
  1420. * @alloc_fn: function to allocate percpu lpage, always called with lpage_size
  1421. * @free_fn: function to free percpu memory, @size <= lpage_size
  1422. * @map_fn: function to map percpu lpage, always called with lpage_size
  1423. *
  1424. * This allocator uses large page as unit. A large page is allocated
  1425. * for each cpu and each is remapped into vmalloc area using large
  1426. * page mapping. As large page can be quite large, only part of it is
  1427. * used for the first chunk. Unused part is returned to the bootmem
  1428. * allocator.
  1429. *
  1430. * So, the large pages are mapped twice - once to the physical mapping
  1431. * and to the vmalloc area for the first percpu chunk. The double
  1432. * mapping does add one more large TLB entry pressure but still is
  1433. * much better than only using 4k mappings while still being NUMA
  1434. * friendly.
  1435. *
  1436. * RETURNS:
  1437. * The determined pcpu_unit_size which can be used to initialize
  1438. * percpu access on success, -errno on failure.
  1439. */
  1440. ssize_t __init pcpu_lpage_first_chunk(size_t static_size, size_t reserved_size,
  1441. ssize_t dyn_size, size_t lpage_size,
  1442. pcpu_fc_alloc_fn_t alloc_fn,
  1443. pcpu_fc_free_fn_t free_fn,
  1444. pcpu_fc_map_fn_t map_fn)
  1445. {
  1446. size_t size_sum;
  1447. size_t map_size;
  1448. unsigned int cpu;
  1449. int i, j;
  1450. ssize_t ret;
  1451. /*
  1452. * Currently supports only single page. Supporting multiple
  1453. * pages won't be too difficult if it ever becomes necessary.
  1454. */
  1455. size_sum = pcpu_calc_fc_sizes(static_size, reserved_size, &dyn_size);
  1456. pcpul_unit_size = lpage_size;
  1457. pcpul_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
  1458. if (pcpul_size > pcpul_unit_size) {
  1459. pr_warning("PERCPU: static data is larger than large page, "
  1460. "can't use large page\n");
  1461. return -EINVAL;
  1462. }
  1463. /* allocate pointer array and alloc large pages */
  1464. map_size = PFN_ALIGN(num_possible_cpus() * sizeof(pcpul_map[0]));
  1465. pcpul_map = alloc_bootmem(map_size);
  1466. for_each_possible_cpu(cpu) {
  1467. void *ptr;
  1468. ptr = alloc_fn(cpu, lpage_size);
  1469. if (!ptr) {
  1470. pr_warning("PERCPU: failed to allocate large page "
  1471. "for cpu%u\n", cpu);
  1472. goto enomem;
  1473. }
  1474. /*
  1475. * Only use pcpul_size bytes and give back the rest.
  1476. *
  1477. * Ingo: The lpage_size up-rounding bootmem is needed
  1478. * to make sure the partial lpage is still fully RAM -
  1479. * it's not well-specified to have a incompatible area
  1480. * (unmapped RAM, device memory, etc.) in that hole.
  1481. */
  1482. free_fn(ptr + pcpul_size, lpage_size - pcpul_size);
  1483. pcpul_map[cpu].cpu = cpu;
  1484. pcpul_map[cpu].ptr = ptr;
  1485. memcpy(ptr, __per_cpu_load, static_size);
  1486. }
  1487. /* allocate address and map */
  1488. pcpul_vm.flags = VM_ALLOC;
  1489. pcpul_vm.size = num_possible_cpus() * pcpul_unit_size;
  1490. vm_area_register_early(&pcpul_vm, pcpul_unit_size);
  1491. for_each_possible_cpu(cpu)
  1492. map_fn(pcpul_map[cpu].ptr, pcpul_unit_size,
  1493. pcpul_vm.addr + cpu * pcpul_unit_size);
  1494. /* we're ready, commit */
  1495. pr_info("PERCPU: Remapped at %p with large pages, static data "
  1496. "%zu bytes\n", pcpul_vm.addr, static_size);
  1497. ret = pcpu_setup_first_chunk(static_size, reserved_size, dyn_size,
  1498. pcpul_unit_size, pcpul_vm.addr, NULL);
  1499. /* sort pcpul_map array for pcpu_lpage_remapped() */
  1500. for (i = 0; i < num_possible_cpus() - 1; i++)
  1501. for (j = i + 1; j < num_possible_cpus(); j++)
  1502. if (pcpul_map[i].ptr > pcpul_map[j].ptr) {
  1503. struct pcpul_ent tmp = pcpul_map[i];
  1504. pcpul_map[i] = pcpul_map[j];
  1505. pcpul_map[j] = tmp;
  1506. }
  1507. return ret;
  1508. enomem:
  1509. for_each_possible_cpu(cpu)
  1510. if (pcpul_map[cpu].ptr)
  1511. free_fn(pcpul_map[cpu].ptr, pcpul_size);
  1512. free_bootmem(__pa(pcpul_map), map_size);
  1513. return -ENOMEM;
  1514. }
  1515. /**
  1516. * pcpu_lpage_remapped - determine whether a kaddr is in pcpul recycled area
  1517. * @kaddr: the kernel address in question
  1518. *
  1519. * Determine whether @kaddr falls in the pcpul recycled area. This is
  1520. * used by pageattr to detect VM aliases and break up the pcpu large
  1521. * page mapping such that the same physical page is not mapped under
  1522. * different attributes.
  1523. *
  1524. * The recycled area is always at the tail of a partially used large
  1525. * page.
  1526. *
  1527. * RETURNS:
  1528. * Address of corresponding remapped pcpu address if match is found;
  1529. * otherwise, NULL.
  1530. */
  1531. void *pcpu_lpage_remapped(void *kaddr)
  1532. {
  1533. unsigned long unit_mask = pcpul_unit_size - 1;
  1534. void *lpage_addr = (void *)((unsigned long)kaddr & ~unit_mask);
  1535. unsigned long offset = (unsigned long)kaddr & unit_mask;
  1536. int left = 0, right = num_possible_cpus() - 1;
  1537. int pos;
  1538. /* pcpul in use at all? */
  1539. if (!pcpul_map)
  1540. return NULL;
  1541. /* okay, perform binary search */
  1542. while (left <= right) {
  1543. pos = (left + right) / 2;
  1544. if (pcpul_map[pos].ptr < lpage_addr)
  1545. left = pos + 1;
  1546. else if (pcpul_map[pos].ptr > lpage_addr)
  1547. right = pos - 1;
  1548. else {
  1549. /* it shouldn't be in the area for the first chunk */
  1550. WARN_ON(offset < pcpul_size);
  1551. return pcpul_vm.addr +
  1552. pcpul_map[pos].cpu * pcpul_unit_size + offset;
  1553. }
  1554. }
  1555. return NULL;
  1556. }
  1557. #endif
  1558. /*
  1559. * Generic percpu area setup.
  1560. *
  1561. * The embedding helper is used because its behavior closely resembles
  1562. * the original non-dynamic generic percpu area setup. This is
  1563. * important because many archs have addressing restrictions and might
  1564. * fail if the percpu area is located far away from the previous
  1565. * location. As an added bonus, in non-NUMA cases, embedding is
  1566. * generally a good idea TLB-wise because percpu area can piggy back
  1567. * on the physical linear memory mapping which uses large page
  1568. * mappings on applicable archs.
  1569. */
  1570. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  1571. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  1572. EXPORT_SYMBOL(__per_cpu_offset);
  1573. void __init setup_per_cpu_areas(void)
  1574. {
  1575. size_t static_size = __per_cpu_end - __per_cpu_start;
  1576. ssize_t unit_size;
  1577. unsigned long delta;
  1578. unsigned int cpu;
  1579. /*
  1580. * Always reserve area for module percpu variables. That's
  1581. * what the legacy allocator did.
  1582. */
  1583. unit_size = pcpu_embed_first_chunk(static_size, PERCPU_MODULE_RESERVE,
  1584. PERCPU_DYNAMIC_RESERVE);
  1585. if (unit_size < 0)
  1586. panic("Failed to initialized percpu areas.");
  1587. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  1588. for_each_possible_cpu(cpu)
  1589. __per_cpu_offset[cpu] = delta + cpu * unit_size;
  1590. }
  1591. #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */