Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Frontswap frontend |
| 3 | * |
| 4 | * This code provides the generic "frontend" layer to call a matching |
| 5 | * "backend" driver implementation of frontswap. See |
| 6 | * Documentation/vm/frontswap.txt for more information. |
| 7 | * |
| 8 | * Copyright (C) 2009-2012 Oracle Corp. All rights reserved. |
| 9 | * Author: Dan Magenheimer |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. |
| 12 | */ |
| 13 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 14 | #include <linux/mman.h> |
| 15 | #include <linux/swap.h> |
| 16 | #include <linux/swapops.h> |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 17 | #include <linux/security.h> |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 18 | #include <linux/module.h> |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/frontswap.h> |
| 21 | #include <linux/swapfile.h> |
| 22 | |
| 23 | /* |
| 24 | * frontswap_ops is set by frontswap_register_ops to contain the pointers |
| 25 | * to the frontswap "backend" implementation functions. |
| 26 | */ |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 27 | static struct frontswap_ops *frontswap_ops __read_mostly; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This global enablement flag reduces overhead on systems where frontswap_ops |
| 31 | * has not been registered, so is preferred to the slower alternative: a |
| 32 | * function call that checks a non-global. |
| 33 | */ |
| 34 | bool frontswap_enabled __read_mostly; |
| 35 | EXPORT_SYMBOL(frontswap_enabled); |
| 36 | |
| 37 | /* |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 38 | * If enabled, frontswap_store will return failure even on success. As |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 39 | * a result, the swap subsystem will always write the page to swap, in |
| 40 | * effect converting frontswap into a writethrough cache. In this mode, |
| 41 | * there is no direct reduction in swap writes, but a frontswap backend |
| 42 | * can unilaterally "reclaim" any pages in use with no data loss, thus |
| 43 | * providing increases control over maximum memory usage due to frontswap. |
| 44 | */ |
| 45 | static bool frontswap_writethrough_enabled __read_mostly; |
| 46 | |
Dan Magenheimer | e3483a5 | 2012-09-20 12:16:52 -0700 | [diff] [blame] | 47 | /* |
| 48 | * If enabled, the underlying tmem implementation is capable of doing |
| 49 | * exclusive gets, so frontswap_load, on a successful tmem_get must |
| 50 | * mark the page as no longer in frontswap AND mark it dirty. |
| 51 | */ |
| 52 | static bool frontswap_tmem_exclusive_gets_enabled __read_mostly; |
| 53 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 54 | #ifdef CONFIG_DEBUG_FS |
| 55 | /* |
| 56 | * Counters available via /sys/kernel/debug/frontswap (if debugfs is |
| 57 | * properly configured). These are for information only so are not protected |
| 58 | * against increment races. |
| 59 | */ |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 60 | static u64 frontswap_loads; |
| 61 | static u64 frontswap_succ_stores; |
| 62 | static u64 frontswap_failed_stores; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 63 | static u64 frontswap_invalidates; |
| 64 | |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 65 | static inline void inc_frontswap_loads(void) { |
| 66 | frontswap_loads++; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 67 | } |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 68 | static inline void inc_frontswap_succ_stores(void) { |
| 69 | frontswap_succ_stores++; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 70 | } |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 71 | static inline void inc_frontswap_failed_stores(void) { |
| 72 | frontswap_failed_stores++; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 73 | } |
| 74 | static inline void inc_frontswap_invalidates(void) { |
| 75 | frontswap_invalidates++; |
| 76 | } |
| 77 | #else |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 78 | static inline void inc_frontswap_loads(void) { } |
| 79 | static inline void inc_frontswap_succ_stores(void) { } |
| 80 | static inline void inc_frontswap_failed_stores(void) { } |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 81 | static inline void inc_frontswap_invalidates(void) { } |
| 82 | #endif |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Due to the asynchronous nature of the backends loading potentially |
| 86 | * _after_ the swap system has been activated, we have chokepoints |
| 87 | * on all frontswap functions to not call the backend until the backend |
| 88 | * has registered. |
| 89 | * |
| 90 | * Specifically when no backend is registered (nobody called |
| 91 | * frontswap_register_ops) all calls to frontswap_init (which is done via |
| 92 | * swapon -> enable_swap_info -> frontswap_init) are registered and remembered |
| 93 | * (via the setting of need_init bitmap) but fail to create tmem_pools. When a |
| 94 | * backend registers with frontswap at some later point the previous |
| 95 | * calls to frontswap_init are executed (by iterating over the need_init |
| 96 | * bitmap) to create tmem_pools and set the respective poolids. All of that is |
| 97 | * guarded by us using atomic bit operations on the 'need_init' bitmap. |
| 98 | * |
| 99 | * This would not guards us against the user deciding to call swapoff right as |
| 100 | * we are calling the backend to initialize (so swapon is in action). |
| 101 | * Fortunatly for us, the swapon_mutex has been taked by the callee so we are |
| 102 | * OK. The other scenario where calls to frontswap_store (called via |
| 103 | * swap_writepage) is racing with frontswap_invalidate_area (called via |
| 104 | * swapoff) is again guarded by the swap subsystem. |
| 105 | * |
| 106 | * While no backend is registered all calls to frontswap_[store|load| |
| 107 | * invalidate_area|invalidate_page] are ignored or fail. |
| 108 | * |
| 109 | * The time between the backend being registered and the swap file system |
| 110 | * calling the backend (via the frontswap_* functions) is indeterminate as |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 111 | * frontswap_ops is not atomic_t (or a value guarded by a spinlock). |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 112 | * That is OK as we are comfortable missing some of these calls to the newly |
| 113 | * registered backend. |
| 114 | * |
| 115 | * Obviously the opposite (unloading the backend) must be done after all |
| 116 | * the frontswap_[store|load|invalidate_area|invalidate_page] start |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 117 | * ignorning or failing the requests - at which point frontswap_ops |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 118 | * would have to be made in some fashion atomic. |
| 119 | */ |
| 120 | static DECLARE_BITMAP(need_init, MAX_SWAPFILES); |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 121 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 122 | /* |
| 123 | * Register operations for frontswap, returning previous thus allowing |
| 124 | * detection of multiple backends and possible nesting. |
| 125 | */ |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 126 | struct frontswap_ops *frontswap_register_ops(struct frontswap_ops *ops) |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 127 | { |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 128 | struct frontswap_ops *old = frontswap_ops; |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 129 | int i; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 130 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 131 | frontswap_enabled = true; |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 132 | |
| 133 | for (i = 0; i < MAX_SWAPFILES; i++) { |
| 134 | if (test_and_clear_bit(i, need_init)) |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 135 | ops->init(i); |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 136 | } |
| 137 | /* |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 138 | * We MUST have frontswap_ops set _after_ the frontswap_init's |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 139 | * have been called. Otherwise __frontswap_store might fail. Hence |
| 140 | * the barrier to make sure compiler does not re-order us. |
| 141 | */ |
| 142 | barrier(); |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 143 | frontswap_ops = ops; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 144 | return old; |
| 145 | } |
| 146 | EXPORT_SYMBOL(frontswap_register_ops); |
| 147 | |
| 148 | /* |
| 149 | * Enable/disable frontswap writethrough (see above). |
| 150 | */ |
| 151 | void frontswap_writethrough(bool enable) |
| 152 | { |
| 153 | frontswap_writethrough_enabled = enable; |
| 154 | } |
| 155 | EXPORT_SYMBOL(frontswap_writethrough); |
| 156 | |
| 157 | /* |
Dan Magenheimer | e3483a5 | 2012-09-20 12:16:52 -0700 | [diff] [blame] | 158 | * Enable/disable frontswap exclusive gets (see above). |
| 159 | */ |
| 160 | void frontswap_tmem_exclusive_gets(bool enable) |
| 161 | { |
| 162 | frontswap_tmem_exclusive_gets_enabled = enable; |
| 163 | } |
| 164 | EXPORT_SYMBOL(frontswap_tmem_exclusive_gets); |
| 165 | |
| 166 | /* |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 167 | * Called when a swap device is swapon'd. |
| 168 | */ |
| 169 | void __frontswap_init(unsigned type) |
| 170 | { |
| 171 | struct swap_info_struct *sis = swap_info[type]; |
| 172 | |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 173 | if (frontswap_ops) { |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 174 | BUG_ON(sis == NULL); |
| 175 | if (sis->frontswap_map == NULL) |
| 176 | return; |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 177 | frontswap_ops->init(type); |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 178 | } else { |
| 179 | BUG_ON(type > MAX_SWAPFILES); |
| 180 | set_bit(type, need_init); |
| 181 | } |
| 182 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 183 | } |
| 184 | EXPORT_SYMBOL(__frontswap_init); |
| 185 | |
Sasha Levin | 611edfe | 2012-06-10 12:51:07 +0200 | [diff] [blame] | 186 | static inline void __frontswap_clear(struct swap_info_struct *sis, pgoff_t offset) |
| 187 | { |
| 188 | frontswap_clear(sis, offset); |
| 189 | atomic_dec(&sis->frontswap_pages); |
| 190 | } |
| 191 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 192 | /* |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 193 | * "Store" data from a page to frontswap and associate it with the page's |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 194 | * swaptype and offset. Page must be locked and in the swap cache. |
| 195 | * If frontswap already contains a page with matching swaptype and |
Wanpeng Li | 1d00015 | 2012-06-16 20:37:48 +0800 | [diff] [blame] | 196 | * offset, the frontswap implementation may either overwrite the data and |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 197 | * return success or invalidate the page from frontswap and return failure. |
| 198 | */ |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 199 | int __frontswap_store(struct page *page) |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 200 | { |
| 201 | int ret = -1, dup = 0; |
| 202 | swp_entry_t entry = { .val = page_private(page), }; |
| 203 | int type = swp_type(entry); |
| 204 | struct swap_info_struct *sis = swap_info[type]; |
| 205 | pgoff_t offset = swp_offset(entry); |
| 206 | |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 207 | if (!frontswap_ops) { |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 208 | inc_frontswap_failed_stores(); |
| 209 | return ret; |
| 210 | } |
| 211 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 212 | BUG_ON(!PageLocked(page)); |
| 213 | BUG_ON(sis == NULL); |
| 214 | if (frontswap_test(sis, offset)) |
| 215 | dup = 1; |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 216 | ret = frontswap_ops->store(type, offset, page); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 217 | if (ret == 0) { |
| 218 | frontswap_set(sis, offset); |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 219 | inc_frontswap_succ_stores(); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 220 | if (!dup) |
| 221 | atomic_inc(&sis->frontswap_pages); |
Sasha Levin | d9674dd | 2012-06-10 12:51:04 +0200 | [diff] [blame] | 222 | } else { |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 223 | /* |
| 224 | failed dup always results in automatic invalidate of |
| 225 | the (older) page from frontswap |
| 226 | */ |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 227 | inc_frontswap_failed_stores(); |
Sasha Levin | 611edfe | 2012-06-10 12:51:07 +0200 | [diff] [blame] | 228 | if (dup) |
| 229 | __frontswap_clear(sis, offset); |
Sasha Levin | 4bb3e31 | 2012-06-10 12:51:00 +0200 | [diff] [blame] | 230 | } |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 231 | if (frontswap_writethrough_enabled) |
| 232 | /* report failure so swap also writes to swap device */ |
| 233 | ret = -1; |
| 234 | return ret; |
| 235 | } |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 236 | EXPORT_SYMBOL(__frontswap_store); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * "Get" data from frontswap associated with swaptype and offset that were |
| 240 | * specified when the data was put to frontswap and use it to fill the |
| 241 | * specified page with data. Page must be locked and in the swap cache. |
| 242 | */ |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 243 | int __frontswap_load(struct page *page) |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 244 | { |
| 245 | int ret = -1; |
| 246 | swp_entry_t entry = { .val = page_private(page), }; |
| 247 | int type = swp_type(entry); |
| 248 | struct swap_info_struct *sis = swap_info[type]; |
| 249 | pgoff_t offset = swp_offset(entry); |
| 250 | |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 251 | if (!frontswap_ops) |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 252 | return ret; |
| 253 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 254 | BUG_ON(!PageLocked(page)); |
| 255 | BUG_ON(sis == NULL); |
| 256 | if (frontswap_test(sis, offset)) |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 257 | ret = frontswap_ops->load(type, offset, page); |
Dan Magenheimer | e3483a5 | 2012-09-20 12:16:52 -0700 | [diff] [blame] | 258 | if (ret == 0) { |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 259 | inc_frontswap_loads(); |
Dan Magenheimer | e3483a5 | 2012-09-20 12:16:52 -0700 | [diff] [blame] | 260 | if (frontswap_tmem_exclusive_gets_enabled) { |
| 261 | SetPageDirty(page); |
| 262 | frontswap_clear(sis, offset); |
| 263 | } |
| 264 | } |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 265 | return ret; |
| 266 | } |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 267 | EXPORT_SYMBOL(__frontswap_load); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * Invalidate any data from frontswap associated with the specified swaptype |
| 271 | * and offset so that a subsequent "get" will fail. |
| 272 | */ |
| 273 | void __frontswap_invalidate_page(unsigned type, pgoff_t offset) |
| 274 | { |
| 275 | struct swap_info_struct *sis = swap_info[type]; |
| 276 | |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 277 | if (!frontswap_ops) |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 278 | return; |
| 279 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 280 | BUG_ON(sis == NULL); |
| 281 | if (frontswap_test(sis, offset)) { |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 282 | frontswap_ops->invalidate_page(type, offset); |
Sasha Levin | 611edfe | 2012-06-10 12:51:07 +0200 | [diff] [blame] | 283 | __frontswap_clear(sis, offset); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 284 | inc_frontswap_invalidates(); |
| 285 | } |
| 286 | } |
| 287 | EXPORT_SYMBOL(__frontswap_invalidate_page); |
| 288 | |
| 289 | /* |
| 290 | * Invalidate all data from frontswap associated with all offsets for the |
| 291 | * specified swaptype. |
| 292 | */ |
| 293 | void __frontswap_invalidate_area(unsigned type) |
| 294 | { |
| 295 | struct swap_info_struct *sis = swap_info[type]; |
| 296 | |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 297 | if (frontswap_ops) { |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 298 | BUG_ON(sis == NULL); |
| 299 | if (sis->frontswap_map == NULL) |
| 300 | return; |
Konrad Rzeszutek Wilk | 1e01c96 | 2013-04-30 15:26:51 -0700 | [diff] [blame^] | 301 | frontswap_ops->invalidate_area(type); |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 302 | atomic_set(&sis->frontswap_pages, 0); |
| 303 | memset(sis->frontswap_map, 0, sis->max / sizeof(long)); |
| 304 | } |
| 305 | clear_bit(type, need_init); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 306 | } |
| 307 | EXPORT_SYMBOL(__frontswap_invalidate_area); |
| 308 | |
Sasha Levin | 9625344 | 2012-06-10 12:51:01 +0200 | [diff] [blame] | 309 | static unsigned long __frontswap_curr_pages(void) |
| 310 | { |
| 311 | int type; |
| 312 | unsigned long totalpages = 0; |
| 313 | struct swap_info_struct *si = NULL; |
| 314 | |
| 315 | assert_spin_locked(&swap_lock); |
| 316 | for (type = swap_list.head; type >= 0; type = si->next) { |
| 317 | si = swap_info[type]; |
| 318 | totalpages += atomic_read(&si->frontswap_pages); |
| 319 | } |
| 320 | return totalpages; |
| 321 | } |
| 322 | |
Sasha Levin | f116695 | 2012-06-10 12:51:02 +0200 | [diff] [blame] | 323 | static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused, |
| 324 | int *swapid) |
| 325 | { |
| 326 | int ret = -EINVAL; |
| 327 | struct swap_info_struct *si = NULL; |
| 328 | int si_frontswap_pages; |
| 329 | unsigned long total_pages_to_unuse = total; |
| 330 | unsigned long pages = 0, pages_to_unuse = 0; |
| 331 | int type; |
| 332 | |
| 333 | assert_spin_locked(&swap_lock); |
| 334 | for (type = swap_list.head; type >= 0; type = si->next) { |
| 335 | si = swap_info[type]; |
| 336 | si_frontswap_pages = atomic_read(&si->frontswap_pages); |
| 337 | if (total_pages_to_unuse < si_frontswap_pages) { |
| 338 | pages = pages_to_unuse = total_pages_to_unuse; |
| 339 | } else { |
| 340 | pages = si_frontswap_pages; |
| 341 | pages_to_unuse = 0; /* unuse all */ |
| 342 | } |
| 343 | /* ensure there is enough RAM to fetch pages from frontswap */ |
| 344 | if (security_vm_enough_memory_mm(current->mm, pages)) { |
| 345 | ret = -ENOMEM; |
| 346 | continue; |
| 347 | } |
| 348 | vm_unacct_memory(pages); |
| 349 | *unused = pages_to_unuse; |
| 350 | *swapid = type; |
| 351 | ret = 0; |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | |
Zhenzhong Duan | a00bb1e | 2012-09-21 16:40:30 +0800 | [diff] [blame] | 358 | /* |
| 359 | * Used to check if it's necessory and feasible to unuse pages. |
| 360 | * Return 1 when nothing to do, 0 when need to shink pages, |
| 361 | * error code when there is an error. |
| 362 | */ |
Sasha Levin | 69217b4 | 2012-06-10 12:51:03 +0200 | [diff] [blame] | 363 | static int __frontswap_shrink(unsigned long target_pages, |
| 364 | unsigned long *pages_to_unuse, |
| 365 | int *type) |
| 366 | { |
| 367 | unsigned long total_pages = 0, total_pages_to_unuse; |
| 368 | |
| 369 | assert_spin_locked(&swap_lock); |
| 370 | |
| 371 | total_pages = __frontswap_curr_pages(); |
| 372 | if (total_pages <= target_pages) { |
| 373 | /* Nothing to do */ |
| 374 | *pages_to_unuse = 0; |
Zhenzhong Duan | a00bb1e | 2012-09-21 16:40:30 +0800 | [diff] [blame] | 375 | return 1; |
Sasha Levin | 69217b4 | 2012-06-10 12:51:03 +0200 | [diff] [blame] | 376 | } |
| 377 | total_pages_to_unuse = total_pages - target_pages; |
| 378 | return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type); |
| 379 | } |
| 380 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 381 | /* |
| 382 | * Frontswap, like a true swap device, may unnecessarily retain pages |
| 383 | * under certain circumstances; "shrink" frontswap is essentially a |
| 384 | * "partial swapoff" and works by calling try_to_unuse to attempt to |
| 385 | * unuse enough frontswap pages to attempt to -- subject to memory |
| 386 | * constraints -- reduce the number of pages in frontswap to the |
| 387 | * number given in the parameter target_pages. |
| 388 | */ |
| 389 | void frontswap_shrink(unsigned long target_pages) |
| 390 | { |
Sasha Levin | f116695 | 2012-06-10 12:51:02 +0200 | [diff] [blame] | 391 | unsigned long pages_to_unuse = 0; |
Seth Jennings | 6b982fc | 2012-07-30 14:47:44 -0500 | [diff] [blame] | 392 | int uninitialized_var(type), ret; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * we don't want to hold swap_lock while doing a very |
| 396 | * lengthy try_to_unuse, but swap_list may change |
| 397 | * so restart scan from swap_list.head each time |
| 398 | */ |
| 399 | spin_lock(&swap_lock); |
Sasha Levin | 69217b4 | 2012-06-10 12:51:03 +0200 | [diff] [blame] | 400 | ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 401 | spin_unlock(&swap_lock); |
Zhenzhong Duan | a00bb1e | 2012-09-21 16:40:30 +0800 | [diff] [blame] | 402 | if (ret == 0) |
Sasha Levin | 69217b4 | 2012-06-10 12:51:03 +0200 | [diff] [blame] | 403 | try_to_unuse(type, true, pages_to_unuse); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | EXPORT_SYMBOL(frontswap_shrink); |
| 407 | |
| 408 | /* |
| 409 | * Count and return the number of frontswap pages across all |
| 410 | * swap devices. This is exported so that backend drivers can |
| 411 | * determine current usage without reading debugfs. |
| 412 | */ |
| 413 | unsigned long frontswap_curr_pages(void) |
| 414 | { |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 415 | unsigned long totalpages = 0; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 416 | |
| 417 | spin_lock(&swap_lock); |
Sasha Levin | 9625344 | 2012-06-10 12:51:01 +0200 | [diff] [blame] | 418 | totalpages = __frontswap_curr_pages(); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 419 | spin_unlock(&swap_lock); |
Sasha Levin | 9625344 | 2012-06-10 12:51:01 +0200 | [diff] [blame] | 420 | |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 421 | return totalpages; |
| 422 | } |
| 423 | EXPORT_SYMBOL(frontswap_curr_pages); |
| 424 | |
| 425 | static int __init init_frontswap(void) |
| 426 | { |
| 427 | #ifdef CONFIG_DEBUG_FS |
| 428 | struct dentry *root = debugfs_create_dir("frontswap", NULL); |
| 429 | if (root == NULL) |
| 430 | return -ENXIO; |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 431 | debugfs_create_u64("loads", S_IRUGO, root, &frontswap_loads); |
| 432 | debugfs_create_u64("succ_stores", S_IRUGO, root, &frontswap_succ_stores); |
| 433 | debugfs_create_u64("failed_stores", S_IRUGO, root, |
| 434 | &frontswap_failed_stores); |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 435 | debugfs_create_u64("invalidates", S_IRUGO, |
| 436 | root, &frontswap_invalidates); |
| 437 | #endif |
Dan Magenheimer | 905cd0e | 2013-04-30 15:26:50 -0700 | [diff] [blame] | 438 | frontswap_enabled = 1; |
Dan Magenheimer | 29f233c | 2012-04-09 17:09:27 -0600 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | module_init(init_frontswap); |