xref: /linux/mm/percpu.c (revision 3c9a024fde58b08745680863859d1483def64f74)
1fbf59bc9STejun Heo /*
288999a89STejun Heo  * mm/percpu.c - percpu memory allocator
3fbf59bc9STejun Heo  *
4fbf59bc9STejun Heo  * Copyright (C) 2009		SUSE Linux Products GmbH
5fbf59bc9STejun Heo  * Copyright (C) 2009		Tejun Heo <tj@kernel.org>
6fbf59bc9STejun Heo  *
7fbf59bc9STejun Heo  * This file is released under the GPLv2.
8fbf59bc9STejun Heo  *
9fbf59bc9STejun Heo  * This is percpu allocator which can handle both static and dynamic
1088999a89STejun Heo  * areas.  Percpu areas are allocated in chunks.  Each chunk is
1188999a89STejun Heo  * consisted of boot-time determined number of units and the first
1288999a89STejun Heo  * chunk is used for static percpu variables in the kernel image
132f39e637STejun Heo  * (special boot time alloc/init handling necessary as these areas
142f39e637STejun Heo  * need to be brought up before allocation services are running).
152f39e637STejun Heo  * Unit grows as necessary and all units grow or shrink in unison.
1688999a89STejun Heo  * When a chunk is filled up, another chunk is allocated.
17fbf59bc9STejun Heo  *
18fbf59bc9STejun Heo  *  c0                           c1                         c2
19fbf59bc9STejun Heo  *  -------------------          -------------------        ------------
20fbf59bc9STejun Heo  * | u0 | u1 | u2 | u3 |        | u0 | u1 | u2 | u3 |      | u0 | u1 | u
21fbf59bc9STejun Heo  *  -------------------  ......  -------------------  ....  ------------
22fbf59bc9STejun Heo  *
23fbf59bc9STejun Heo  * Allocation is done in offset-size areas of single unit space.  Ie,
24fbf59bc9STejun Heo  * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0,
252f39e637STejun Heo  * c1:u1, c1:u2 and c1:u3.  On UMA, units corresponds directly to
262f39e637STejun Heo  * cpus.  On NUMA, the mapping can be non-linear and even sparse.
272f39e637STejun Heo  * Percpu access can be done by configuring percpu base registers
282f39e637STejun Heo  * according to cpu to unit mapping and pcpu_unit_size.
29fbf59bc9STejun Heo  *
302f39e637STejun Heo  * There are usually many small percpu allocations many of them being
312f39e637STejun Heo  * as small as 4 bytes.  The allocator organizes chunks into lists
32fbf59bc9STejun Heo  * according to free size and tries to allocate from the fullest one.
33fbf59bc9STejun Heo  * Each chunk keeps the maximum contiguous area size hint which is
34fbf59bc9STejun Heo  * guaranteed to be eqaul to or larger than the maximum contiguous
35fbf59bc9STejun Heo  * area in the chunk.  This helps the allocator not to iterate the
36fbf59bc9STejun Heo  * chunk maps unnecessarily.
37fbf59bc9STejun Heo  *
38fbf59bc9STejun Heo  * Allocation state in each chunk is kept using an array of integers
39fbf59bc9STejun Heo  * on chunk->map.  A positive value in the map represents a free
40fbf59bc9STejun Heo  * region and negative allocated.  Allocation inside a chunk is done
41fbf59bc9STejun Heo  * by scanning this map sequentially and serving the first matching
42fbf59bc9STejun Heo  * entry.  This is mostly copied from the percpu_modalloc() allocator.
43e1b9aa3fSChristoph Lameter  * Chunks can be determined from the address using the index field
44e1b9aa3fSChristoph Lameter  * in the page struct. The index field contains a pointer to the chunk.
45fbf59bc9STejun Heo  *
46fbf59bc9STejun Heo  * To use this allocator, arch code should do the followings.
47fbf59bc9STejun Heo  *
48fbf59bc9STejun Heo  * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
49e0100983STejun Heo  *   regular address to percpu pointer and back if they need to be
50e0100983STejun Heo  *   different from the default
51fbf59bc9STejun Heo  *
528d408b4bSTejun Heo  * - use pcpu_setup_first_chunk() during percpu area initialization to
538d408b4bSTejun Heo  *   setup the first chunk containing the kernel static percpu area
54fbf59bc9STejun Heo  */
55fbf59bc9STejun Heo 
56fbf59bc9STejun Heo #include <linux/bitmap.h>
57fbf59bc9STejun Heo #include <linux/bootmem.h>
58fd1e8a1fSTejun Heo #include <linux/err.h>
59fbf59bc9STejun Heo #include <linux/list.h>
60a530b795STejun Heo #include <linux/log2.h>
61fbf59bc9STejun Heo #include <linux/mm.h>
62fbf59bc9STejun Heo #include <linux/module.h>
63fbf59bc9STejun Heo #include <linux/mutex.h>
64fbf59bc9STejun Heo #include <linux/percpu.h>
65fbf59bc9STejun Heo #include <linux/pfn.h>
66fbf59bc9STejun Heo #include <linux/slab.h>
67ccea34b5STejun Heo #include <linux/spinlock.h>
68fbf59bc9STejun Heo #include <linux/vmalloc.h>
69a56dbddfSTejun Heo #include <linux/workqueue.h>
70fbf59bc9STejun Heo 
71fbf59bc9STejun Heo #include <asm/cacheflush.h>
72e0100983STejun Heo #include <asm/sections.h>
73fbf59bc9STejun Heo #include <asm/tlbflush.h>
743b034b0dSVivek Goyal #include <asm/io.h>
75fbf59bc9STejun Heo 
76fbf59bc9STejun Heo #define PCPU_SLOT_BASE_SHIFT		5	/* 1-31 shares the same slot */
77fbf59bc9STejun Heo #define PCPU_DFL_MAP_ALLOC		16	/* start a map with 16 ents */
78fbf59bc9STejun Heo 
79bbddff05STejun Heo #ifdef CONFIG_SMP
80e0100983STejun Heo /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */
81e0100983STejun Heo #ifndef __addr_to_pcpu_ptr
82e0100983STejun Heo #define __addr_to_pcpu_ptr(addr)					\
8343cf38ebSTejun Heo 	(void __percpu *)((unsigned long)(addr) -			\
8443cf38ebSTejun Heo 			  (unsigned long)pcpu_base_addr	+		\
8543cf38ebSTejun Heo 			  (unsigned long)__per_cpu_start)
86e0100983STejun Heo #endif
87e0100983STejun Heo #ifndef __pcpu_ptr_to_addr
88e0100983STejun Heo #define __pcpu_ptr_to_addr(ptr)						\
8943cf38ebSTejun Heo 	(void __force *)((unsigned long)(ptr) +				\
9043cf38ebSTejun Heo 			 (unsigned long)pcpu_base_addr -		\
9143cf38ebSTejun Heo 			 (unsigned long)__per_cpu_start)
92e0100983STejun Heo #endif
93bbddff05STejun Heo #else	/* CONFIG_SMP */
94bbddff05STejun Heo /* on UP, it's always identity mapped */
95bbddff05STejun Heo #define __addr_to_pcpu_ptr(addr)	(void __percpu *)(addr)
96bbddff05STejun Heo #define __pcpu_ptr_to_addr(ptr)		(void __force *)(ptr)
97bbddff05STejun Heo #endif	/* CONFIG_SMP */
98e0100983STejun Heo 
99fbf59bc9STejun Heo struct pcpu_chunk {
100fbf59bc9STejun Heo 	struct list_head	list;		/* linked to pcpu_slot lists */
101fbf59bc9STejun Heo 	int			free_size;	/* free bytes in the chunk */
102fbf59bc9STejun Heo 	int			contig_hint;	/* max contiguous size hint */
103bba174f5STejun Heo 	void			*base_addr;	/* base address of this chunk */
104fbf59bc9STejun Heo 	int			map_used;	/* # of map entries used */
105fbf59bc9STejun Heo 	int			map_alloc;	/* # of map entries allocated */
106fbf59bc9STejun Heo 	int			*map;		/* allocation map */
10788999a89STejun Heo 	void			*data;		/* chunk data */
1088d408b4bSTejun Heo 	bool			immutable;	/* no [de]population allowed */
109ce3141a2STejun Heo 	unsigned long		populated[];	/* populated bitmap */
110fbf59bc9STejun Heo };
111fbf59bc9STejun Heo 
11240150d37STejun Heo static int pcpu_unit_pages __read_mostly;
11340150d37STejun Heo static int pcpu_unit_size __read_mostly;
1142f39e637STejun Heo static int pcpu_nr_units __read_mostly;
1156563297cSTejun Heo static int pcpu_atom_size __read_mostly;
11640150d37STejun Heo static int pcpu_nr_slots __read_mostly;
11740150d37STejun Heo static size_t pcpu_chunk_struct_size __read_mostly;
118fbf59bc9STejun Heo 
1192f39e637STejun Heo /* cpus with the lowest and highest unit numbers */
1202f39e637STejun Heo static unsigned int pcpu_first_unit_cpu __read_mostly;
1212f39e637STejun Heo static unsigned int pcpu_last_unit_cpu __read_mostly;
1222f39e637STejun Heo 
123fbf59bc9STejun Heo /* the address of the first chunk which starts with the kernel static area */
12440150d37STejun Heo void *pcpu_base_addr __read_mostly;
125fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(pcpu_base_addr);
126fbf59bc9STejun Heo 
127fb435d52STejun Heo static const int *pcpu_unit_map __read_mostly;		/* cpu -> unit */
128fb435d52STejun Heo const unsigned long *pcpu_unit_offsets __read_mostly;	/* cpu -> unit offset */
1292f39e637STejun Heo 
1306563297cSTejun Heo /* group information, used for vm allocation */
1316563297cSTejun Heo static int pcpu_nr_groups __read_mostly;
1326563297cSTejun Heo static const unsigned long *pcpu_group_offsets __read_mostly;
1336563297cSTejun Heo static const size_t *pcpu_group_sizes __read_mostly;
1346563297cSTejun Heo 
135ae9e6bc9STejun Heo /*
136ae9e6bc9STejun Heo  * The first chunk which always exists.  Note that unlike other
137ae9e6bc9STejun Heo  * chunks, this one can be allocated and mapped in several different
138ae9e6bc9STejun Heo  * ways and thus often doesn't live in the vmalloc area.
139ae9e6bc9STejun Heo  */
140ae9e6bc9STejun Heo static struct pcpu_chunk *pcpu_first_chunk;
141ae9e6bc9STejun Heo 
142ae9e6bc9STejun Heo /*
143ae9e6bc9STejun Heo  * Optional reserved chunk.  This chunk reserves part of the first
144ae9e6bc9STejun Heo  * chunk and serves it for reserved allocations.  The amount of
145ae9e6bc9STejun Heo  * reserved offset is in pcpu_reserved_chunk_limit.  When reserved
146ae9e6bc9STejun Heo  * area doesn't exist, the following variables contain NULL and 0
147ae9e6bc9STejun Heo  * respectively.
148ae9e6bc9STejun Heo  */
149edcb4639STejun Heo static struct pcpu_chunk *pcpu_reserved_chunk;
150edcb4639STejun Heo static int pcpu_reserved_chunk_limit;
151edcb4639STejun Heo 
152fbf59bc9STejun Heo /*
153ccea34b5STejun Heo  * Synchronization rules.
154fbf59bc9STejun Heo  *
155ccea34b5STejun Heo  * There are two locks - pcpu_alloc_mutex and pcpu_lock.  The former
156ce3141a2STejun Heo  * protects allocation/reclaim paths, chunks, populated bitmap and
157ce3141a2STejun Heo  * vmalloc mapping.  The latter is a spinlock and protects the index
158ce3141a2STejun Heo  * data structures - chunk slots, chunks and area maps in chunks.
159fbf59bc9STejun Heo  *
160ccea34b5STejun Heo  * During allocation, pcpu_alloc_mutex is kept locked all the time and
161ccea34b5STejun Heo  * pcpu_lock is grabbed and released as necessary.  All actual memory
162403a91b1SJiri Kosina  * allocations are done using GFP_KERNEL with pcpu_lock released.  In
163403a91b1SJiri Kosina  * general, percpu memory can't be allocated with irq off but
164403a91b1SJiri Kosina  * irqsave/restore are still used in alloc path so that it can be used
165403a91b1SJiri Kosina  * from early init path - sched_init() specifically.
166ccea34b5STejun Heo  *
167ccea34b5STejun Heo  * Free path accesses and alters only the index data structures, so it
168ccea34b5STejun Heo  * can be safely called from atomic context.  When memory needs to be
169ccea34b5STejun Heo  * returned to the system, free path schedules reclaim_work which
170ccea34b5STejun Heo  * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be
171ccea34b5STejun Heo  * reclaimed, release both locks and frees the chunks.  Note that it's
172ccea34b5STejun Heo  * necessary to grab both locks to remove a chunk from circulation as
173ccea34b5STejun Heo  * allocation path might be referencing the chunk with only
174ccea34b5STejun Heo  * pcpu_alloc_mutex locked.
175fbf59bc9STejun Heo  */
176ccea34b5STejun Heo static DEFINE_MUTEX(pcpu_alloc_mutex);	/* protects whole alloc and reclaim */
177ccea34b5STejun Heo static DEFINE_SPINLOCK(pcpu_lock);	/* protects index data structures */
178fbf59bc9STejun Heo 
17940150d37STejun Heo static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
180fbf59bc9STejun Heo 
181a56dbddfSTejun Heo /* reclaim work to release fully free chunks, scheduled from free path */
182a56dbddfSTejun Heo static void pcpu_reclaim(struct work_struct *work);
183a56dbddfSTejun Heo static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
184a56dbddfSTejun Heo 
185020ec653STejun Heo static bool pcpu_addr_in_first_chunk(void *addr)
186020ec653STejun Heo {
187020ec653STejun Heo 	void *first_start = pcpu_first_chunk->base_addr;
188020ec653STejun Heo 
189020ec653STejun Heo 	return addr >= first_start && addr < first_start + pcpu_unit_size;
190020ec653STejun Heo }
191020ec653STejun Heo 
192020ec653STejun Heo static bool pcpu_addr_in_reserved_chunk(void *addr)
193020ec653STejun Heo {
194020ec653STejun Heo 	void *first_start = pcpu_first_chunk->base_addr;
195020ec653STejun Heo 
196020ec653STejun Heo 	return addr >= first_start &&
197020ec653STejun Heo 		addr < first_start + pcpu_reserved_chunk_limit;
198020ec653STejun Heo }
199020ec653STejun Heo 
200d9b55eebSTejun Heo static int __pcpu_size_to_slot(int size)
201fbf59bc9STejun Heo {
202cae3aeb8STejun Heo 	int highbit = fls(size);	/* size is in bytes */
203fbf59bc9STejun Heo 	return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
204fbf59bc9STejun Heo }
205fbf59bc9STejun Heo 
206d9b55eebSTejun Heo static int pcpu_size_to_slot(int size)
207d9b55eebSTejun Heo {
208d9b55eebSTejun Heo 	if (size == pcpu_unit_size)
209d9b55eebSTejun Heo 		return pcpu_nr_slots - 1;
210d9b55eebSTejun Heo 	return __pcpu_size_to_slot(size);
211d9b55eebSTejun Heo }
212d9b55eebSTejun Heo 
213fbf59bc9STejun Heo static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
214fbf59bc9STejun Heo {
215fbf59bc9STejun Heo 	if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
216fbf59bc9STejun Heo 		return 0;
217fbf59bc9STejun Heo 
218fbf59bc9STejun Heo 	return pcpu_size_to_slot(chunk->free_size);
219fbf59bc9STejun Heo }
220fbf59bc9STejun Heo 
22188999a89STejun Heo /* set the pointer to a chunk in a page struct */
22288999a89STejun Heo static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu)
22388999a89STejun Heo {
22488999a89STejun Heo 	page->index = (unsigned long)pcpu;
22588999a89STejun Heo }
22688999a89STejun Heo 
22788999a89STejun Heo /* obtain pointer to a chunk from a page struct */
22888999a89STejun Heo static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page)
22988999a89STejun Heo {
23088999a89STejun Heo 	return (struct pcpu_chunk *)page->index;
23188999a89STejun Heo }
23288999a89STejun Heo 
23388999a89STejun Heo static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx)
234fbf59bc9STejun Heo {
2352f39e637STejun Heo 	return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx;
236fbf59bc9STejun Heo }
237fbf59bc9STejun Heo 
2389983b6f0STejun Heo static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
239fbf59bc9STejun Heo 				     unsigned int cpu, int page_idx)
240fbf59bc9STejun Heo {
241bba174f5STejun Heo 	return (unsigned long)chunk->base_addr + pcpu_unit_offsets[cpu] +
242fb435d52STejun Heo 		(page_idx << PAGE_SHIFT);
243fbf59bc9STejun Heo }
244fbf59bc9STejun Heo 
24588999a89STejun Heo static void __maybe_unused pcpu_next_unpop(struct pcpu_chunk *chunk,
24688999a89STejun Heo 					   int *rs, int *re, int end)
247ce3141a2STejun Heo {
248ce3141a2STejun Heo 	*rs = find_next_zero_bit(chunk->populated, end, *rs);
249ce3141a2STejun Heo 	*re = find_next_bit(chunk->populated, end, *rs + 1);
250ce3141a2STejun Heo }
251ce3141a2STejun Heo 
25288999a89STejun Heo static void __maybe_unused pcpu_next_pop(struct pcpu_chunk *chunk,
25388999a89STejun Heo 					 int *rs, int *re, int end)
254ce3141a2STejun Heo {
255ce3141a2STejun Heo 	*rs = find_next_bit(chunk->populated, end, *rs);
256ce3141a2STejun Heo 	*re = find_next_zero_bit(chunk->populated, end, *rs + 1);
257ce3141a2STejun Heo }
258ce3141a2STejun Heo 
259ce3141a2STejun Heo /*
260ce3141a2STejun Heo  * (Un)populated page region iterators.  Iterate over (un)populated
261ce3141a2STejun Heo  * page regions betwen @start and @end in @chunk.  @rs and @re should
262ce3141a2STejun Heo  * be integer variables and will be set to start and end page index of
263ce3141a2STejun Heo  * the current region.
264ce3141a2STejun Heo  */
265ce3141a2STejun Heo #define pcpu_for_each_unpop_region(chunk, rs, re, start, end)		    \
266ce3141a2STejun Heo 	for ((rs) = (start), pcpu_next_unpop((chunk), &(rs), &(re), (end)); \
267ce3141a2STejun Heo 	     (rs) < (re);						    \
268ce3141a2STejun Heo 	     (rs) = (re) + 1, pcpu_next_unpop((chunk), &(rs), &(re), (end)))
269ce3141a2STejun Heo 
270ce3141a2STejun Heo #define pcpu_for_each_pop_region(chunk, rs, re, start, end)		    \
271ce3141a2STejun Heo 	for ((rs) = (start), pcpu_next_pop((chunk), &(rs), &(re), (end));   \
272ce3141a2STejun Heo 	     (rs) < (re);						    \
273ce3141a2STejun Heo 	     (rs) = (re) + 1, pcpu_next_pop((chunk), &(rs), &(re), (end)))
274ce3141a2STejun Heo 
275fbf59bc9STejun Heo /**
2761880d93bSTejun Heo  * pcpu_mem_alloc - allocate memory
2771880d93bSTejun Heo  * @size: bytes to allocate
278fbf59bc9STejun Heo  *
2791880d93bSTejun Heo  * Allocate @size bytes.  If @size is smaller than PAGE_SIZE,
2801880d93bSTejun Heo  * kzalloc() is used; otherwise, vmalloc() is used.  The returned
2811880d93bSTejun Heo  * memory is always zeroed.
282fbf59bc9STejun Heo  *
283ccea34b5STejun Heo  * CONTEXT:
284ccea34b5STejun Heo  * Does GFP_KERNEL allocation.
285ccea34b5STejun Heo  *
286fbf59bc9STejun Heo  * RETURNS:
2871880d93bSTejun Heo  * Pointer to the allocated area on success, NULL on failure.
288fbf59bc9STejun Heo  */
2891880d93bSTejun Heo static void *pcpu_mem_alloc(size_t size)
290fbf59bc9STejun Heo {
291099a19d9STejun Heo 	if (WARN_ON_ONCE(!slab_is_available()))
292099a19d9STejun Heo 		return NULL;
293099a19d9STejun Heo 
294fbf59bc9STejun Heo 	if (size <= PAGE_SIZE)
2951880d93bSTejun Heo 		return kzalloc(size, GFP_KERNEL);
2961880d93bSTejun Heo 	else {
2971880d93bSTejun Heo 		void *ptr = vmalloc(size);
2981880d93bSTejun Heo 		if (ptr)
2991880d93bSTejun Heo 			memset(ptr, 0, size);
3001880d93bSTejun Heo 		return ptr;
3011880d93bSTejun Heo 	}
3021880d93bSTejun Heo }
303fbf59bc9STejun Heo 
3041880d93bSTejun Heo /**
3051880d93bSTejun Heo  * pcpu_mem_free - free memory
3061880d93bSTejun Heo  * @ptr: memory to free
3071880d93bSTejun Heo  * @size: size of the area
3081880d93bSTejun Heo  *
3091880d93bSTejun Heo  * Free @ptr.  @ptr should have been allocated using pcpu_mem_alloc().
3101880d93bSTejun Heo  */
3111880d93bSTejun Heo static void pcpu_mem_free(void *ptr, size_t size)
3121880d93bSTejun Heo {
3131880d93bSTejun Heo 	if (size <= PAGE_SIZE)
3141880d93bSTejun Heo 		kfree(ptr);
3151880d93bSTejun Heo 	else
3161880d93bSTejun Heo 		vfree(ptr);
317fbf59bc9STejun Heo }
318fbf59bc9STejun Heo 
319fbf59bc9STejun Heo /**
320fbf59bc9STejun Heo  * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
321fbf59bc9STejun Heo  * @chunk: chunk of interest
322fbf59bc9STejun Heo  * @oslot: the previous slot it was on
323fbf59bc9STejun Heo  *
324fbf59bc9STejun Heo  * This function is called after an allocation or free changed @chunk.
325fbf59bc9STejun Heo  * New slot according to the changed state is determined and @chunk is
326edcb4639STejun Heo  * moved to the slot.  Note that the reserved chunk is never put on
327edcb4639STejun Heo  * chunk slots.
328ccea34b5STejun Heo  *
329ccea34b5STejun Heo  * CONTEXT:
330ccea34b5STejun Heo  * pcpu_lock.
331fbf59bc9STejun Heo  */
332fbf59bc9STejun Heo static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
333fbf59bc9STejun Heo {
334fbf59bc9STejun Heo 	int nslot = pcpu_chunk_slot(chunk);
335fbf59bc9STejun Heo 
336edcb4639STejun Heo 	if (chunk != pcpu_reserved_chunk && oslot != nslot) {
337fbf59bc9STejun Heo 		if (oslot < nslot)
338fbf59bc9STejun Heo 			list_move(&chunk->list, &pcpu_slot[nslot]);
339fbf59bc9STejun Heo 		else
340fbf59bc9STejun Heo 			list_move_tail(&chunk->list, &pcpu_slot[nslot]);
341fbf59bc9STejun Heo 	}
342fbf59bc9STejun Heo }
343fbf59bc9STejun Heo 
344fbf59bc9STejun Heo /**
345833af842STejun Heo  * pcpu_need_to_extend - determine whether chunk area map needs to be extended
346833af842STejun Heo  * @chunk: chunk of interest
3479f7dcf22STejun Heo  *
348833af842STejun Heo  * Determine whether area map of @chunk needs to be extended to
349833af842STejun Heo  * accomodate a new allocation.
3509f7dcf22STejun Heo  *
351ccea34b5STejun Heo  * CONTEXT:
352833af842STejun Heo  * pcpu_lock.
353ccea34b5STejun Heo  *
3549f7dcf22STejun Heo  * RETURNS:
355833af842STejun Heo  * New target map allocation length if extension is necessary, 0
356833af842STejun Heo  * otherwise.
3579f7dcf22STejun Heo  */
358833af842STejun Heo static int pcpu_need_to_extend(struct pcpu_chunk *chunk)
3599f7dcf22STejun Heo {
3609f7dcf22STejun Heo 	int new_alloc;
3619f7dcf22STejun Heo 
3629f7dcf22STejun Heo 	if (chunk->map_alloc >= chunk->map_used + 2)
3639f7dcf22STejun Heo 		return 0;
3649f7dcf22STejun Heo 
3659f7dcf22STejun Heo 	new_alloc = PCPU_DFL_MAP_ALLOC;
3669f7dcf22STejun Heo 	while (new_alloc < chunk->map_used + 2)
3679f7dcf22STejun Heo 		new_alloc *= 2;
3689f7dcf22STejun Heo 
369833af842STejun Heo 	return new_alloc;
370ccea34b5STejun Heo }
371ccea34b5STejun Heo 
372833af842STejun Heo /**
373833af842STejun Heo  * pcpu_extend_area_map - extend area map of a chunk
374833af842STejun Heo  * @chunk: chunk of interest
375833af842STejun Heo  * @new_alloc: new target allocation length of the area map
376833af842STejun Heo  *
377833af842STejun Heo  * Extend area map of @chunk to have @new_alloc entries.
378833af842STejun Heo  *
379833af842STejun Heo  * CONTEXT:
380833af842STejun Heo  * Does GFP_KERNEL allocation.  Grabs and releases pcpu_lock.
381833af842STejun Heo  *
382833af842STejun Heo  * RETURNS:
383833af842STejun Heo  * 0 on success, -errno on failure.
384ccea34b5STejun Heo  */
385833af842STejun Heo static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
386833af842STejun Heo {
387833af842STejun Heo 	int *old = NULL, *new = NULL;
388833af842STejun Heo 	size_t old_size = 0, new_size = new_alloc * sizeof(new[0]);
389833af842STejun Heo 	unsigned long flags;
3909f7dcf22STejun Heo 
391833af842STejun Heo 	new = pcpu_mem_alloc(new_size);
392833af842STejun Heo 	if (!new)
393833af842STejun Heo 		return -ENOMEM;
394833af842STejun Heo 
395833af842STejun Heo 	/* acquire pcpu_lock and switch to new area map */
396833af842STejun Heo 	spin_lock_irqsave(&pcpu_lock, flags);
397833af842STejun Heo 
398833af842STejun Heo 	if (new_alloc <= chunk->map_alloc)
399833af842STejun Heo 		goto out_unlock;
400833af842STejun Heo 
401833af842STejun Heo 	old_size = chunk->map_alloc * sizeof(chunk->map[0]);
402a002d148SHuang Shijie 	old = chunk->map;
403a002d148SHuang Shijie 
404a002d148SHuang Shijie 	memcpy(new, old, old_size);
4059f7dcf22STejun Heo 
4069f7dcf22STejun Heo 	chunk->map_alloc = new_alloc;
4079f7dcf22STejun Heo 	chunk->map = new;
408833af842STejun Heo 	new = NULL;
409833af842STejun Heo 
410833af842STejun Heo out_unlock:
411833af842STejun Heo 	spin_unlock_irqrestore(&pcpu_lock, flags);
412833af842STejun Heo 
413833af842STejun Heo 	/*
414833af842STejun Heo 	 * pcpu_mem_free() might end up calling vfree() which uses
415833af842STejun Heo 	 * IRQ-unsafe lock and thus can't be called under pcpu_lock.
416833af842STejun Heo 	 */
417833af842STejun Heo 	pcpu_mem_free(old, old_size);
418833af842STejun Heo 	pcpu_mem_free(new, new_size);
419833af842STejun Heo 
4209f7dcf22STejun Heo 	return 0;
4219f7dcf22STejun Heo }
4229f7dcf22STejun Heo 
4239f7dcf22STejun Heo /**
424fbf59bc9STejun Heo  * pcpu_split_block - split a map block
425fbf59bc9STejun Heo  * @chunk: chunk of interest
426fbf59bc9STejun Heo  * @i: index of map block to split
427cae3aeb8STejun Heo  * @head: head size in bytes (can be 0)
428cae3aeb8STejun Heo  * @tail: tail size in bytes (can be 0)
429fbf59bc9STejun Heo  *
430fbf59bc9STejun Heo  * Split the @i'th map block into two or three blocks.  If @head is
431fbf59bc9STejun Heo  * non-zero, @head bytes block is inserted before block @i moving it
432fbf59bc9STejun Heo  * to @i+1 and reducing its size by @head bytes.
433fbf59bc9STejun Heo  *
434fbf59bc9STejun Heo  * If @tail is non-zero, the target block, which can be @i or @i+1
435fbf59bc9STejun Heo  * depending on @head, is reduced by @tail bytes and @tail byte block
436fbf59bc9STejun Heo  * is inserted after the target block.
437fbf59bc9STejun Heo  *
4389f7dcf22STejun Heo  * @chunk->map must have enough free slots to accomodate the split.
439ccea34b5STejun Heo  *
440ccea34b5STejun Heo  * CONTEXT:
441ccea34b5STejun Heo  * pcpu_lock.
442fbf59bc9STejun Heo  */
4439f7dcf22STejun Heo static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
4449f7dcf22STejun Heo 			     int head, int tail)
445fbf59bc9STejun Heo {
446fbf59bc9STejun Heo 	int nr_extra = !!head + !!tail;
447fbf59bc9STejun Heo 
4489f7dcf22STejun Heo 	BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
449fbf59bc9STejun Heo 
4509f7dcf22STejun Heo 	/* insert new subblocks */
451fbf59bc9STejun Heo 	memmove(&chunk->map[i + nr_extra], &chunk->map[i],
452fbf59bc9STejun Heo 		sizeof(chunk->map[0]) * (chunk->map_used - i));
453fbf59bc9STejun Heo 	chunk->map_used += nr_extra;
454fbf59bc9STejun Heo 
455fbf59bc9STejun Heo 	if (head) {
456fbf59bc9STejun Heo 		chunk->map[i + 1] = chunk->map[i] - head;
457fbf59bc9STejun Heo 		chunk->map[i++] = head;
458fbf59bc9STejun Heo 	}
459fbf59bc9STejun Heo 	if (tail) {
460fbf59bc9STejun Heo 		chunk->map[i++] -= tail;
461fbf59bc9STejun Heo 		chunk->map[i] = tail;
462fbf59bc9STejun Heo 	}
463fbf59bc9STejun Heo }
464fbf59bc9STejun Heo 
465fbf59bc9STejun Heo /**
466fbf59bc9STejun Heo  * pcpu_alloc_area - allocate area from a pcpu_chunk
467fbf59bc9STejun Heo  * @chunk: chunk of interest
468cae3aeb8STejun Heo  * @size: wanted size in bytes
469fbf59bc9STejun Heo  * @align: wanted align
470fbf59bc9STejun Heo  *
471fbf59bc9STejun Heo  * Try to allocate @size bytes area aligned at @align from @chunk.
472fbf59bc9STejun Heo  * Note that this function only allocates the offset.  It doesn't
473fbf59bc9STejun Heo  * populate or map the area.
474fbf59bc9STejun Heo  *
4759f7dcf22STejun Heo  * @chunk->map must have at least two free slots.
4769f7dcf22STejun Heo  *
477ccea34b5STejun Heo  * CONTEXT:
478ccea34b5STejun Heo  * pcpu_lock.
479ccea34b5STejun Heo  *
480fbf59bc9STejun Heo  * RETURNS:
4819f7dcf22STejun Heo  * Allocated offset in @chunk on success, -1 if no matching area is
4829f7dcf22STejun Heo  * found.
483fbf59bc9STejun Heo  */
484fbf59bc9STejun Heo static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
485fbf59bc9STejun Heo {
486fbf59bc9STejun Heo 	int oslot = pcpu_chunk_slot(chunk);
487fbf59bc9STejun Heo 	int max_contig = 0;
488fbf59bc9STejun Heo 	int i, off;
489fbf59bc9STejun Heo 
490fbf59bc9STejun Heo 	for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
491fbf59bc9STejun Heo 		bool is_last = i + 1 == chunk->map_used;
492fbf59bc9STejun Heo 		int head, tail;
493fbf59bc9STejun Heo 
494fbf59bc9STejun Heo 		/* extra for alignment requirement */
495fbf59bc9STejun Heo 		head = ALIGN(off, align) - off;
496fbf59bc9STejun Heo 		BUG_ON(i == 0 && head != 0);
497fbf59bc9STejun Heo 
498fbf59bc9STejun Heo 		if (chunk->map[i] < 0)
499fbf59bc9STejun Heo 			continue;
500fbf59bc9STejun Heo 		if (chunk->map[i] < head + size) {
501fbf59bc9STejun Heo 			max_contig = max(chunk->map[i], max_contig);
502fbf59bc9STejun Heo 			continue;
503fbf59bc9STejun Heo 		}
504fbf59bc9STejun Heo 
505fbf59bc9STejun Heo 		/*
506fbf59bc9STejun Heo 		 * If head is small or the previous block is free,
507fbf59bc9STejun Heo 		 * merge'em.  Note that 'small' is defined as smaller
508fbf59bc9STejun Heo 		 * than sizeof(int), which is very small but isn't too
509fbf59bc9STejun Heo 		 * uncommon for percpu allocations.
510fbf59bc9STejun Heo 		 */
511fbf59bc9STejun Heo 		if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
512fbf59bc9STejun Heo 			if (chunk->map[i - 1] > 0)
513fbf59bc9STejun Heo 				chunk->map[i - 1] += head;
514fbf59bc9STejun Heo 			else {
515fbf59bc9STejun Heo 				chunk->map[i - 1] -= head;
516fbf59bc9STejun Heo 				chunk->free_size -= head;
517fbf59bc9STejun Heo 			}
518fbf59bc9STejun Heo 			chunk->map[i] -= head;
519fbf59bc9STejun Heo 			off += head;
520fbf59bc9STejun Heo 			head = 0;
521fbf59bc9STejun Heo 		}
522fbf59bc9STejun Heo 
523fbf59bc9STejun Heo 		/* if tail is small, just keep it around */
524fbf59bc9STejun Heo 		tail = chunk->map[i] - head - size;
525fbf59bc9STejun Heo 		if (tail < sizeof(int))
526fbf59bc9STejun Heo 			tail = 0;
527fbf59bc9STejun Heo 
528fbf59bc9STejun Heo 		/* split if warranted */
529fbf59bc9STejun Heo 		if (head || tail) {
5309f7dcf22STejun Heo 			pcpu_split_block(chunk, i, head, tail);
531fbf59bc9STejun Heo 			if (head) {
532fbf59bc9STejun Heo 				i++;
533fbf59bc9STejun Heo 				off += head;
534fbf59bc9STejun Heo 				max_contig = max(chunk->map[i - 1], max_contig);
535fbf59bc9STejun Heo 			}
536fbf59bc9STejun Heo 			if (tail)
537fbf59bc9STejun Heo 				max_contig = max(chunk->map[i + 1], max_contig);
538fbf59bc9STejun Heo 		}
539fbf59bc9STejun Heo 
540fbf59bc9STejun Heo 		/* update hint and mark allocated */
541fbf59bc9STejun Heo 		if (is_last)
542fbf59bc9STejun Heo 			chunk->contig_hint = max_contig; /* fully scanned */
543fbf59bc9STejun Heo 		else
544fbf59bc9STejun Heo 			chunk->contig_hint = max(chunk->contig_hint,
545fbf59bc9STejun Heo 						 max_contig);
546fbf59bc9STejun Heo 
547fbf59bc9STejun Heo 		chunk->free_size -= chunk->map[i];
548fbf59bc9STejun Heo 		chunk->map[i] = -chunk->map[i];
549fbf59bc9STejun Heo 
550fbf59bc9STejun Heo 		pcpu_chunk_relocate(chunk, oslot);
551fbf59bc9STejun Heo 		return off;
552fbf59bc9STejun Heo 	}
553fbf59bc9STejun Heo 
554fbf59bc9STejun Heo 	chunk->contig_hint = max_contig;	/* fully scanned */
555fbf59bc9STejun Heo 	pcpu_chunk_relocate(chunk, oslot);
556fbf59bc9STejun Heo 
5579f7dcf22STejun Heo 	/* tell the upper layer that this chunk has no matching area */
5589f7dcf22STejun Heo 	return -1;
559fbf59bc9STejun Heo }
560fbf59bc9STejun Heo 
561fbf59bc9STejun Heo /**
562fbf59bc9STejun Heo  * pcpu_free_area - free area to a pcpu_chunk
563fbf59bc9STejun Heo  * @chunk: chunk of interest
564fbf59bc9STejun Heo  * @freeme: offset of area to free
565fbf59bc9STejun Heo  *
566fbf59bc9STejun Heo  * Free area starting from @freeme to @chunk.  Note that this function
567fbf59bc9STejun Heo  * only modifies the allocation map.  It doesn't depopulate or unmap
568fbf59bc9STejun Heo  * the area.
569ccea34b5STejun Heo  *
570ccea34b5STejun Heo  * CONTEXT:
571ccea34b5STejun Heo  * pcpu_lock.
572fbf59bc9STejun Heo  */
573fbf59bc9STejun Heo static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
574fbf59bc9STejun Heo {
575fbf59bc9STejun Heo 	int oslot = pcpu_chunk_slot(chunk);
576fbf59bc9STejun Heo 	int i, off;
577fbf59bc9STejun Heo 
578fbf59bc9STejun Heo 	for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
579fbf59bc9STejun Heo 		if (off == freeme)
580fbf59bc9STejun Heo 			break;
581fbf59bc9STejun Heo 	BUG_ON(off != freeme);
582fbf59bc9STejun Heo 	BUG_ON(chunk->map[i] > 0);
583fbf59bc9STejun Heo 
584fbf59bc9STejun Heo 	chunk->map[i] = -chunk->map[i];
585fbf59bc9STejun Heo 	chunk->free_size += chunk->map[i];
586fbf59bc9STejun Heo 
587fbf59bc9STejun Heo 	/* merge with previous? */
588fbf59bc9STejun Heo 	if (i > 0 && chunk->map[i - 1] >= 0) {
589fbf59bc9STejun Heo 		chunk->map[i - 1] += chunk->map[i];
590fbf59bc9STejun Heo 		chunk->map_used--;
591fbf59bc9STejun Heo 		memmove(&chunk->map[i], &chunk->map[i + 1],
592fbf59bc9STejun Heo 			(chunk->map_used - i) * sizeof(chunk->map[0]));
593fbf59bc9STejun Heo 		i--;
594fbf59bc9STejun Heo 	}
595fbf59bc9STejun Heo 	/* merge with next? */
596fbf59bc9STejun Heo 	if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
597fbf59bc9STejun Heo 		chunk->map[i] += chunk->map[i + 1];
598fbf59bc9STejun Heo 		chunk->map_used--;
599fbf59bc9STejun Heo 		memmove(&chunk->map[i + 1], &chunk->map[i + 2],
600fbf59bc9STejun Heo 			(chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
601fbf59bc9STejun Heo 	}
602fbf59bc9STejun Heo 
603fbf59bc9STejun Heo 	chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
604fbf59bc9STejun Heo 	pcpu_chunk_relocate(chunk, oslot);
605fbf59bc9STejun Heo }
606fbf59bc9STejun Heo 
6076081089fSTejun Heo static struct pcpu_chunk *pcpu_alloc_chunk(void)
6086081089fSTejun Heo {
6096081089fSTejun Heo 	struct pcpu_chunk *chunk;
6106081089fSTejun Heo 
611099a19d9STejun Heo 	chunk = pcpu_mem_alloc(pcpu_chunk_struct_size);
6126081089fSTejun Heo 	if (!chunk)
6136081089fSTejun Heo 		return NULL;
6146081089fSTejun Heo 
6156081089fSTejun Heo 	chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
6166081089fSTejun Heo 	if (!chunk->map) {
6176081089fSTejun Heo 		kfree(chunk);
6186081089fSTejun Heo 		return NULL;
6196081089fSTejun Heo 	}
6206081089fSTejun Heo 
6216081089fSTejun Heo 	chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
6226081089fSTejun Heo 	chunk->map[chunk->map_used++] = pcpu_unit_size;
6236081089fSTejun Heo 
6246081089fSTejun Heo 	INIT_LIST_HEAD(&chunk->list);
6256081089fSTejun Heo 	chunk->free_size = pcpu_unit_size;
6266081089fSTejun Heo 	chunk->contig_hint = pcpu_unit_size;
6276081089fSTejun Heo 
6286081089fSTejun Heo 	return chunk;
6296081089fSTejun Heo }
6306081089fSTejun Heo 
6316081089fSTejun Heo static void pcpu_free_chunk(struct pcpu_chunk *chunk)
6326081089fSTejun Heo {
6336081089fSTejun Heo 	if (!chunk)
6346081089fSTejun Heo 		return;
6356081089fSTejun Heo 	pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
6366081089fSTejun Heo 	kfree(chunk);
6376081089fSTejun Heo }
6386081089fSTejun Heo 
639fbf59bc9STejun Heo /*
6409f645532STejun Heo  * Chunk management implementation.
641fbf59bc9STejun Heo  *
6429f645532STejun Heo  * To allow different implementations, chunk alloc/free and
6439f645532STejun Heo  * [de]population are implemented in a separate file which is pulled
6449f645532STejun Heo  * into this file and compiled together.  The following functions
6459f645532STejun Heo  * should be implemented.
646ccea34b5STejun Heo  *
6479f645532STejun Heo  * pcpu_populate_chunk		- populate the specified range of a chunk
6489f645532STejun Heo  * pcpu_depopulate_chunk	- depopulate the specified range of a chunk
6499f645532STejun Heo  * pcpu_create_chunk		- create a new chunk
6509f645532STejun Heo  * pcpu_destroy_chunk		- destroy a chunk, always preceded by full depop
6519f645532STejun Heo  * pcpu_addr_to_page		- translate address to physical address
6529f645532STejun Heo  * pcpu_verify_alloc_info	- check alloc_info is acceptable during init
653fbf59bc9STejun Heo  */
6549f645532STejun Heo static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size);
6559f645532STejun Heo static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size);
6569f645532STejun Heo static struct pcpu_chunk *pcpu_create_chunk(void);
6579f645532STejun Heo static void pcpu_destroy_chunk(struct pcpu_chunk *chunk);
6589f645532STejun Heo static struct page *pcpu_addr_to_page(void *addr);
6599f645532STejun Heo static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai);
660fbf59bc9STejun Heo 
661b0c9778bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_KM
662b0c9778bSTejun Heo #include "percpu-km.c"
663b0c9778bSTejun Heo #else
6649f645532STejun Heo #include "percpu-vm.c"
665b0c9778bSTejun Heo #endif
666fbf59bc9STejun Heo 
667fbf59bc9STejun Heo /**
66888999a89STejun Heo  * pcpu_chunk_addr_search - determine chunk containing specified address
66988999a89STejun Heo  * @addr: address for which the chunk needs to be determined.
67088999a89STejun Heo  *
67188999a89STejun Heo  * RETURNS:
67288999a89STejun Heo  * The address of the found chunk.
67388999a89STejun Heo  */
67488999a89STejun Heo static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
67588999a89STejun Heo {
67688999a89STejun Heo 	/* is it in the first chunk? */
67788999a89STejun Heo 	if (pcpu_addr_in_first_chunk(addr)) {
67888999a89STejun Heo 		/* is it in the reserved area? */
67988999a89STejun Heo 		if (pcpu_addr_in_reserved_chunk(addr))
68088999a89STejun Heo 			return pcpu_reserved_chunk;
68188999a89STejun Heo 		return pcpu_first_chunk;
68288999a89STejun Heo 	}
68388999a89STejun Heo 
68488999a89STejun Heo 	/*
68588999a89STejun Heo 	 * The address is relative to unit0 which might be unused and
68688999a89STejun Heo 	 * thus unmapped.  Offset the address to the unit space of the
68788999a89STejun Heo 	 * current processor before looking it up in the vmalloc
68888999a89STejun Heo 	 * space.  Note that any possible cpu id can be used here, so
68988999a89STejun Heo 	 * there's no need to worry about preemption or cpu hotplug.
69088999a89STejun Heo 	 */
69188999a89STejun Heo 	addr += pcpu_unit_offsets[raw_smp_processor_id()];
6929f645532STejun Heo 	return pcpu_get_page_chunk(pcpu_addr_to_page(addr));
69388999a89STejun Heo }
69488999a89STejun Heo 
69588999a89STejun Heo /**
696edcb4639STejun Heo  * pcpu_alloc - the percpu allocator
697cae3aeb8STejun Heo  * @size: size of area to allocate in bytes
698fbf59bc9STejun Heo  * @align: alignment of area (max PAGE_SIZE)
699edcb4639STejun Heo  * @reserved: allocate from the reserved chunk if available
700fbf59bc9STejun Heo  *
701ccea34b5STejun Heo  * Allocate percpu area of @size bytes aligned at @align.
702ccea34b5STejun Heo  *
703ccea34b5STejun Heo  * CONTEXT:
704ccea34b5STejun Heo  * Does GFP_KERNEL allocation.
705fbf59bc9STejun Heo  *
706fbf59bc9STejun Heo  * RETURNS:
707fbf59bc9STejun Heo  * Percpu pointer to the allocated area on success, NULL on failure.
708fbf59bc9STejun Heo  */
70943cf38ebSTejun Heo static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved)
710fbf59bc9STejun Heo {
711f2badb0cSTejun Heo 	static int warn_limit = 10;
712fbf59bc9STejun Heo 	struct pcpu_chunk *chunk;
713f2badb0cSTejun Heo 	const char *err;
714833af842STejun Heo 	int slot, off, new_alloc;
715403a91b1SJiri Kosina 	unsigned long flags;
716fbf59bc9STejun Heo 
7178d408b4bSTejun Heo 	if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
718fbf59bc9STejun Heo 		WARN(true, "illegal size (%zu) or align (%zu) for "
719fbf59bc9STejun Heo 		     "percpu allocation\n", size, align);
720fbf59bc9STejun Heo 		return NULL;
721fbf59bc9STejun Heo 	}
722fbf59bc9STejun Heo 
723ccea34b5STejun Heo 	mutex_lock(&pcpu_alloc_mutex);
724403a91b1SJiri Kosina 	spin_lock_irqsave(&pcpu_lock, flags);
725fbf59bc9STejun Heo 
726edcb4639STejun Heo 	/* serve reserved allocations from the reserved chunk if available */
727edcb4639STejun Heo 	if (reserved && pcpu_reserved_chunk) {
728edcb4639STejun Heo 		chunk = pcpu_reserved_chunk;
729833af842STejun Heo 
730833af842STejun Heo 		if (size > chunk->contig_hint) {
731833af842STejun Heo 			err = "alloc from reserved chunk failed";
732ccea34b5STejun Heo 			goto fail_unlock;
733f2badb0cSTejun Heo 		}
734833af842STejun Heo 
735833af842STejun Heo 		while ((new_alloc = pcpu_need_to_extend(chunk))) {
736833af842STejun Heo 			spin_unlock_irqrestore(&pcpu_lock, flags);
737833af842STejun Heo 			if (pcpu_extend_area_map(chunk, new_alloc) < 0) {
738833af842STejun Heo 				err = "failed to extend area map of reserved chunk";
739833af842STejun Heo 				goto fail_unlock_mutex;
740833af842STejun Heo 			}
741833af842STejun Heo 			spin_lock_irqsave(&pcpu_lock, flags);
742833af842STejun Heo 		}
743833af842STejun Heo 
744edcb4639STejun Heo 		off = pcpu_alloc_area(chunk, size, align);
745edcb4639STejun Heo 		if (off >= 0)
746edcb4639STejun Heo 			goto area_found;
747833af842STejun Heo 
748f2badb0cSTejun Heo 		err = "alloc from reserved chunk failed";
749ccea34b5STejun Heo 		goto fail_unlock;
750edcb4639STejun Heo 	}
751edcb4639STejun Heo 
752ccea34b5STejun Heo restart:
753edcb4639STejun Heo 	/* search through normal chunks */
754fbf59bc9STejun Heo 	for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
755fbf59bc9STejun Heo 		list_for_each_entry(chunk, &pcpu_slot[slot], list) {
756fbf59bc9STejun Heo 			if (size > chunk->contig_hint)
757fbf59bc9STejun Heo 				continue;
758ccea34b5STejun Heo 
759833af842STejun Heo 			new_alloc = pcpu_need_to_extend(chunk);
760833af842STejun Heo 			if (new_alloc) {
761833af842STejun Heo 				spin_unlock_irqrestore(&pcpu_lock, flags);
762833af842STejun Heo 				if (pcpu_extend_area_map(chunk,
763833af842STejun Heo 							 new_alloc) < 0) {
764f2badb0cSTejun Heo 					err = "failed to extend area map";
765833af842STejun Heo 					goto fail_unlock_mutex;
766833af842STejun Heo 				}
767833af842STejun Heo 				spin_lock_irqsave(&pcpu_lock, flags);
768833af842STejun Heo 				/*
769833af842STejun Heo 				 * pcpu_lock has been dropped, need to
770833af842STejun Heo 				 * restart cpu_slot list walking.
771833af842STejun Heo 				 */
772833af842STejun Heo 				goto restart;
773ccea34b5STejun Heo 			}
774ccea34b5STejun Heo 
775fbf59bc9STejun Heo 			off = pcpu_alloc_area(chunk, size, align);
776fbf59bc9STejun Heo 			if (off >= 0)
777fbf59bc9STejun Heo 				goto area_found;
778fbf59bc9STejun Heo 		}
779fbf59bc9STejun Heo 	}
780fbf59bc9STejun Heo 
781fbf59bc9STejun Heo 	/* hmmm... no space left, create a new chunk */
782403a91b1SJiri Kosina 	spin_unlock_irqrestore(&pcpu_lock, flags);
783ccea34b5STejun Heo 
7846081089fSTejun Heo 	chunk = pcpu_create_chunk();
785f2badb0cSTejun Heo 	if (!chunk) {
786f2badb0cSTejun Heo 		err = "failed to allocate new chunk";
787ccea34b5STejun Heo 		goto fail_unlock_mutex;
788f2badb0cSTejun Heo 	}
789ccea34b5STejun Heo 
790403a91b1SJiri Kosina 	spin_lock_irqsave(&pcpu_lock, flags);
791fbf59bc9STejun Heo 	pcpu_chunk_relocate(chunk, -1);
792ccea34b5STejun Heo 	goto restart;
793fbf59bc9STejun Heo 
794fbf59bc9STejun Heo area_found:
795403a91b1SJiri Kosina 	spin_unlock_irqrestore(&pcpu_lock, flags);
796ccea34b5STejun Heo 
797fbf59bc9STejun Heo 	/* populate, map and clear the area */
798fbf59bc9STejun Heo 	if (pcpu_populate_chunk(chunk, off, size)) {
799403a91b1SJiri Kosina 		spin_lock_irqsave(&pcpu_lock, flags);
800fbf59bc9STejun Heo 		pcpu_free_area(chunk, off);
801f2badb0cSTejun Heo 		err = "failed to populate";
802ccea34b5STejun Heo 		goto fail_unlock;
803fbf59bc9STejun Heo 	}
804fbf59bc9STejun Heo 
805ccea34b5STejun Heo 	mutex_unlock(&pcpu_alloc_mutex);
806ccea34b5STejun Heo 
807bba174f5STejun Heo 	/* return address relative to base address */
808bba174f5STejun Heo 	return __addr_to_pcpu_ptr(chunk->base_addr + off);
809ccea34b5STejun Heo 
810ccea34b5STejun Heo fail_unlock:
811403a91b1SJiri Kosina 	spin_unlock_irqrestore(&pcpu_lock, flags);
812ccea34b5STejun Heo fail_unlock_mutex:
813ccea34b5STejun Heo 	mutex_unlock(&pcpu_alloc_mutex);
814f2badb0cSTejun Heo 	if (warn_limit) {
815f2badb0cSTejun Heo 		pr_warning("PERCPU: allocation failed, size=%zu align=%zu, "
816f2badb0cSTejun Heo 			   "%s\n", size, align, err);
817f2badb0cSTejun Heo 		dump_stack();
818f2badb0cSTejun Heo 		if (!--warn_limit)
819f2badb0cSTejun Heo 			pr_info("PERCPU: limit reached, disable warning\n");
820f2badb0cSTejun Heo 	}
821ccea34b5STejun Heo 	return NULL;
822fbf59bc9STejun Heo }
823edcb4639STejun Heo 
824edcb4639STejun Heo /**
825edcb4639STejun Heo  * __alloc_percpu - allocate dynamic percpu area
826edcb4639STejun Heo  * @size: size of area to allocate in bytes
827edcb4639STejun Heo  * @align: alignment of area (max PAGE_SIZE)
828edcb4639STejun Heo  *
829edcb4639STejun Heo  * Allocate percpu area of @size bytes aligned at @align.  Might
830edcb4639STejun Heo  * sleep.  Might trigger writeouts.
831edcb4639STejun Heo  *
832ccea34b5STejun Heo  * CONTEXT:
833ccea34b5STejun Heo  * Does GFP_KERNEL allocation.
834ccea34b5STejun Heo  *
835edcb4639STejun Heo  * RETURNS:
836edcb4639STejun Heo  * Percpu pointer to the allocated area on success, NULL on failure.
837edcb4639STejun Heo  */
83843cf38ebSTejun Heo void __percpu *__alloc_percpu(size_t size, size_t align)
839edcb4639STejun Heo {
840edcb4639STejun Heo 	return pcpu_alloc(size, align, false);
841edcb4639STejun Heo }
842fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu);
843fbf59bc9STejun Heo 
844edcb4639STejun Heo /**
845edcb4639STejun Heo  * __alloc_reserved_percpu - allocate reserved percpu area
846edcb4639STejun Heo  * @size: size of area to allocate in bytes
847edcb4639STejun Heo  * @align: alignment of area (max PAGE_SIZE)
848edcb4639STejun Heo  *
849edcb4639STejun Heo  * Allocate percpu area of @size bytes aligned at @align from reserved
850edcb4639STejun Heo  * percpu area if arch has set it up; otherwise, allocation is served
851edcb4639STejun Heo  * from the same dynamic area.  Might sleep.  Might trigger writeouts.
852edcb4639STejun Heo  *
853ccea34b5STejun Heo  * CONTEXT:
854ccea34b5STejun Heo  * Does GFP_KERNEL allocation.
855ccea34b5STejun Heo  *
856edcb4639STejun Heo  * RETURNS:
857edcb4639STejun Heo  * Percpu pointer to the allocated area on success, NULL on failure.
858edcb4639STejun Heo  */
85943cf38ebSTejun Heo void __percpu *__alloc_reserved_percpu(size_t size, size_t align)
860edcb4639STejun Heo {
861edcb4639STejun Heo 	return pcpu_alloc(size, align, true);
862edcb4639STejun Heo }
863edcb4639STejun Heo 
864a56dbddfSTejun Heo /**
865a56dbddfSTejun Heo  * pcpu_reclaim - reclaim fully free chunks, workqueue function
866a56dbddfSTejun Heo  * @work: unused
867a56dbddfSTejun Heo  *
868a56dbddfSTejun Heo  * Reclaim all fully free chunks except for the first one.
869ccea34b5STejun Heo  *
870ccea34b5STejun Heo  * CONTEXT:
871ccea34b5STejun Heo  * workqueue context.
872a56dbddfSTejun Heo  */
873a56dbddfSTejun Heo static void pcpu_reclaim(struct work_struct *work)
874fbf59bc9STejun Heo {
875a56dbddfSTejun Heo 	LIST_HEAD(todo);
876a56dbddfSTejun Heo 	struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
877a56dbddfSTejun Heo 	struct pcpu_chunk *chunk, *next;
878a56dbddfSTejun Heo 
879ccea34b5STejun Heo 	mutex_lock(&pcpu_alloc_mutex);
880ccea34b5STejun Heo 	spin_lock_irq(&pcpu_lock);
881a56dbddfSTejun Heo 
882a56dbddfSTejun Heo 	list_for_each_entry_safe(chunk, next, head, list) {
8838d408b4bSTejun Heo 		WARN_ON(chunk->immutable);
884a56dbddfSTejun Heo 
885a56dbddfSTejun Heo 		/* spare the first one */
886a56dbddfSTejun Heo 		if (chunk == list_first_entry(head, struct pcpu_chunk, list))
887a56dbddfSTejun Heo 			continue;
888a56dbddfSTejun Heo 
889a56dbddfSTejun Heo 		list_move(&chunk->list, &todo);
890a56dbddfSTejun Heo 	}
891a56dbddfSTejun Heo 
892ccea34b5STejun Heo 	spin_unlock_irq(&pcpu_lock);
893a56dbddfSTejun Heo 
894a56dbddfSTejun Heo 	list_for_each_entry_safe(chunk, next, &todo, list) {
895ce3141a2STejun Heo 		pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size);
8966081089fSTejun Heo 		pcpu_destroy_chunk(chunk);
897fbf59bc9STejun Heo 	}
898971f3918STejun Heo 
899971f3918STejun Heo 	mutex_unlock(&pcpu_alloc_mutex);
900a56dbddfSTejun Heo }
901fbf59bc9STejun Heo 
902fbf59bc9STejun Heo /**
903fbf59bc9STejun Heo  * free_percpu - free percpu area
904fbf59bc9STejun Heo  * @ptr: pointer to area to free
905fbf59bc9STejun Heo  *
906ccea34b5STejun Heo  * Free percpu area @ptr.
907ccea34b5STejun Heo  *
908ccea34b5STejun Heo  * CONTEXT:
909ccea34b5STejun Heo  * Can be called from atomic context.
910fbf59bc9STejun Heo  */
91143cf38ebSTejun Heo void free_percpu(void __percpu *ptr)
912fbf59bc9STejun Heo {
913129182e5SAndrew Morton 	void *addr;
914fbf59bc9STejun Heo 	struct pcpu_chunk *chunk;
915ccea34b5STejun Heo 	unsigned long flags;
916fbf59bc9STejun Heo 	int off;
917fbf59bc9STejun Heo 
918fbf59bc9STejun Heo 	if (!ptr)
919fbf59bc9STejun Heo 		return;
920fbf59bc9STejun Heo 
921129182e5SAndrew Morton 	addr = __pcpu_ptr_to_addr(ptr);
922129182e5SAndrew Morton 
923ccea34b5STejun Heo 	spin_lock_irqsave(&pcpu_lock, flags);
924fbf59bc9STejun Heo 
925fbf59bc9STejun Heo 	chunk = pcpu_chunk_addr_search(addr);
926bba174f5STejun Heo 	off = addr - chunk->base_addr;
927fbf59bc9STejun Heo 
928fbf59bc9STejun Heo 	pcpu_free_area(chunk, off);
929fbf59bc9STejun Heo 
930a56dbddfSTejun Heo 	/* if there are more than one fully free chunks, wake up grim reaper */
931fbf59bc9STejun Heo 	if (chunk->free_size == pcpu_unit_size) {
932fbf59bc9STejun Heo 		struct pcpu_chunk *pos;
933fbf59bc9STejun Heo 
934a56dbddfSTejun Heo 		list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
935fbf59bc9STejun Heo 			if (pos != chunk) {
936a56dbddfSTejun Heo 				schedule_work(&pcpu_reclaim_work);
937fbf59bc9STejun Heo 				break;
938fbf59bc9STejun Heo 			}
939fbf59bc9STejun Heo 	}
940fbf59bc9STejun Heo 
941ccea34b5STejun Heo 	spin_unlock_irqrestore(&pcpu_lock, flags);
942fbf59bc9STejun Heo }
943fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(free_percpu);
944fbf59bc9STejun Heo 
9453b034b0dSVivek Goyal /**
94610fad5e4STejun Heo  * is_kernel_percpu_address - test whether address is from static percpu area
94710fad5e4STejun Heo  * @addr: address to test
94810fad5e4STejun Heo  *
94910fad5e4STejun Heo  * Test whether @addr belongs to in-kernel static percpu area.  Module
95010fad5e4STejun Heo  * static percpu areas are not considered.  For those, use
95110fad5e4STejun Heo  * is_module_percpu_address().
95210fad5e4STejun Heo  *
95310fad5e4STejun Heo  * RETURNS:
95410fad5e4STejun Heo  * %true if @addr is from in-kernel static percpu area, %false otherwise.
95510fad5e4STejun Heo  */
95610fad5e4STejun Heo bool is_kernel_percpu_address(unsigned long addr)
95710fad5e4STejun Heo {
958bbddff05STejun Heo #ifdef CONFIG_SMP
95910fad5e4STejun Heo 	const size_t static_size = __per_cpu_end - __per_cpu_start;
96010fad5e4STejun Heo 	void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
96110fad5e4STejun Heo 	unsigned int cpu;
96210fad5e4STejun Heo 
96310fad5e4STejun Heo 	for_each_possible_cpu(cpu) {
96410fad5e4STejun Heo 		void *start = per_cpu_ptr(base, cpu);
96510fad5e4STejun Heo 
96610fad5e4STejun Heo 		if ((void *)addr >= start && (void *)addr < start + static_size)
96710fad5e4STejun Heo 			return true;
96810fad5e4STejun Heo         }
969bbddff05STejun Heo #endif
970bbddff05STejun Heo 	/* on UP, can't distinguish from other static vars, always false */
97110fad5e4STejun Heo 	return false;
97210fad5e4STejun Heo }
97310fad5e4STejun Heo 
97410fad5e4STejun Heo /**
9753b034b0dSVivek Goyal  * per_cpu_ptr_to_phys - convert translated percpu address to physical address
9763b034b0dSVivek Goyal  * @addr: the address to be converted to physical address
9773b034b0dSVivek Goyal  *
9783b034b0dSVivek Goyal  * Given @addr which is dereferenceable address obtained via one of
9793b034b0dSVivek Goyal  * percpu access macros, this function translates it into its physical
9803b034b0dSVivek Goyal  * address.  The caller is responsible for ensuring @addr stays valid
9813b034b0dSVivek Goyal  * until this function finishes.
9823b034b0dSVivek Goyal  *
9833b034b0dSVivek Goyal  * RETURNS:
9843b034b0dSVivek Goyal  * The physical address for @addr.
9853b034b0dSVivek Goyal  */
9863b034b0dSVivek Goyal phys_addr_t per_cpu_ptr_to_phys(void *addr)
9873b034b0dSVivek Goyal {
9889983b6f0STejun Heo 	void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr);
9899983b6f0STejun Heo 	bool in_first_chunk = false;
9909983b6f0STejun Heo 	unsigned long first_start, first_end;
9919983b6f0STejun Heo 	unsigned int cpu;
9929983b6f0STejun Heo 
9939983b6f0STejun Heo 	/*
9949983b6f0STejun Heo 	 * The following test on first_start/end isn't strictly
9959983b6f0STejun Heo 	 * necessary but will speed up lookups of addresses which
9969983b6f0STejun Heo 	 * aren't in the first chunk.
9979983b6f0STejun Heo 	 */
9989983b6f0STejun Heo 	first_start = pcpu_chunk_addr(pcpu_first_chunk, pcpu_first_unit_cpu, 0);
9999983b6f0STejun Heo 	first_end = pcpu_chunk_addr(pcpu_first_chunk, pcpu_last_unit_cpu,
10009983b6f0STejun Heo 				    pcpu_unit_pages);
10019983b6f0STejun Heo 	if ((unsigned long)addr >= first_start &&
10029983b6f0STejun Heo 	    (unsigned long)addr < first_end) {
10039983b6f0STejun Heo 		for_each_possible_cpu(cpu) {
10049983b6f0STejun Heo 			void *start = per_cpu_ptr(base, cpu);
10059983b6f0STejun Heo 
10069983b6f0STejun Heo 			if (addr >= start && addr < start + pcpu_unit_size) {
10079983b6f0STejun Heo 				in_first_chunk = true;
10089983b6f0STejun Heo 				break;
10099983b6f0STejun Heo 			}
10109983b6f0STejun Heo 		}
10119983b6f0STejun Heo 	}
10129983b6f0STejun Heo 
10139983b6f0STejun Heo 	if (in_first_chunk) {
10143b034b0dSVivek Goyal 		if ((unsigned long)addr < VMALLOC_START ||
10153b034b0dSVivek Goyal 		    (unsigned long)addr >= VMALLOC_END)
10163b034b0dSVivek Goyal 			return __pa(addr);
10173b034b0dSVivek Goyal 		else
10183b034b0dSVivek Goyal 			return page_to_phys(vmalloc_to_page(addr));
1019020ec653STejun Heo 	} else
10209f645532STejun Heo 		return page_to_phys(pcpu_addr_to_page(addr));
10213b034b0dSVivek Goyal }
10223b034b0dSVivek Goyal 
1023fbf59bc9STejun Heo /**
1024fd1e8a1fSTejun Heo  * pcpu_alloc_alloc_info - allocate percpu allocation info
1025fd1e8a1fSTejun Heo  * @nr_groups: the number of groups
1026fd1e8a1fSTejun Heo  * @nr_units: the number of units
1027033e48fbSTejun Heo  *
1028fd1e8a1fSTejun Heo  * Allocate ai which is large enough for @nr_groups groups containing
1029fd1e8a1fSTejun Heo  * @nr_units units.  The returned ai's groups[0].cpu_map points to the
1030fd1e8a1fSTejun Heo  * cpu_map array which is long enough for @nr_units and filled with
1031fd1e8a1fSTejun Heo  * NR_CPUS.  It's the caller's responsibility to initialize cpu_map
1032fd1e8a1fSTejun Heo  * pointer of other groups.
1033033e48fbSTejun Heo  *
1034033e48fbSTejun Heo  * RETURNS:
1035fd1e8a1fSTejun Heo  * Pointer to the allocated pcpu_alloc_info on success, NULL on
1036fd1e8a1fSTejun Heo  * failure.
1037033e48fbSTejun Heo  */
1038fd1e8a1fSTejun Heo struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
1039fd1e8a1fSTejun Heo 						      int nr_units)
1040fd1e8a1fSTejun Heo {
1041fd1e8a1fSTejun Heo 	struct pcpu_alloc_info *ai;
1042fd1e8a1fSTejun Heo 	size_t base_size, ai_size;
1043fd1e8a1fSTejun Heo 	void *ptr;
1044fd1e8a1fSTejun Heo 	int unit;
1045fd1e8a1fSTejun Heo 
1046fd1e8a1fSTejun Heo 	base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]),
1047fd1e8a1fSTejun Heo 			  __alignof__(ai->groups[0].cpu_map[0]));
1048fd1e8a1fSTejun Heo 	ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]);
1049fd1e8a1fSTejun Heo 
1050fd1e8a1fSTejun Heo 	ptr = alloc_bootmem_nopanic(PFN_ALIGN(ai_size));
1051fd1e8a1fSTejun Heo 	if (!ptr)
1052fd1e8a1fSTejun Heo 		return NULL;
1053fd1e8a1fSTejun Heo 	ai = ptr;
1054fd1e8a1fSTejun Heo 	ptr += base_size;
1055fd1e8a1fSTejun Heo 
1056fd1e8a1fSTejun Heo 	ai->groups[0].cpu_map = ptr;
1057fd1e8a1fSTejun Heo 
1058fd1e8a1fSTejun Heo 	for (unit = 0; unit < nr_units; unit++)
1059fd1e8a1fSTejun Heo 		ai->groups[0].cpu_map[unit] = NR_CPUS;
1060fd1e8a1fSTejun Heo 
1061fd1e8a1fSTejun Heo 	ai->nr_groups = nr_groups;
1062fd1e8a1fSTejun Heo 	ai->__ai_size = PFN_ALIGN(ai_size);
1063fd1e8a1fSTejun Heo 
1064fd1e8a1fSTejun Heo 	return ai;
1065fd1e8a1fSTejun Heo }
1066fd1e8a1fSTejun Heo 
1067fd1e8a1fSTejun Heo /**
1068fd1e8a1fSTejun Heo  * pcpu_free_alloc_info - free percpu allocation info
1069fd1e8a1fSTejun Heo  * @ai: pcpu_alloc_info to free
1070fd1e8a1fSTejun Heo  *
1071fd1e8a1fSTejun Heo  * Free @ai which was allocated by pcpu_alloc_alloc_info().
1072fd1e8a1fSTejun Heo  */
1073fd1e8a1fSTejun Heo void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai)
1074fd1e8a1fSTejun Heo {
1075fd1e8a1fSTejun Heo 	free_bootmem(__pa(ai), ai->__ai_size);
1076fd1e8a1fSTejun Heo }
1077fd1e8a1fSTejun Heo 
1078fd1e8a1fSTejun Heo /**
1079fd1e8a1fSTejun Heo  * pcpu_dump_alloc_info - print out information about pcpu_alloc_info
1080fd1e8a1fSTejun Heo  * @lvl: loglevel
1081fd1e8a1fSTejun Heo  * @ai: allocation info to dump
1082fd1e8a1fSTejun Heo  *
1083fd1e8a1fSTejun Heo  * Print out information about @ai using loglevel @lvl.
1084fd1e8a1fSTejun Heo  */
1085fd1e8a1fSTejun Heo static void pcpu_dump_alloc_info(const char *lvl,
1086fd1e8a1fSTejun Heo 				 const struct pcpu_alloc_info *ai)
1087033e48fbSTejun Heo {
1088fd1e8a1fSTejun Heo 	int group_width = 1, cpu_width = 1, width;
1089033e48fbSTejun Heo 	char empty_str[] = "--------";
1090fd1e8a1fSTejun Heo 	int alloc = 0, alloc_end = 0;
1091fd1e8a1fSTejun Heo 	int group, v;
1092fd1e8a1fSTejun Heo 	int upa, apl;	/* units per alloc, allocs per line */
1093033e48fbSTejun Heo 
1094fd1e8a1fSTejun Heo 	v = ai->nr_groups;
1095033e48fbSTejun Heo 	while (v /= 10)
1096fd1e8a1fSTejun Heo 		group_width++;
1097033e48fbSTejun Heo 
1098fd1e8a1fSTejun Heo 	v = num_possible_cpus();
1099fd1e8a1fSTejun Heo 	while (v /= 10)
1100fd1e8a1fSTejun Heo 		cpu_width++;
1101fd1e8a1fSTejun Heo 	empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0';
1102033e48fbSTejun Heo 
1103fd1e8a1fSTejun Heo 	upa = ai->alloc_size / ai->unit_size;
1104fd1e8a1fSTejun Heo 	width = upa * (cpu_width + 1) + group_width + 3;
1105fd1e8a1fSTejun Heo 	apl = rounddown_pow_of_two(max(60 / width, 1));
1106033e48fbSTejun Heo 
1107fd1e8a1fSTejun Heo 	printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu",
1108fd1e8a1fSTejun Heo 	       lvl, ai->static_size, ai->reserved_size, ai->dyn_size,
1109fd1e8a1fSTejun Heo 	       ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size);
1110fd1e8a1fSTejun Heo 
1111fd1e8a1fSTejun Heo 	for (group = 0; group < ai->nr_groups; group++) {
1112fd1e8a1fSTejun Heo 		const struct pcpu_group_info *gi = &ai->groups[group];
1113fd1e8a1fSTejun Heo 		int unit = 0, unit_end = 0;
1114fd1e8a1fSTejun Heo 
1115fd1e8a1fSTejun Heo 		BUG_ON(gi->nr_units % upa);
1116fd1e8a1fSTejun Heo 		for (alloc_end += gi->nr_units / upa;
1117fd1e8a1fSTejun Heo 		     alloc < alloc_end; alloc++) {
1118fd1e8a1fSTejun Heo 			if (!(alloc % apl)) {
1119033e48fbSTejun Heo 				printk("\n");
1120fd1e8a1fSTejun Heo 				printk("%spcpu-alloc: ", lvl);
1121033e48fbSTejun Heo 			}
1122fd1e8a1fSTejun Heo 			printk("[%0*d] ", group_width, group);
1123fd1e8a1fSTejun Heo 
1124fd1e8a1fSTejun Heo 			for (unit_end += upa; unit < unit_end; unit++)
1125fd1e8a1fSTejun Heo 				if (gi->cpu_map[unit] != NR_CPUS)
1126fd1e8a1fSTejun Heo 					printk("%0*d ", cpu_width,
1127fd1e8a1fSTejun Heo 					       gi->cpu_map[unit]);
1128033e48fbSTejun Heo 				else
1129033e48fbSTejun Heo 					printk("%s ", empty_str);
1130033e48fbSTejun Heo 		}
1131fd1e8a1fSTejun Heo 	}
1132033e48fbSTejun Heo 	printk("\n");
1133033e48fbSTejun Heo }
1134033e48fbSTejun Heo 
1135fbf59bc9STejun Heo /**
11368d408b4bSTejun Heo  * pcpu_setup_first_chunk - initialize the first percpu chunk
1137fd1e8a1fSTejun Heo  * @ai: pcpu_alloc_info describing how to percpu area is shaped
113838a6be52STejun Heo  * @base_addr: mapped address
1139fbf59bc9STejun Heo  *
11408d408b4bSTejun Heo  * Initialize the first percpu chunk which contains the kernel static
11418d408b4bSTejun Heo  * perpcu area.  This function is to be called from arch percpu area
114238a6be52STejun Heo  * setup path.
11438d408b4bSTejun Heo  *
1144fd1e8a1fSTejun Heo  * @ai contains all information necessary to initialize the first
1145fd1e8a1fSTejun Heo  * chunk and prime the dynamic percpu allocator.
11468d408b4bSTejun Heo  *
1147fd1e8a1fSTejun Heo  * @ai->static_size is the size of static percpu area.
1148fd1e8a1fSTejun Heo  *
1149fd1e8a1fSTejun Heo  * @ai->reserved_size, if non-zero, specifies the amount of bytes to
1150edcb4639STejun Heo  * reserve after the static area in the first chunk.  This reserves
1151edcb4639STejun Heo  * the first chunk such that it's available only through reserved
1152edcb4639STejun Heo  * percpu allocation.  This is primarily used to serve module percpu
1153edcb4639STejun Heo  * static areas on architectures where the addressing model has
1154edcb4639STejun Heo  * limited offset range for symbol relocations to guarantee module
1155edcb4639STejun Heo  * percpu symbols fall inside the relocatable range.
1156edcb4639STejun Heo  *
1157fd1e8a1fSTejun Heo  * @ai->dyn_size determines the number of bytes available for dynamic
1158fd1e8a1fSTejun Heo  * allocation in the first chunk.  The area between @ai->static_size +
1159fd1e8a1fSTejun Heo  * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused.
11606074d5b0STejun Heo  *
1161fd1e8a1fSTejun Heo  * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE
1162fd1e8a1fSTejun Heo  * and equal to or larger than @ai->static_size + @ai->reserved_size +
1163fd1e8a1fSTejun Heo  * @ai->dyn_size.
11648d408b4bSTejun Heo  *
1165fd1e8a1fSTejun Heo  * @ai->atom_size is the allocation atom size and used as alignment
1166fd1e8a1fSTejun Heo  * for vm areas.
11678d408b4bSTejun Heo  *
1168fd1e8a1fSTejun Heo  * @ai->alloc_size is the allocation size and always multiple of
1169fd1e8a1fSTejun Heo  * @ai->atom_size.  This is larger than @ai->atom_size if
1170fd1e8a1fSTejun Heo  * @ai->unit_size is larger than @ai->atom_size.
1171fd1e8a1fSTejun Heo  *
1172fd1e8a1fSTejun Heo  * @ai->nr_groups and @ai->groups describe virtual memory layout of
1173fd1e8a1fSTejun Heo  * percpu areas.  Units which should be colocated are put into the
1174fd1e8a1fSTejun Heo  * same group.  Dynamic VM areas will be allocated according to these
1175fd1e8a1fSTejun Heo  * groupings.  If @ai->nr_groups is zero, a single group containing
1176fd1e8a1fSTejun Heo  * all units is assumed.
11778d408b4bSTejun Heo  *
117838a6be52STejun Heo  * The caller should have mapped the first chunk at @base_addr and
117938a6be52STejun Heo  * copied static data to each unit.
1180fbf59bc9STejun Heo  *
1181edcb4639STejun Heo  * If the first chunk ends up with both reserved and dynamic areas, it
1182edcb4639STejun Heo  * is served by two chunks - one to serve the core static and reserved
1183edcb4639STejun Heo  * areas and the other for the dynamic area.  They share the same vm
1184edcb4639STejun Heo  * and page map but uses different area allocation map to stay away
1185edcb4639STejun Heo  * from each other.  The latter chunk is circulated in the chunk slots
1186edcb4639STejun Heo  * and available for dynamic allocation like any other chunks.
1187edcb4639STejun Heo  *
1188fbf59bc9STejun Heo  * RETURNS:
1189fb435d52STejun Heo  * 0 on success, -errno on failure.
1190fbf59bc9STejun Heo  */
1191fb435d52STejun Heo int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
1192fd1e8a1fSTejun Heo 				  void *base_addr)
1193fbf59bc9STejun Heo {
1194635b75fcSTejun Heo 	static char cpus_buf[4096] __initdata;
1195099a19d9STejun Heo 	static int smap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
1196099a19d9STejun Heo 	static int dmap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata;
1197fd1e8a1fSTejun Heo 	size_t dyn_size = ai->dyn_size;
1198fd1e8a1fSTejun Heo 	size_t size_sum = ai->static_size + ai->reserved_size + dyn_size;
1199edcb4639STejun Heo 	struct pcpu_chunk *schunk, *dchunk = NULL;
12006563297cSTejun Heo 	unsigned long *group_offsets;
12016563297cSTejun Heo 	size_t *group_sizes;
1202fb435d52STejun Heo 	unsigned long *unit_off;
1203fbf59bc9STejun Heo 	unsigned int cpu;
1204fd1e8a1fSTejun Heo 	int *unit_map;
1205fd1e8a1fSTejun Heo 	int group, unit, i;
1206fbf59bc9STejun Heo 
1207635b75fcSTejun Heo 	cpumask_scnprintf(cpus_buf, sizeof(cpus_buf), cpu_possible_mask);
1208635b75fcSTejun Heo 
1209635b75fcSTejun Heo #define PCPU_SETUP_BUG_ON(cond)	do {					\
1210635b75fcSTejun Heo 	if (unlikely(cond)) {						\
1211635b75fcSTejun Heo 		pr_emerg("PERCPU: failed to initialize, %s", #cond);	\
1212635b75fcSTejun Heo 		pr_emerg("PERCPU: cpu_possible_mask=%s\n", cpus_buf);	\
1213635b75fcSTejun Heo 		pcpu_dump_alloc_info(KERN_EMERG, ai);			\
1214635b75fcSTejun Heo 		BUG();							\
1215635b75fcSTejun Heo 	}								\
1216635b75fcSTejun Heo } while (0)
1217635b75fcSTejun Heo 
12182f39e637STejun Heo 	/* sanity checks */
1219635b75fcSTejun Heo 	PCPU_SETUP_BUG_ON(ai->nr_groups <= 0);
1220bbddff05STejun Heo #ifdef CONFIG_SMP
1221635b75fcSTejun Heo 	PCPU_SETUP_BUG_ON(!ai->static_size);
1222bbddff05STejun Heo #endif
1223635b75fcSTejun Heo 	PCPU_SETUP_BUG_ON(!base_addr);
1224635b75fcSTejun Heo 	PCPU_SETUP_BUG_ON(ai->unit_size < size_sum);
1225635b75fcSTejun Heo 	PCPU_SETUP_BUG_ON(ai->unit_size & ~PAGE_MASK);
1226635b75fcSTejun Heo 	PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE);
1227099a19d9STejun Heo 	PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE);
12289f645532STejun Heo 	PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
12298d408b4bSTejun Heo 
12306563297cSTejun Heo 	/* process group information and build config tables accordingly */
12316563297cSTejun Heo 	group_offsets = alloc_bootmem(ai->nr_groups * sizeof(group_offsets[0]));
12326563297cSTejun Heo 	group_sizes = alloc_bootmem(ai->nr_groups * sizeof(group_sizes[0]));
1233fd1e8a1fSTejun Heo 	unit_map = alloc_bootmem(nr_cpu_ids * sizeof(unit_map[0]));
1234fb435d52STejun Heo 	unit_off = alloc_bootmem(nr_cpu_ids * sizeof(unit_off[0]));
12352f39e637STejun Heo 
1236fd1e8a1fSTejun Heo 	for (cpu = 0; cpu < nr_cpu_ids; cpu++)
1237ffe0d5a5STejun Heo 		unit_map[cpu] = UINT_MAX;
1238fd1e8a1fSTejun Heo 	pcpu_first_unit_cpu = NR_CPUS;
12392f39e637STejun Heo 
1240fd1e8a1fSTejun Heo 	for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) {
1241fd1e8a1fSTejun Heo 		const struct pcpu_group_info *gi = &ai->groups[group];
12422f39e637STejun Heo 
12436563297cSTejun Heo 		group_offsets[group] = gi->base_offset;
12446563297cSTejun Heo 		group_sizes[group] = gi->nr_units * ai->unit_size;
12456563297cSTejun Heo 
1246fd1e8a1fSTejun Heo 		for (i = 0; i < gi->nr_units; i++) {
1247fd1e8a1fSTejun Heo 			cpu = gi->cpu_map[i];
1248fd1e8a1fSTejun Heo 			if (cpu == NR_CPUS)
1249fd1e8a1fSTejun Heo 				continue;
1250fd1e8a1fSTejun Heo 
1251635b75fcSTejun Heo 			PCPU_SETUP_BUG_ON(cpu > nr_cpu_ids);
1252635b75fcSTejun Heo 			PCPU_SETUP_BUG_ON(!cpu_possible(cpu));
1253635b75fcSTejun Heo 			PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX);
1254fd1e8a1fSTejun Heo 
1255fd1e8a1fSTejun Heo 			unit_map[cpu] = unit + i;
1256fb435d52STejun Heo 			unit_off[cpu] = gi->base_offset + i * ai->unit_size;
1257fb435d52STejun Heo 
1258fd1e8a1fSTejun Heo 			if (pcpu_first_unit_cpu == NR_CPUS)
12592f39e637STejun Heo 				pcpu_first_unit_cpu = cpu;
12602f39e637STejun Heo 		}
1261fd1e8a1fSTejun Heo 	}
12622f39e637STejun Heo 	pcpu_last_unit_cpu = cpu;
1263fd1e8a1fSTejun Heo 	pcpu_nr_units = unit;
12642f39e637STejun Heo 
12652f39e637STejun Heo 	for_each_possible_cpu(cpu)
1266635b75fcSTejun Heo 		PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX);
1267635b75fcSTejun Heo 
1268635b75fcSTejun Heo 	/* we're done parsing the input, undefine BUG macro and dump config */
1269635b75fcSTejun Heo #undef PCPU_SETUP_BUG_ON
1270635b75fcSTejun Heo 	pcpu_dump_alloc_info(KERN_INFO, ai);
12712f39e637STejun Heo 
12726563297cSTejun Heo 	pcpu_nr_groups = ai->nr_groups;
12736563297cSTejun Heo 	pcpu_group_offsets = group_offsets;
12746563297cSTejun Heo 	pcpu_group_sizes = group_sizes;
1275fd1e8a1fSTejun Heo 	pcpu_unit_map = unit_map;
1276fb435d52STejun Heo 	pcpu_unit_offsets = unit_off;
12772f39e637STejun Heo 
12782f39e637STejun Heo 	/* determine basic parameters */
1279fd1e8a1fSTejun Heo 	pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
1280d9b55eebSTejun Heo 	pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
12816563297cSTejun Heo 	pcpu_atom_size = ai->atom_size;
1282ce3141a2STejun Heo 	pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
1283ce3141a2STejun Heo 		BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
1284cafe8816STejun Heo 
1285d9b55eebSTejun Heo 	/*
1286d9b55eebSTejun Heo 	 * Allocate chunk slots.  The additional last slot is for
1287d9b55eebSTejun Heo 	 * empty chunks.
1288d9b55eebSTejun Heo 	 */
1289d9b55eebSTejun Heo 	pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
1290fbf59bc9STejun Heo 	pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
1291fbf59bc9STejun Heo 	for (i = 0; i < pcpu_nr_slots; i++)
1292fbf59bc9STejun Heo 		INIT_LIST_HEAD(&pcpu_slot[i]);
1293fbf59bc9STejun Heo 
1294edcb4639STejun Heo 	/*
1295edcb4639STejun Heo 	 * Initialize static chunk.  If reserved_size is zero, the
1296edcb4639STejun Heo 	 * static chunk covers static area + dynamic allocation area
1297edcb4639STejun Heo 	 * in the first chunk.  If reserved_size is not zero, it
1298edcb4639STejun Heo 	 * covers static area + reserved area (mostly used for module
1299edcb4639STejun Heo 	 * static percpu allocation).
1300edcb4639STejun Heo 	 */
13012441d15cSTejun Heo 	schunk = alloc_bootmem(pcpu_chunk_struct_size);
13022441d15cSTejun Heo 	INIT_LIST_HEAD(&schunk->list);
1303bba174f5STejun Heo 	schunk->base_addr = base_addr;
130461ace7faSTejun Heo 	schunk->map = smap;
130561ace7faSTejun Heo 	schunk->map_alloc = ARRAY_SIZE(smap);
130638a6be52STejun Heo 	schunk->immutable = true;
1307ce3141a2STejun Heo 	bitmap_fill(schunk->populated, pcpu_unit_pages);
1308edcb4639STejun Heo 
1309fd1e8a1fSTejun Heo 	if (ai->reserved_size) {
1310fd1e8a1fSTejun Heo 		schunk->free_size = ai->reserved_size;
1311ae9e6bc9STejun Heo 		pcpu_reserved_chunk = schunk;
1312fd1e8a1fSTejun Heo 		pcpu_reserved_chunk_limit = ai->static_size + ai->reserved_size;
1313edcb4639STejun Heo 	} else {
13142441d15cSTejun Heo 		schunk->free_size = dyn_size;
1315edcb4639STejun Heo 		dyn_size = 0;			/* dynamic area covered */
1316edcb4639STejun Heo 	}
13172441d15cSTejun Heo 	schunk->contig_hint = schunk->free_size;
1318fbf59bc9STejun Heo 
1319fd1e8a1fSTejun Heo 	schunk->map[schunk->map_used++] = -ai->static_size;
132061ace7faSTejun Heo 	if (schunk->free_size)
132161ace7faSTejun Heo 		schunk->map[schunk->map_used++] = schunk->free_size;
132261ace7faSTejun Heo 
1323edcb4639STejun Heo 	/* init dynamic chunk if necessary */
1324edcb4639STejun Heo 	if (dyn_size) {
1325ce3141a2STejun Heo 		dchunk = alloc_bootmem(pcpu_chunk_struct_size);
1326edcb4639STejun Heo 		INIT_LIST_HEAD(&dchunk->list);
1327bba174f5STejun Heo 		dchunk->base_addr = base_addr;
1328edcb4639STejun Heo 		dchunk->map = dmap;
1329edcb4639STejun Heo 		dchunk->map_alloc = ARRAY_SIZE(dmap);
133038a6be52STejun Heo 		dchunk->immutable = true;
1331ce3141a2STejun Heo 		bitmap_fill(dchunk->populated, pcpu_unit_pages);
1332edcb4639STejun Heo 
1333edcb4639STejun Heo 		dchunk->contig_hint = dchunk->free_size = dyn_size;
1334edcb4639STejun Heo 		dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
1335edcb4639STejun Heo 		dchunk->map[dchunk->map_used++] = dchunk->free_size;
1336edcb4639STejun Heo 	}
1337edcb4639STejun Heo 
13382441d15cSTejun Heo 	/* link the first chunk in */
1339ae9e6bc9STejun Heo 	pcpu_first_chunk = dchunk ?: schunk;
1340ae9e6bc9STejun Heo 	pcpu_chunk_relocate(pcpu_first_chunk, -1);
1341fbf59bc9STejun Heo 
1342fbf59bc9STejun Heo 	/* we're done */
1343bba174f5STejun Heo 	pcpu_base_addr = base_addr;
1344fb435d52STejun Heo 	return 0;
1345fbf59bc9STejun Heo }
134666c3a757STejun Heo 
1347bbddff05STejun Heo #ifdef CONFIG_SMP
1348bbddff05STejun Heo 
1349f58dc01bSTejun Heo const char *pcpu_fc_names[PCPU_FC_NR] __initdata = {
1350f58dc01bSTejun Heo 	[PCPU_FC_AUTO]	= "auto",
1351f58dc01bSTejun Heo 	[PCPU_FC_EMBED]	= "embed",
1352f58dc01bSTejun Heo 	[PCPU_FC_PAGE]	= "page",
1353f58dc01bSTejun Heo };
135466c3a757STejun Heo 
1355f58dc01bSTejun Heo enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO;
1356f58dc01bSTejun Heo 
1357f58dc01bSTejun Heo static int __init percpu_alloc_setup(char *str)
135866c3a757STejun Heo {
1359f58dc01bSTejun Heo 	if (0)
1360f58dc01bSTejun Heo 		/* nada */;
1361f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
1362f58dc01bSTejun Heo 	else if (!strcmp(str, "embed"))
1363f58dc01bSTejun Heo 		pcpu_chosen_fc = PCPU_FC_EMBED;
1364f58dc01bSTejun Heo #endif
1365f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
1366f58dc01bSTejun Heo 	else if (!strcmp(str, "page"))
1367f58dc01bSTejun Heo 		pcpu_chosen_fc = PCPU_FC_PAGE;
1368f58dc01bSTejun Heo #endif
1369f58dc01bSTejun Heo 	else
1370f58dc01bSTejun Heo 		pr_warning("PERCPU: unknown allocator %s specified\n", str);
137166c3a757STejun Heo 
1372f58dc01bSTejun Heo 	return 0;
137366c3a757STejun Heo }
1374f58dc01bSTejun Heo early_param("percpu_alloc", percpu_alloc_setup);
137566c3a757STejun Heo 
1376*3c9a024fSTejun Heo /*
1377*3c9a024fSTejun Heo  * pcpu_embed_first_chunk() is used by the generic percpu setup.
1378*3c9a024fSTejun Heo  * Build it if needed by the arch config or the generic setup is going
1379*3c9a024fSTejun Heo  * to be used.
1380*3c9a024fSTejun Heo  */
138108fc4580STejun Heo #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \
138208fc4580STejun Heo 	!defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
1383*3c9a024fSTejun Heo #define BUILD_EMBED_FIRST_CHUNK
1384*3c9a024fSTejun Heo #endif
1385*3c9a024fSTejun Heo 
1386*3c9a024fSTejun Heo /* build pcpu_page_first_chunk() iff needed by the arch config */
1387*3c9a024fSTejun Heo #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK)
1388*3c9a024fSTejun Heo #define BUILD_PAGE_FIRST_CHUNK
1389*3c9a024fSTejun Heo #endif
1390*3c9a024fSTejun Heo 
1391*3c9a024fSTejun Heo /* pcpu_build_alloc_info() is used by both embed and page first chunk */
1392*3c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK)
1393*3c9a024fSTejun Heo /**
1394*3c9a024fSTejun Heo  * pcpu_build_alloc_info - build alloc_info considering distances between CPUs
1395*3c9a024fSTejun Heo  * @reserved_size: the size of reserved percpu area in bytes
1396*3c9a024fSTejun Heo  * @dyn_size: minimum free size for dynamic allocation in bytes
1397*3c9a024fSTejun Heo  * @atom_size: allocation atom size
1398*3c9a024fSTejun Heo  * @cpu_distance_fn: callback to determine distance between cpus, optional
1399*3c9a024fSTejun Heo  *
1400*3c9a024fSTejun Heo  * This function determines grouping of units, their mappings to cpus
1401*3c9a024fSTejun Heo  * and other parameters considering needed percpu size, allocation
1402*3c9a024fSTejun Heo  * atom size and distances between CPUs.
1403*3c9a024fSTejun Heo  *
1404*3c9a024fSTejun Heo  * Groups are always mutliples of atom size and CPUs which are of
1405*3c9a024fSTejun Heo  * LOCAL_DISTANCE both ways are grouped together and share space for
1406*3c9a024fSTejun Heo  * units in the same group.  The returned configuration is guaranteed
1407*3c9a024fSTejun Heo  * to have CPUs on different nodes on different groups and >=75% usage
1408*3c9a024fSTejun Heo  * of allocated virtual address space.
1409*3c9a024fSTejun Heo  *
1410*3c9a024fSTejun Heo  * RETURNS:
1411*3c9a024fSTejun Heo  * On success, pointer to the new allocation_info is returned.  On
1412*3c9a024fSTejun Heo  * failure, ERR_PTR value is returned.
1413*3c9a024fSTejun Heo  */
1414*3c9a024fSTejun Heo static struct pcpu_alloc_info * __init pcpu_build_alloc_info(
1415*3c9a024fSTejun Heo 				size_t reserved_size, size_t dyn_size,
1416*3c9a024fSTejun Heo 				size_t atom_size,
1417*3c9a024fSTejun Heo 				pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
1418*3c9a024fSTejun Heo {
1419*3c9a024fSTejun Heo 	static int group_map[NR_CPUS] __initdata;
1420*3c9a024fSTejun Heo 	static int group_cnt[NR_CPUS] __initdata;
1421*3c9a024fSTejun Heo 	const size_t static_size = __per_cpu_end - __per_cpu_start;
1422*3c9a024fSTejun Heo 	int nr_groups = 1, nr_units = 0;
1423*3c9a024fSTejun Heo 	size_t size_sum, min_unit_size, alloc_size;
1424*3c9a024fSTejun Heo 	int upa, max_upa, uninitialized_var(best_upa);	/* units_per_alloc */
1425*3c9a024fSTejun Heo 	int last_allocs, group, unit;
1426*3c9a024fSTejun Heo 	unsigned int cpu, tcpu;
1427*3c9a024fSTejun Heo 	struct pcpu_alloc_info *ai;
1428*3c9a024fSTejun Heo 	unsigned int *cpu_map;
1429*3c9a024fSTejun Heo 
1430*3c9a024fSTejun Heo 	/* this function may be called multiple times */
1431*3c9a024fSTejun Heo 	memset(group_map, 0, sizeof(group_map));
1432*3c9a024fSTejun Heo 	memset(group_cnt, 0, sizeof(group_cnt));
1433*3c9a024fSTejun Heo 
1434*3c9a024fSTejun Heo 	/* calculate size_sum and ensure dyn_size is enough for early alloc */
1435*3c9a024fSTejun Heo 	size_sum = PFN_ALIGN(static_size + reserved_size +
1436*3c9a024fSTejun Heo 			    max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE));
1437*3c9a024fSTejun Heo 	dyn_size = size_sum - static_size - reserved_size;
1438*3c9a024fSTejun Heo 
1439*3c9a024fSTejun Heo 	/*
1440*3c9a024fSTejun Heo 	 * Determine min_unit_size, alloc_size and max_upa such that
1441*3c9a024fSTejun Heo 	 * alloc_size is multiple of atom_size and is the smallest
1442*3c9a024fSTejun Heo 	 * which can accomodate 4k aligned segments which are equal to
1443*3c9a024fSTejun Heo 	 * or larger than min_unit_size.
1444*3c9a024fSTejun Heo 	 */
1445*3c9a024fSTejun Heo 	min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE);
1446*3c9a024fSTejun Heo 
1447*3c9a024fSTejun Heo 	alloc_size = roundup(min_unit_size, atom_size);
1448*3c9a024fSTejun Heo 	upa = alloc_size / min_unit_size;
1449*3c9a024fSTejun Heo 	while (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK))
1450*3c9a024fSTejun Heo 		upa--;
1451*3c9a024fSTejun Heo 	max_upa = upa;
1452*3c9a024fSTejun Heo 
1453*3c9a024fSTejun Heo 	/* group cpus according to their proximity */
1454*3c9a024fSTejun Heo 	for_each_possible_cpu(cpu) {
1455*3c9a024fSTejun Heo 		group = 0;
1456*3c9a024fSTejun Heo 	next_group:
1457*3c9a024fSTejun Heo 		for_each_possible_cpu(tcpu) {
1458*3c9a024fSTejun Heo 			if (cpu == tcpu)
1459*3c9a024fSTejun Heo 				break;
1460*3c9a024fSTejun Heo 			if (group_map[tcpu] == group && cpu_distance_fn &&
1461*3c9a024fSTejun Heo 			    (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE ||
1462*3c9a024fSTejun Heo 			     cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) {
1463*3c9a024fSTejun Heo 				group++;
1464*3c9a024fSTejun Heo 				nr_groups = max(nr_groups, group + 1);
1465*3c9a024fSTejun Heo 				goto next_group;
1466*3c9a024fSTejun Heo 			}
1467*3c9a024fSTejun Heo 		}
1468*3c9a024fSTejun Heo 		group_map[cpu] = group;
1469*3c9a024fSTejun Heo 		group_cnt[group]++;
1470*3c9a024fSTejun Heo 	}
1471*3c9a024fSTejun Heo 
1472*3c9a024fSTejun Heo 	/*
1473*3c9a024fSTejun Heo 	 * Expand unit size until address space usage goes over 75%
1474*3c9a024fSTejun Heo 	 * and then as much as possible without using more address
1475*3c9a024fSTejun Heo 	 * space.
1476*3c9a024fSTejun Heo 	 */
1477*3c9a024fSTejun Heo 	last_allocs = INT_MAX;
1478*3c9a024fSTejun Heo 	for (upa = max_upa; upa; upa--) {
1479*3c9a024fSTejun Heo 		int allocs = 0, wasted = 0;
1480*3c9a024fSTejun Heo 
1481*3c9a024fSTejun Heo 		if (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK))
1482*3c9a024fSTejun Heo 			continue;
1483*3c9a024fSTejun Heo 
1484*3c9a024fSTejun Heo 		for (group = 0; group < nr_groups; group++) {
1485*3c9a024fSTejun Heo 			int this_allocs = DIV_ROUND_UP(group_cnt[group], upa);
1486*3c9a024fSTejun Heo 			allocs += this_allocs;
1487*3c9a024fSTejun Heo 			wasted += this_allocs * upa - group_cnt[group];
1488*3c9a024fSTejun Heo 		}
1489*3c9a024fSTejun Heo 
1490*3c9a024fSTejun Heo 		/*
1491*3c9a024fSTejun Heo 		 * Don't accept if wastage is over 1/3.  The
1492*3c9a024fSTejun Heo 		 * greater-than comparison ensures upa==1 always
1493*3c9a024fSTejun Heo 		 * passes the following check.
1494*3c9a024fSTejun Heo 		 */
1495*3c9a024fSTejun Heo 		if (wasted > num_possible_cpus() / 3)
1496*3c9a024fSTejun Heo 			continue;
1497*3c9a024fSTejun Heo 
1498*3c9a024fSTejun Heo 		/* and then don't consume more memory */
1499*3c9a024fSTejun Heo 		if (allocs > last_allocs)
1500*3c9a024fSTejun Heo 			break;
1501*3c9a024fSTejun Heo 		last_allocs = allocs;
1502*3c9a024fSTejun Heo 		best_upa = upa;
1503*3c9a024fSTejun Heo 	}
1504*3c9a024fSTejun Heo 	upa = best_upa;
1505*3c9a024fSTejun Heo 
1506*3c9a024fSTejun Heo 	/* allocate and fill alloc_info */
1507*3c9a024fSTejun Heo 	for (group = 0; group < nr_groups; group++)
1508*3c9a024fSTejun Heo 		nr_units += roundup(group_cnt[group], upa);
1509*3c9a024fSTejun Heo 
1510*3c9a024fSTejun Heo 	ai = pcpu_alloc_alloc_info(nr_groups, nr_units);
1511*3c9a024fSTejun Heo 	if (!ai)
1512*3c9a024fSTejun Heo 		return ERR_PTR(-ENOMEM);
1513*3c9a024fSTejun Heo 	cpu_map = ai->groups[0].cpu_map;
1514*3c9a024fSTejun Heo 
1515*3c9a024fSTejun Heo 	for (group = 0; group < nr_groups; group++) {
1516*3c9a024fSTejun Heo 		ai->groups[group].cpu_map = cpu_map;
1517*3c9a024fSTejun Heo 		cpu_map += roundup(group_cnt[group], upa);
1518*3c9a024fSTejun Heo 	}
1519*3c9a024fSTejun Heo 
1520*3c9a024fSTejun Heo 	ai->static_size = static_size;
1521*3c9a024fSTejun Heo 	ai->reserved_size = reserved_size;
1522*3c9a024fSTejun Heo 	ai->dyn_size = dyn_size;
1523*3c9a024fSTejun Heo 	ai->unit_size = alloc_size / upa;
1524*3c9a024fSTejun Heo 	ai->atom_size = atom_size;
1525*3c9a024fSTejun Heo 	ai->alloc_size = alloc_size;
1526*3c9a024fSTejun Heo 
1527*3c9a024fSTejun Heo 	for (group = 0, unit = 0; group_cnt[group]; group++) {
1528*3c9a024fSTejun Heo 		struct pcpu_group_info *gi = &ai->groups[group];
1529*3c9a024fSTejun Heo 
1530*3c9a024fSTejun Heo 		/*
1531*3c9a024fSTejun Heo 		 * Initialize base_offset as if all groups are located
1532*3c9a024fSTejun Heo 		 * back-to-back.  The caller should update this to
1533*3c9a024fSTejun Heo 		 * reflect actual allocation.
1534*3c9a024fSTejun Heo 		 */
1535*3c9a024fSTejun Heo 		gi->base_offset = unit * ai->unit_size;
1536*3c9a024fSTejun Heo 
1537*3c9a024fSTejun Heo 		for_each_possible_cpu(cpu)
1538*3c9a024fSTejun Heo 			if (group_map[cpu] == group)
1539*3c9a024fSTejun Heo 				gi->cpu_map[gi->nr_units++] = cpu;
1540*3c9a024fSTejun Heo 		gi->nr_units = roundup(gi->nr_units, upa);
1541*3c9a024fSTejun Heo 		unit += gi->nr_units;
1542*3c9a024fSTejun Heo 	}
1543*3c9a024fSTejun Heo 	BUG_ON(unit != nr_units);
1544*3c9a024fSTejun Heo 
1545*3c9a024fSTejun Heo 	return ai;
1546*3c9a024fSTejun Heo }
1547*3c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */
1548*3c9a024fSTejun Heo 
1549*3c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK)
155066c3a757STejun Heo /**
155166c3a757STejun Heo  * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
155266c3a757STejun Heo  * @reserved_size: the size of reserved percpu area in bytes
15534ba6ce25STejun Heo  * @dyn_size: minimum free size for dynamic allocation in bytes
1554c8826dd5STejun Heo  * @atom_size: allocation atom size
1555c8826dd5STejun Heo  * @cpu_distance_fn: callback to determine distance between cpus, optional
1556c8826dd5STejun Heo  * @alloc_fn: function to allocate percpu page
1557c8826dd5STejun Heo  * @free_fn: funtion to free percpu page
155866c3a757STejun Heo  *
155966c3a757STejun Heo  * This is a helper to ease setting up embedded first percpu chunk and
156066c3a757STejun Heo  * can be called where pcpu_setup_first_chunk() is expected.
156166c3a757STejun Heo  *
156266c3a757STejun Heo  * If this function is used to setup the first chunk, it is allocated
1563c8826dd5STejun Heo  * by calling @alloc_fn and used as-is without being mapped into
1564c8826dd5STejun Heo  * vmalloc area.  Allocations are always whole multiples of @atom_size
1565c8826dd5STejun Heo  * aligned to @atom_size.
1566c8826dd5STejun Heo  *
1567c8826dd5STejun Heo  * This enables the first chunk to piggy back on the linear physical
1568c8826dd5STejun Heo  * mapping which often uses larger page size.  Please note that this
1569c8826dd5STejun Heo  * can result in very sparse cpu->unit mapping on NUMA machines thus
1570c8826dd5STejun Heo  * requiring large vmalloc address space.  Don't use this allocator if
1571c8826dd5STejun Heo  * vmalloc space is not orders of magnitude larger than distances
1572c8826dd5STejun Heo  * between node memory addresses (ie. 32bit NUMA machines).
157366c3a757STejun Heo  *
15744ba6ce25STejun Heo  * @dyn_size specifies the minimum dynamic area size.
157566c3a757STejun Heo  *
157666c3a757STejun Heo  * If the needed size is smaller than the minimum or specified unit
1577c8826dd5STejun Heo  * size, the leftover is returned using @free_fn.
157866c3a757STejun Heo  *
157966c3a757STejun Heo  * RETURNS:
1580fb435d52STejun Heo  * 0 on success, -errno on failure.
158166c3a757STejun Heo  */
15824ba6ce25STejun Heo int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
1583c8826dd5STejun Heo 				  size_t atom_size,
1584c8826dd5STejun Heo 				  pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
1585c8826dd5STejun Heo 				  pcpu_fc_alloc_fn_t alloc_fn,
1586c8826dd5STejun Heo 				  pcpu_fc_free_fn_t free_fn)
158766c3a757STejun Heo {
1588c8826dd5STejun Heo 	void *base = (void *)ULONG_MAX;
1589c8826dd5STejun Heo 	void **areas = NULL;
1590fd1e8a1fSTejun Heo 	struct pcpu_alloc_info *ai;
15916ea529a2STejun Heo 	size_t size_sum, areas_size, max_distance;
1592c8826dd5STejun Heo 	int group, i, rc;
159366c3a757STejun Heo 
1594c8826dd5STejun Heo 	ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size,
1595c8826dd5STejun Heo 				   cpu_distance_fn);
1596fd1e8a1fSTejun Heo 	if (IS_ERR(ai))
1597fd1e8a1fSTejun Heo 		return PTR_ERR(ai);
159866c3a757STejun Heo 
1599fd1e8a1fSTejun Heo 	size_sum = ai->static_size + ai->reserved_size + ai->dyn_size;
1600c8826dd5STejun Heo 	areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *));
160166c3a757STejun Heo 
1602c8826dd5STejun Heo 	areas = alloc_bootmem_nopanic(areas_size);
1603c8826dd5STejun Heo 	if (!areas) {
1604fb435d52STejun Heo 		rc = -ENOMEM;
1605c8826dd5STejun Heo 		goto out_free;
1606fa8a7094STejun Heo 	}
160766c3a757STejun Heo 
1608c8826dd5STejun Heo 	/* allocate, copy and determine base address */
1609c8826dd5STejun Heo 	for (group = 0; group < ai->nr_groups; group++) {
1610c8826dd5STejun Heo 		struct pcpu_group_info *gi = &ai->groups[group];
1611c8826dd5STejun Heo 		unsigned int cpu = NR_CPUS;
1612c8826dd5STejun Heo 		void *ptr;
161366c3a757STejun Heo 
1614c8826dd5STejun Heo 		for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++)
1615c8826dd5STejun Heo 			cpu = gi->cpu_map[i];
1616c8826dd5STejun Heo 		BUG_ON(cpu == NR_CPUS);
1617c8826dd5STejun Heo 
1618c8826dd5STejun Heo 		/* allocate space for the whole group */
1619c8826dd5STejun Heo 		ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size);
1620c8826dd5STejun Heo 		if (!ptr) {
1621c8826dd5STejun Heo 			rc = -ENOMEM;
1622c8826dd5STejun Heo 			goto out_free_areas;
1623c8826dd5STejun Heo 		}
1624c8826dd5STejun Heo 		areas[group] = ptr;
1625c8826dd5STejun Heo 
1626c8826dd5STejun Heo 		base = min(ptr, base);
1627c8826dd5STejun Heo 
1628c8826dd5STejun Heo 		for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) {
1629c8826dd5STejun Heo 			if (gi->cpu_map[i] == NR_CPUS) {
1630c8826dd5STejun Heo 				/* unused unit, free whole */
1631c8826dd5STejun Heo 				free_fn(ptr, ai->unit_size);
1632c8826dd5STejun Heo 				continue;
1633c8826dd5STejun Heo 			}
1634c8826dd5STejun Heo 			/* copy and return the unused part */
1635fd1e8a1fSTejun Heo 			memcpy(ptr, __per_cpu_load, ai->static_size);
1636c8826dd5STejun Heo 			free_fn(ptr + size_sum, ai->unit_size - size_sum);
1637c8826dd5STejun Heo 		}
163866c3a757STejun Heo 	}
163966c3a757STejun Heo 
1640c8826dd5STejun Heo 	/* base address is now known, determine group base offsets */
16416ea529a2STejun Heo 	max_distance = 0;
16426ea529a2STejun Heo 	for (group = 0; group < ai->nr_groups; group++) {
1643c8826dd5STejun Heo 		ai->groups[group].base_offset = areas[group] - base;
16441a0c3298STejun Heo 		max_distance = max_t(size_t, max_distance,
16451a0c3298STejun Heo 				     ai->groups[group].base_offset);
16466ea529a2STejun Heo 	}
16476ea529a2STejun Heo 	max_distance += ai->unit_size;
16486ea529a2STejun Heo 
16496ea529a2STejun Heo 	/* warn if maximum distance is further than 75% of vmalloc space */
16506ea529a2STejun Heo 	if (max_distance > (VMALLOC_END - VMALLOC_START) * 3 / 4) {
16511a0c3298STejun Heo 		pr_warning("PERCPU: max_distance=0x%zx too large for vmalloc "
16526ea529a2STejun Heo 			   "space 0x%lx\n",
16536ea529a2STejun Heo 			   max_distance, VMALLOC_END - VMALLOC_START);
16546ea529a2STejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
16556ea529a2STejun Heo 		/* and fail if we have fallback */
16566ea529a2STejun Heo 		rc = -EINVAL;
16576ea529a2STejun Heo 		goto out_free;
16586ea529a2STejun Heo #endif
16596ea529a2STejun Heo 	}
1660c8826dd5STejun Heo 
1661004018e2STejun Heo 	pr_info("PERCPU: Embedded %zu pages/cpu @%p s%zu r%zu d%zu u%zu\n",
1662fd1e8a1fSTejun Heo 		PFN_DOWN(size_sum), base, ai->static_size, ai->reserved_size,
1663fd1e8a1fSTejun Heo 		ai->dyn_size, ai->unit_size);
166466c3a757STejun Heo 
1665fb435d52STejun Heo 	rc = pcpu_setup_first_chunk(ai, base);
1666c8826dd5STejun Heo 	goto out_free;
1667c8826dd5STejun Heo 
1668c8826dd5STejun Heo out_free_areas:
1669c8826dd5STejun Heo 	for (group = 0; group < ai->nr_groups; group++)
1670c8826dd5STejun Heo 		free_fn(areas[group],
1671c8826dd5STejun Heo 			ai->groups[group].nr_units * ai->unit_size);
1672c8826dd5STejun Heo out_free:
1673fd1e8a1fSTejun Heo 	pcpu_free_alloc_info(ai);
1674c8826dd5STejun Heo 	if (areas)
1675c8826dd5STejun Heo 		free_bootmem(__pa(areas), areas_size);
1676fb435d52STejun Heo 	return rc;
1677d4b95f80STejun Heo }
1678*3c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK */
1679d4b95f80STejun Heo 
1680*3c9a024fSTejun Heo #ifdef BUILD_PAGE_FIRST_CHUNK
1681d4b95f80STejun Heo /**
168200ae4064STejun Heo  * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages
1683d4b95f80STejun Heo  * @reserved_size: the size of reserved percpu area in bytes
1684d4b95f80STejun Heo  * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE
1685d4b95f80STejun Heo  * @free_fn: funtion to free percpu page, always called with PAGE_SIZE
1686d4b95f80STejun Heo  * @populate_pte_fn: function to populate pte
1687d4b95f80STejun Heo  *
168800ae4064STejun Heo  * This is a helper to ease setting up page-remapped first percpu
168900ae4064STejun Heo  * chunk and can be called where pcpu_setup_first_chunk() is expected.
1690d4b95f80STejun Heo  *
1691d4b95f80STejun Heo  * This is the basic allocator.  Static percpu area is allocated
1692d4b95f80STejun Heo  * page-by-page into vmalloc area.
1693d4b95f80STejun Heo  *
1694d4b95f80STejun Heo  * RETURNS:
1695fb435d52STejun Heo  * 0 on success, -errno on failure.
1696d4b95f80STejun Heo  */
1697fb435d52STejun Heo int __init pcpu_page_first_chunk(size_t reserved_size,
1698d4b95f80STejun Heo 				 pcpu_fc_alloc_fn_t alloc_fn,
1699d4b95f80STejun Heo 				 pcpu_fc_free_fn_t free_fn,
1700d4b95f80STejun Heo 				 pcpu_fc_populate_pte_fn_t populate_pte_fn)
1701d4b95f80STejun Heo {
17028f05a6a6STejun Heo 	static struct vm_struct vm;
1703fd1e8a1fSTejun Heo 	struct pcpu_alloc_info *ai;
170400ae4064STejun Heo 	char psize_str[16];
1705ce3141a2STejun Heo 	int unit_pages;
1706d4b95f80STejun Heo 	size_t pages_size;
1707ce3141a2STejun Heo 	struct page **pages;
1708fb435d52STejun Heo 	int unit, i, j, rc;
1709d4b95f80STejun Heo 
171000ae4064STejun Heo 	snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10);
171100ae4064STejun Heo 
17124ba6ce25STejun Heo 	ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL);
1713fd1e8a1fSTejun Heo 	if (IS_ERR(ai))
1714fd1e8a1fSTejun Heo 		return PTR_ERR(ai);
1715fd1e8a1fSTejun Heo 	BUG_ON(ai->nr_groups != 1);
1716fd1e8a1fSTejun Heo 	BUG_ON(ai->groups[0].nr_units != num_possible_cpus());
1717fd1e8a1fSTejun Heo 
1718fd1e8a1fSTejun Heo 	unit_pages = ai->unit_size >> PAGE_SHIFT;
1719d4b95f80STejun Heo 
1720d4b95f80STejun Heo 	/* unaligned allocations can't be freed, round up to page size */
1721fd1e8a1fSTejun Heo 	pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
1722fd1e8a1fSTejun Heo 			       sizeof(pages[0]));
1723ce3141a2STejun Heo 	pages = alloc_bootmem(pages_size);
1724d4b95f80STejun Heo 
17258f05a6a6STejun Heo 	/* allocate pages */
1726d4b95f80STejun Heo 	j = 0;
1727fd1e8a1fSTejun Heo 	for (unit = 0; unit < num_possible_cpus(); unit++)
1728ce3141a2STejun Heo 		for (i = 0; i < unit_pages; i++) {
1729fd1e8a1fSTejun Heo 			unsigned int cpu = ai->groups[0].cpu_map[unit];
1730d4b95f80STejun Heo 			void *ptr;
1731d4b95f80STejun Heo 
17323cbc8565STejun Heo 			ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE);
1733d4b95f80STejun Heo 			if (!ptr) {
173400ae4064STejun Heo 				pr_warning("PERCPU: failed to allocate %s page "
173500ae4064STejun Heo 					   "for cpu%u\n", psize_str, cpu);
1736d4b95f80STejun Heo 				goto enomem;
1737d4b95f80STejun Heo 			}
1738ce3141a2STejun Heo 			pages[j++] = virt_to_page(ptr);
1739d4b95f80STejun Heo 		}
1740d4b95f80STejun Heo 
17418f05a6a6STejun Heo 	/* allocate vm area, map the pages and copy static data */
17428f05a6a6STejun Heo 	vm.flags = VM_ALLOC;
1743fd1e8a1fSTejun Heo 	vm.size = num_possible_cpus() * ai->unit_size;
17448f05a6a6STejun Heo 	vm_area_register_early(&vm, PAGE_SIZE);
17458f05a6a6STejun Heo 
1746fd1e8a1fSTejun Heo 	for (unit = 0; unit < num_possible_cpus(); unit++) {
17471d9d3257STejun Heo 		unsigned long unit_addr =
1748fd1e8a1fSTejun Heo 			(unsigned long)vm.addr + unit * ai->unit_size;
17498f05a6a6STejun Heo 
1750ce3141a2STejun Heo 		for (i = 0; i < unit_pages; i++)
17518f05a6a6STejun Heo 			populate_pte_fn(unit_addr + (i << PAGE_SHIFT));
17528f05a6a6STejun Heo 
17538f05a6a6STejun Heo 		/* pte already populated, the following shouldn't fail */
1754fb435d52STejun Heo 		rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages],
1755ce3141a2STejun Heo 				      unit_pages);
1756fb435d52STejun Heo 		if (rc < 0)
1757fb435d52STejun Heo 			panic("failed to map percpu area, err=%d\n", rc);
17588f05a6a6STejun Heo 
17598f05a6a6STejun Heo 		/*
17608f05a6a6STejun Heo 		 * FIXME: Archs with virtual cache should flush local
17618f05a6a6STejun Heo 		 * cache for the linear mapping here - something
17628f05a6a6STejun Heo 		 * equivalent to flush_cache_vmap() on the local cpu.
17638f05a6a6STejun Heo 		 * flush_cache_vmap() can't be used as most supporting
17648f05a6a6STejun Heo 		 * data structures are not set up yet.
17658f05a6a6STejun Heo 		 */
17668f05a6a6STejun Heo 
17678f05a6a6STejun Heo 		/* copy static data */
1768fd1e8a1fSTejun Heo 		memcpy((void *)unit_addr, __per_cpu_load, ai->static_size);
176966c3a757STejun Heo 	}
177066c3a757STejun Heo 
177166c3a757STejun Heo 	/* we're ready, commit */
17721d9d3257STejun Heo 	pr_info("PERCPU: %d %s pages/cpu @%p s%zu r%zu d%zu\n",
1773fd1e8a1fSTejun Heo 		unit_pages, psize_str, vm.addr, ai->static_size,
1774fd1e8a1fSTejun Heo 		ai->reserved_size, ai->dyn_size);
177566c3a757STejun Heo 
1776fb435d52STejun Heo 	rc = pcpu_setup_first_chunk(ai, vm.addr);
1777d4b95f80STejun Heo 	goto out_free_ar;
1778d4b95f80STejun Heo 
1779d4b95f80STejun Heo enomem:
1780d4b95f80STejun Heo 	while (--j >= 0)
1781ce3141a2STejun Heo 		free_fn(page_address(pages[j]), PAGE_SIZE);
1782fb435d52STejun Heo 	rc = -ENOMEM;
1783d4b95f80STejun Heo out_free_ar:
1784ce3141a2STejun Heo 	free_bootmem(__pa(pages), pages_size);
1785fd1e8a1fSTejun Heo 	pcpu_free_alloc_info(ai);
1786fb435d52STejun Heo 	return rc;
178766c3a757STejun Heo }
1788*3c9a024fSTejun Heo #endif /* BUILD_PAGE_FIRST_CHUNK */
1789d4b95f80STejun Heo 
1790bbddff05STejun Heo #ifndef	CONFIG_HAVE_SETUP_PER_CPU_AREA
17918c4bfc6eSTejun Heo /*
1792bbddff05STejun Heo  * Generic SMP percpu area setup.
1793e74e3962STejun Heo  *
1794e74e3962STejun Heo  * The embedding helper is used because its behavior closely resembles
1795e74e3962STejun Heo  * the original non-dynamic generic percpu area setup.  This is
1796e74e3962STejun Heo  * important because many archs have addressing restrictions and might
1797e74e3962STejun Heo  * fail if the percpu area is located far away from the previous
1798e74e3962STejun Heo  * location.  As an added bonus, in non-NUMA cases, embedding is
1799e74e3962STejun Heo  * generally a good idea TLB-wise because percpu area can piggy back
1800e74e3962STejun Heo  * on the physical linear memory mapping which uses large page
1801e74e3962STejun Heo  * mappings on applicable archs.
1802e74e3962STejun Heo  */
1803e74e3962STejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
1804e74e3962STejun Heo EXPORT_SYMBOL(__per_cpu_offset);
1805e74e3962STejun Heo 
1806c8826dd5STejun Heo static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size,
1807c8826dd5STejun Heo 				       size_t align)
1808c8826dd5STejun Heo {
1809c8826dd5STejun Heo 	return __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
1810c8826dd5STejun Heo }
1811c8826dd5STejun Heo 
1812c8826dd5STejun Heo static void __init pcpu_dfl_fc_free(void *ptr, size_t size)
1813c8826dd5STejun Heo {
1814c8826dd5STejun Heo 	free_bootmem(__pa(ptr), size);
1815c8826dd5STejun Heo }
1816c8826dd5STejun Heo 
1817e74e3962STejun Heo void __init setup_per_cpu_areas(void)
1818e74e3962STejun Heo {
1819e74e3962STejun Heo 	unsigned long delta;
1820e74e3962STejun Heo 	unsigned int cpu;
1821fb435d52STejun Heo 	int rc;
1822e74e3962STejun Heo 
1823e74e3962STejun Heo 	/*
1824e74e3962STejun Heo 	 * Always reserve area for module percpu variables.  That's
1825e74e3962STejun Heo 	 * what the legacy allocator did.
1826e74e3962STejun Heo 	 */
1827fb435d52STejun Heo 	rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
1828c8826dd5STejun Heo 				    PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL,
1829c8826dd5STejun Heo 				    pcpu_dfl_fc_alloc, pcpu_dfl_fc_free);
1830fb435d52STejun Heo 	if (rc < 0)
1831bbddff05STejun Heo 		panic("Failed to initialize percpu areas.");
1832e74e3962STejun Heo 
1833e74e3962STejun Heo 	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
1834e74e3962STejun Heo 	for_each_possible_cpu(cpu)
1835fb435d52STejun Heo 		__per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
1836e74e3962STejun Heo }
1837e74e3962STejun Heo #endif	/* CONFIG_HAVE_SETUP_PER_CPU_AREA */
1838099a19d9STejun Heo 
1839bbddff05STejun Heo #else	/* CONFIG_SMP */
1840bbddff05STejun Heo 
1841bbddff05STejun Heo /*
1842bbddff05STejun Heo  * UP percpu area setup.
1843bbddff05STejun Heo  *
1844bbddff05STejun Heo  * UP always uses km-based percpu allocator with identity mapping.
1845bbddff05STejun Heo  * Static percpu variables are indistinguishable from the usual static
1846bbddff05STejun Heo  * variables and don't require any special preparation.
1847bbddff05STejun Heo  */
1848bbddff05STejun Heo void __init setup_per_cpu_areas(void)
1849bbddff05STejun Heo {
1850bbddff05STejun Heo 	const size_t unit_size =
1851bbddff05STejun Heo 		roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE,
1852bbddff05STejun Heo 					 PERCPU_DYNAMIC_RESERVE));
1853bbddff05STejun Heo 	struct pcpu_alloc_info *ai;
1854bbddff05STejun Heo 	void *fc;
1855bbddff05STejun Heo 
1856bbddff05STejun Heo 	ai = pcpu_alloc_alloc_info(1, 1);
1857bbddff05STejun Heo 	fc = __alloc_bootmem(unit_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
1858bbddff05STejun Heo 	if (!ai || !fc)
1859bbddff05STejun Heo 		panic("Failed to allocate memory for percpu areas.");
1860bbddff05STejun Heo 
1861bbddff05STejun Heo 	ai->dyn_size = unit_size;
1862bbddff05STejun Heo 	ai->unit_size = unit_size;
1863bbddff05STejun Heo 	ai->atom_size = unit_size;
1864bbddff05STejun Heo 	ai->alloc_size = unit_size;
1865bbddff05STejun Heo 	ai->groups[0].nr_units = 1;
1866bbddff05STejun Heo 	ai->groups[0].cpu_map[0] = 0;
1867bbddff05STejun Heo 
1868bbddff05STejun Heo 	if (pcpu_setup_first_chunk(ai, fc) < 0)
1869bbddff05STejun Heo 		panic("Failed to initialize percpu areas.");
1870bbddff05STejun Heo }
1871bbddff05STejun Heo 
1872bbddff05STejun Heo #endif	/* CONFIG_SMP */
1873bbddff05STejun Heo 
1874099a19d9STejun Heo /*
1875099a19d9STejun Heo  * First and reserved chunks are initialized with temporary allocation
1876099a19d9STejun Heo  * map in initdata so that they can be used before slab is online.
1877099a19d9STejun Heo  * This function is called after slab is brought up and replaces those
1878099a19d9STejun Heo  * with properly allocated maps.
1879099a19d9STejun Heo  */
1880099a19d9STejun Heo void __init percpu_init_late(void)
1881099a19d9STejun Heo {
1882099a19d9STejun Heo 	struct pcpu_chunk *target_chunks[] =
1883099a19d9STejun Heo 		{ pcpu_first_chunk, pcpu_reserved_chunk, NULL };
1884099a19d9STejun Heo 	struct pcpu_chunk *chunk;
1885099a19d9STejun Heo 	unsigned long flags;
1886099a19d9STejun Heo 	int i;
1887099a19d9STejun Heo 
1888099a19d9STejun Heo 	for (i = 0; (chunk = target_chunks[i]); i++) {
1889099a19d9STejun Heo 		int *map;
1890099a19d9STejun Heo 		const size_t size = PERCPU_DYNAMIC_EARLY_SLOTS * sizeof(map[0]);
1891099a19d9STejun Heo 
1892099a19d9STejun Heo 		BUILD_BUG_ON(size > PAGE_SIZE);
1893099a19d9STejun Heo 
1894099a19d9STejun Heo 		map = pcpu_mem_alloc(size);
1895099a19d9STejun Heo 		BUG_ON(!map);
1896099a19d9STejun Heo 
1897099a19d9STejun Heo 		spin_lock_irqsave(&pcpu_lock, flags);
1898099a19d9STejun Heo 		memcpy(map, chunk->map, size);
1899099a19d9STejun Heo 		chunk->map = map;
1900099a19d9STejun Heo 		spin_unlock_irqrestore(&pcpu_lock, flags);
1901099a19d9STejun Heo 	}
1902099a19d9STejun Heo }
1903