]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/percpu-internal.h
percpu: introduce bitmap metadata blocks
[mirror_ubuntu-jammy-kernel.git] / mm / percpu-internal.h
CommitLineData
8fa3ed80
DZ
1#ifndef _MM_PERCPU_INTERNAL_H
2#define _MM_PERCPU_INTERNAL_H
3
4#include <linux/types.h>
5#include <linux/percpu.h>
6
ca460b3c
DZF
7/*
8 * pcpu_block_md is the metadata block struct.
9 * Each chunk's bitmap is split into a number of full blocks.
10 * All units are in terms of bits.
11 */
12struct pcpu_block_md {
13 int contig_hint; /* contig hint for block */
14 int contig_hint_start; /* block relative starting
15 position of the contig hint */
16 int left_free; /* size of free space along
17 the left side of the block */
18 int right_free; /* size of free space along
19 the right side of the block */
20 int first_free; /* block position of first free */
21};
22
8fa3ed80 23struct pcpu_chunk {
30a5b536
DZ
24#ifdef CONFIG_PERCPU_STATS
25 int nr_alloc; /* # of allocations */
26 size_t max_alloc_size; /* largest allocation size */
27#endif
28
8fa3ed80 29 struct list_head list; /* linked to pcpu_slot lists */
40064aec
DZF
30 int free_bytes; /* free bytes in the chunk */
31 int contig_bits; /* max contiguous size hint */
8fa3ed80
DZ
32 void *base_addr; /* base address of this chunk */
33
40064aec
DZF
34 unsigned long *alloc_map; /* allocation map */
35 unsigned long *bound_map; /* boundary map */
ca460b3c 36 struct pcpu_block_md *md_blocks; /* metadata blocks */
8fa3ed80
DZ
37
38 void *data; /* chunk data */
39 int first_free; /* no free below this */
40 bool immutable; /* no [de]population allowed */
e2266705
DZF
41 int start_offset; /* the overlap with the previous
42 region to have a page aligned
43 base_addr */
6b9d7c8e
DZF
44 int end_offset; /* additional area required to
45 have the region end page
46 aligned */
c0ebfdc3
DZF
47
48 int nr_pages; /* # of pages served by this chunk */
8fa3ed80 49 int nr_populated; /* # of populated pages */
0cecf50c 50 int nr_empty_pop_pages; /* # of empty populated pages */
8fa3ed80
DZ
51 unsigned long populated[]; /* populated bitmap */
52};
53
54extern spinlock_t pcpu_lock;
55
56extern struct list_head *pcpu_slot;
57extern int pcpu_nr_slots;
6b9b6f39 58extern int pcpu_nr_empty_pop_pages;
8fa3ed80
DZ
59
60extern struct pcpu_chunk *pcpu_first_chunk;
61extern struct pcpu_chunk *pcpu_reserved_chunk;
62
ca460b3c
DZF
63/**
64 * pcpu_chunk_nr_blocks - converts nr_pages to # of md_blocks
65 * @chunk: chunk of interest
66 *
67 * This conversion is from the number of physical pages that the chunk
68 * serves to the number of bitmap blocks used.
69 */
70static inline int pcpu_chunk_nr_blocks(struct pcpu_chunk *chunk)
71{
72 return chunk->nr_pages * PAGE_SIZE / PCPU_BITMAP_BLOCK_SIZE;
73}
74
40064aec
DZF
75/**
76 * pcpu_nr_pages_to_map_bits - converts the pages to size of bitmap
77 * @pages: number of physical pages
78 *
79 * This conversion is from physical pages to the number of bits
80 * required in the bitmap.
81 */
82static inline int pcpu_nr_pages_to_map_bits(int pages)
83{
84 return pages * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE;
85}
86
87/**
88 * pcpu_chunk_map_bits - helper to convert nr_pages to size of bitmap
89 * @chunk: chunk of interest
90 *
91 * This conversion is from the number of physical pages that the chunk
92 * serves to the number of bits in the bitmap.
93 */
94static inline int pcpu_chunk_map_bits(struct pcpu_chunk *chunk)
95{
96 return pcpu_nr_pages_to_map_bits(chunk->nr_pages);
97}
98
30a5b536
DZ
99#ifdef CONFIG_PERCPU_STATS
100
101#include <linux/spinlock.h>
102
103struct percpu_stats {
104 u64 nr_alloc; /* lifetime # of allocations */
105 u64 nr_dealloc; /* lifetime # of deallocations */
106 u64 nr_cur_alloc; /* current # of allocations */
107 u64 nr_max_alloc; /* max # of live allocations */
108 u32 nr_chunks; /* current # of live chunks */
109 u32 nr_max_chunks; /* max # of live chunks */
110 size_t min_alloc_size; /* min allocaiton size */
111 size_t max_alloc_size; /* max allocation size */
112};
113
114extern struct percpu_stats pcpu_stats;
115extern struct pcpu_alloc_info pcpu_stats_ai;
116
117/*
118 * For debug purposes. We don't care about the flexible array.
119 */
120static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
121{
122 memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info));
123
124 /* initialize min_alloc_size to unit_size */
125 pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size;
126}
127
128/*
129 * pcpu_stats_area_alloc - increment area allocation stats
130 * @chunk: the location of the area being allocated
131 * @size: size of area to allocate in bytes
132 *
133 * CONTEXT:
134 * pcpu_lock.
135 */
136static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
137{
138 lockdep_assert_held(&pcpu_lock);
139
140 pcpu_stats.nr_alloc++;
141 pcpu_stats.nr_cur_alloc++;
142 pcpu_stats.nr_max_alloc =
143 max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc);
144 pcpu_stats.min_alloc_size =
145 min(pcpu_stats.min_alloc_size, size);
146 pcpu_stats.max_alloc_size =
147 max(pcpu_stats.max_alloc_size, size);
148
149 chunk->nr_alloc++;
150 chunk->max_alloc_size = max(chunk->max_alloc_size, size);
151}
152
153/*
154 * pcpu_stats_area_dealloc - decrement allocation stats
155 * @chunk: the location of the area being deallocated
156 *
157 * CONTEXT:
158 * pcpu_lock.
159 */
160static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
161{
162 lockdep_assert_held(&pcpu_lock);
163
164 pcpu_stats.nr_dealloc++;
165 pcpu_stats.nr_cur_alloc--;
166
167 chunk->nr_alloc--;
168}
169
170/*
171 * pcpu_stats_chunk_alloc - increment chunk stats
172 */
173static inline void pcpu_stats_chunk_alloc(void)
174{
303abfdf
DZ
175 unsigned long flags;
176 spin_lock_irqsave(&pcpu_lock, flags);
30a5b536
DZ
177
178 pcpu_stats.nr_chunks++;
179 pcpu_stats.nr_max_chunks =
180 max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
181
303abfdf 182 spin_unlock_irqrestore(&pcpu_lock, flags);
30a5b536
DZ
183}
184
185/*
186 * pcpu_stats_chunk_dealloc - decrement chunk stats
187 */
188static inline void pcpu_stats_chunk_dealloc(void)
189{
303abfdf
DZ
190 unsigned long flags;
191 spin_lock_irqsave(&pcpu_lock, flags);
30a5b536
DZ
192
193 pcpu_stats.nr_chunks--;
194
303abfdf 195 spin_unlock_irqrestore(&pcpu_lock, flags);
30a5b536
DZ
196}
197
198#else
199
200static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
201{
202}
203
204static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
205{
206}
207
208static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
209{
210}
211
212static inline void pcpu_stats_chunk_alloc(void)
213{
214}
215
216static inline void pcpu_stats_chunk_dealloc(void)
217{
218}
219
220#endif /* !CONFIG_PERCPU_STATS */
221
8fa3ed80 222#endif