Commit | Line | Data |
---|---|---|
7a338472 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
29f233cf DM |
2 | /* |
3 | * Frontswap frontend | |
4 | * | |
5 | * This code provides the generic "frontend" layer to call a matching | |
6 | * "backend" driver implementation of frontswap. See | |
ad56b738 | 7 | * Documentation/vm/frontswap.rst for more information. |
29f233cf DM |
8 | * |
9 | * Copyright (C) 2009-2012 Oracle Corp. All rights reserved. | |
10 | * Author: Dan Magenheimer | |
29f233cf DM |
11 | */ |
12 | ||
29f233cf DM |
13 | #include <linux/mman.h> |
14 | #include <linux/swap.h> | |
15 | #include <linux/swapops.h> | |
29f233cf | 16 | #include <linux/security.h> |
29f233cf | 17 | #include <linux/module.h> |
29f233cf DM |
18 | #include <linux/debugfs.h> |
19 | #include <linux/frontswap.h> | |
20 | #include <linux/swapfile.h> | |
21 | ||
8ea1d2a1 VB |
22 | DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key); |
23 | ||
29f233cf | 24 | /* |
d1dc6f1b DS |
25 | * frontswap_ops are added by frontswap_register_ops, and provide the |
26 | * frontswap "backend" implementation functions. Multiple implementations | |
27 | * may be registered, but implementations can never deregister. This | |
28 | * is a simple singly-linked list of all registered implementations. | |
29f233cf | 29 | */ |
1e01c968 | 30 | static struct frontswap_ops *frontswap_ops __read_mostly; |
29f233cf | 31 | |
d1dc6f1b DS |
32 | #define for_each_frontswap_ops(ops) \ |
33 | for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next) | |
34 | ||
e3483a5f DM |
35 | /* |
36 | * If enabled, the underlying tmem implementation is capable of doing | |
37 | * exclusive gets, so frontswap_load, on a successful tmem_get must | |
38 | * mark the page as no longer in frontswap AND mark it dirty. | |
39 | */ | |
40 | static bool frontswap_tmem_exclusive_gets_enabled __read_mostly; | |
41 | ||
29f233cf DM |
42 | #ifdef CONFIG_DEBUG_FS |
43 | /* | |
44 | * Counters available via /sys/kernel/debug/frontswap (if debugfs is | |
45 | * properly configured). These are for information only so are not protected | |
46 | * against increment races. | |
47 | */ | |
165c8aed KRW |
48 | static u64 frontswap_loads; |
49 | static u64 frontswap_succ_stores; | |
50 | static u64 frontswap_failed_stores; | |
29f233cf DM |
51 | static u64 frontswap_invalidates; |
52 | ||
68d68ff6 ZD |
53 | static inline void inc_frontswap_loads(void) |
54 | { | |
96bdd2bc | 55 | data_race(frontswap_loads++); |
29f233cf | 56 | } |
68d68ff6 ZD |
57 | static inline void inc_frontswap_succ_stores(void) |
58 | { | |
96bdd2bc | 59 | data_race(frontswap_succ_stores++); |
29f233cf | 60 | } |
68d68ff6 ZD |
61 | static inline void inc_frontswap_failed_stores(void) |
62 | { | |
96bdd2bc | 63 | data_race(frontswap_failed_stores++); |
29f233cf | 64 | } |
68d68ff6 ZD |
65 | static inline void inc_frontswap_invalidates(void) |
66 | { | |
96bdd2bc | 67 | data_race(frontswap_invalidates++); |
29f233cf DM |
68 | } |
69 | #else | |
165c8aed KRW |
70 | static inline void inc_frontswap_loads(void) { } |
71 | static inline void inc_frontswap_succ_stores(void) { } | |
72 | static inline void inc_frontswap_failed_stores(void) { } | |
29f233cf DM |
73 | static inline void inc_frontswap_invalidates(void) { } |
74 | #endif | |
905cd0e1 DM |
75 | |
76 | /* | |
77 | * Due to the asynchronous nature of the backends loading potentially | |
78 | * _after_ the swap system has been activated, we have chokepoints | |
79 | * on all frontswap functions to not call the backend until the backend | |
80 | * has registered. | |
81 | * | |
905cd0e1 DM |
82 | * This would not guards us against the user deciding to call swapoff right as |
83 | * we are calling the backend to initialize (so swapon is in action). | |
404f3ecf | 84 | * Fortunately for us, the swapon_mutex has been taken by the callee so we are |
905cd0e1 DM |
85 | * OK. The other scenario where calls to frontswap_store (called via |
86 | * swap_writepage) is racing with frontswap_invalidate_area (called via | |
87 | * swapoff) is again guarded by the swap subsystem. | |
88 | * | |
89 | * While no backend is registered all calls to frontswap_[store|load| | |
90 | * invalidate_area|invalidate_page] are ignored or fail. | |
91 | * | |
92 | * The time between the backend being registered and the swap file system | |
93 | * calling the backend (via the frontswap_* functions) is indeterminate as | |
1e01c968 | 94 | * frontswap_ops is not atomic_t (or a value guarded by a spinlock). |
905cd0e1 DM |
95 | * That is OK as we are comfortable missing some of these calls to the newly |
96 | * registered backend. | |
97 | * | |
98 | * Obviously the opposite (unloading the backend) must be done after all | |
99 | * the frontswap_[store|load|invalidate_area|invalidate_page] start | |
d1dc6f1b DS |
100 | * ignoring or failing the requests. However, there is currently no way |
101 | * to unload a backend once it is registered. | |
905cd0e1 | 102 | */ |
905cd0e1 | 103 | |
29f233cf | 104 | /* |
d1dc6f1b | 105 | * Register operations for frontswap |
29f233cf | 106 | */ |
d1dc6f1b | 107 | void frontswap_register_ops(struct frontswap_ops *ops) |
29f233cf | 108 | { |
d1dc6f1b DS |
109 | DECLARE_BITMAP(a, MAX_SWAPFILES); |
110 | DECLARE_BITMAP(b, MAX_SWAPFILES); | |
111 | struct swap_info_struct *si; | |
112 | unsigned int i; | |
113 | ||
114 | bitmap_zero(a, MAX_SWAPFILES); | |
115 | bitmap_zero(b, MAX_SWAPFILES); | |
116 | ||
117 | spin_lock(&swap_lock); | |
118 | plist_for_each_entry(si, &swap_active_head, list) { | |
119 | if (!WARN_ON(!si->frontswap_map)) | |
3795f46b | 120 | __set_bit(si->type, a); |
d1dc6f1b DS |
121 | } |
122 | spin_unlock(&swap_lock); | |
123 | ||
124 | /* the new ops needs to know the currently active swap devices */ | |
125 | for_each_set_bit(i, a, MAX_SWAPFILES) | |
126 | ops->init(i); | |
127 | ||
128 | /* | |
129 | * Setting frontswap_ops must happen after the ops->init() calls | |
130 | * above; cmpxchg implies smp_mb() which will ensure the init is | |
131 | * complete at this point. | |
132 | */ | |
133 | do { | |
134 | ops->next = frontswap_ops; | |
135 | } while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next); | |
136 | ||
8ea1d2a1 VB |
137 | static_branch_inc(&frontswap_enabled_key); |
138 | ||
d1dc6f1b DS |
139 | spin_lock(&swap_lock); |
140 | plist_for_each_entry(si, &swap_active_head, list) { | |
141 | if (si->frontswap_map) | |
3795f46b | 142 | __set_bit(si->type, b); |
905cd0e1 | 143 | } |
d1dc6f1b DS |
144 | spin_unlock(&swap_lock); |
145 | ||
905cd0e1 | 146 | /* |
d1dc6f1b DS |
147 | * On the very unlikely chance that a swap device was added or |
148 | * removed between setting the "a" list bits and the ops init | |
149 | * calls, we re-check and do init or invalidate for any changed | |
150 | * bits. | |
905cd0e1 | 151 | */ |
d1dc6f1b DS |
152 | if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) { |
153 | for (i = 0; i < MAX_SWAPFILES; i++) { | |
154 | if (!test_bit(i, a) && test_bit(i, b)) | |
155 | ops->init(i); | |
156 | else if (test_bit(i, a) && !test_bit(i, b)) | |
157 | ops->invalidate_area(i); | |
158 | } | |
159 | } | |
29f233cf DM |
160 | } |
161 | EXPORT_SYMBOL(frontswap_register_ops); | |
162 | ||
e3483a5f DM |
163 | /* |
164 | * Enable/disable frontswap exclusive gets (see above). | |
165 | */ | |
166 | void frontswap_tmem_exclusive_gets(bool enable) | |
167 | { | |
168 | frontswap_tmem_exclusive_gets_enabled = enable; | |
169 | } | |
170 | EXPORT_SYMBOL(frontswap_tmem_exclusive_gets); | |
171 | ||
29f233cf DM |
172 | /* |
173 | * Called when a swap device is swapon'd. | |
174 | */ | |
4f89849d | 175 | void __frontswap_init(unsigned type, unsigned long *map) |
29f233cf DM |
176 | { |
177 | struct swap_info_struct *sis = swap_info[type]; | |
d1dc6f1b | 178 | struct frontswap_ops *ops; |
29f233cf | 179 | |
8ea1d2a1 | 180 | VM_BUG_ON(sis == NULL); |
4f89849d MK |
181 | |
182 | /* | |
183 | * p->frontswap is a bitmap that we MUST have to figure out which page | |
184 | * has gone in frontswap. Without it there is no point of continuing. | |
185 | */ | |
186 | if (WARN_ON(!map)) | |
187 | return; | |
188 | /* | |
189 | * Irregardless of whether the frontswap backend has been loaded | |
190 | * before this function or it will be later, we _MUST_ have the | |
191 | * p->frontswap set to something valid to work properly. | |
192 | */ | |
193 | frontswap_map_set(sis, map); | |
d1dc6f1b DS |
194 | |
195 | for_each_frontswap_ops(ops) | |
196 | ops->init(type); | |
29f233cf DM |
197 | } |
198 | EXPORT_SYMBOL(__frontswap_init); | |
199 | ||
f066ea23 BL |
200 | bool __frontswap_test(struct swap_info_struct *sis, |
201 | pgoff_t offset) | |
202 | { | |
d1dc6f1b DS |
203 | if (sis->frontswap_map) |
204 | return test_bit(offset, sis->frontswap_map); | |
205 | return false; | |
f066ea23 BL |
206 | } |
207 | EXPORT_SYMBOL(__frontswap_test); | |
208 | ||
d1dc6f1b DS |
209 | static inline void __frontswap_set(struct swap_info_struct *sis, |
210 | pgoff_t offset) | |
211 | { | |
212 | set_bit(offset, sis->frontswap_map); | |
213 | atomic_inc(&sis->frontswap_pages); | |
214 | } | |
215 | ||
f066ea23 | 216 | static inline void __frontswap_clear(struct swap_info_struct *sis, |
d1dc6f1b | 217 | pgoff_t offset) |
611edfed | 218 | { |
f066ea23 | 219 | clear_bit(offset, sis->frontswap_map); |
611edfed SL |
220 | atomic_dec(&sis->frontswap_pages); |
221 | } | |
222 | ||
29f233cf | 223 | /* |
165c8aed | 224 | * "Store" data from a page to frontswap and associate it with the page's |
29f233cf DM |
225 | * swaptype and offset. Page must be locked and in the swap cache. |
226 | * If frontswap already contains a page with matching swaptype and | |
1d00015e | 227 | * offset, the frontswap implementation may either overwrite the data and |
29f233cf DM |
228 | * return success or invalidate the page from frontswap and return failure. |
229 | */ | |
165c8aed | 230 | int __frontswap_store(struct page *page) |
29f233cf | 231 | { |
d1dc6f1b | 232 | int ret = -1; |
29f233cf DM |
233 | swp_entry_t entry = { .val = page_private(page), }; |
234 | int type = swp_type(entry); | |
235 | struct swap_info_struct *sis = swap_info[type]; | |
236 | pgoff_t offset = swp_offset(entry); | |
d1dc6f1b | 237 | struct frontswap_ops *ops; |
29f233cf | 238 | |
8ea1d2a1 VB |
239 | VM_BUG_ON(!frontswap_ops); |
240 | VM_BUG_ON(!PageLocked(page)); | |
241 | VM_BUG_ON(sis == NULL); | |
d1dc6f1b DS |
242 | |
243 | /* | |
244 | * If a dup, we must remove the old page first; we can't leave the | |
245 | * old page no matter if the store of the new page succeeds or fails, | |
246 | * and we can't rely on the new page replacing the old page as we may | |
247 | * not store to the same implementation that contains the old page. | |
248 | */ | |
249 | if (__frontswap_test(sis, offset)) { | |
250 | __frontswap_clear(sis, offset); | |
251 | for_each_frontswap_ops(ops) | |
252 | ops->invalidate_page(type, offset); | |
253 | } | |
254 | ||
255 | /* Try to store in each implementation, until one succeeds. */ | |
256 | for_each_frontswap_ops(ops) { | |
257 | ret = ops->store(type, offset, page); | |
258 | if (!ret) /* successful store */ | |
259 | break; | |
260 | } | |
29f233cf | 261 | if (ret == 0) { |
d1dc6f1b | 262 | __frontswap_set(sis, offset); |
165c8aed | 263 | inc_frontswap_succ_stores(); |
d9674dda | 264 | } else { |
165c8aed | 265 | inc_frontswap_failed_stores(); |
4bb3e31e | 266 | } |
3d6035f1 | 267 | |
29f233cf DM |
268 | return ret; |
269 | } | |
165c8aed | 270 | EXPORT_SYMBOL(__frontswap_store); |
29f233cf DM |
271 | |
272 | /* | |
273 | * "Get" data from frontswap associated with swaptype and offset that were | |
274 | * specified when the data was put to frontswap and use it to fill the | |
275 | * specified page with data. Page must be locked and in the swap cache. | |
276 | */ | |
165c8aed | 277 | int __frontswap_load(struct page *page) |
29f233cf DM |
278 | { |
279 | int ret = -1; | |
280 | swp_entry_t entry = { .val = page_private(page), }; | |
281 | int type = swp_type(entry); | |
282 | struct swap_info_struct *sis = swap_info[type]; | |
283 | pgoff_t offset = swp_offset(entry); | |
d1dc6f1b DS |
284 | struct frontswap_ops *ops; |
285 | ||
8ea1d2a1 VB |
286 | VM_BUG_ON(!frontswap_ops); |
287 | VM_BUG_ON(!PageLocked(page)); | |
288 | VM_BUG_ON(sis == NULL); | |
29f233cf | 289 | |
d1dc6f1b DS |
290 | if (!__frontswap_test(sis, offset)) |
291 | return -1; | |
292 | ||
293 | /* Try loading from each implementation, until one succeeds. */ | |
294 | for_each_frontswap_ops(ops) { | |
295 | ret = ops->load(type, offset, page); | |
296 | if (!ret) /* successful load */ | |
297 | break; | |
298 | } | |
e3483a5f | 299 | if (ret == 0) { |
165c8aed | 300 | inc_frontswap_loads(); |
e3483a5f DM |
301 | if (frontswap_tmem_exclusive_gets_enabled) { |
302 | SetPageDirty(page); | |
f066ea23 | 303 | __frontswap_clear(sis, offset); |
e3483a5f DM |
304 | } |
305 | } | |
29f233cf DM |
306 | return ret; |
307 | } | |
165c8aed | 308 | EXPORT_SYMBOL(__frontswap_load); |
29f233cf DM |
309 | |
310 | /* | |
311 | * Invalidate any data from frontswap associated with the specified swaptype | |
312 | * and offset so that a subsequent "get" will fail. | |
313 | */ | |
314 | void __frontswap_invalidate_page(unsigned type, pgoff_t offset) | |
315 | { | |
316 | struct swap_info_struct *sis = swap_info[type]; | |
d1dc6f1b DS |
317 | struct frontswap_ops *ops; |
318 | ||
8ea1d2a1 VB |
319 | VM_BUG_ON(!frontswap_ops); |
320 | VM_BUG_ON(sis == NULL); | |
29f233cf | 321 | |
d1dc6f1b DS |
322 | if (!__frontswap_test(sis, offset)) |
323 | return; | |
324 | ||
325 | for_each_frontswap_ops(ops) | |
326 | ops->invalidate_page(type, offset); | |
327 | __frontswap_clear(sis, offset); | |
328 | inc_frontswap_invalidates(); | |
29f233cf DM |
329 | } |
330 | EXPORT_SYMBOL(__frontswap_invalidate_page); | |
331 | ||
332 | /* | |
333 | * Invalidate all data from frontswap associated with all offsets for the | |
334 | * specified swaptype. | |
335 | */ | |
336 | void __frontswap_invalidate_area(unsigned type) | |
337 | { | |
338 | struct swap_info_struct *sis = swap_info[type]; | |
d1dc6f1b | 339 | struct frontswap_ops *ops; |
29f233cf | 340 | |
8ea1d2a1 VB |
341 | VM_BUG_ON(!frontswap_ops); |
342 | VM_BUG_ON(sis == NULL); | |
d1dc6f1b | 343 | |
d1dc6f1b DS |
344 | if (sis->frontswap_map == NULL) |
345 | return; | |
346 | ||
347 | for_each_frontswap_ops(ops) | |
348 | ops->invalidate_area(type); | |
349 | atomic_set(&sis->frontswap_pages, 0); | |
350 | bitmap_zero(sis->frontswap_map, sis->max); | |
29f233cf DM |
351 | } |
352 | EXPORT_SYMBOL(__frontswap_invalidate_area); | |
353 | ||
96253444 SL |
354 | static unsigned long __frontswap_curr_pages(void) |
355 | { | |
96253444 SL |
356 | unsigned long totalpages = 0; |
357 | struct swap_info_struct *si = NULL; | |
358 | ||
359 | assert_spin_locked(&swap_lock); | |
18ab4d4c | 360 | plist_for_each_entry(si, &swap_active_head, list) |
96253444 | 361 | totalpages += atomic_read(&si->frontswap_pages); |
96253444 SL |
362 | return totalpages; |
363 | } | |
364 | ||
f116695a SL |
365 | static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused, |
366 | int *swapid) | |
367 | { | |
368 | int ret = -EINVAL; | |
369 | struct swap_info_struct *si = NULL; | |
370 | int si_frontswap_pages; | |
371 | unsigned long total_pages_to_unuse = total; | |
372 | unsigned long pages = 0, pages_to_unuse = 0; | |
f116695a SL |
373 | |
374 | assert_spin_locked(&swap_lock); | |
18ab4d4c | 375 | plist_for_each_entry(si, &swap_active_head, list) { |
f116695a SL |
376 | si_frontswap_pages = atomic_read(&si->frontswap_pages); |
377 | if (total_pages_to_unuse < si_frontswap_pages) { | |
378 | pages = pages_to_unuse = total_pages_to_unuse; | |
379 | } else { | |
380 | pages = si_frontswap_pages; | |
381 | pages_to_unuse = 0; /* unuse all */ | |
382 | } | |
383 | /* ensure there is enough RAM to fetch pages from frontswap */ | |
384 | if (security_vm_enough_memory_mm(current->mm, pages)) { | |
385 | ret = -ENOMEM; | |
386 | continue; | |
387 | } | |
388 | vm_unacct_memory(pages); | |
389 | *unused = pages_to_unuse; | |
adfab836 | 390 | *swapid = si->type; |
f116695a SL |
391 | ret = 0; |
392 | break; | |
393 | } | |
394 | ||
395 | return ret; | |
396 | } | |
397 | ||
a00bb1e9 | 398 | /* |
404f3ecf EP |
399 | * Used to check if it's necessary and feasible to unuse pages. |
400 | * Return 1 when nothing to do, 0 when need to shrink pages, | |
a00bb1e9 ZD |
401 | * error code when there is an error. |
402 | */ | |
69217b4c SL |
403 | static int __frontswap_shrink(unsigned long target_pages, |
404 | unsigned long *pages_to_unuse, | |
405 | int *type) | |
406 | { | |
407 | unsigned long total_pages = 0, total_pages_to_unuse; | |
408 | ||
409 | assert_spin_locked(&swap_lock); | |
410 | ||
411 | total_pages = __frontswap_curr_pages(); | |
412 | if (total_pages <= target_pages) { | |
413 | /* Nothing to do */ | |
414 | *pages_to_unuse = 0; | |
a00bb1e9 | 415 | return 1; |
69217b4c SL |
416 | } |
417 | total_pages_to_unuse = total_pages - target_pages; | |
418 | return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type); | |
419 | } | |
420 | ||
29f233cf DM |
421 | /* |
422 | * Frontswap, like a true swap device, may unnecessarily retain pages | |
423 | * under certain circumstances; "shrink" frontswap is essentially a | |
424 | * "partial swapoff" and works by calling try_to_unuse to attempt to | |
425 | * unuse enough frontswap pages to attempt to -- subject to memory | |
426 | * constraints -- reduce the number of pages in frontswap to the | |
427 | * number given in the parameter target_pages. | |
428 | */ | |
429 | void frontswap_shrink(unsigned long target_pages) | |
430 | { | |
f116695a | 431 | unsigned long pages_to_unuse = 0; |
3f649ab7 | 432 | int type, ret; |
29f233cf DM |
433 | |
434 | /* | |
435 | * we don't want to hold swap_lock while doing a very | |
436 | * lengthy try_to_unuse, but swap_list may change | |
18ab4d4c | 437 | * so restart scan from swap_active_head each time |
29f233cf DM |
438 | */ |
439 | spin_lock(&swap_lock); | |
69217b4c | 440 | ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type); |
29f233cf | 441 | spin_unlock(&swap_lock); |
a00bb1e9 | 442 | if (ret == 0) |
69217b4c | 443 | try_to_unuse(type, true, pages_to_unuse); |
29f233cf DM |
444 | return; |
445 | } | |
446 | EXPORT_SYMBOL(frontswap_shrink); | |
447 | ||
448 | /* | |
449 | * Count and return the number of frontswap pages across all | |
450 | * swap devices. This is exported so that backend drivers can | |
451 | * determine current usage without reading debugfs. | |
452 | */ | |
453 | unsigned long frontswap_curr_pages(void) | |
454 | { | |
29f233cf | 455 | unsigned long totalpages = 0; |
29f233cf DM |
456 | |
457 | spin_lock(&swap_lock); | |
96253444 | 458 | totalpages = __frontswap_curr_pages(); |
29f233cf | 459 | spin_unlock(&swap_lock); |
96253444 | 460 | |
29f233cf DM |
461 | return totalpages; |
462 | } | |
463 | EXPORT_SYMBOL(frontswap_curr_pages); | |
464 | ||
465 | static int __init init_frontswap(void) | |
466 | { | |
467 | #ifdef CONFIG_DEBUG_FS | |
468 | struct dentry *root = debugfs_create_dir("frontswap", NULL); | |
469 | if (root == NULL) | |
470 | return -ENXIO; | |
0825a6f9 JP |
471 | debugfs_create_u64("loads", 0444, root, &frontswap_loads); |
472 | debugfs_create_u64("succ_stores", 0444, root, &frontswap_succ_stores); | |
473 | debugfs_create_u64("failed_stores", 0444, root, | |
474 | &frontswap_failed_stores); | |
475 | debugfs_create_u64("invalidates", 0444, root, &frontswap_invalidates); | |
29f233cf DM |
476 | #endif |
477 | return 0; | |
478 | } | |
479 | ||
480 | module_init(init_frontswap); |