1 // SPDX-License-Identifier: GPL-2.0-only
5 * This code provides the generic "frontend" layer to call a matching
6 * "backend" driver implementation of frontswap. See
7 * Documentation/vm/frontswap.rst for more information.
9 * Copyright (C) 2009-2012 Oracle Corp. All rights reserved.
10 * Author: Dan Magenheimer
13 #include <linux/mman.h>
14 #include <linux/swap.h>
15 #include <linux/swapops.h>
16 #include <linux/security.h>
17 #include <linux/module.h>
18 #include <linux/debugfs.h>
19 #include <linux/frontswap.h>
20 #include <linux/swapfile.h>
22 DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
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.
30 static struct frontswap_ops *frontswap_ops __read_mostly;
32 #define for_each_frontswap_ops(ops) \
33 for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
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.
40 static bool frontswap_tmem_exclusive_gets_enabled __read_mostly;
42 #ifdef CONFIG_DEBUG_FS
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.
48 static u64 frontswap_loads;
49 static u64 frontswap_succ_stores;
50 static u64 frontswap_failed_stores;
51 static u64 frontswap_invalidates;
53 static inline void inc_frontswap_loads(void)
55 data_race(frontswap_loads++);
57 static inline void inc_frontswap_succ_stores(void)
59 data_race(frontswap_succ_stores++);
61 static inline void inc_frontswap_failed_stores(void)
63 data_race(frontswap_failed_stores++);
65 static inline void inc_frontswap_invalidates(void)
67 data_race(frontswap_invalidates++);
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) { }
73 static inline void inc_frontswap_invalidates(void) { }
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
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).
84 * Fortunately for us, the swapon_mutex has been taken by the callee so we are
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.
89 * While no backend is registered all calls to frontswap_[store|load|
90 * invalidate_area|invalidate_page] are ignored or fail.
92 * The time between the backend being registered and the swap file system
93 * calling the backend (via the frontswap_* functions) is indeterminate as
94 * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
95 * That is OK as we are comfortable missing some of these calls to the newly
98 * Obviously the opposite (unloading the backend) must be done after all
99 * the frontswap_[store|load|invalidate_area|invalidate_page] start
100 * ignoring or failing the requests. However, there is currently no way
101 * to unload a backend once it is registered.
105 * Register operations for frontswap
107 void frontswap_register_ops(struct frontswap_ops *ops)
109 DECLARE_BITMAP(a, MAX_SWAPFILES);
110 DECLARE_BITMAP(b, MAX_SWAPFILES);
111 struct swap_info_struct *si;
114 bitmap_zero(a, MAX_SWAPFILES);
115 bitmap_zero(b, MAX_SWAPFILES);
117 spin_lock(&swap_lock);
118 plist_for_each_entry(si, &swap_active_head, list) {
119 if (!WARN_ON(!si->frontswap_map))
120 __set_bit(si->type, a);
122 spin_unlock(&swap_lock);
124 /* the new ops needs to know the currently active swap devices */
125 for_each_set_bit(i, a, MAX_SWAPFILES)
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.
134 ops->next = frontswap_ops;
135 } while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
137 static_branch_inc(&frontswap_enabled_key);
139 spin_lock(&swap_lock);
140 plist_for_each_entry(si, &swap_active_head, list) {
141 if (si->frontswap_map)
142 __set_bit(si->type, b);
144 spin_unlock(&swap_lock);
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
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))
156 else if (test_bit(i, a) && !test_bit(i, b))
157 ops->invalidate_area(i);
161 EXPORT_SYMBOL(frontswap_register_ops);
164 * Enable/disable frontswap exclusive gets (see above).
166 void frontswap_tmem_exclusive_gets(bool enable)
168 frontswap_tmem_exclusive_gets_enabled = enable;
170 EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
173 * Called when a swap device is swapon'd.
175 void __frontswap_init(unsigned type, unsigned long *map)
177 struct swap_info_struct *sis = swap_info[type];
178 struct frontswap_ops *ops;
180 VM_BUG_ON(sis == NULL);
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.
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.
193 frontswap_map_set(sis, map);
195 for_each_frontswap_ops(ops)
198 EXPORT_SYMBOL(__frontswap_init);
200 bool __frontswap_test(struct swap_info_struct *sis,
203 if (sis->frontswap_map)
204 return test_bit(offset, sis->frontswap_map);
207 EXPORT_SYMBOL(__frontswap_test);
209 static inline void __frontswap_set(struct swap_info_struct *sis,
212 set_bit(offset, sis->frontswap_map);
213 atomic_inc(&sis->frontswap_pages);
216 static inline void __frontswap_clear(struct swap_info_struct *sis,
219 clear_bit(offset, sis->frontswap_map);
220 atomic_dec(&sis->frontswap_pages);
224 * "Store" data from a page to frontswap and associate it with the page's
225 * swaptype and offset. Page must be locked and in the swap cache.
226 * If frontswap already contains a page with matching swaptype and
227 * offset, the frontswap implementation may either overwrite the data and
228 * return success or invalidate the page from frontswap and return failure.
230 int __frontswap_store(struct page *page)
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);
237 struct frontswap_ops *ops;
239 VM_BUG_ON(!frontswap_ops);
240 VM_BUG_ON(!PageLocked(page));
241 VM_BUG_ON(sis == NULL);
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.
249 if (__frontswap_test(sis, offset)) {
250 __frontswap_clear(sis, offset);
251 for_each_frontswap_ops(ops)
252 ops->invalidate_page(type, offset);
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 */
262 __frontswap_set(sis, offset);
263 inc_frontswap_succ_stores();
265 inc_frontswap_failed_stores();
270 EXPORT_SYMBOL(__frontswap_store);
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.
277 int __frontswap_load(struct page *page)
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);
284 struct frontswap_ops *ops;
286 VM_BUG_ON(!frontswap_ops);
287 VM_BUG_ON(!PageLocked(page));
288 VM_BUG_ON(sis == NULL);
290 if (!__frontswap_test(sis, offset))
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 */
300 inc_frontswap_loads();
301 if (frontswap_tmem_exclusive_gets_enabled) {
303 __frontswap_clear(sis, offset);
308 EXPORT_SYMBOL(__frontswap_load);
311 * Invalidate any data from frontswap associated with the specified swaptype
312 * and offset so that a subsequent "get" will fail.
314 void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
316 struct swap_info_struct *sis = swap_info[type];
317 struct frontswap_ops *ops;
319 VM_BUG_ON(!frontswap_ops);
320 VM_BUG_ON(sis == NULL);
322 if (!__frontswap_test(sis, offset))
325 for_each_frontswap_ops(ops)
326 ops->invalidate_page(type, offset);
327 __frontswap_clear(sis, offset);
328 inc_frontswap_invalidates();
330 EXPORT_SYMBOL(__frontswap_invalidate_page);
333 * Invalidate all data from frontswap associated with all offsets for the
334 * specified swaptype.
336 void __frontswap_invalidate_area(unsigned type)
338 struct swap_info_struct *sis = swap_info[type];
339 struct frontswap_ops *ops;
341 VM_BUG_ON(!frontswap_ops);
342 VM_BUG_ON(sis == NULL);
344 if (sis->frontswap_map == NULL)
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);
352 EXPORT_SYMBOL(__frontswap_invalidate_area);
354 static unsigned long __frontswap_curr_pages(void)
356 unsigned long totalpages = 0;
357 struct swap_info_struct *si = NULL;
359 assert_spin_locked(&swap_lock);
360 plist_for_each_entry(si, &swap_active_head, list)
361 totalpages += atomic_read(&si->frontswap_pages);
365 static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
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;
374 assert_spin_locked(&swap_lock);
375 plist_for_each_entry(si, &swap_active_head, list) {
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;
380 pages = si_frontswap_pages;
381 pages_to_unuse = 0; /* unuse all */
383 /* ensure there is enough RAM to fetch pages from frontswap */
384 if (security_vm_enough_memory_mm(current->mm, pages)) {
388 vm_unacct_memory(pages);
389 *unused = pages_to_unuse;
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,
401 * error code when there is an error.
403 static int __frontswap_shrink(unsigned long target_pages,
404 unsigned long *pages_to_unuse,
407 unsigned long total_pages = 0, total_pages_to_unuse;
409 assert_spin_locked(&swap_lock);
411 total_pages = __frontswap_curr_pages();
412 if (total_pages <= target_pages) {
417 total_pages_to_unuse = total_pages - target_pages;
418 return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
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.
429 void frontswap_shrink(unsigned long target_pages)
431 unsigned long pages_to_unuse = 0;
435 * we don't want to hold swap_lock while doing a very
436 * lengthy try_to_unuse, but swap_list may change
437 * so restart scan from swap_active_head each time
439 spin_lock(&swap_lock);
440 ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
441 spin_unlock(&swap_lock);
443 try_to_unuse(type, true, pages_to_unuse);
446 EXPORT_SYMBOL(frontswap_shrink);
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.
453 unsigned long frontswap_curr_pages(void)
455 unsigned long totalpages = 0;
457 spin_lock(&swap_lock);
458 totalpages = __frontswap_curr_pages();
459 spin_unlock(&swap_lock);
463 EXPORT_SYMBOL(frontswap_curr_pages);
465 static int __init init_frontswap(void)
467 #ifdef CONFIG_DEBUG_FS
468 struct dentry *root = debugfs_create_dir("frontswap", NULL);
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);
480 module_init(init_frontswap);