Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 1 | #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) | ca460b3 | 2017-07-24 19:02:12 -0400 | [diff] [blame^] | 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 | */ |
| 12 | struct 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 Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 23 | struct pcpu_chunk { |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 24 | #ifdef CONFIG_PERCPU_STATS |
| 25 | int nr_alloc; /* # of allocations */ |
| 26 | size_t max_alloc_size; /* largest allocation size */ |
| 27 | #endif |
| 28 | |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 29 | struct list_head list; /* linked to pcpu_slot lists */ |
Dennis Zhou (Facebook) | 40064ae | 2017-07-12 11:27:32 -0700 | [diff] [blame] | 30 | int free_bytes; /* free bytes in the chunk */ |
| 31 | int contig_bits; /* max contiguous size hint */ |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 32 | void *base_addr; /* base address of this chunk */ |
| 33 | |
Dennis Zhou (Facebook) | 40064ae | 2017-07-12 11:27:32 -0700 | [diff] [blame] | 34 | unsigned long *alloc_map; /* allocation map */ |
| 35 | unsigned long *bound_map; /* boundary map */ |
Dennis Zhou (Facebook) | ca460b3 | 2017-07-24 19:02:12 -0400 | [diff] [blame^] | 36 | struct pcpu_block_md *md_blocks; /* metadata blocks */ |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 37 | |
| 38 | void *data; /* chunk data */ |
| 39 | int first_free; /* no free below this */ |
| 40 | bool immutable; /* no [de]population allowed */ |
Dennis Zhou (Facebook) | e226670 | 2017-07-24 19:01:59 -0400 | [diff] [blame] | 41 | int start_offset; /* the overlap with the previous |
| 42 | region to have a page aligned |
| 43 | base_addr */ |
Dennis Zhou (Facebook) | 6b9d7c8 | 2017-07-24 19:02:03 -0400 | [diff] [blame] | 44 | int end_offset; /* additional area required to |
| 45 | have the region end page |
| 46 | aligned */ |
Dennis Zhou (Facebook) | c0ebfdc | 2017-07-24 19:02:05 -0400 | [diff] [blame] | 47 | |
| 48 | int nr_pages; /* # of pages served by this chunk */ |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 49 | int nr_populated; /* # of populated pages */ |
Dennis Zhou (Facebook) | 0cecf50 | 2017-07-24 19:02:08 -0400 | [diff] [blame] | 50 | int nr_empty_pop_pages; /* # of empty populated pages */ |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 51 | unsigned long populated[]; /* populated bitmap */ |
| 52 | }; |
| 53 | |
| 54 | extern spinlock_t pcpu_lock; |
| 55 | |
| 56 | extern struct list_head *pcpu_slot; |
| 57 | extern int pcpu_nr_slots; |
Dennis Zhou (Facebook) | 6b9b6f3 | 2017-07-15 22:23:08 -0400 | [diff] [blame] | 58 | extern int pcpu_nr_empty_pop_pages; |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 59 | |
| 60 | extern struct pcpu_chunk *pcpu_first_chunk; |
| 61 | extern struct pcpu_chunk *pcpu_reserved_chunk; |
| 62 | |
Dennis Zhou (Facebook) | 40064ae | 2017-07-12 11:27:32 -0700 | [diff] [blame] | 63 | /** |
Dennis Zhou (Facebook) | ca460b3 | 2017-07-24 19:02:12 -0400 | [diff] [blame^] | 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 | */ |
| 70 | static 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) | 40064ae | 2017-07-12 11:27:32 -0700 | [diff] [blame] | 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 | */ |
| 82 | static 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 | */ |
| 94 | static 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 Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 99 | #ifdef CONFIG_PERCPU_STATS |
| 100 | |
| 101 | #include <linux/spinlock.h> |
| 102 | |
| 103 | struct 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 | |
| 114 | extern struct percpu_stats pcpu_stats; |
| 115 | extern struct pcpu_alloc_info pcpu_stats_ai; |
| 116 | |
| 117 | /* |
| 118 | * For debug purposes. We don't care about the flexible array. |
| 119 | */ |
| 120 | static 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 | */ |
| 136 | static 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 | */ |
| 160 | static 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 | */ |
| 173 | static inline void pcpu_stats_chunk_alloc(void) |
| 174 | { |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 175 | unsigned long flags; |
| 176 | spin_lock_irqsave(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 177 | |
| 178 | pcpu_stats.nr_chunks++; |
| 179 | pcpu_stats.nr_max_chunks = |
| 180 | max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks); |
| 181 | |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 182 | spin_unlock_irqrestore(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
| 186 | * pcpu_stats_chunk_dealloc - decrement chunk stats |
| 187 | */ |
| 188 | static inline void pcpu_stats_chunk_dealloc(void) |
| 189 | { |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 190 | unsigned long flags; |
| 191 | spin_lock_irqsave(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 192 | |
| 193 | pcpu_stats.nr_chunks--; |
| 194 | |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 195 | spin_unlock_irqrestore(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #else |
| 199 | |
| 200 | static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size) |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | static inline void pcpu_stats_chunk_alloc(void) |
| 213 | { |
| 214 | } |
| 215 | |
| 216 | static inline void pcpu_stats_chunk_dealloc(void) |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | #endif /* !CONFIG_PERCPU_STATS */ |
| 221 | |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 222 | #endif |