1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _MM_PERCPU_INTERNAL_H
3*4882a593Smuzhiyun #define _MM_PERCPU_INTERNAL_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun #include <linux/percpu.h>
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * There are two chunk types: root and memcg-aware.
10*4882a593Smuzhiyun * Chunks of each type have separate slots list.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Memcg-aware chunks have an attached vector of obj_cgroup pointers, which is
13*4882a593Smuzhiyun * used to store memcg membership data of a percpu object. Obj_cgroups are
14*4882a593Smuzhiyun * ref-counted pointers to a memory cgroup with an ability to switch dynamically
15*4882a593Smuzhiyun * to the parent memory cgroup. This allows to reclaim a deleted memory cgroup
16*4882a593Smuzhiyun * without reclaiming of all outstanding objects, which hold a reference at it.
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun enum pcpu_chunk_type {
19*4882a593Smuzhiyun PCPU_CHUNK_ROOT,
20*4882a593Smuzhiyun #ifdef CONFIG_MEMCG_KMEM
21*4882a593Smuzhiyun PCPU_CHUNK_MEMCG,
22*4882a593Smuzhiyun #endif
23*4882a593Smuzhiyun PCPU_NR_CHUNK_TYPES,
24*4882a593Smuzhiyun PCPU_FAIL_ALLOC = PCPU_NR_CHUNK_TYPES
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * pcpu_block_md is the metadata block struct.
29*4882a593Smuzhiyun * Each chunk's bitmap is split into a number of full blocks.
30*4882a593Smuzhiyun * All units are in terms of bits.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * The scan hint is the largest known contiguous area before the contig hint.
33*4882a593Smuzhiyun * It is not necessarily the actual largest contig hint though. There is an
34*4882a593Smuzhiyun * invariant that the scan_hint_start > contig_hint_start iff
35*4882a593Smuzhiyun * scan_hint == contig_hint. This is necessary because when scanning forward,
36*4882a593Smuzhiyun * we don't know if a new contig hint would be better than the current one.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun struct pcpu_block_md {
39*4882a593Smuzhiyun int scan_hint; /* scan hint for block */
40*4882a593Smuzhiyun int scan_hint_start; /* block relative starting
41*4882a593Smuzhiyun position of the scan hint */
42*4882a593Smuzhiyun int contig_hint; /* contig hint for block */
43*4882a593Smuzhiyun int contig_hint_start; /* block relative starting
44*4882a593Smuzhiyun position of the contig hint */
45*4882a593Smuzhiyun int left_free; /* size of free space along
46*4882a593Smuzhiyun the left side of the block */
47*4882a593Smuzhiyun int right_free; /* size of free space along
48*4882a593Smuzhiyun the right side of the block */
49*4882a593Smuzhiyun int first_free; /* block position of first free */
50*4882a593Smuzhiyun int nr_bits; /* total bits responsible for */
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun struct pcpu_chunk {
54*4882a593Smuzhiyun #ifdef CONFIG_PERCPU_STATS
55*4882a593Smuzhiyun int nr_alloc; /* # of allocations */
56*4882a593Smuzhiyun size_t max_alloc_size; /* largest allocation size */
57*4882a593Smuzhiyun #endif
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun struct list_head list; /* linked to pcpu_slot lists */
60*4882a593Smuzhiyun int free_bytes; /* free bytes in the chunk */
61*4882a593Smuzhiyun struct pcpu_block_md chunk_md;
62*4882a593Smuzhiyun void *base_addr; /* base address of this chunk */
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun unsigned long *alloc_map; /* allocation map */
65*4882a593Smuzhiyun unsigned long *bound_map; /* boundary map */
66*4882a593Smuzhiyun struct pcpu_block_md *md_blocks; /* metadata blocks */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun void *data; /* chunk data */
69*4882a593Smuzhiyun bool immutable; /* no [de]population allowed */
70*4882a593Smuzhiyun int start_offset; /* the overlap with the previous
71*4882a593Smuzhiyun region to have a page aligned
72*4882a593Smuzhiyun base_addr */
73*4882a593Smuzhiyun int end_offset; /* additional area required to
74*4882a593Smuzhiyun have the region end page
75*4882a593Smuzhiyun aligned */
76*4882a593Smuzhiyun #ifdef CONFIG_MEMCG_KMEM
77*4882a593Smuzhiyun struct obj_cgroup **obj_cgroups; /* vector of object cgroups */
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun int nr_pages; /* # of pages served by this chunk */
81*4882a593Smuzhiyun int nr_populated; /* # of populated pages */
82*4882a593Smuzhiyun int nr_empty_pop_pages; /* # of empty populated pages */
83*4882a593Smuzhiyun unsigned long populated[]; /* populated bitmap */
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun extern spinlock_t pcpu_lock;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun extern struct list_head *pcpu_chunk_lists;
89*4882a593Smuzhiyun extern int pcpu_nr_slots;
90*4882a593Smuzhiyun extern int pcpu_nr_empty_pop_pages[];
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun extern struct pcpu_chunk *pcpu_first_chunk;
93*4882a593Smuzhiyun extern struct pcpu_chunk *pcpu_reserved_chunk;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun * pcpu_chunk_nr_blocks - converts nr_pages to # of md_blocks
97*4882a593Smuzhiyun * @chunk: chunk of interest
98*4882a593Smuzhiyun *
99*4882a593Smuzhiyun * This conversion is from the number of physical pages that the chunk
100*4882a593Smuzhiyun * serves to the number of bitmap blocks used.
101*4882a593Smuzhiyun */
pcpu_chunk_nr_blocks(struct pcpu_chunk * chunk)102*4882a593Smuzhiyun static inline int pcpu_chunk_nr_blocks(struct pcpu_chunk *chunk)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun return chunk->nr_pages * PAGE_SIZE / PCPU_BITMAP_BLOCK_SIZE;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /**
108*4882a593Smuzhiyun * pcpu_nr_pages_to_map_bits - converts the pages to size of bitmap
109*4882a593Smuzhiyun * @pages: number of physical pages
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * This conversion is from physical pages to the number of bits
112*4882a593Smuzhiyun * required in the bitmap.
113*4882a593Smuzhiyun */
pcpu_nr_pages_to_map_bits(int pages)114*4882a593Smuzhiyun static inline int pcpu_nr_pages_to_map_bits(int pages)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun return pages * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun * pcpu_chunk_map_bits - helper to convert nr_pages to size of bitmap
121*4882a593Smuzhiyun * @chunk: chunk of interest
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * This conversion is from the number of physical pages that the chunk
124*4882a593Smuzhiyun * serves to the number of bits in the bitmap.
125*4882a593Smuzhiyun */
pcpu_chunk_map_bits(struct pcpu_chunk * chunk)126*4882a593Smuzhiyun static inline int pcpu_chunk_map_bits(struct pcpu_chunk *chunk)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun return pcpu_nr_pages_to_map_bits(chunk->nr_pages);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun #ifdef CONFIG_MEMCG_KMEM
pcpu_chunk_type(struct pcpu_chunk * chunk)132*4882a593Smuzhiyun static inline enum pcpu_chunk_type pcpu_chunk_type(struct pcpu_chunk *chunk)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun if (chunk->obj_cgroups)
135*4882a593Smuzhiyun return PCPU_CHUNK_MEMCG;
136*4882a593Smuzhiyun return PCPU_CHUNK_ROOT;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
pcpu_is_memcg_chunk(enum pcpu_chunk_type chunk_type)139*4882a593Smuzhiyun static inline bool pcpu_is_memcg_chunk(enum pcpu_chunk_type chunk_type)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun return chunk_type == PCPU_CHUNK_MEMCG;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun #else
pcpu_chunk_type(struct pcpu_chunk * chunk)145*4882a593Smuzhiyun static inline enum pcpu_chunk_type pcpu_chunk_type(struct pcpu_chunk *chunk)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun return PCPU_CHUNK_ROOT;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
pcpu_is_memcg_chunk(enum pcpu_chunk_type chunk_type)150*4882a593Smuzhiyun static inline bool pcpu_is_memcg_chunk(enum pcpu_chunk_type chunk_type)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun return false;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun
pcpu_chunk_list(enum pcpu_chunk_type chunk_type)156*4882a593Smuzhiyun static inline struct list_head *pcpu_chunk_list(enum pcpu_chunk_type chunk_type)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun return &pcpu_chunk_lists[pcpu_nr_slots *
159*4882a593Smuzhiyun pcpu_is_memcg_chunk(chunk_type)];
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun #ifdef CONFIG_PERCPU_STATS
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun #include <linux/spinlock.h>
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun struct percpu_stats {
167*4882a593Smuzhiyun u64 nr_alloc; /* lifetime # of allocations */
168*4882a593Smuzhiyun u64 nr_dealloc; /* lifetime # of deallocations */
169*4882a593Smuzhiyun u64 nr_cur_alloc; /* current # of allocations */
170*4882a593Smuzhiyun u64 nr_max_alloc; /* max # of live allocations */
171*4882a593Smuzhiyun u32 nr_chunks; /* current # of live chunks */
172*4882a593Smuzhiyun u32 nr_max_chunks; /* max # of live chunks */
173*4882a593Smuzhiyun size_t min_alloc_size; /* min allocaiton size */
174*4882a593Smuzhiyun size_t max_alloc_size; /* max allocation size */
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun extern struct percpu_stats pcpu_stats;
178*4882a593Smuzhiyun extern struct pcpu_alloc_info pcpu_stats_ai;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * For debug purposes. We don't care about the flexible array.
182*4882a593Smuzhiyun */
pcpu_stats_save_ai(const struct pcpu_alloc_info * ai)183*4882a593Smuzhiyun static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info));
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* initialize min_alloc_size to unit_size */
188*4882a593Smuzhiyun pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * pcpu_stats_area_alloc - increment area allocation stats
193*4882a593Smuzhiyun * @chunk: the location of the area being allocated
194*4882a593Smuzhiyun * @size: size of area to allocate in bytes
195*4882a593Smuzhiyun *
196*4882a593Smuzhiyun * CONTEXT:
197*4882a593Smuzhiyun * pcpu_lock.
198*4882a593Smuzhiyun */
pcpu_stats_area_alloc(struct pcpu_chunk * chunk,size_t size)199*4882a593Smuzhiyun static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun lockdep_assert_held(&pcpu_lock);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun pcpu_stats.nr_alloc++;
204*4882a593Smuzhiyun pcpu_stats.nr_cur_alloc++;
205*4882a593Smuzhiyun pcpu_stats.nr_max_alloc =
206*4882a593Smuzhiyun max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc);
207*4882a593Smuzhiyun pcpu_stats.min_alloc_size =
208*4882a593Smuzhiyun min(pcpu_stats.min_alloc_size, size);
209*4882a593Smuzhiyun pcpu_stats.max_alloc_size =
210*4882a593Smuzhiyun max(pcpu_stats.max_alloc_size, size);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun chunk->nr_alloc++;
213*4882a593Smuzhiyun chunk->max_alloc_size = max(chunk->max_alloc_size, size);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * pcpu_stats_area_dealloc - decrement allocation stats
218*4882a593Smuzhiyun * @chunk: the location of the area being deallocated
219*4882a593Smuzhiyun *
220*4882a593Smuzhiyun * CONTEXT:
221*4882a593Smuzhiyun * pcpu_lock.
222*4882a593Smuzhiyun */
pcpu_stats_area_dealloc(struct pcpu_chunk * chunk)223*4882a593Smuzhiyun static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun lockdep_assert_held(&pcpu_lock);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun pcpu_stats.nr_dealloc++;
228*4882a593Smuzhiyun pcpu_stats.nr_cur_alloc--;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun chunk->nr_alloc--;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /*
234*4882a593Smuzhiyun * pcpu_stats_chunk_alloc - increment chunk stats
235*4882a593Smuzhiyun */
pcpu_stats_chunk_alloc(void)236*4882a593Smuzhiyun static inline void pcpu_stats_chunk_alloc(void)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun unsigned long flags;
239*4882a593Smuzhiyun spin_lock_irqsave(&pcpu_lock, flags);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun pcpu_stats.nr_chunks++;
242*4882a593Smuzhiyun pcpu_stats.nr_max_chunks =
243*4882a593Smuzhiyun max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun spin_unlock_irqrestore(&pcpu_lock, flags);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * pcpu_stats_chunk_dealloc - decrement chunk stats
250*4882a593Smuzhiyun */
pcpu_stats_chunk_dealloc(void)251*4882a593Smuzhiyun static inline void pcpu_stats_chunk_dealloc(void)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun unsigned long flags;
254*4882a593Smuzhiyun spin_lock_irqsave(&pcpu_lock, flags);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun pcpu_stats.nr_chunks--;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun spin_unlock_irqrestore(&pcpu_lock, flags);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun #else
262*4882a593Smuzhiyun
pcpu_stats_save_ai(const struct pcpu_alloc_info * ai)263*4882a593Smuzhiyun static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
pcpu_stats_area_alloc(struct pcpu_chunk * chunk,size_t size)267*4882a593Smuzhiyun static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
pcpu_stats_area_dealloc(struct pcpu_chunk * chunk)271*4882a593Smuzhiyun static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
pcpu_stats_chunk_alloc(void)275*4882a593Smuzhiyun static inline void pcpu_stats_chunk_alloc(void)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
pcpu_stats_chunk_dealloc(void)279*4882a593Smuzhiyun static inline void pcpu_stats_chunk_dealloc(void)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun #endif /* !CONFIG_PERCPU_STATS */
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun #endif
286