Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / mm / frontswap.c
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-2010 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
14 #include <linux/mm.h>
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>
26
27 /*
28  * frontswap_ops is set by frontswap_register_ops to contain the pointers
29  * to the frontswap "backend" implementation functions.
30  */
31 static struct frontswap_ops frontswap_ops __read_mostly;
32
33 /*
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.
37  */
38 int frontswap_enabled __read_mostly;
39 EXPORT_SYMBOL(frontswap_enabled);
40
41 /*
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.
45  */
46 static u64 frontswap_gets;
47 static u64 frontswap_succ_puts;
48 static u64 frontswap_failed_puts;
49 static u64 frontswap_invalidates;
50
51 /*
52  * Register operations for frontswap, returning previous thus allowing
53  * detection of multiple backends and possible nesting
54  */
55 struct frontswap_ops frontswap_register_ops(struct frontswap_ops *ops)
56 {
57         struct frontswap_ops old = frontswap_ops;
58
59         frontswap_ops = *ops;
60         frontswap_enabled = 1;
61         return old;
62 }
63 EXPORT_SYMBOL(frontswap_register_ops);
64
65 /* Called when a swap device is swapon'd */
66 void __frontswap_init(unsigned type)
67 {
68         struct swap_info_struct *sis = swap_info[type];
69
70         BUG_ON(sis == NULL);
71         if (sis->frontswap_map == NULL)
72                 return;
73         if (frontswap_enabled)
74                 (*frontswap_ops.init)(type);
75 }
76 EXPORT_SYMBOL(__frontswap_init);
77
78 /*
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
84  */
85 int __frontswap_put_page(struct page *page)
86 {
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);
92
93         BUG_ON(!PageLocked(page));
94         BUG_ON(sis == NULL);
95         if (frontswap_test(sis, offset))
96                 dup = 1;
97         ret = (*frontswap_ops.put_page)(type, offset, page);
98         if (ret == 0) {
99                 frontswap_set(sis, offset);
100                 frontswap_succ_puts++;
101                 if (!dup)
102                         atomic_inc(&sis->frontswap_pages);
103         } else if (dup) {
104                 /*
105                   failed dup always results in automatic invalidate of
106                   the (older) page from frontswap
107                  */
108                 frontswap_clear(sis, offset);
109                 atomic_dec(&sis->frontswap_pages);
110                 frontswap_failed_puts++;
111         } else
112                 frontswap_failed_puts++;
113         return ret;
114 }
115 EXPORT_SYMBOL(__frontswap_put_page);
116
117 /*
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
121  */
122 int __frontswap_get_page(struct page *page)
123 {
124         int ret = -1;
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);
129
130         BUG_ON(!PageLocked(page));
131         BUG_ON(sis == NULL);
132         if (frontswap_test(sis, offset))
133                 ret = (*frontswap_ops.get_page)(type, offset, page);
134         if (ret == 0)
135                 frontswap_gets++;
136         return ret;
137 }
138 EXPORT_SYMBOL(__frontswap_get_page);
139
140 /*
141  * Invalidate any data from frontswap associated with the specified swaptype
142  * and offset so that a subsequent "get" will fail.
143  */
144 void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
145 {
146         struct swap_info_struct *sis = swap_info[type];
147
148         BUG_ON(sis == NULL);
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++;
154         }
155 }
156 EXPORT_SYMBOL(__frontswap_invalidate_page);
157
158 /*
159  * Invalidate all data from frontswap associated with all offsets for the
160  * specified swaptype.
161  */
162 void __frontswap_invalidate_area(unsigned type)
163 {
164         struct swap_info_struct *sis = swap_info[type];
165
166         BUG_ON(sis == NULL);
167         if (sis->frontswap_map == NULL)
168                 return;
169         (*frontswap_ops.invalidate_area)(type);
170         atomic_set(&sis->frontswap_pages, 0);
171         memset(sis->frontswap_map, 0, sis->max / sizeof(long));
172 }
173 EXPORT_SYMBOL(__frontswap_invalidate_area);
174
175 /*
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.
182  */
183 void frontswap_shrink(unsigned long target_pages)
184 {
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;
189         int type;
190         bool locked = false;
191
192         /*
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
196          */
197         spin_lock(&swap_lock);
198         locked = true;
199         total_pages = 0;
200         for (type = swap_list.head; type >= 0; type = si->next) {
201                 si = swap_info[type];
202                 total_pages += atomic_read(&si->frontswap_pages);
203         }
204         if (total_pages <= target_pages)
205                 goto out;
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;
212                 else {
213                         pages = si_frontswap_pages;
214                         pages_to_unuse = 0; /* unuse all */
215                 }
216                 /* ensure there is enough RAM to fetch pages from frontswap */
217                 if (security_vm_enough_memory_mm(current->mm, pages))
218                         continue;
219                 vm_unacct_memory(pages);
220                 break;
221         }
222         if (type < 0)
223                 goto out;
224         locked = false;
225         spin_unlock(&swap_lock);
226         try_to_unuse(type, true, pages_to_unuse);
227 out:
228         if (locked)
229                 spin_unlock(&swap_lock);
230         return;
231 }
232 EXPORT_SYMBOL(frontswap_shrink);
233
234 /*
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.
238  */
239 unsigned long frontswap_curr_pages(void)
240 {
241         int type;
242         unsigned long totalpages = 0;
243         struct swap_info_struct *si = NULL;
244
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);
249         }
250         spin_unlock(&swap_lock);
251         return totalpages;
252 }
253 EXPORT_SYMBOL(frontswap_curr_pages);
254
255 static int __init init_frontswap(void)
256 {
257         int err = 0;
258
259 #ifdef CONFIG_DEBUG_FS
260         struct dentry *root = debugfs_create_dir("frontswap", NULL);
261         if (root == NULL)
262                 return -ENXIO;
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);
268 #endif
269         return err;
270 }
271
272 module_init(init_frontswap);