frontswap: remove frontswap_writethrough
[linux-2.6-microblaze.git] / mm / frontswap.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Frontswap frontend
4  *
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.
8  *
9  * Copyright (C) 2009-2012 Oracle Corp.  All rights reserved.
10  * Author: Dan Magenheimer
11  */
12
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>
21
22 DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
23
24 /*
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.
29  */
30 static struct frontswap_ops *frontswap_ops __read_mostly;
31
32 #define for_each_frontswap_ops(ops)             \
33         for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
34
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
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  */
48 static u64 frontswap_loads;
49 static u64 frontswap_succ_stores;
50 static u64 frontswap_failed_stores;
51 static u64 frontswap_invalidates;
52
53 static inline void inc_frontswap_loads(void)
54 {
55         data_race(frontswap_loads++);
56 }
57 static inline void inc_frontswap_succ_stores(void)
58 {
59         data_race(frontswap_succ_stores++);
60 }
61 static inline void inc_frontswap_failed_stores(void)
62 {
63         data_race(frontswap_failed_stores++);
64 }
65 static inline void inc_frontswap_invalidates(void)
66 {
67         data_race(frontswap_invalidates++);
68 }
69 #else
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) { }
74 #endif
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  *
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.
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
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
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
100  * ignoring or failing the requests.  However, there is currently no way
101  * to unload a backend once it is registered.
102  */
103
104 /*
105  * Register operations for frontswap
106  */
107 void frontswap_register_ops(struct frontswap_ops *ops)
108 {
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))
120                         __set_bit(si->type, a);
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
137         static_branch_inc(&frontswap_enabled_key);
138
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);
143         }
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);
158                 }
159         }
160 }
161 EXPORT_SYMBOL(frontswap_register_ops);
162
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
172 /*
173  * Called when a swap device is swapon'd.
174  */
175 void __frontswap_init(unsigned type, unsigned long *map)
176 {
177         struct swap_info_struct *sis = swap_info[type];
178         struct frontswap_ops *ops;
179
180         VM_BUG_ON(sis == NULL);
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);
194
195         for_each_frontswap_ops(ops)
196                 ops->init(type);
197 }
198 EXPORT_SYMBOL(__frontswap_init);
199
200 bool __frontswap_test(struct swap_info_struct *sis,
201                                 pgoff_t offset)
202 {
203         if (sis->frontswap_map)
204                 return test_bit(offset, sis->frontswap_map);
205         return false;
206 }
207 EXPORT_SYMBOL(__frontswap_test);
208
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
216 static inline void __frontswap_clear(struct swap_info_struct *sis,
217                                      pgoff_t offset)
218 {
219         clear_bit(offset, sis->frontswap_map);
220         atomic_dec(&sis->frontswap_pages);
221 }
222
223 /*
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.
229  */
230 int __frontswap_store(struct page *page)
231 {
232         int ret = -1;
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;
238
239         VM_BUG_ON(!frontswap_ops);
240         VM_BUG_ON(!PageLocked(page));
241         VM_BUG_ON(sis == NULL);
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         }
261         if (ret == 0) {
262                 __frontswap_set(sis, offset);
263                 inc_frontswap_succ_stores();
264         } else {
265                 inc_frontswap_failed_stores();
266         }
267
268         return ret;
269 }
270 EXPORT_SYMBOL(__frontswap_store);
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  */
277 int __frontswap_load(struct page *page)
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);
284         struct frontswap_ops *ops;
285
286         VM_BUG_ON(!frontswap_ops);
287         VM_BUG_ON(!PageLocked(page));
288         VM_BUG_ON(sis == NULL);
289
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         }
299         if (ret == 0) {
300                 inc_frontswap_loads();
301                 if (frontswap_tmem_exclusive_gets_enabled) {
302                         SetPageDirty(page);
303                         __frontswap_clear(sis, offset);
304                 }
305         }
306         return ret;
307 }
308 EXPORT_SYMBOL(__frontswap_load);
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];
317         struct frontswap_ops *ops;
318
319         VM_BUG_ON(!frontswap_ops);
320         VM_BUG_ON(sis == NULL);
321
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();
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];
339         struct frontswap_ops *ops;
340
341         VM_BUG_ON(!frontswap_ops);
342         VM_BUG_ON(sis == NULL);
343
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);
351 }
352 EXPORT_SYMBOL(__frontswap_invalidate_area);
353
354 static unsigned long __frontswap_curr_pages(void)
355 {
356         unsigned long totalpages = 0;
357         struct swap_info_struct *si = NULL;
358
359         assert_spin_locked(&swap_lock);
360         plist_for_each_entry(si, &swap_active_head, list)
361                 totalpages += atomic_read(&si->frontswap_pages);
362         return totalpages;
363 }
364
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;
373
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;
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;
390                 *swapid = si->type;
391                 ret = 0;
392                 break;
393         }
394
395         return ret;
396 }
397
398 /*
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.
402  */
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;
415                 return 1;
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
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 {
431         unsigned long pages_to_unuse = 0;
432         int type, ret;
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
437          * so restart scan from swap_active_head each time
438          */
439         spin_lock(&swap_lock);
440         ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
441         spin_unlock(&swap_lock);
442         if (ret == 0)
443                 try_to_unuse(type, true, pages_to_unuse);
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 {
455         unsigned long totalpages = 0;
456
457         spin_lock(&swap_lock);
458         totalpages = __frontswap_curr_pages();
459         spin_unlock(&swap_lock);
460
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;
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);
476 #endif
477         return 0;
478 }
479
480 module_init(init_frontswap);