1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/swap_slots.h>
30#include <linux/cpu.h>
31#include <linux/cpumask.h>
32#include <linux/vmalloc.h>
33#include <linux/mutex.h>
34
35#ifdef CONFIG_SWAP
36
37static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots);
38static bool swap_slot_cache_active;
39bool swap_slot_cache_enabled;
40static bool swap_slot_cache_initialized;
41DEFINE_MUTEX(swap_slots_cache_mutex);
42
43DEFINE_MUTEX(swap_slots_cache_enable_mutex);
44
45static void __drain_swap_slots_cache(unsigned int type);
46static void deactivate_swap_slots_cache(void);
47static void reactivate_swap_slots_cache(void);
48
49#define use_swap_slot_cache (swap_slot_cache_active && \
50 swap_slot_cache_enabled && swap_slot_cache_initialized)
51#define SLOTS_CACHE 0x1
52#define SLOTS_CACHE_RET 0x2
53
54static void deactivate_swap_slots_cache(void)
55{
56 mutex_lock(&swap_slots_cache_mutex);
57 swap_slot_cache_active = false;
58 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
59 mutex_unlock(&swap_slots_cache_mutex);
60}
61
62static void reactivate_swap_slots_cache(void)
63{
64 mutex_lock(&swap_slots_cache_mutex);
65 swap_slot_cache_active = true;
66 mutex_unlock(&swap_slots_cache_mutex);
67}
68
69
70void disable_swap_slots_cache_lock(void)
71{
72 mutex_lock(&swap_slots_cache_enable_mutex);
73 swap_slot_cache_enabled = false;
74 if (swap_slot_cache_initialized) {
75
76 get_online_cpus();
77 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
78 put_online_cpus();
79 }
80}
81
82static void __reenable_swap_slots_cache(void)
83{
84 swap_slot_cache_enabled = has_usable_swap();
85}
86
87void reenable_swap_slots_cache_unlock(void)
88{
89 __reenable_swap_slots_cache();
90 mutex_unlock(&swap_slots_cache_enable_mutex);
91}
92
93static bool check_cache_active(void)
94{
95 long pages;
96
97 if (!swap_slot_cache_enabled || !swap_slot_cache_initialized)
98 return false;
99
100 pages = get_nr_swap_pages();
101 if (!swap_slot_cache_active) {
102 if (pages > num_online_cpus() *
103 THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE)
104 reactivate_swap_slots_cache();
105 goto out;
106 }
107
108
109 if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE)
110 deactivate_swap_slots_cache();
111out:
112 return swap_slot_cache_active;
113}
114
115static int alloc_swap_slot_cache(unsigned int cpu)
116{
117 struct swap_slots_cache *cache;
118 swp_entry_t *slots, *slots_ret;
119
120
121
122
123
124
125 slots = vzalloc(sizeof(swp_entry_t) * SWAP_SLOTS_CACHE_SIZE);
126 if (!slots)
127 return -ENOMEM;
128
129 slots_ret = vzalloc(sizeof(swp_entry_t) * SWAP_SLOTS_CACHE_SIZE);
130 if (!slots_ret) {
131 vfree(slots);
132 return -ENOMEM;
133 }
134
135 mutex_lock(&swap_slots_cache_mutex);
136 cache = &per_cpu(swp_slots, cpu);
137 if (cache->slots || cache->slots_ret)
138
139 goto out;
140 if (!cache->lock_initialized) {
141 mutex_init(&cache->alloc_lock);
142 spin_lock_init(&cache->free_lock);
143 cache->lock_initialized = true;
144 }
145 cache->nr = 0;
146 cache->cur = 0;
147 cache->n_ret = 0;
148 cache->slots = slots;
149 slots = NULL;
150 cache->slots_ret = slots_ret;
151 slots_ret = NULL;
152out:
153 mutex_unlock(&swap_slots_cache_mutex);
154 if (slots)
155 vfree(slots);
156 if (slots_ret)
157 vfree(slots_ret);
158 return 0;
159}
160
161static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
162 bool free_slots)
163{
164 struct swap_slots_cache *cache;
165 swp_entry_t *slots = NULL;
166
167 cache = &per_cpu(swp_slots, cpu);
168 if ((type & SLOTS_CACHE) && cache->slots) {
169 mutex_lock(&cache->alloc_lock);
170 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
171 cache->cur = 0;
172 cache->nr = 0;
173 if (free_slots && cache->slots) {
174 vfree(cache->slots);
175 cache->slots = NULL;
176 }
177 mutex_unlock(&cache->alloc_lock);
178 }
179 if ((type & SLOTS_CACHE_RET) && cache->slots_ret) {
180 spin_lock_irq(&cache->free_lock);
181 swapcache_free_entries(cache->slots_ret, cache->n_ret);
182 cache->n_ret = 0;
183 if (free_slots && cache->slots_ret) {
184 slots = cache->slots_ret;
185 cache->slots_ret = NULL;
186 }
187 spin_unlock_irq(&cache->free_lock);
188 if (slots)
189 vfree(slots);
190 }
191}
192
193static void __drain_swap_slots_cache(unsigned int type)
194{
195 unsigned int cpu;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 for_each_online_cpu(cpu)
221 drain_slots_cache_cpu(cpu, type, false);
222}
223
224static int free_slot_cache(unsigned int cpu)
225{
226 mutex_lock(&swap_slots_cache_mutex);
227 drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true);
228 mutex_unlock(&swap_slots_cache_mutex);
229 return 0;
230}
231
232int enable_swap_slots_cache(void)
233{
234 int ret = 0;
235
236 mutex_lock(&swap_slots_cache_enable_mutex);
237 if (swap_slot_cache_initialized) {
238 __reenable_swap_slots_cache();
239 goto out_unlock;
240 }
241
242 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
243 alloc_swap_slot_cache, free_slot_cache);
244 if (ret < 0)
245 goto out_unlock;
246 swap_slot_cache_initialized = true;
247 __reenable_swap_slots_cache();
248out_unlock:
249 mutex_unlock(&swap_slots_cache_enable_mutex);
250 return 0;
251}
252
253
254static int refill_swap_slots_cache(struct swap_slots_cache *cache)
255{
256 if (!use_swap_slot_cache || cache->nr)
257 return 0;
258
259 cache->cur = 0;
260 if (swap_slot_cache_active)
261 cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE, cache->slots);
262
263 return cache->nr;
264}
265
266int free_swap_slot(swp_entry_t entry)
267{
268 struct swap_slots_cache *cache;
269
270 cache = &get_cpu_var(swp_slots);
271 if (use_swap_slot_cache && cache->slots_ret) {
272 spin_lock_irq(&cache->free_lock);
273
274 if (!use_swap_slot_cache) {
275 spin_unlock_irq(&cache->free_lock);
276 goto direct_free;
277 }
278 if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) {
279
280
281
282
283
284
285 swapcache_free_entries(cache->slots_ret, cache->n_ret);
286 cache->n_ret = 0;
287 }
288 cache->slots_ret[cache->n_ret++] = entry;
289 spin_unlock_irq(&cache->free_lock);
290 } else {
291direct_free:
292 swapcache_free_entries(&entry, 1);
293 }
294 put_cpu_var(swp_slots);
295
296 return 0;
297}
298
299swp_entry_t get_swap_page(void)
300{
301 swp_entry_t entry, *pentry;
302 struct swap_slots_cache *cache;
303
304
305
306
307
308
309
310
311
312
313 cache = raw_cpu_ptr(&swp_slots);
314
315 entry.val = 0;
316 if (check_cache_active()) {
317 mutex_lock(&cache->alloc_lock);
318 if (cache->slots) {
319repeat:
320 if (cache->nr) {
321 pentry = &cache->slots[cache->cur++];
322 entry = *pentry;
323 pentry->val = 0;
324 cache->nr--;
325 } else {
326 if (refill_swap_slots_cache(cache))
327 goto repeat;
328 }
329 }
330 mutex_unlock(&cache->alloc_lock);
331 if (entry.val)
332 return entry;
333 }
334
335 get_swap_pages(1, &entry);
336
337 return entry;
338}
339
340#endif
341