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 | |
| 7 | struct pcpu_chunk { |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 8 | #ifdef CONFIG_PERCPU_STATS |
| 9 | int nr_alloc; /* # of allocations */ |
| 10 | size_t max_alloc_size; /* largest allocation size */ |
| 11 | #endif |
| 12 | |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 13 | struct list_head list; /* linked to pcpu_slot lists */ |
| 14 | int free_size; /* free bytes in the chunk */ |
| 15 | int contig_hint; /* max contiguous size hint */ |
| 16 | void *base_addr; /* base address of this chunk */ |
| 17 | |
| 18 | int map_used; /* # of map entries used before the sentry */ |
| 19 | int map_alloc; /* # of map entries allocated */ |
| 20 | int *map; /* allocation map */ |
| 21 | struct list_head map_extend_list;/* on pcpu_map_extend_chunks */ |
| 22 | |
| 23 | void *data; /* chunk data */ |
| 24 | int first_free; /* no free below this */ |
| 25 | bool immutable; /* no [de]population allowed */ |
Dennis Zhou (Facebook) | e226670 | 2017-07-24 19:01:59 -0400 | [diff] [blame] | 26 | int start_offset; /* the overlap with the previous |
| 27 | region to have a page aligned |
| 28 | base_addr */ |
Dennis Zhou (Facebook) | 6b9d7c8 | 2017-07-24 19:02:03 -0400 | [diff] [blame] | 29 | int end_offset; /* additional area required to |
| 30 | have the region end page |
| 31 | aligned */ |
Dennis Zhou (Facebook) | c0ebfdc | 2017-07-24 19:02:05 -0400 | [diff] [blame] | 32 | |
| 33 | int nr_pages; /* # of pages served by this chunk */ |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 34 | int nr_populated; /* # of populated pages */ |
Dennis Zhou (Facebook) | 0cecf50 | 2017-07-24 19:02:08 -0400 | [diff] [blame^] | 35 | int nr_empty_pop_pages; /* # of empty populated pages */ |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 36 | unsigned long populated[]; /* populated bitmap */ |
| 37 | }; |
| 38 | |
| 39 | extern spinlock_t pcpu_lock; |
| 40 | |
| 41 | extern struct list_head *pcpu_slot; |
| 42 | extern int pcpu_nr_slots; |
Dennis Zhou (Facebook) | 6b9b6f3 | 2017-07-15 22:23:08 -0400 | [diff] [blame] | 43 | extern int pcpu_nr_empty_pop_pages; |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 44 | |
| 45 | extern struct pcpu_chunk *pcpu_first_chunk; |
| 46 | extern struct pcpu_chunk *pcpu_reserved_chunk; |
| 47 | |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 48 | #ifdef CONFIG_PERCPU_STATS |
| 49 | |
| 50 | #include <linux/spinlock.h> |
| 51 | |
| 52 | struct percpu_stats { |
| 53 | u64 nr_alloc; /* lifetime # of allocations */ |
| 54 | u64 nr_dealloc; /* lifetime # of deallocations */ |
| 55 | u64 nr_cur_alloc; /* current # of allocations */ |
| 56 | u64 nr_max_alloc; /* max # of live allocations */ |
| 57 | u32 nr_chunks; /* current # of live chunks */ |
| 58 | u32 nr_max_chunks; /* max # of live chunks */ |
| 59 | size_t min_alloc_size; /* min allocaiton size */ |
| 60 | size_t max_alloc_size; /* max allocation size */ |
| 61 | }; |
| 62 | |
| 63 | extern struct percpu_stats pcpu_stats; |
| 64 | extern struct pcpu_alloc_info pcpu_stats_ai; |
| 65 | |
| 66 | /* |
| 67 | * For debug purposes. We don't care about the flexible array. |
| 68 | */ |
| 69 | static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai) |
| 70 | { |
| 71 | memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info)); |
| 72 | |
| 73 | /* initialize min_alloc_size to unit_size */ |
| 74 | pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * pcpu_stats_area_alloc - increment area allocation stats |
| 79 | * @chunk: the location of the area being allocated |
| 80 | * @size: size of area to allocate in bytes |
| 81 | * |
| 82 | * CONTEXT: |
| 83 | * pcpu_lock. |
| 84 | */ |
| 85 | static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size) |
| 86 | { |
| 87 | lockdep_assert_held(&pcpu_lock); |
| 88 | |
| 89 | pcpu_stats.nr_alloc++; |
| 90 | pcpu_stats.nr_cur_alloc++; |
| 91 | pcpu_stats.nr_max_alloc = |
| 92 | max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc); |
| 93 | pcpu_stats.min_alloc_size = |
| 94 | min(pcpu_stats.min_alloc_size, size); |
| 95 | pcpu_stats.max_alloc_size = |
| 96 | max(pcpu_stats.max_alloc_size, size); |
| 97 | |
| 98 | chunk->nr_alloc++; |
| 99 | chunk->max_alloc_size = max(chunk->max_alloc_size, size); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * pcpu_stats_area_dealloc - decrement allocation stats |
| 104 | * @chunk: the location of the area being deallocated |
| 105 | * |
| 106 | * CONTEXT: |
| 107 | * pcpu_lock. |
| 108 | */ |
| 109 | static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk) |
| 110 | { |
| 111 | lockdep_assert_held(&pcpu_lock); |
| 112 | |
| 113 | pcpu_stats.nr_dealloc++; |
| 114 | pcpu_stats.nr_cur_alloc--; |
| 115 | |
| 116 | chunk->nr_alloc--; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * pcpu_stats_chunk_alloc - increment chunk stats |
| 121 | */ |
| 122 | static inline void pcpu_stats_chunk_alloc(void) |
| 123 | { |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 124 | unsigned long flags; |
| 125 | spin_lock_irqsave(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 126 | |
| 127 | pcpu_stats.nr_chunks++; |
| 128 | pcpu_stats.nr_max_chunks = |
| 129 | max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks); |
| 130 | |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 131 | spin_unlock_irqrestore(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
| 135 | * pcpu_stats_chunk_dealloc - decrement chunk stats |
| 136 | */ |
| 137 | static inline void pcpu_stats_chunk_dealloc(void) |
| 138 | { |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 139 | unsigned long flags; |
| 140 | spin_lock_irqsave(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 141 | |
| 142 | pcpu_stats.nr_chunks--; |
| 143 | |
Dennis Zhou | 303abfd | 2017-06-21 13:52:46 -0400 | [diff] [blame] | 144 | spin_unlock_irqrestore(&pcpu_lock, flags); |
Dennis Zhou | 30a5b53 | 2017-06-19 19:28:31 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | #else |
| 148 | |
| 149 | static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk) |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | static inline void pcpu_stats_chunk_alloc(void) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | static inline void pcpu_stats_chunk_dealloc(void) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | #endif /* !CONFIG_PERCPU_STATS */ |
| 170 | |
Dennis Zhou | 8fa3ed8 | 2017-06-19 19:28:30 -0400 | [diff] [blame] | 171 | #endif |