blob: 51a662a839559afaa94ac2fed5d6a1ab66470468 [file] [log] [blame]
Thomas Gleixner7a338472019-06-04 10:11:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Dan Magenheimer29f233c2012-04-09 17:09:27 -06002/*
3 * Frontswap frontend
4 *
5 * This code provides the generic "frontend" layer to call a matching
6 * "backend" driver implementation of frontswap. See
Mike Rapoportad56b732018-03-21 21:22:47 +02007 * Documentation/vm/frontswap.rst for more information.
Dan Magenheimer29f233c2012-04-09 17:09:27 -06008 *
9 * Copyright (C) 2009-2012 Oracle Corp. All rights reserved.
10 * Author: Dan Magenheimer
Dan Magenheimer29f233c2012-04-09 17:09:27 -060011 */
12
Dan Magenheimer29f233c2012-04-09 17:09:27 -060013#include <linux/mman.h>
14#include <linux/swap.h>
15#include <linux/swapops.h>
Dan Magenheimer29f233c2012-04-09 17:09:27 -060016#include <linux/security.h>
Dan Magenheimer29f233c2012-04-09 17:09:27 -060017#include <linux/module.h>
Dan Magenheimer29f233c2012-04-09 17:09:27 -060018#include <linux/debugfs.h>
19#include <linux/frontswap.h>
20#include <linux/swapfile.h>
21
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -070022DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
23
Dan Magenheimer29f233c2012-04-09 17:09:27 -060024/*
Dan Streetmand1dc6f12015-06-24 16:58:18 -070025 * 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.
Dan Magenheimer29f233c2012-04-09 17:09:27 -060029 */
Konrad Rzeszutek Wilk1e01c962013-04-30 15:26:51 -070030static struct frontswap_ops *frontswap_ops __read_mostly;
Dan Magenheimer29f233c2012-04-09 17:09:27 -060031
Dan Streetmand1dc6f12015-06-24 16:58:18 -070032#define for_each_frontswap_ops(ops) \
33 for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
34
Dan Magenheimer29f233c2012-04-09 17:09:27 -060035/*
Dan Magenheimere3483a52012-09-20 12:16:52 -070036 * 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 */
40static bool frontswap_tmem_exclusive_gets_enabled __read_mostly;
41
Dan Magenheimer29f233c2012-04-09 17:09:27 -060042#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 */
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -040048static u64 frontswap_loads;
49static u64 frontswap_succ_stores;
50static u64 frontswap_failed_stores;
Dan Magenheimer29f233c2012-04-09 17:09:27 -060051static u64 frontswap_invalidates;
52
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -070053static inline void inc_frontswap_loads(void)
54{
Qian Cai96bdd2b2020-08-14 17:31:17 -070055 data_race(frontswap_loads++);
Dan Magenheimer29f233c2012-04-09 17:09:27 -060056}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -070057static inline void inc_frontswap_succ_stores(void)
58{
Qian Cai96bdd2b2020-08-14 17:31:17 -070059 data_race(frontswap_succ_stores++);
Dan Magenheimer29f233c2012-04-09 17:09:27 -060060}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -070061static inline void inc_frontswap_failed_stores(void)
62{
Qian Cai96bdd2b2020-08-14 17:31:17 -070063 data_race(frontswap_failed_stores++);
Dan Magenheimer29f233c2012-04-09 17:09:27 -060064}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -070065static inline void inc_frontswap_invalidates(void)
66{
Qian Cai96bdd2b2020-08-14 17:31:17 -070067 data_race(frontswap_invalidates++);
Dan Magenheimer29f233c2012-04-09 17:09:27 -060068}
69#else
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -040070static inline void inc_frontswap_loads(void) { }
71static inline void inc_frontswap_succ_stores(void) { }
72static inline void inc_frontswap_failed_stores(void) { }
Dan Magenheimer29f233c2012-04-09 17:09:27 -060073static inline void inc_frontswap_invalidates(void) { }
74#endif
Dan Magenheimer905cd0e2013-04-30 15:26:50 -070075
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 *
Dan Magenheimer905cd0e2013-04-30 15:26:50 -070082 * 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).
Ethon Paul404f3ec2020-06-04 16:49:25 -070084 * Fortunately for us, the swapon_mutex has been taken by the callee so we are
Dan Magenheimer905cd0e2013-04-30 15:26:50 -070085 * 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
Konrad Rzeszutek Wilk1e01c962013-04-30 15:26:51 -070094 * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
Dan Magenheimer905cd0e2013-04-30 15:26:50 -070095 * 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
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700100 * ignoring or failing the requests. However, there is currently no way
101 * to unload a backend once it is registered.
Dan Magenheimer905cd0e2013-04-30 15:26:50 -0700102 */
Dan Magenheimer905cd0e2013-04-30 15:26:50 -0700103
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600104/*
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700105 * Register operations for frontswap
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600106 */
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700107void frontswap_register_ops(struct frontswap_ops *ops)
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600108{
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700109 DECLARE_BITMAP(a, MAX_SWAPFILES);
110 DECLARE_BITMAP(b, MAX_SWAPFILES);
111 struct swap_info_struct *si;
112 unsigned int i;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600113
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700114 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))
Christophe JAILLET3795f462022-01-14 14:05:26 -0800120 __set_bit(si->type, a);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700121 }
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
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -0700137 static_branch_inc(&frontswap_enabled_key);
138
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700139 spin_lock(&swap_lock);
140 plist_for_each_entry(si, &swap_active_head, list) {
141 if (si->frontswap_map)
Christophe JAILLET3795f462022-01-14 14:05:26 -0800142 __set_bit(si->type, b);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700143 }
144 spin_unlock(&swap_lock);
145
146 /*
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.
151 */
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);
Minchan Kim4f898492013-04-30 15:26:54 -0700158 }
Dan Magenheimer905cd0e2013-04-30 15:26:50 -0700159 }
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600160}
161EXPORT_SYMBOL(frontswap_register_ops);
162
163/*
Dan Magenheimere3483a52012-09-20 12:16:52 -0700164 * Enable/disable frontswap exclusive gets (see above).
165 */
166void frontswap_tmem_exclusive_gets(bool enable)
167{
168 frontswap_tmem_exclusive_gets_enabled = enable;
169}
170EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
171
172/*
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600173 * Called when a swap device is swapon'd.
174 */
Minchan Kim4f898492013-04-30 15:26:54 -0700175void __frontswap_init(unsigned type, unsigned long *map)
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600176{
177 struct swap_info_struct *sis = swap_info[type];
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700178 struct frontswap_ops *ops;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600179
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -0700180 VM_BUG_ON(sis == NULL);
Minchan Kim4f898492013-04-30 15:26:54 -0700181
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);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700194
195 for_each_frontswap_ops(ops)
196 ops->init(type);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600197}
198EXPORT_SYMBOL(__frontswap_init);
199
Bob Liuf066ea22013-04-30 15:26:53 -0700200bool __frontswap_test(struct swap_info_struct *sis,
201 pgoff_t offset)
Sasha Levin611edfe2012-06-10 12:51:07 +0200202{
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700203 if (sis->frontswap_map)
204 return test_bit(offset, sis->frontswap_map);
205 return false;
Bob Liuf066ea22013-04-30 15:26:53 -0700206}
207EXPORT_SYMBOL(__frontswap_test);
208
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700209static 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
Bob Liuf066ea22013-04-30 15:26:53 -0700216static inline void __frontswap_clear(struct swap_info_struct *sis,
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700217 pgoff_t offset)
Bob Liuf066ea22013-04-30 15:26:53 -0700218{
219 clear_bit(offset, sis->frontswap_map);
Sasha Levin611edfe2012-06-10 12:51:07 +0200220 atomic_dec(&sis->frontswap_pages);
221}
222
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600223/*
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400224 * "Store" data from a page to frontswap and associate it with the page's
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600225 * swaptype and offset. Page must be locked and in the swap cache.
226 * If frontswap already contains a page with matching swaptype and
Wanpeng Li1d000152012-06-16 20:37:48 +0800227 * offset, the frontswap implementation may either overwrite the data and
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600228 * return success or invalidate the page from frontswap and return failure.
229 */
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400230int __frontswap_store(struct page *page)
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600231{
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700232 int ret = -1;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600233 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);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700237 struct frontswap_ops *ops;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600238
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -0700239 VM_BUG_ON(!frontswap_ops);
240 VM_BUG_ON(!PageLocked(page));
241 VM_BUG_ON(sis == NULL);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700242
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 }
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600261 if (ret == 0) {
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700262 __frontswap_set(sis, offset);
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400263 inc_frontswap_succ_stores();
Sasha Levind9674dd2012-06-10 12:51:04 +0200264 } else {
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400265 inc_frontswap_failed_stores();
Sasha Levin4bb3e312012-06-10 12:51:00 +0200266 }
Christoph Hellwig3d6035f2022-01-21 22:14:38 -0800267
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600268 return ret;
269}
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400270EXPORT_SYMBOL(__frontswap_store);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600271
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 */
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400277int __frontswap_load(struct page *page)
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600278{
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);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700284 struct frontswap_ops *ops;
285
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -0700286 VM_BUG_ON(!frontswap_ops);
287 VM_BUG_ON(!PageLocked(page));
288 VM_BUG_ON(sis == NULL);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600289
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700290 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 }
Dan Magenheimere3483a52012-09-20 12:16:52 -0700299 if (ret == 0) {
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400300 inc_frontswap_loads();
Dan Magenheimere3483a52012-09-20 12:16:52 -0700301 if (frontswap_tmem_exclusive_gets_enabled) {
302 SetPageDirty(page);
Bob Liuf066ea22013-04-30 15:26:53 -0700303 __frontswap_clear(sis, offset);
Dan Magenheimere3483a52012-09-20 12:16:52 -0700304 }
305 }
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600306 return ret;
307}
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400308EXPORT_SYMBOL(__frontswap_load);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600309
310/*
311 * Invalidate any data from frontswap associated with the specified swaptype
312 * and offset so that a subsequent "get" will fail.
313 */
314void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
315{
316 struct swap_info_struct *sis = swap_info[type];
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700317 struct frontswap_ops *ops;
318
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -0700319 VM_BUG_ON(!frontswap_ops);
320 VM_BUG_ON(sis == NULL);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600321
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700322 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();
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600329}
330EXPORT_SYMBOL(__frontswap_invalidate_page);
331
332/*
333 * Invalidate all data from frontswap associated with all offsets for the
334 * specified swaptype.
335 */
336void __frontswap_invalidate_area(unsigned type)
337{
338 struct swap_info_struct *sis = swap_info[type];
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700339 struct frontswap_ops *ops;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600340
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -0700341 VM_BUG_ON(!frontswap_ops);
342 VM_BUG_ON(sis == NULL);
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700343
Dan Streetmand1dc6f12015-06-24 16:58:18 -0700344 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);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600351}
352EXPORT_SYMBOL(__frontswap_invalidate_area);
353
Sasha Levin96253442012-06-10 12:51:01 +0200354static unsigned long __frontswap_curr_pages(void)
355{
Sasha Levin96253442012-06-10 12:51:01 +0200356 unsigned long totalpages = 0;
357 struct swap_info_struct *si = NULL;
358
359 assert_spin_locked(&swap_lock);
Dan Streetman18ab4d42014-06-04 16:09:59 -0700360 plist_for_each_entry(si, &swap_active_head, list)
Sasha Levin96253442012-06-10 12:51:01 +0200361 totalpages += atomic_read(&si->frontswap_pages);
Sasha Levin96253442012-06-10 12:51:01 +0200362 return totalpages;
363}
364
Sasha Levinf1166952012-06-10 12:51:02 +0200365static 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;
Sasha Levinf1166952012-06-10 12:51:02 +0200373
374 assert_spin_locked(&swap_lock);
Dan Streetman18ab4d42014-06-04 16:09:59 -0700375 plist_for_each_entry(si, &swap_active_head, list) {
Sasha Levinf1166952012-06-10 12:51:02 +0200376 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;
Dan Streetmanadfab832014-06-04 16:09:53 -0700390 *swapid = si->type;
Sasha Levinf1166952012-06-10 12:51:02 +0200391 ret = 0;
392 break;
393 }
394
395 return ret;
396}
397
Zhenzhong Duana00bb1e2012-09-21 16:40:30 +0800398/*
Ethon Paul404f3ec2020-06-04 16:49:25 -0700399 * 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,
Zhenzhong Duana00bb1e2012-09-21 16:40:30 +0800401 * error code when there is an error.
402 */
Sasha Levin69217b42012-06-10 12:51:03 +0200403static 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;
Zhenzhong Duana00bb1e2012-09-21 16:40:30 +0800415 return 1;
Sasha Levin69217b42012-06-10 12:51:03 +0200416 }
417 total_pages_to_unuse = total_pages - target_pages;
418 return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
419}
420
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600421/*
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 */
429void frontswap_shrink(unsigned long target_pages)
430{
Sasha Levinf1166952012-06-10 12:51:02 +0200431 unsigned long pages_to_unuse = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -0700432 int type, ret;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600433
434 /*
435 * we don't want to hold swap_lock while doing a very
436 * lengthy try_to_unuse, but swap_list may change
Dan Streetman18ab4d42014-06-04 16:09:59 -0700437 * so restart scan from swap_active_head each time
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600438 */
439 spin_lock(&swap_lock);
Sasha Levin69217b42012-06-10 12:51:03 +0200440 ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600441 spin_unlock(&swap_lock);
Zhenzhong Duana00bb1e2012-09-21 16:40:30 +0800442 if (ret == 0)
Sasha Levin69217b42012-06-10 12:51:03 +0200443 try_to_unuse(type, true, pages_to_unuse);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600444 return;
445}
446EXPORT_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 */
453unsigned long frontswap_curr_pages(void)
454{
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600455 unsigned long totalpages = 0;
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600456
457 spin_lock(&swap_lock);
Sasha Levin96253442012-06-10 12:51:01 +0200458 totalpages = __frontswap_curr_pages();
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600459 spin_unlock(&swap_lock);
Sasha Levin96253442012-06-10 12:51:01 +0200460
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600461 return totalpages;
462}
463EXPORT_SYMBOL(frontswap_curr_pages);
464
465static 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;
Joe Perches0825a6f2018-06-14 15:27:58 -0700471 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);
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600476#endif
Dan Magenheimer29f233c2012-04-09 17:09:27 -0600477 return 0;
478}
479
480module_init(init_frontswap);