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