Back to home page

LXR

 
 

    


0001 /*
0002  * Frontswap frontend
0003  *
0004  * This code provides the generic "frontend" layer to call a matching
0005  * "backend" driver implementation of frontswap.  See
0006  * Documentation/vm/frontswap.txt for more information.
0007  *
0008  * Copyright (C) 2009-2012 Oracle Corp.  All rights reserved.
0009  * Author: Dan Magenheimer
0010  *
0011  * This work is licensed under the terms of the GNU GPL, version 2.
0012  */
0013 
0014 #include <linux/mman.h>
0015 #include <linux/swap.h>
0016 #include <linux/swapops.h>
0017 #include <linux/security.h>
0018 #include <linux/module.h>
0019 #include <linux/debugfs.h>
0020 #include <linux/frontswap.h>
0021 #include <linux/swapfile.h>
0022 
0023 DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
0024 
0025 /*
0026  * frontswap_ops are added by frontswap_register_ops, and provide the
0027  * frontswap "backend" implementation functions.  Multiple implementations
0028  * may be registered, but implementations can never deregister.  This
0029  * is a simple singly-linked list of all registered implementations.
0030  */
0031 static struct frontswap_ops *frontswap_ops __read_mostly;
0032 
0033 #define for_each_frontswap_ops(ops)     \
0034     for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
0035 
0036 /*
0037  * If enabled, frontswap_store will return failure even on success.  As
0038  * a result, the swap subsystem will always write the page to swap, in
0039  * effect converting frontswap into a writethrough cache.  In this mode,
0040  * there is no direct reduction in swap writes, but a frontswap backend
0041  * can unilaterally "reclaim" any pages in use with no data loss, thus
0042  * providing increases control over maximum memory usage due to frontswap.
0043  */
0044 static bool frontswap_writethrough_enabled __read_mostly;
0045 
0046 /*
0047  * If enabled, the underlying tmem implementation is capable of doing
0048  * exclusive gets, so frontswap_load, on a successful tmem_get must
0049  * mark the page as no longer in frontswap AND mark it dirty.
0050  */
0051 static bool frontswap_tmem_exclusive_gets_enabled __read_mostly;
0052 
0053 #ifdef CONFIG_DEBUG_FS
0054 /*
0055  * Counters available via /sys/kernel/debug/frontswap (if debugfs is
0056  * properly configured).  These are for information only so are not protected
0057  * against increment races.
0058  */
0059 static u64 frontswap_loads;
0060 static u64 frontswap_succ_stores;
0061 static u64 frontswap_failed_stores;
0062 static u64 frontswap_invalidates;
0063 
0064 static inline void inc_frontswap_loads(void) {
0065     frontswap_loads++;
0066 }
0067 static inline void inc_frontswap_succ_stores(void) {
0068     frontswap_succ_stores++;
0069 }
0070 static inline void inc_frontswap_failed_stores(void) {
0071     frontswap_failed_stores++;
0072 }
0073 static inline void inc_frontswap_invalidates(void) {
0074     frontswap_invalidates++;
0075 }
0076 #else
0077 static inline void inc_frontswap_loads(void) { }
0078 static inline void inc_frontswap_succ_stores(void) { }
0079 static inline void inc_frontswap_failed_stores(void) { }
0080 static inline void inc_frontswap_invalidates(void) { }
0081 #endif
0082 
0083 /*
0084  * Due to the asynchronous nature of the backends loading potentially
0085  * _after_ the swap system has been activated, we have chokepoints
0086  * on all frontswap functions to not call the backend until the backend
0087  * has registered.
0088  *
0089  * This would not guards us against the user deciding to call swapoff right as
0090  * we are calling the backend to initialize (so swapon is in action).
0091  * Fortunatly for us, the swapon_mutex has been taked by the callee so we are
0092  * OK. The other scenario where calls to frontswap_store (called via
0093  * swap_writepage) is racing with frontswap_invalidate_area (called via
0094  * swapoff) is again guarded by the swap subsystem.
0095  *
0096  * While no backend is registered all calls to frontswap_[store|load|
0097  * invalidate_area|invalidate_page] are ignored or fail.
0098  *
0099  * The time between the backend being registered and the swap file system
0100  * calling the backend (via the frontswap_* functions) is indeterminate as
0101  * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
0102  * That is OK as we are comfortable missing some of these calls to the newly
0103  * registered backend.
0104  *
0105  * Obviously the opposite (unloading the backend) must be done after all
0106  * the frontswap_[store|load|invalidate_area|invalidate_page] start
0107  * ignoring or failing the requests.  However, there is currently no way
0108  * to unload a backend once it is registered.
0109  */
0110 
0111 /*
0112  * Register operations for frontswap
0113  */
0114 void frontswap_register_ops(struct frontswap_ops *ops)
0115 {
0116     DECLARE_BITMAP(a, MAX_SWAPFILES);
0117     DECLARE_BITMAP(b, MAX_SWAPFILES);
0118     struct swap_info_struct *si;
0119     unsigned int i;
0120 
0121     bitmap_zero(a, MAX_SWAPFILES);
0122     bitmap_zero(b, MAX_SWAPFILES);
0123 
0124     spin_lock(&swap_lock);
0125     plist_for_each_entry(si, &swap_active_head, list) {
0126         if (!WARN_ON(!si->frontswap_map))
0127             set_bit(si->type, a);
0128     }
0129     spin_unlock(&swap_lock);
0130 
0131     /* the new ops needs to know the currently active swap devices */
0132     for_each_set_bit(i, a, MAX_SWAPFILES)
0133         ops->init(i);
0134 
0135     /*
0136      * Setting frontswap_ops must happen after the ops->init() calls
0137      * above; cmpxchg implies smp_mb() which will ensure the init is
0138      * complete at this point.
0139      */
0140     do {
0141         ops->next = frontswap_ops;
0142     } while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
0143 
0144     static_branch_inc(&frontswap_enabled_key);
0145 
0146     spin_lock(&swap_lock);
0147     plist_for_each_entry(si, &swap_active_head, list) {
0148         if (si->frontswap_map)
0149             set_bit(si->type, b);
0150     }
0151     spin_unlock(&swap_lock);
0152 
0153     /*
0154      * On the very unlikely chance that a swap device was added or
0155      * removed between setting the "a" list bits and the ops init
0156      * calls, we re-check and do init or invalidate for any changed
0157      * bits.
0158      */
0159     if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
0160         for (i = 0; i < MAX_SWAPFILES; i++) {
0161             if (!test_bit(i, a) && test_bit(i, b))
0162                 ops->init(i);
0163             else if (test_bit(i, a) && !test_bit(i, b))
0164                 ops->invalidate_area(i);
0165         }
0166     }
0167 }
0168 EXPORT_SYMBOL(frontswap_register_ops);
0169 
0170 /*
0171  * Enable/disable frontswap writethrough (see above).
0172  */
0173 void frontswap_writethrough(bool enable)
0174 {
0175     frontswap_writethrough_enabled = enable;
0176 }
0177 EXPORT_SYMBOL(frontswap_writethrough);
0178 
0179 /*
0180  * Enable/disable frontswap exclusive gets (see above).
0181  */
0182 void frontswap_tmem_exclusive_gets(bool enable)
0183 {
0184     frontswap_tmem_exclusive_gets_enabled = enable;
0185 }
0186 EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
0187 
0188 /*
0189  * Called when a swap device is swapon'd.
0190  */
0191 void __frontswap_init(unsigned type, unsigned long *map)
0192 {
0193     struct swap_info_struct *sis = swap_info[type];
0194     struct frontswap_ops *ops;
0195 
0196     VM_BUG_ON(sis == NULL);
0197 
0198     /*
0199      * p->frontswap is a bitmap that we MUST have to figure out which page
0200      * has gone in frontswap. Without it there is no point of continuing.
0201      */
0202     if (WARN_ON(!map))
0203         return;
0204     /*
0205      * Irregardless of whether the frontswap backend has been loaded
0206      * before this function or it will be later, we _MUST_ have the
0207      * p->frontswap set to something valid to work properly.
0208      */
0209     frontswap_map_set(sis, map);
0210 
0211     for_each_frontswap_ops(ops)
0212         ops->init(type);
0213 }
0214 EXPORT_SYMBOL(__frontswap_init);
0215 
0216 bool __frontswap_test(struct swap_info_struct *sis,
0217                 pgoff_t offset)
0218 {
0219     if (sis->frontswap_map)
0220         return test_bit(offset, sis->frontswap_map);
0221     return false;
0222 }
0223 EXPORT_SYMBOL(__frontswap_test);
0224 
0225 static inline void __frontswap_set(struct swap_info_struct *sis,
0226                    pgoff_t offset)
0227 {
0228     set_bit(offset, sis->frontswap_map);
0229     atomic_inc(&sis->frontswap_pages);
0230 }
0231 
0232 static inline void __frontswap_clear(struct swap_info_struct *sis,
0233                      pgoff_t offset)
0234 {
0235     clear_bit(offset, sis->frontswap_map);
0236     atomic_dec(&sis->frontswap_pages);
0237 }
0238 
0239 /*
0240  * "Store" data from a page to frontswap and associate it with the page's
0241  * swaptype and offset.  Page must be locked and in the swap cache.
0242  * If frontswap already contains a page with matching swaptype and
0243  * offset, the frontswap implementation may either overwrite the data and
0244  * return success or invalidate the page from frontswap and return failure.
0245  */
0246 int __frontswap_store(struct page *page)
0247 {
0248     int ret = -1;
0249     swp_entry_t entry = { .val = page_private(page), };
0250     int type = swp_type(entry);
0251     struct swap_info_struct *sis = swap_info[type];
0252     pgoff_t offset = swp_offset(entry);
0253     struct frontswap_ops *ops;
0254 
0255     VM_BUG_ON(!frontswap_ops);
0256     VM_BUG_ON(!PageLocked(page));
0257     VM_BUG_ON(sis == NULL);
0258 
0259     /*
0260      * If a dup, we must remove the old page first; we can't leave the
0261      * old page no matter if the store of the new page succeeds or fails,
0262      * and we can't rely on the new page replacing the old page as we may
0263      * not store to the same implementation that contains the old page.
0264      */
0265     if (__frontswap_test(sis, offset)) {
0266         __frontswap_clear(sis, offset);
0267         for_each_frontswap_ops(ops)
0268             ops->invalidate_page(type, offset);
0269     }
0270 
0271     /* Try to store in each implementation, until one succeeds. */
0272     for_each_frontswap_ops(ops) {
0273         ret = ops->store(type, offset, page);
0274         if (!ret) /* successful store */
0275             break;
0276     }
0277     if (ret == 0) {
0278         __frontswap_set(sis, offset);
0279         inc_frontswap_succ_stores();
0280     } else {
0281         inc_frontswap_failed_stores();
0282     }
0283     if (frontswap_writethrough_enabled)
0284         /* report failure so swap also writes to swap device */
0285         ret = -1;
0286     return ret;
0287 }
0288 EXPORT_SYMBOL(__frontswap_store);
0289 
0290 /*
0291  * "Get" data from frontswap associated with swaptype and offset that were
0292  * specified when the data was put to frontswap and use it to fill the
0293  * specified page with data. Page must be locked and in the swap cache.
0294  */
0295 int __frontswap_load(struct page *page)
0296 {
0297     int ret = -1;
0298     swp_entry_t entry = { .val = page_private(page), };
0299     int type = swp_type(entry);
0300     struct swap_info_struct *sis = swap_info[type];
0301     pgoff_t offset = swp_offset(entry);
0302     struct frontswap_ops *ops;
0303 
0304     VM_BUG_ON(!frontswap_ops);
0305     VM_BUG_ON(!PageLocked(page));
0306     VM_BUG_ON(sis == NULL);
0307 
0308     if (!__frontswap_test(sis, offset))
0309         return -1;
0310 
0311     /* Try loading from each implementation, until one succeeds. */
0312     for_each_frontswap_ops(ops) {
0313         ret = ops->load(type, offset, page);
0314         if (!ret) /* successful load */
0315             break;
0316     }
0317     if (ret == 0) {
0318         inc_frontswap_loads();
0319         if (frontswap_tmem_exclusive_gets_enabled) {
0320             SetPageDirty(page);
0321             __frontswap_clear(sis, offset);
0322         }
0323     }
0324     return ret;
0325 }
0326 EXPORT_SYMBOL(__frontswap_load);
0327 
0328 /*
0329  * Invalidate any data from frontswap associated with the specified swaptype
0330  * and offset so that a subsequent "get" will fail.
0331  */
0332 void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
0333 {
0334     struct swap_info_struct *sis = swap_info[type];
0335     struct frontswap_ops *ops;
0336 
0337     VM_BUG_ON(!frontswap_ops);
0338     VM_BUG_ON(sis == NULL);
0339 
0340     if (!__frontswap_test(sis, offset))
0341         return;
0342 
0343     for_each_frontswap_ops(ops)
0344         ops->invalidate_page(type, offset);
0345     __frontswap_clear(sis, offset);
0346     inc_frontswap_invalidates();
0347 }
0348 EXPORT_SYMBOL(__frontswap_invalidate_page);
0349 
0350 /*
0351  * Invalidate all data from frontswap associated with all offsets for the
0352  * specified swaptype.
0353  */
0354 void __frontswap_invalidate_area(unsigned type)
0355 {
0356     struct swap_info_struct *sis = swap_info[type];
0357     struct frontswap_ops *ops;
0358 
0359     VM_BUG_ON(!frontswap_ops);
0360     VM_BUG_ON(sis == NULL);
0361 
0362     if (sis->frontswap_map == NULL)
0363         return;
0364 
0365     for_each_frontswap_ops(ops)
0366         ops->invalidate_area(type);
0367     atomic_set(&sis->frontswap_pages, 0);
0368     bitmap_zero(sis->frontswap_map, sis->max);
0369 }
0370 EXPORT_SYMBOL(__frontswap_invalidate_area);
0371 
0372 static unsigned long __frontswap_curr_pages(void)
0373 {
0374     unsigned long totalpages = 0;
0375     struct swap_info_struct *si = NULL;
0376 
0377     assert_spin_locked(&swap_lock);
0378     plist_for_each_entry(si, &swap_active_head, list)
0379         totalpages += atomic_read(&si->frontswap_pages);
0380     return totalpages;
0381 }
0382 
0383 static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
0384                     int *swapid)
0385 {
0386     int ret = -EINVAL;
0387     struct swap_info_struct *si = NULL;
0388     int si_frontswap_pages;
0389     unsigned long total_pages_to_unuse = total;
0390     unsigned long pages = 0, pages_to_unuse = 0;
0391 
0392     assert_spin_locked(&swap_lock);
0393     plist_for_each_entry(si, &swap_active_head, list) {
0394         si_frontswap_pages = atomic_read(&si->frontswap_pages);
0395         if (total_pages_to_unuse < si_frontswap_pages) {
0396             pages = pages_to_unuse = total_pages_to_unuse;
0397         } else {
0398             pages = si_frontswap_pages;
0399             pages_to_unuse = 0; /* unuse all */
0400         }
0401         /* ensure there is enough RAM to fetch pages from frontswap */
0402         if (security_vm_enough_memory_mm(current->mm, pages)) {
0403             ret = -ENOMEM;
0404             continue;
0405         }
0406         vm_unacct_memory(pages);
0407         *unused = pages_to_unuse;
0408         *swapid = si->type;
0409         ret = 0;
0410         break;
0411     }
0412 
0413     return ret;
0414 }
0415 
0416 /*
0417  * Used to check if it's necessory and feasible to unuse pages.
0418  * Return 1 when nothing to do, 0 when need to shink pages,
0419  * error code when there is an error.
0420  */
0421 static int __frontswap_shrink(unsigned long target_pages,
0422                 unsigned long *pages_to_unuse,
0423                 int *type)
0424 {
0425     unsigned long total_pages = 0, total_pages_to_unuse;
0426 
0427     assert_spin_locked(&swap_lock);
0428 
0429     total_pages = __frontswap_curr_pages();
0430     if (total_pages <= target_pages) {
0431         /* Nothing to do */
0432         *pages_to_unuse = 0;
0433         return 1;
0434     }
0435     total_pages_to_unuse = total_pages - target_pages;
0436     return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
0437 }
0438 
0439 /*
0440  * Frontswap, like a true swap device, may unnecessarily retain pages
0441  * under certain circumstances; "shrink" frontswap is essentially a
0442  * "partial swapoff" and works by calling try_to_unuse to attempt to
0443  * unuse enough frontswap pages to attempt to -- subject to memory
0444  * constraints -- reduce the number of pages in frontswap to the
0445  * number given in the parameter target_pages.
0446  */
0447 void frontswap_shrink(unsigned long target_pages)
0448 {
0449     unsigned long pages_to_unuse = 0;
0450     int uninitialized_var(type), ret;
0451 
0452     /*
0453      * we don't want to hold swap_lock while doing a very
0454      * lengthy try_to_unuse, but swap_list may change
0455      * so restart scan from swap_active_head each time
0456      */
0457     spin_lock(&swap_lock);
0458     ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
0459     spin_unlock(&swap_lock);
0460     if (ret == 0)
0461         try_to_unuse(type, true, pages_to_unuse);
0462     return;
0463 }
0464 EXPORT_SYMBOL(frontswap_shrink);
0465 
0466 /*
0467  * Count and return the number of frontswap pages across all
0468  * swap devices.  This is exported so that backend drivers can
0469  * determine current usage without reading debugfs.
0470  */
0471 unsigned long frontswap_curr_pages(void)
0472 {
0473     unsigned long totalpages = 0;
0474 
0475     spin_lock(&swap_lock);
0476     totalpages = __frontswap_curr_pages();
0477     spin_unlock(&swap_lock);
0478 
0479     return totalpages;
0480 }
0481 EXPORT_SYMBOL(frontswap_curr_pages);
0482 
0483 static int __init init_frontswap(void)
0484 {
0485 #ifdef CONFIG_DEBUG_FS
0486     struct dentry *root = debugfs_create_dir("frontswap", NULL);
0487     if (root == NULL)
0488         return -ENXIO;
0489     debugfs_create_u64("loads", S_IRUGO, root, &frontswap_loads);
0490     debugfs_create_u64("succ_stores", S_IRUGO, root, &frontswap_succ_stores);
0491     debugfs_create_u64("failed_stores", S_IRUGO, root,
0492                 &frontswap_failed_stores);
0493     debugfs_create_u64("invalidates", S_IRUGO,
0494                 root, &frontswap_invalidates);
0495 #endif
0496     return 0;
0497 }
0498 
0499 module_init(init_frontswap);