blob: cd2442e13d8f5add724bb6be9de1c3dc2ae8f715 [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 Zhou8fa3ed82017-06-19 19:28:30 -040031 int nr_populated; /* # of populated pages */
32 unsigned long populated[]; /* populated bitmap */
33};
34
35extern spinlock_t pcpu_lock;
36
37extern struct list_head *pcpu_slot;
38extern int pcpu_nr_slots;
39
40extern struct pcpu_chunk *pcpu_first_chunk;
41extern struct pcpu_chunk *pcpu_reserved_chunk;
42
Dennis Zhou30a5b532017-06-19 19:28:31 -040043#ifdef CONFIG_PERCPU_STATS
44
45#include <linux/spinlock.h>
46
47struct percpu_stats {
48 u64 nr_alloc; /* lifetime # of allocations */
49 u64 nr_dealloc; /* lifetime # of deallocations */
50 u64 nr_cur_alloc; /* current # of allocations */
51 u64 nr_max_alloc; /* max # of live allocations */
52 u32 nr_chunks; /* current # of live chunks */
53 u32 nr_max_chunks; /* max # of live chunks */
54 size_t min_alloc_size; /* min allocaiton size */
55 size_t max_alloc_size; /* max allocation size */
56};
57
58extern struct percpu_stats pcpu_stats;
59extern struct pcpu_alloc_info pcpu_stats_ai;
60
61/*
62 * For debug purposes. We don't care about the flexible array.
63 */
64static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
65{
66 memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info));
67
68 /* initialize min_alloc_size to unit_size */
69 pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size;
70}
71
72/*
73 * pcpu_stats_area_alloc - increment area allocation stats
74 * @chunk: the location of the area being allocated
75 * @size: size of area to allocate in bytes
76 *
77 * CONTEXT:
78 * pcpu_lock.
79 */
80static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
81{
82 lockdep_assert_held(&pcpu_lock);
83
84 pcpu_stats.nr_alloc++;
85 pcpu_stats.nr_cur_alloc++;
86 pcpu_stats.nr_max_alloc =
87 max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc);
88 pcpu_stats.min_alloc_size =
89 min(pcpu_stats.min_alloc_size, size);
90 pcpu_stats.max_alloc_size =
91 max(pcpu_stats.max_alloc_size, size);
92
93 chunk->nr_alloc++;
94 chunk->max_alloc_size = max(chunk->max_alloc_size, size);
95}
96
97/*
98 * pcpu_stats_area_dealloc - decrement allocation stats
99 * @chunk: the location of the area being deallocated
100 *
101 * CONTEXT:
102 * pcpu_lock.
103 */
104static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
105{
106 lockdep_assert_held(&pcpu_lock);
107
108 pcpu_stats.nr_dealloc++;
109 pcpu_stats.nr_cur_alloc--;
110
111 chunk->nr_alloc--;
112}
113
114/*
115 * pcpu_stats_chunk_alloc - increment chunk stats
116 */
117static inline void pcpu_stats_chunk_alloc(void)
118{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400119 unsigned long flags;
120 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400121
122 pcpu_stats.nr_chunks++;
123 pcpu_stats.nr_max_chunks =
124 max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
125
Dennis Zhou303abfd2017-06-21 13:52:46 -0400126 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400127}
128
129/*
130 * pcpu_stats_chunk_dealloc - decrement chunk stats
131 */
132static inline void pcpu_stats_chunk_dealloc(void)
133{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400134 unsigned long flags;
135 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400136
137 pcpu_stats.nr_chunks--;
138
Dennis Zhou303abfd2017-06-21 13:52:46 -0400139 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400140}
141
142#else
143
144static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
145{
146}
147
148static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
149{
150}
151
152static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
153{
154}
155
156static inline void pcpu_stats_chunk_alloc(void)
157{
158}
159
160static inline void pcpu_stats_chunk_dealloc(void)
161{
162}
163
164#endif /* !CONFIG_PERCPU_STATS */
165
Dennis Zhou8fa3ed82017-06-19 19:28:30 -0400166#endif