1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_PERCPU_H
3 #define __LINUX_PERCPU_H
4 
5 #include <linux/mmdebug.h>
6 #include <linux/preempt.h>
7 #include <linux/smp.h>
8 #include <linux/cpumask.h>
9 #include <linux/printk.h>
10 #include <linux/pfn.h>
11 #include <linux/init.h>
12 
13 #include <asm/percpu.h>
14 
15 /* enough to cover all DEFINE_PER_CPUs in modules */
16 #ifdef CONFIG_MODULES
17 #define PERCPU_MODULE_RESERVE		(8 << 10)
18 #else
19 #define PERCPU_MODULE_RESERVE		0
20 #endif
21 
22 /* minimum unit size, also is the maximum supported allocation size */
23 #define PCPU_MIN_UNIT_SIZE		PFN_ALIGN(32 << 10)
24 
25 /* minimum allocation size and shift in bytes */
26 #define PCPU_MIN_ALLOC_SHIFT		2
27 #define PCPU_MIN_ALLOC_SIZE		(1 << PCPU_MIN_ALLOC_SHIFT)
28 
29 /* number of bits per page, used to trigger a scan if blocks are > PAGE_SIZE */
30 #define PCPU_BITS_PER_PAGE		(PAGE_SIZE >> PCPU_MIN_ALLOC_SHIFT)
31 
32 /*
33  * This determines the size of each metadata block.  There are several subtle
34  * constraints around this constant.  The reserved region must be a multiple of
35  * PCPU_BITMAP_BLOCK_SIZE.  Additionally, PCPU_BITMAP_BLOCK_SIZE must be a
36  * multiple of PAGE_SIZE or PAGE_SIZE must be a multiple of
37  * PCPU_BITMAP_BLOCK_SIZE to align with the populated page map. The unit_size
38  * also has to be a multiple of PCPU_BITMAP_BLOCK_SIZE to ensure full blocks.
39  */
40 #define PCPU_BITMAP_BLOCK_SIZE		PAGE_SIZE
41 #define PCPU_BITMAP_BLOCK_BITS		(PCPU_BITMAP_BLOCK_SIZE >>	\
42 					 PCPU_MIN_ALLOC_SHIFT)
43 
44 /*
45  * Percpu allocator can serve percpu allocations before slab is
46  * initialized which allows slab to depend on the percpu allocator.
47  * The following two parameters decide how much resource to
48  * preallocate for this.  Keep PERCPU_DYNAMIC_RESERVE equal to or
49  * larger than PERCPU_DYNAMIC_EARLY_SIZE.
50  */
51 #define PERCPU_DYNAMIC_EARLY_SLOTS	128
52 #define PERCPU_DYNAMIC_EARLY_SIZE	(12 << 10)
53 
54 /*
55  * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
56  * back on the first chunk for dynamic percpu allocation if arch is
57  * manually allocating and mapping it for faster access (as a part of
58  * large page mapping for example).
59  *
60  * The following values give between one and two pages of free space
61  * after typical minimal boot (2-way SMP, single disk and NIC) with
62  * both defconfig and a distro config on x86_64 and 32.  More
63  * intelligent way to determine this would be nice.
64  */
65 #if BITS_PER_LONG > 32
66 #define PERCPU_DYNAMIC_RESERVE		(28 << 10)
67 #else
68 #define PERCPU_DYNAMIC_RESERVE		(20 << 10)
69 #endif
70 
71 extern void *pcpu_base_addr;
72 extern const unsigned long *pcpu_unit_offsets;
73 
74 struct pcpu_group_info {
75 	int			nr_units;	/* aligned # of units */
76 	unsigned long		base_offset;	/* base address offset */
77 	unsigned int		*cpu_map;	/* unit->cpu map, empty
78 						 * entries contain NR_CPUS */
79 };
80 
81 struct pcpu_alloc_info {
82 	size_t			static_size;
83 	size_t			reserved_size;
84 	size_t			dyn_size;
85 	size_t			unit_size;
86 	size_t			atom_size;
87 	size_t			alloc_size;
88 	size_t			__ai_size;	/* internal, don't use */
89 	int			nr_groups;	/* 0 if grouping unnecessary */
90 	struct pcpu_group_info	groups[];
91 };
92 
93 enum pcpu_fc {
94 	PCPU_FC_AUTO,
95 	PCPU_FC_EMBED,
96 	PCPU_FC_PAGE,
97 
98 	PCPU_FC_NR,
99 };
100 extern const char * const pcpu_fc_names[PCPU_FC_NR];
101 
102 extern enum pcpu_fc pcpu_chosen_fc;
103 
104 typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
105 				     size_t align);
106 typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
107 typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
108 typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
109 
110 extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
111 							     int nr_units);
112 extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
113 
114 extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
115 					 void *base_addr);
116 
117 #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
118 extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
119 				size_t atom_size,
120 				pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
121 				pcpu_fc_alloc_fn_t alloc_fn,
122 				pcpu_fc_free_fn_t free_fn);
123 #endif
124 
125 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
126 extern int __init pcpu_page_first_chunk(size_t reserved_size,
127 				pcpu_fc_alloc_fn_t alloc_fn,
128 				pcpu_fc_free_fn_t free_fn,
129 				pcpu_fc_populate_pte_fn_t populate_pte_fn);
130 #endif
131 
132 extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
133 extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
134 extern bool is_kernel_percpu_address(unsigned long addr);
135 
136 #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
137 extern void __init setup_per_cpu_areas(void);
138 #endif
139 
140 extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
141 extern void __percpu *__alloc_percpu(size_t size, size_t align);
142 extern void free_percpu(void __percpu *__pdata);
143 extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
144 
145 #define alloc_percpu_gfp(type, gfp)					\
146 	(typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type),	\
147 						__alignof__(type), gfp)
148 #define alloc_percpu(type)						\
149 	(typeof(type) __percpu *)__alloc_percpu(sizeof(type),		\
150 						__alignof__(type))
151 
152 extern unsigned long pcpu_nr_pages(void);
153 
154 #endif /* __LINUX_PERCPU_H */
155