blob: c2ee1887806a44e02675dea0c49982210ede4304 [file] [log] [blame]
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -06001/*
2 * Xen implementation for transcendent memory (tmem)
3 *
Dan Magenheimerafec6e02011-06-17 15:06:20 -06004 * Copyright (C) 2009-2011 Oracle Corp. All rights reserved.
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -06005 * Author: Dan Magenheimer
6 */
7
Dan Magenheimer10a7a0772013-04-30 15:27:00 -07008#include <linux/module.h>
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -06009#include <linux/kernel.h>
10#include <linux/types.h>
11#include <linux/init.h>
12#include <linux/pagemap.h>
13#include <linux/cleancache.h>
Dan Magenheimerafec6e02011-06-17 15:06:20 -060014#include <linux/frontswap.h>
Dan Magenheimerafec6e02011-06-17 15:06:20 -060015
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -060016#include <xen/xen.h>
17#include <xen/interface/xen.h>
18#include <asm/xen/hypercall.h>
19#include <asm/xen/page.h>
20#include <asm/xen/hypervisor.h>
Konrad Rzeszutek Wilkb8b0f552012-08-21 14:49:34 -040021#include <xen/tmem.h>
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -060022
Konrad Rzeszutek Wilk0cb401d2013-05-08 15:50:59 -040023#ifndef CONFIG_XEN_TMEM_MODULE
24bool __read_mostly tmem_enabled = false;
25
26static int __init enable_tmem(char *s)
27{
28 tmem_enabled = true;
29 return 1;
30}
31__setup("tmem", enable_tmem);
32#endif
33
34#ifdef CONFIG_CLEANCACHE
35static bool disable_cleancache __read_mostly;
36static bool disable_selfballooning __read_mostly;
37#ifdef CONFIG_XEN_TMEM_MODULE
38module_param(disable_cleancache, bool, S_IRUGO);
39module_param(disable_selfballooning, bool, S_IRUGO);
40#else
41static int __init no_cleancache(char *s)
42{
43 disable_cleancache = true;
44 return 1;
45}
46__setup("nocleancache", no_cleancache);
47#endif
48#endif /* CONFIG_CLEANCACHE */
49
50#ifdef CONFIG_FRONTSWAP
51static bool disable_frontswap __read_mostly;
Konrad Rzeszutek Wilk0cb401d2013-05-08 15:50:59 -040052#ifdef CONFIG_XEN_TMEM_MODULE
53module_param(disable_frontswap, bool, S_IRUGO);
Konrad Rzeszutek Wilk0cb401d2013-05-08 15:50:59 -040054#else
55static int __init no_frontswap(char *s)
56{
57 disable_frontswap = true;
58 return 1;
59}
60__setup("nofrontswap", no_frontswap);
61#endif
Konrad Rzeszutek Wilke8f9cb02013-05-08 15:58:06 -040062#endif /* CONFIG_FRONTSWAP */
63
64#ifdef CONFIG_FRONTSWAP
65static bool disable_frontswap_selfshrinking __read_mostly;
66#ifdef CONFIG_XEN_TMEM_MODULE
67module_param(disable_frontswap_selfshrinking, bool, S_IRUGO);
68#else
Konrad Rzeszutek Wilk0cb401d2013-05-08 15:50:59 -040069#define disable_frontswap_selfshrinking 1
Konrad Rzeszutek Wilke8f9cb02013-05-08 15:58:06 -040070#endif
Konrad Rzeszutek Wilk0cb401d2013-05-08 15:50:59 -040071#endif /* CONFIG_FRONTSWAP */
72
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -060073#define TMEM_CONTROL 0
74#define TMEM_NEW_POOL 1
75#define TMEM_DESTROY_POOL 2
76#define TMEM_NEW_PAGE 3
77#define TMEM_PUT_PAGE 4
78#define TMEM_GET_PAGE 5
79#define TMEM_FLUSH_PAGE 6
80#define TMEM_FLUSH_OBJECT 7
81#define TMEM_READ 8
82#define TMEM_WRITE 9
83#define TMEM_XCHG 10
84
85/* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */
86#define TMEM_POOL_PERSIST 1
87#define TMEM_POOL_SHARED 2
88#define TMEM_POOL_PAGESIZE_SHIFT 4
89#define TMEM_VERSION_SHIFT 24
90
91
92struct tmem_pool_uuid {
93 u64 uuid_lo;
94 u64 uuid_hi;
95};
96
97struct tmem_oid {
98 u64 oid[3];
99};
100
101#define TMEM_POOL_PRIVATE_UUID { 0, 0 }
102
103/* flags for tmem_ops.new_pool */
104#define TMEM_POOL_PERSIST 1
105#define TMEM_POOL_SHARED 2
106
107/* xen tmem foundation ops/hypercalls */
108
109static inline int xen_tmem_op(u32 tmem_cmd, u32 tmem_pool, struct tmem_oid oid,
110 u32 index, unsigned long gmfn, u32 tmem_offset, u32 pfn_offset, u32 len)
111{
112 struct tmem_op op;
113 int rc = 0;
114
115 op.cmd = tmem_cmd;
116 op.pool_id = tmem_pool;
117 op.u.gen.oid[0] = oid.oid[0];
118 op.u.gen.oid[1] = oid.oid[1];
119 op.u.gen.oid[2] = oid.oid[2];
120 op.u.gen.index = index;
121 op.u.gen.tmem_offset = tmem_offset;
122 op.u.gen.pfn_offset = pfn_offset;
123 op.u.gen.len = len;
124 set_xen_guest_handle(op.u.gen.gmfn, (void *)gmfn);
125 rc = HYPERVISOR_tmem_op(&op);
126 return rc;
127}
128
129static int xen_tmem_new_pool(struct tmem_pool_uuid uuid,
130 u32 flags, unsigned long pagesize)
131{
132 struct tmem_op op;
133 int rc = 0, pageshift;
134
135 for (pageshift = 0; pagesize != 1; pageshift++)
136 pagesize >>= 1;
137 flags |= (pageshift - 12) << TMEM_POOL_PAGESIZE_SHIFT;
138 flags |= TMEM_SPEC_VERSION << TMEM_VERSION_SHIFT;
139 op.cmd = TMEM_NEW_POOL;
140 op.u.new.uuid[0] = uuid.uuid_lo;
141 op.u.new.uuid[1] = uuid.uuid_hi;
142 op.u.new.flags = flags;
143 rc = HYPERVISOR_tmem_op(&op);
144 return rc;
145}
146
147/* xen generic tmem ops */
148
149static int xen_tmem_put_page(u32 pool_id, struct tmem_oid oid,
150 u32 index, unsigned long pfn)
151{
152 unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
153
154 return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index,
155 gmfn, 0, 0, 0);
156}
157
158static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid,
159 u32 index, unsigned long pfn)
160{
161 unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
162
163 return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index,
164 gmfn, 0, 0, 0);
165}
166
167static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index)
168{
169 return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index,
170 0, 0, 0, 0);
171}
172
173static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid)
174{
175 return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0);
176}
177
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600178
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600179#ifdef CONFIG_CLEANCACHE
180static int xen_tmem_destroy_pool(u32 pool_id)
181{
182 struct tmem_oid oid = { { 0 } };
183
184 return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0);
185}
186
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600187/* cleancache ops */
188
189static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key,
190 pgoff_t index, struct page *page)
191{
192 u32 ind = (u32) index;
193 struct tmem_oid oid = *(struct tmem_oid *)&key;
194 unsigned long pfn = page_to_pfn(page);
195
196 if (pool < 0)
197 return;
198 if (ind != index)
199 return;
200 mb(); /* ensure page is quiescent; tmem may address it with an alias */
201 (void)xen_tmem_put_page((u32)pool, oid, ind, pfn);
202}
203
204static int tmem_cleancache_get_page(int pool, struct cleancache_filekey key,
205 pgoff_t index, struct page *page)
206{
207 u32 ind = (u32) index;
208 struct tmem_oid oid = *(struct tmem_oid *)&key;
209 unsigned long pfn = page_to_pfn(page);
210 int ret;
211
212 /* translate return values to linux semantics */
213 if (pool < 0)
214 return -1;
215 if (ind != index)
216 return -1;
217 ret = xen_tmem_get_page((u32)pool, oid, ind, pfn);
218 if (ret == 1)
219 return 0;
220 else
221 return -1;
222}
223
224static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key,
225 pgoff_t index)
226{
227 u32 ind = (u32) index;
228 struct tmem_oid oid = *(struct tmem_oid *)&key;
229
230 if (pool < 0)
231 return;
232 if (ind != index)
233 return;
234 (void)xen_tmem_flush_page((u32)pool, oid, ind);
235}
236
237static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key)
238{
239 struct tmem_oid oid = *(struct tmem_oid *)&key;
240
241 if (pool < 0)
242 return;
243 (void)xen_tmem_flush_object((u32)pool, oid);
244}
245
246static void tmem_cleancache_flush_fs(int pool)
247{
248 if (pool < 0)
249 return;
250 (void)xen_tmem_destroy_pool((u32)pool);
251}
252
253static int tmem_cleancache_init_fs(size_t pagesize)
254{
255 struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID;
256
257 return xen_tmem_new_pool(uuid_private, 0, pagesize);
258}
259
260static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize)
261{
262 struct tmem_pool_uuid shared_uuid;
263
264 shared_uuid.uuid_lo = *(u64 *)uuid;
265 shared_uuid.uuid_hi = *(u64 *)(&uuid[8]);
266 return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize);
267}
268
Konrad Rzeszutek Wilk833f8662013-04-30 15:26:57 -0700269static struct cleancache_ops tmem_cleancache_ops = {
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600270 .put_page = tmem_cleancache_put_page,
271 .get_page = tmem_cleancache_get_page,
Dan Magenheimer91c6cc92012-01-12 14:03:25 -0500272 .invalidate_page = tmem_cleancache_flush_page,
273 .invalidate_inode = tmem_cleancache_flush_inode,
274 .invalidate_fs = tmem_cleancache_flush_fs,
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600275 .init_shared_fs = tmem_cleancache_init_shared_fs,
276 .init_fs = tmem_cleancache_init_fs
277};
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600278#endif
279
280#ifdef CONFIG_FRONTSWAP
281/* frontswap tmem operations */
282
283/* a single tmem poolid is used for all frontswap "types" (swapfiles) */
284static int tmem_frontswap_poolid;
285
286/*
287 * Swizzling increases objects per swaptype, increasing tmem concurrency
288 * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS
289 */
290#define SWIZ_BITS 4
291#define SWIZ_MASK ((1 << SWIZ_BITS) - 1)
292#define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK))
293#define iswiz(_ind) (_ind >> SWIZ_BITS)
294
295static inline struct tmem_oid oswiz(unsigned type, u32 ind)
296{
297 struct tmem_oid oid = { .oid = { 0 } };
298 oid.oid[0] = _oswiz(type, ind);
299 return oid;
300}
301
302/* returns 0 if the page was successfully put into frontswap, -1 if not */
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400303static int tmem_frontswap_store(unsigned type, pgoff_t offset,
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600304 struct page *page)
305{
306 u64 ind64 = (u64)offset;
307 u32 ind = (u32)offset;
308 unsigned long pfn = page_to_pfn(page);
309 int pool = tmem_frontswap_poolid;
310 int ret;
311
312 if (pool < 0)
313 return -1;
314 if (ind64 != ind)
315 return -1;
316 mb(); /* ensure page is quiescent; tmem may address it with an alias */
317 ret = xen_tmem_put_page(pool, oswiz(type, ind), iswiz(ind), pfn);
318 /* translate Xen tmem return values to linux semantics */
319 if (ret == 1)
320 return 0;
321 else
322 return -1;
323}
324
325/*
326 * returns 0 if the page was successfully gotten from frontswap, -1 if
327 * was not present (should never happen!)
328 */
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400329static int tmem_frontswap_load(unsigned type, pgoff_t offset,
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600330 struct page *page)
331{
332 u64 ind64 = (u64)offset;
333 u32 ind = (u32)offset;
334 unsigned long pfn = page_to_pfn(page);
335 int pool = tmem_frontswap_poolid;
336 int ret;
337
338 if (pool < 0)
339 return -1;
340 if (ind64 != ind)
341 return -1;
342 ret = xen_tmem_get_page(pool, oswiz(type, ind), iswiz(ind), pfn);
343 /* translate Xen tmem return values to linux semantics */
344 if (ret == 1)
345 return 0;
346 else
347 return -1;
348}
349
350/* flush a single page from frontswap */
351static void tmem_frontswap_flush_page(unsigned type, pgoff_t offset)
352{
353 u64 ind64 = (u64)offset;
354 u32 ind = (u32)offset;
355 int pool = tmem_frontswap_poolid;
356
357 if (pool < 0)
358 return;
359 if (ind64 != ind)
360 return;
361 (void) xen_tmem_flush_page(pool, oswiz(type, ind), iswiz(ind));
362}
363
364/* flush all pages from the passed swaptype */
365static void tmem_frontswap_flush_area(unsigned type)
366{
367 int pool = tmem_frontswap_poolid;
368 int ind;
369
370 if (pool < 0)
371 return;
372 for (ind = SWIZ_MASK; ind >= 0; ind--)
373 (void)xen_tmem_flush_object(pool, oswiz(type, ind));
374}
375
376static void tmem_frontswap_init(unsigned ignored)
377{
378 struct tmem_pool_uuid private = TMEM_POOL_PRIVATE_UUID;
379
380 /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
381 if (tmem_frontswap_poolid < 0)
382 tmem_frontswap_poolid =
383 xen_tmem_new_pool(private, TMEM_POOL_PERSIST, PAGE_SIZE);
384}
385
Konrad Rzeszutek Wilk1e01c962013-04-30 15:26:51 -0700386static struct frontswap_ops tmem_frontswap_ops = {
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400387 .store = tmem_frontswap_store,
388 .load = tmem_frontswap_load,
Dan Magenheimer91c6cc92012-01-12 14:03:25 -0500389 .invalidate_page = tmem_frontswap_flush_page,
390 .invalidate_area = tmem_frontswap_flush_area,
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600391 .init = tmem_frontswap_init
392};
393#endif
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600394
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700395static int xen_tmem_init(void)
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600396{
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600397 if (!xen_domain())
398 return 0;
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600399#ifdef CONFIG_FRONTSWAP
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700400 if (tmem_enabled && !disable_frontswap) {
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600401 char *s = "";
Konrad Rzeszutek Wilk1e01c962013-04-30 15:26:51 -0700402 struct frontswap_ops *old_ops =
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600403 frontswap_register_ops(&tmem_frontswap_ops);
404
405 tmem_frontswap_poolid = -1;
Konrad Rzeszutek Wilkf42158f2013-04-30 15:27:01 -0700406 if (IS_ERR(old_ops) || old_ops) {
407 if (IS_ERR(old_ops))
408 return PTR_ERR(old_ops);
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600409 s = " (WARNING: frontswap_ops overridden)";
Konrad Rzeszutek Wilkf42158f2013-04-30 15:27:01 -0700410 }
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600411 printk(KERN_INFO "frontswap enabled, RAM provided by "
Konrad Rzeszutek Wilk22230c12013-02-01 14:10:44 -0500412 "Xen Transcendent Memory%s\n", s);
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600413 }
414#endif
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600415#ifdef CONFIG_CLEANCACHE
416 BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700417 if (tmem_enabled && !disable_cleancache) {
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600418 char *s = "";
Konrad Rzeszutek Wilk833f8662013-04-30 15:26:57 -0700419 struct cleancache_ops *old_ops =
Dan Magenheimerafec6e02011-06-17 15:06:20 -0600420 cleancache_register_ops(&tmem_cleancache_ops);
Konrad Rzeszutek Wilk833f8662013-04-30 15:26:57 -0700421 if (old_ops)
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600422 s = " (WARNING: cleancache_ops overridden)";
423 printk(KERN_INFO "cleancache enabled, RAM provided by "
424 "Xen Transcendent Memory%s\n", s);
425 }
426#endif
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700427#ifdef CONFIG_XEN_SELFBALLOONING
428 xen_selfballoon_init(!disable_selfballooning,
429 !disable_frontswap_selfshrinking);
430#endif
Dan Magenheimer5bc20fc2011-05-26 10:02:21 -0600431 return 0;
432}
433
434module_init(xen_tmem_init)
Dan Magenheimer10a7a0772013-04-30 15:27:00 -0700435MODULE_LICENSE("GPL");
436MODULE_AUTHOR("Dan Magenheimer <dan.magenheimer@oracle.com>");
437MODULE_DESCRIPTION("Shim to Xen transcendent memory");