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.
8 * Copyright (C) 2009-2010 Oracle Corp. All rights reserved.
9 * Author: Dan Magenheimer
11 * This work is licensed under the terms of the GNU GPL, version 2.
15 #include <linux/mman.h>
16 #include <linux/swap.h>
17 #include <linux/swapops.h>
18 #include <linux/proc_fs.h>
19 #include <linux/security.h>
20 #include <linux/capability.h>
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/debugfs.h>
24 #include <linux/frontswap.h>
25 #include <linux/swapfile.h>
28 * frontswap_ops is set by frontswap_register_ops to contain the pointers
29 * to the frontswap "backend" implementation functions.
31 static struct frontswap_ops frontswap_ops __read_mostly;
34 * This global enablement flag reduces overhead on systems where frontswap_ops
35 * has not been registered, so is preferred to the slower alternative: a
36 * function call that checks a non-global.
38 int frontswap_enabled __read_mostly;
39 EXPORT_SYMBOL(frontswap_enabled);
42 * Counters available via /sys/kernel/debug/frontswap (if debugfs is
43 * properly configured. These are for information only so are not protected
44 * against increment races.
46 static u64 frontswap_gets;
47 static u64 frontswap_succ_puts;
48 static u64 frontswap_failed_puts;
49 static u64 frontswap_invalidates;
52 * Register operations for frontswap, returning previous thus allowing
53 * detection of multiple backends and possible nesting
55 struct frontswap_ops frontswap_register_ops(struct frontswap_ops *ops)
57 struct frontswap_ops old = frontswap_ops;
60 frontswap_enabled = 1;
63 EXPORT_SYMBOL(frontswap_register_ops);
65 /* Called when a swap device is swapon'd */
66 void __frontswap_init(unsigned type)
68 struct swap_info_struct *sis = swap_info[type];
71 if (sis->frontswap_map == NULL)
73 if (frontswap_enabled)
74 (*frontswap_ops.init)(type);
76 EXPORT_SYMBOL(__frontswap_init);
79 * "Put" data from a page to frontswap and associate it with the page's
80 * swaptype and offset. Page must be locked and in the swap cache.
81 * If frontswap already contains a page with matching swaptype and
82 * offset, the frontswap implmentation may either overwrite the data and
83 * return success or invalidate the page from frontswap and return failure
85 int __frontswap_put_page(struct page *page)
87 int ret = -1, dup = 0;
88 swp_entry_t entry = { .val = page_private(page), };
89 int type = swp_type(entry);
90 struct swap_info_struct *sis = swap_info[type];
91 pgoff_t offset = swp_offset(entry);
93 BUG_ON(!PageLocked(page));
95 if (frontswap_test(sis, offset))
97 ret = (*frontswap_ops.put_page)(type, offset, page);
99 frontswap_set(sis, offset);
100 frontswap_succ_puts++;
102 atomic_inc(&sis->frontswap_pages);
105 failed dup always results in automatic invalidate of
106 the (older) page from frontswap
108 frontswap_clear(sis, offset);
109 atomic_dec(&sis->frontswap_pages);
110 frontswap_failed_puts++;
112 frontswap_failed_puts++;
115 EXPORT_SYMBOL(__frontswap_put_page);
118 * "Get" data from frontswap associated with swaptype and offset that were
119 * specified when the data was put to frontswap and use it to fill the
120 * specified page with data. Page must be locked and in the swap cache
122 int __frontswap_get_page(struct page *page)
125 swp_entry_t entry = { .val = page_private(page), };
126 int type = swp_type(entry);
127 struct swap_info_struct *sis = swap_info[type];
128 pgoff_t offset = swp_offset(entry);
130 BUG_ON(!PageLocked(page));
132 if (frontswap_test(sis, offset))
133 ret = (*frontswap_ops.get_page)(type, offset, page);
138 EXPORT_SYMBOL(__frontswap_get_page);
141 * Invalidate any data from frontswap associated with the specified swaptype
142 * and offset so that a subsequent "get" will fail.
144 void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
146 struct swap_info_struct *sis = swap_info[type];
149 if (frontswap_test(sis, offset)) {
150 (*frontswap_ops.invalidate_page)(type, offset);
151 atomic_dec(&sis->frontswap_pages);
152 frontswap_clear(sis, offset);
153 frontswap_invalidates++;
156 EXPORT_SYMBOL(__frontswap_invalidate_page);
159 * Invalidate all data from frontswap associated with all offsets for the
160 * specified swaptype.
162 void __frontswap_invalidate_area(unsigned type)
164 struct swap_info_struct *sis = swap_info[type];
167 if (sis->frontswap_map == NULL)
169 (*frontswap_ops.invalidate_area)(type);
170 atomic_set(&sis->frontswap_pages, 0);
171 memset(sis->frontswap_map, 0, sis->max / sizeof(long));
173 EXPORT_SYMBOL(__frontswap_invalidate_area);
176 * Frontswap, like a true swap device, may unnecessarily retain pages
177 * under certain circumstances; "shrink" frontswap is essentially a
178 * "partial swapoff" and works by calling try_to_unuse to attempt to
179 * unuse enough frontswap pages to attempt to -- subject to memory
180 * constraints -- reduce the number of pages in frontswap to the
181 * number given in the parameter target_pages.
183 void frontswap_shrink(unsigned long target_pages)
185 struct swap_info_struct *si = NULL;
186 int si_frontswap_pages;
187 unsigned long total_pages = 0, total_pages_to_unuse;
188 unsigned long pages = 0, pages_to_unuse = 0;
193 * we don't want to hold swap_lock while doing a very
194 * lengthy try_to_unuse, but swap_list may change
195 * so restart scan from swap_list.head each time
197 spin_lock(&swap_lock);
200 for (type = swap_list.head; type >= 0; type = si->next) {
201 si = swap_info[type];
202 total_pages += atomic_read(&si->frontswap_pages);
204 if (total_pages <= target_pages)
206 total_pages_to_unuse = total_pages - target_pages;
207 for (type = swap_list.head; type >= 0; type = si->next) {
208 si = swap_info[type];
209 si_frontswap_pages = atomic_read(&si->frontswap_pages);
210 if (total_pages_to_unuse < si_frontswap_pages)
211 pages = pages_to_unuse = total_pages_to_unuse;
213 pages = si_frontswap_pages;
214 pages_to_unuse = 0; /* unuse all */
216 /* ensure there is enough RAM to fetch pages from frontswap */
217 if (security_vm_enough_memory_mm(current->mm, pages))
219 vm_unacct_memory(pages);
225 spin_unlock(&swap_lock);
226 try_to_unuse(type, true, pages_to_unuse);
229 spin_unlock(&swap_lock);
232 EXPORT_SYMBOL(frontswap_shrink);
235 * Count and return the number of frontswap pages across all
236 * swap devices. This is exported so that backend drivers can
237 * determine current usage without reading debugfs.
239 unsigned long frontswap_curr_pages(void)
242 unsigned long totalpages = 0;
243 struct swap_info_struct *si = NULL;
245 spin_lock(&swap_lock);
246 for (type = swap_list.head; type >= 0; type = si->next) {
247 si = swap_info[type];
248 totalpages += atomic_read(&si->frontswap_pages);
250 spin_unlock(&swap_lock);
253 EXPORT_SYMBOL(frontswap_curr_pages);
255 static int __init init_frontswap(void)
259 #ifdef CONFIG_DEBUG_FS
260 struct dentry *root = debugfs_create_dir("frontswap", NULL);
263 debugfs_create_u64("gets", S_IRUGO, root, &frontswap_gets);
264 debugfs_create_u64("succ_puts", S_IRUGO, root, &frontswap_succ_puts);
265 debugfs_create_u64("puts", S_IRUGO, root, &frontswap_failed_puts);
266 debugfs_create_u64("invalidates", S_IRUGO,
267 root, &frontswap_invalidates);
272 module_init(init_frontswap);