blob: 92fc0121dfff00128761c1fb96e0887c40207b7c [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
7struct pcpu_chunk {
Dennis Zhou30a5b532017-06-19 19:28:31 -04008#ifdef CONFIG_PERCPU_STATS
9 int nr_alloc; /* # of allocations */
10 size_t max_alloc_size; /* largest allocation size */
11#endif
12
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040013 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 Zhou30a5b532017-06-19 19:28:31 -040026 bool has_reserved; /* Indicates if chunk has reserved space
27 at the beginning. Reserved chunk will
28 contain reservation for static chunk.
29 Dynamic chunk will contain reservation
30 for static and reserved chunks. */
Dennis Zhou (Facebook)e2266702017-07-24 19:01:59 -040031 int start_offset; /* the overlap with the previous
32 region to have a page aligned
33 base_addr */
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040034 int nr_populated; /* # of populated pages */
35 unsigned long populated[]; /* populated bitmap */
36};
37
38extern spinlock_t pcpu_lock;
39
40extern struct list_head *pcpu_slot;
41extern int pcpu_nr_slots;
Dennis Zhou (Facebook)6b9b6f32017-07-15 22:23:08 -040042extern int pcpu_nr_empty_pop_pages;
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040043
44extern struct pcpu_chunk *pcpu_first_chunk;
45extern struct pcpu_chunk *pcpu_reserved_chunk;
46
Dennis Zhou30a5b532017-06-19 19:28:31 -040047#ifdef CONFIG_PERCPU_STATS
48
49#include <linux/spinlock.h>
50
51struct percpu_stats {
52 u64 nr_alloc; /* lifetime # of allocations */
53 u64 nr_dealloc; /* lifetime # of deallocations */
54 u64 nr_cur_alloc; /* current # of allocations */
55 u64 nr_max_alloc; /* max # of live allocations */
56 u32 nr_chunks; /* current # of live chunks */
57 u32 nr_max_chunks; /* max # of live chunks */
58 size_t min_alloc_size; /* min allocaiton size */
59 size_t max_alloc_size; /* max allocation size */
60};
61
62extern struct percpu_stats pcpu_stats;
63extern struct pcpu_alloc_info pcpu_stats_ai;
64
65/*
66 * For debug purposes. We don't care about the flexible array.
67 */
68static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
69{
70 memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info));
71
72 /* initialize min_alloc_size to unit_size */
73 pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size;
74}
75
76/*
77 * pcpu_stats_area_alloc - increment area allocation stats
78 * @chunk: the location of the area being allocated
79 * @size: size of area to allocate in bytes
80 *
81 * CONTEXT:
82 * pcpu_lock.
83 */
84static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
85{
86 lockdep_assert_held(&pcpu_lock);
87
88 pcpu_stats.nr_alloc++;
89 pcpu_stats.nr_cur_alloc++;
90 pcpu_stats.nr_max_alloc =
91 max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc);
92 pcpu_stats.min_alloc_size =
93 min(pcpu_stats.min_alloc_size, size);
94 pcpu_stats.max_alloc_size =
95 max(pcpu_stats.max_alloc_size, size);
96
97 chunk->nr_alloc++;
98 chunk->max_alloc_size = max(chunk->max_alloc_size, size);
99}
100
101/*
102 * pcpu_stats_area_dealloc - decrement allocation stats
103 * @chunk: the location of the area being deallocated
104 *
105 * CONTEXT:
106 * pcpu_lock.
107 */
108static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
109{
110 lockdep_assert_held(&pcpu_lock);
111
112 pcpu_stats.nr_dealloc++;
113 pcpu_stats.nr_cur_alloc--;
114
115 chunk->nr_alloc--;
116}
117
118/*
119 * pcpu_stats_chunk_alloc - increment chunk stats
120 */
121static inline void pcpu_stats_chunk_alloc(void)
122{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400123 unsigned long flags;
124 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400125
126 pcpu_stats.nr_chunks++;
127 pcpu_stats.nr_max_chunks =
128 max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
129
Dennis Zhou303abfd2017-06-21 13:52:46 -0400130 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400131}
132
133/*
134 * pcpu_stats_chunk_dealloc - decrement chunk stats
135 */
136static inline void pcpu_stats_chunk_dealloc(void)
137{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400138 unsigned long flags;
139 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400140
141 pcpu_stats.nr_chunks--;
142
Dennis Zhou303abfd2017-06-21 13:52:46 -0400143 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400144}
145
146#else
147
148static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
149{
150}
151
152static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
153{
154}
155
156static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
157{
158}
159
160static inline void pcpu_stats_chunk_alloc(void)
161{
162}
163
164static inline void pcpu_stats_chunk_dealloc(void)
165{
166}
167
168#endif /* !CONFIG_PERCPU_STATS */
169
Dennis Zhou8fa3ed82017-06-19 19:28:30 -0400170#endif