blob: f02f31cea0e6c5dc3991cd12d37f06d71f38c77e [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 Zhou (Facebook)e2266702017-07-24 19:01:59 -040026 int start_offset; /* the overlap with the previous
27 region to have a page aligned
28 base_addr */
Dennis Zhou (Facebook)6b9d7c82017-07-24 19:02:03 -040029 int end_offset; /* additional area required to
30 have the region end page
31 aligned */
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040032 int nr_populated; /* # of populated pages */
33 unsigned long populated[]; /* populated bitmap */
34};
35
36extern spinlock_t pcpu_lock;
37
38extern struct list_head *pcpu_slot;
39extern int pcpu_nr_slots;
Dennis Zhou (Facebook)6b9b6f32017-07-15 22:23:08 -040040extern int pcpu_nr_empty_pop_pages;
Dennis Zhou8fa3ed82017-06-19 19:28:30 -040041
42extern struct pcpu_chunk *pcpu_first_chunk;
43extern struct pcpu_chunk *pcpu_reserved_chunk;
44
Dennis Zhou30a5b532017-06-19 19:28:31 -040045#ifdef CONFIG_PERCPU_STATS
46
47#include <linux/spinlock.h>
48
49struct percpu_stats {
50 u64 nr_alloc; /* lifetime # of allocations */
51 u64 nr_dealloc; /* lifetime # of deallocations */
52 u64 nr_cur_alloc; /* current # of allocations */
53 u64 nr_max_alloc; /* max # of live allocations */
54 u32 nr_chunks; /* current # of live chunks */
55 u32 nr_max_chunks; /* max # of live chunks */
56 size_t min_alloc_size; /* min allocaiton size */
57 size_t max_alloc_size; /* max allocation size */
58};
59
60extern struct percpu_stats pcpu_stats;
61extern struct pcpu_alloc_info pcpu_stats_ai;
62
63/*
64 * For debug purposes. We don't care about the flexible array.
65 */
66static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
67{
68 memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info));
69
70 /* initialize min_alloc_size to unit_size */
71 pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size;
72}
73
74/*
75 * pcpu_stats_area_alloc - increment area allocation stats
76 * @chunk: the location of the area being allocated
77 * @size: size of area to allocate in bytes
78 *
79 * CONTEXT:
80 * pcpu_lock.
81 */
82static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
83{
84 lockdep_assert_held(&pcpu_lock);
85
86 pcpu_stats.nr_alloc++;
87 pcpu_stats.nr_cur_alloc++;
88 pcpu_stats.nr_max_alloc =
89 max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc);
90 pcpu_stats.min_alloc_size =
91 min(pcpu_stats.min_alloc_size, size);
92 pcpu_stats.max_alloc_size =
93 max(pcpu_stats.max_alloc_size, size);
94
95 chunk->nr_alloc++;
96 chunk->max_alloc_size = max(chunk->max_alloc_size, size);
97}
98
99/*
100 * pcpu_stats_area_dealloc - decrement allocation stats
101 * @chunk: the location of the area being deallocated
102 *
103 * CONTEXT:
104 * pcpu_lock.
105 */
106static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
107{
108 lockdep_assert_held(&pcpu_lock);
109
110 pcpu_stats.nr_dealloc++;
111 pcpu_stats.nr_cur_alloc--;
112
113 chunk->nr_alloc--;
114}
115
116/*
117 * pcpu_stats_chunk_alloc - increment chunk stats
118 */
119static inline void pcpu_stats_chunk_alloc(void)
120{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400121 unsigned long flags;
122 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400123
124 pcpu_stats.nr_chunks++;
125 pcpu_stats.nr_max_chunks =
126 max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
127
Dennis Zhou303abfd2017-06-21 13:52:46 -0400128 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400129}
130
131/*
132 * pcpu_stats_chunk_dealloc - decrement chunk stats
133 */
134static inline void pcpu_stats_chunk_dealloc(void)
135{
Dennis Zhou303abfd2017-06-21 13:52:46 -0400136 unsigned long flags;
137 spin_lock_irqsave(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400138
139 pcpu_stats.nr_chunks--;
140
Dennis Zhou303abfd2017-06-21 13:52:46 -0400141 spin_unlock_irqrestore(&pcpu_lock, flags);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400142}
143
144#else
145
146static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
147{
148}
149
150static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
151{
152}
153
154static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
155{
156}
157
158static inline void pcpu_stats_chunk_alloc(void)
159{
160}
161
162static inline void pcpu_stats_chunk_dealloc(void)
163{
164}
165
166#endif /* !CONFIG_PERCPU_STATS */
167
Dennis Zhou8fa3ed82017-06-19 19:28:30 -0400168#endif