blob: 252ae9e960e0ce6f17f77fffd9a393dfae6f1da4 [file] [log] [blame]
Dennis Zhou8fa3ed82017-06-19 19:28:30 -04001#ifndef _MM_PERCPU_INTERNAL_H
2#define _MM_PERCPU_INTERNAL_H
3
4#include <linux/types.h>
5#include <linux/percpu.h>
6
Dennis Zhou (Facebook)ca460b32017-07-24 19:02:12 -04007/*
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
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040023struct pcpu_chunk {
Dennis Zhou30a5b532017-06-19 19:28:31 -040024#ifdef CONFIG_PERCPU_STATS
25 int nr_alloc; /* # of allocations */
26 size_t max_alloc_size; /* largest allocation size */
27#endif
28
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040029 struct list_head list; /* linked to pcpu_slot lists */
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070030 int free_bytes; /* free bytes in the chunk */
31 int contig_bits; /* max contiguous size hint */
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040032 void *base_addr; /* base address of this chunk */
33
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070034 unsigned long *alloc_map; /* allocation map */
35 unsigned long *bound_map; /* boundary map */
Dennis Zhou (Facebook)ca460b32017-07-24 19:02:12 -040036 struct pcpu_block_md *md_blocks; /* metadata blocks */
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040037
38 void *data; /* chunk data */
39 int first_free; /* no free below this */
40 bool immutable; /* no [de]population allowed */
Dennis Zhou (Facebook)e2266702017-07-24 19:01:59 -040041 int start_offset; /* the overlap with the previous
42 region to have a page aligned
43 base_addr */
Dennis Zhou (Facebook)6b9d7c82017-07-24 19:02:03 -040044 int end_offset; /* additional area required to
45 have the region end page
46 aligned */
Dennis Zhou (Facebook)c0ebfdc2017-07-24 19:02:05 -040047
48 int nr_pages; /* # of pages served by this chunk */
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040049 int nr_populated; /* # of populated pages */
Dennis Zhou (Facebook)0cecf502017-07-24 19:02:08 -040050 int nr_empty_pop_pages; /* # of empty populated pages */
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040051 unsigned long populated[]; /* populated bitmap */
52};
53
54extern spinlock_t pcpu_lock;
55
56extern struct list_head *pcpu_slot;
57extern int pcpu_nr_slots;
Dennis Zhou (Facebook)6b9b6f32017-07-15 22:23:08 -040058extern int pcpu_nr_empty_pop_pages;
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040059
60extern struct pcpu_chunk *pcpu_first_chunk;
61extern struct pcpu_chunk *pcpu_reserved_chunk;
62
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070063/**
Dennis Zhou (Facebook)ca460b32017-07-24 19:02:12 -040064 * 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
75/**
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070076 * 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
Dennis Zhou30a5b532017-06-19 19:28:31 -040099#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{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400175 unsigned long flags;
176 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400177
178 pcpu_stats.nr_chunks++;
179 pcpu_stats.nr_max_chunks =
180 max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
181
Dennis Zhou303abfd2017-06-21 13:52:46 -0400182 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400183}
184
185/*
186 * pcpu_stats_chunk_dealloc - decrement chunk stats
187 */
188static inline void pcpu_stats_chunk_dealloc(void)
189{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400190 unsigned long flags;
191 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400192
193 pcpu_stats.nr_chunks--;
194
Dennis Zhou303abfd2017-06-21 13:52:46 -0400195 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400196}
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
Dennis Zhou8fa3ed82017-06-19 19:28:30 -0400222#endif