Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Frontswap frontend
0004  *
0005  * This code provides the generic "frontend" layer to call a matching
0006  * "backend" driver implementation of frontswap.  See
0007  * Documentation/mm/frontswap.rst for more information.
0008  *
0009  * Copyright (C) 2009-2012 Oracle Corp.  All rights reserved.
0010  * Author: Dan Magenheimer
0011  */
0012 
0013 #include <linux/mman.h>
0014 #include <linux/swap.h>
0015 #include <linux/swapops.h>
0016 #include <linux/security.h>
0017 #include <linux/module.h>
0018 #include <linux/debugfs.h>
0019 #include <linux/frontswap.h>
0020 #include <linux/swapfile.h>
0021 
0022 DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
0023 
0024 /*
0025  * frontswap_ops are added by frontswap_register_ops, and provide the
0026  * frontswap "backend" implementation functions.  Multiple implementations
0027  * may be registered, but implementations can never deregister.  This
0028  * is a simple singly-linked list of all registered implementations.
0029  */
0030 static const struct frontswap_ops *frontswap_ops __read_mostly;
0031 
0032 #ifdef CONFIG_DEBUG_FS
0033 /*
0034  * Counters available via /sys/kernel/debug/frontswap (if debugfs is
0035  * properly configured).  These are for information only so are not protected
0036  * against increment races.
0037  */
0038 static u64 frontswap_loads;
0039 static u64 frontswap_succ_stores;
0040 static u64 frontswap_failed_stores;
0041 static u64 frontswap_invalidates;
0042 
0043 static inline void inc_frontswap_loads(void)
0044 {
0045     data_race(frontswap_loads++);
0046 }
0047 static inline void inc_frontswap_succ_stores(void)
0048 {
0049     data_race(frontswap_succ_stores++);
0050 }
0051 static inline void inc_frontswap_failed_stores(void)
0052 {
0053     data_race(frontswap_failed_stores++);
0054 }
0055 static inline void inc_frontswap_invalidates(void)
0056 {
0057     data_race(frontswap_invalidates++);
0058 }
0059 #else
0060 static inline void inc_frontswap_loads(void) { }
0061 static inline void inc_frontswap_succ_stores(void) { }
0062 static inline void inc_frontswap_failed_stores(void) { }
0063 static inline void inc_frontswap_invalidates(void) { }
0064 #endif
0065 
0066 /*
0067  * Due to the asynchronous nature of the backends loading potentially
0068  * _after_ the swap system has been activated, we have chokepoints
0069  * on all frontswap functions to not call the backend until the backend
0070  * has registered.
0071  *
0072  * This would not guards us against the user deciding to call swapoff right as
0073  * we are calling the backend to initialize (so swapon is in action).
0074  * Fortunately for us, the swapon_mutex has been taken by the callee so we are
0075  * OK. The other scenario where calls to frontswap_store (called via
0076  * swap_writepage) is racing with frontswap_invalidate_area (called via
0077  * swapoff) is again guarded by the swap subsystem.
0078  *
0079  * While no backend is registered all calls to frontswap_[store|load|
0080  * invalidate_area|invalidate_page] are ignored or fail.
0081  *
0082  * The time between the backend being registered and the swap file system
0083  * calling the backend (via the frontswap_* functions) is indeterminate as
0084  * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
0085  * That is OK as we are comfortable missing some of these calls to the newly
0086  * registered backend.
0087  *
0088  * Obviously the opposite (unloading the backend) must be done after all
0089  * the frontswap_[store|load|invalidate_area|invalidate_page] start
0090  * ignoring or failing the requests.  However, there is currently no way
0091  * to unload a backend once it is registered.
0092  */
0093 
0094 /*
0095  * Register operations for frontswap
0096  */
0097 int frontswap_register_ops(const struct frontswap_ops *ops)
0098 {
0099     if (frontswap_ops)
0100         return -EINVAL;
0101 
0102     frontswap_ops = ops;
0103     static_branch_inc(&frontswap_enabled_key);
0104     return 0;
0105 }
0106 
0107 /*
0108  * Called when a swap device is swapon'd.
0109  */
0110 void frontswap_init(unsigned type, unsigned long *map)
0111 {
0112     struct swap_info_struct *sis = swap_info[type];
0113 
0114     VM_BUG_ON(sis == NULL);
0115 
0116     /*
0117      * p->frontswap is a bitmap that we MUST have to figure out which page
0118      * has gone in frontswap. Without it there is no point of continuing.
0119      */
0120     if (WARN_ON(!map))
0121         return;
0122     /*
0123      * Irregardless of whether the frontswap backend has been loaded
0124      * before this function or it will be later, we _MUST_ have the
0125      * p->frontswap set to something valid to work properly.
0126      */
0127     frontswap_map_set(sis, map);
0128 
0129     if (!frontswap_enabled())
0130         return;
0131     frontswap_ops->init(type);
0132 }
0133 
0134 static bool __frontswap_test(struct swap_info_struct *sis,
0135                 pgoff_t offset)
0136 {
0137     if (sis->frontswap_map)
0138         return test_bit(offset, sis->frontswap_map);
0139     return false;
0140 }
0141 
0142 static inline void __frontswap_set(struct swap_info_struct *sis,
0143                    pgoff_t offset)
0144 {
0145     set_bit(offset, sis->frontswap_map);
0146     atomic_inc(&sis->frontswap_pages);
0147 }
0148 
0149 static inline void __frontswap_clear(struct swap_info_struct *sis,
0150                      pgoff_t offset)
0151 {
0152     clear_bit(offset, sis->frontswap_map);
0153     atomic_dec(&sis->frontswap_pages);
0154 }
0155 
0156 /*
0157  * "Store" data from a page to frontswap and associate it with the page's
0158  * swaptype and offset.  Page must be locked and in the swap cache.
0159  * If frontswap already contains a page with matching swaptype and
0160  * offset, the frontswap implementation may either overwrite the data and
0161  * return success or invalidate the page from frontswap and return failure.
0162  */
0163 int __frontswap_store(struct page *page)
0164 {
0165     int ret = -1;
0166     swp_entry_t entry = { .val = page_private(page), };
0167     int type = swp_type(entry);
0168     struct swap_info_struct *sis = swap_info[type];
0169     pgoff_t offset = swp_offset(entry);
0170 
0171     VM_BUG_ON(!frontswap_ops);
0172     VM_BUG_ON(!PageLocked(page));
0173     VM_BUG_ON(sis == NULL);
0174 
0175     /*
0176      * If a dup, we must remove the old page first; we can't leave the
0177      * old page no matter if the store of the new page succeeds or fails,
0178      * and we can't rely on the new page replacing the old page as we may
0179      * not store to the same implementation that contains the old page.
0180      */
0181     if (__frontswap_test(sis, offset)) {
0182         __frontswap_clear(sis, offset);
0183         frontswap_ops->invalidate_page(type, offset);
0184     }
0185 
0186     ret = frontswap_ops->store(type, offset, page);
0187     if (ret == 0) {
0188         __frontswap_set(sis, offset);
0189         inc_frontswap_succ_stores();
0190     } else {
0191         inc_frontswap_failed_stores();
0192     }
0193 
0194     return ret;
0195 }
0196 
0197 /*
0198  * "Get" data from frontswap associated with swaptype and offset that were
0199  * specified when the data was put to frontswap and use it to fill the
0200  * specified page with data. Page must be locked and in the swap cache.
0201  */
0202 int __frontswap_load(struct page *page)
0203 {
0204     int ret = -1;
0205     swp_entry_t entry = { .val = page_private(page), };
0206     int type = swp_type(entry);
0207     struct swap_info_struct *sis = swap_info[type];
0208     pgoff_t offset = swp_offset(entry);
0209 
0210     VM_BUG_ON(!frontswap_ops);
0211     VM_BUG_ON(!PageLocked(page));
0212     VM_BUG_ON(sis == NULL);
0213 
0214     if (!__frontswap_test(sis, offset))
0215         return -1;
0216 
0217     /* Try loading from each implementation, until one succeeds. */
0218     ret = frontswap_ops->load(type, offset, page);
0219     if (ret == 0)
0220         inc_frontswap_loads();
0221     return ret;
0222 }
0223 
0224 /*
0225  * Invalidate any data from frontswap associated with the specified swaptype
0226  * and offset so that a subsequent "get" will fail.
0227  */
0228 void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
0229 {
0230     struct swap_info_struct *sis = swap_info[type];
0231 
0232     VM_BUG_ON(!frontswap_ops);
0233     VM_BUG_ON(sis == NULL);
0234 
0235     if (!__frontswap_test(sis, offset))
0236         return;
0237 
0238     frontswap_ops->invalidate_page(type, offset);
0239     __frontswap_clear(sis, offset);
0240     inc_frontswap_invalidates();
0241 }
0242 
0243 /*
0244  * Invalidate all data from frontswap associated with all offsets for the
0245  * specified swaptype.
0246  */
0247 void __frontswap_invalidate_area(unsigned type)
0248 {
0249     struct swap_info_struct *sis = swap_info[type];
0250 
0251     VM_BUG_ON(!frontswap_ops);
0252     VM_BUG_ON(sis == NULL);
0253 
0254     if (sis->frontswap_map == NULL)
0255         return;
0256 
0257     frontswap_ops->invalidate_area(type);
0258     atomic_set(&sis->frontswap_pages, 0);
0259     bitmap_zero(sis->frontswap_map, sis->max);
0260 }
0261 
0262 static int __init init_frontswap(void)
0263 {
0264 #ifdef CONFIG_DEBUG_FS
0265     struct dentry *root = debugfs_create_dir("frontswap", NULL);
0266     if (root == NULL)
0267         return -ENXIO;
0268     debugfs_create_u64("loads", 0444, root, &frontswap_loads);
0269     debugfs_create_u64("succ_stores", 0444, root, &frontswap_succ_stores);
0270     debugfs_create_u64("failed_stores", 0444, root,
0271                &frontswap_failed_stores);
0272     debugfs_create_u64("invalidates", 0444, root, &frontswap_invalidates);
0273 #endif
0274     return 0;
0275 }
0276 
0277 module_init(init_frontswap);