2 * Xen implementation for transcendent memory (tmem)
4 * Copyright (C) 2009-2011 Oracle Corp. All rights reserved.
5 * Author: Dan Magenheimer
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/init.h>
11 #include <linux/pagemap.h>
12 #include <linux/cleancache.h>
14 /* temporary ifdef until include/linux/frontswap.h is upstream */
15 #ifdef CONFIG_FRONTSWAP
16 #include <linux/frontswap.h>
20 #include <xen/interface/xen.h>
21 #include <asm/xen/hypercall.h>
22 #include <asm/xen/page.h>
23 #include <asm/xen/hypervisor.h>
25 #define TMEM_CONTROL 0
26 #define TMEM_NEW_POOL 1
27 #define TMEM_DESTROY_POOL 2
28 #define TMEM_NEW_PAGE 3
29 #define TMEM_PUT_PAGE 4
30 #define TMEM_GET_PAGE 5
31 #define TMEM_FLUSH_PAGE 6
32 #define TMEM_FLUSH_OBJECT 7
37 /* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */
38 #define TMEM_POOL_PERSIST 1
39 #define TMEM_POOL_SHARED 2
40 #define TMEM_POOL_PAGESIZE_SHIFT 4
41 #define TMEM_VERSION_SHIFT 24
44 struct tmem_pool_uuid
{
53 #define TMEM_POOL_PRIVATE_UUID { 0, 0 }
55 /* flags for tmem_ops.new_pool */
56 #define TMEM_POOL_PERSIST 1
57 #define TMEM_POOL_SHARED 2
59 /* xen tmem foundation ops/hypercalls */
61 static inline int xen_tmem_op(u32 tmem_cmd
, u32 tmem_pool
, struct tmem_oid oid
,
62 u32 index
, unsigned long gmfn
, u32 tmem_offset
, u32 pfn_offset
, u32 len
)
68 op
.pool_id
= tmem_pool
;
69 op
.u
.gen
.oid
[0] = oid
.oid
[0];
70 op
.u
.gen
.oid
[1] = oid
.oid
[1];
71 op
.u
.gen
.oid
[2] = oid
.oid
[2];
72 op
.u
.gen
.index
= index
;
73 op
.u
.gen
.tmem_offset
= tmem_offset
;
74 op
.u
.gen
.pfn_offset
= pfn_offset
;
76 set_xen_guest_handle(op
.u
.gen
.gmfn
, (void *)gmfn
);
77 rc
= HYPERVISOR_tmem_op(&op
);
81 static int xen_tmem_new_pool(struct tmem_pool_uuid uuid
,
82 u32 flags
, unsigned long pagesize
)
85 int rc
= 0, pageshift
;
87 for (pageshift
= 0; pagesize
!= 1; pageshift
++)
89 flags
|= (pageshift
- 12) << TMEM_POOL_PAGESIZE_SHIFT
;
90 flags
|= TMEM_SPEC_VERSION
<< TMEM_VERSION_SHIFT
;
91 op
.cmd
= TMEM_NEW_POOL
;
92 op
.u
.new.uuid
[0] = uuid
.uuid_lo
;
93 op
.u
.new.uuid
[1] = uuid
.uuid_hi
;
94 op
.u
.new.flags
= flags
;
95 rc
= HYPERVISOR_tmem_op(&op
);
99 /* xen generic tmem ops */
101 static int xen_tmem_put_page(u32 pool_id
, struct tmem_oid oid
,
102 u32 index
, unsigned long pfn
)
104 unsigned long gmfn
= xen_pv_domain() ? pfn_to_mfn(pfn
) : pfn
;
106 return xen_tmem_op(TMEM_PUT_PAGE
, pool_id
, oid
, index
,
110 static int xen_tmem_get_page(u32 pool_id
, struct tmem_oid oid
,
111 u32 index
, unsigned long pfn
)
113 unsigned long gmfn
= xen_pv_domain() ? pfn_to_mfn(pfn
) : pfn
;
115 return xen_tmem_op(TMEM_GET_PAGE
, pool_id
, oid
, index
,
119 static int xen_tmem_flush_page(u32 pool_id
, struct tmem_oid oid
, u32 index
)
121 return xen_tmem_op(TMEM_FLUSH_PAGE
, pool_id
, oid
, index
,
125 static int xen_tmem_flush_object(u32 pool_id
, struct tmem_oid oid
)
127 return xen_tmem_op(TMEM_FLUSH_OBJECT
, pool_id
, oid
, 0, 0, 0, 0, 0);
130 bool __read_mostly tmem_enabled
= false;
132 static int __init
enable_tmem(char *s
)
137 __setup("tmem", enable_tmem
);
139 #ifdef CONFIG_CLEANCACHE
140 static int xen_tmem_destroy_pool(u32 pool_id
)
142 struct tmem_oid oid
= { { 0 } };
144 return xen_tmem_op(TMEM_DESTROY_POOL
, pool_id
, oid
, 0, 0, 0, 0, 0);
149 static void tmem_cleancache_put_page(int pool
, struct cleancache_filekey key
,
150 pgoff_t index
, struct page
*page
)
152 u32 ind
= (u32
) index
;
153 struct tmem_oid oid
= *(struct tmem_oid
*)&key
;
154 unsigned long pfn
= page_to_pfn(page
);
160 mb(); /* ensure page is quiescent; tmem may address it with an alias */
161 (void)xen_tmem_put_page((u32
)pool
, oid
, ind
, pfn
);
164 static int tmem_cleancache_get_page(int pool
, struct cleancache_filekey key
,
165 pgoff_t index
, struct page
*page
)
167 u32 ind
= (u32
) index
;
168 struct tmem_oid oid
= *(struct tmem_oid
*)&key
;
169 unsigned long pfn
= page_to_pfn(page
);
172 /* translate return values to linux semantics */
177 ret
= xen_tmem_get_page((u32
)pool
, oid
, ind
, pfn
);
184 static void tmem_cleancache_flush_page(int pool
, struct cleancache_filekey key
,
187 u32 ind
= (u32
) index
;
188 struct tmem_oid oid
= *(struct tmem_oid
*)&key
;
194 (void)xen_tmem_flush_page((u32
)pool
, oid
, ind
);
197 static void tmem_cleancache_flush_inode(int pool
, struct cleancache_filekey key
)
199 struct tmem_oid oid
= *(struct tmem_oid
*)&key
;
203 (void)xen_tmem_flush_object((u32
)pool
, oid
);
206 static void tmem_cleancache_flush_fs(int pool
)
210 (void)xen_tmem_destroy_pool((u32
)pool
);
213 static int tmem_cleancache_init_fs(size_t pagesize
)
215 struct tmem_pool_uuid uuid_private
= TMEM_POOL_PRIVATE_UUID
;
217 return xen_tmem_new_pool(uuid_private
, 0, pagesize
);
220 static int tmem_cleancache_init_shared_fs(char *uuid
, size_t pagesize
)
222 struct tmem_pool_uuid shared_uuid
;
224 shared_uuid
.uuid_lo
= *(u64
*)uuid
;
225 shared_uuid
.uuid_hi
= *(u64
*)(&uuid
[8]);
226 return xen_tmem_new_pool(shared_uuid
, TMEM_POOL_SHARED
, pagesize
);
229 static bool __initdata use_cleancache
= true;
231 static int __init
no_cleancache(char *s
)
233 use_cleancache
= false;
236 __setup("nocleancache", no_cleancache
);
238 static struct cleancache_ops __initdata tmem_cleancache_ops
= {
239 .put_page
= tmem_cleancache_put_page
,
240 .get_page
= tmem_cleancache_get_page
,
241 .invalidate_page
= tmem_cleancache_flush_page
,
242 .invalidate_inode
= tmem_cleancache_flush_inode
,
243 .invalidate_fs
= tmem_cleancache_flush_fs
,
244 .init_shared_fs
= tmem_cleancache_init_shared_fs
,
245 .init_fs
= tmem_cleancache_init_fs
249 #ifdef CONFIG_FRONTSWAP
250 /* frontswap tmem operations */
252 /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
253 static int tmem_frontswap_poolid
;
256 * Swizzling increases objects per swaptype, increasing tmem concurrency
257 * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS
260 #define SWIZ_MASK ((1 << SWIZ_BITS) - 1)
261 #define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK))
262 #define iswiz(_ind) (_ind >> SWIZ_BITS)
264 static inline struct tmem_oid
oswiz(unsigned type
, u32 ind
)
266 struct tmem_oid oid
= { .oid
= { 0 } };
267 oid
.oid
[0] = _oswiz(type
, ind
);
271 /* returns 0 if the page was successfully put into frontswap, -1 if not */
272 static int tmem_frontswap_store(unsigned type
, pgoff_t offset
,
275 u64 ind64
= (u64
)offset
;
276 u32 ind
= (u32
)offset
;
277 unsigned long pfn
= page_to_pfn(page
);
278 int pool
= tmem_frontswap_poolid
;
285 mb(); /* ensure page is quiescent; tmem may address it with an alias */
286 ret
= xen_tmem_put_page(pool
, oswiz(type
, ind
), iswiz(ind
), pfn
);
287 /* translate Xen tmem return values to linux semantics */
295 * returns 0 if the page was successfully gotten from frontswap, -1 if
296 * was not present (should never happen!)
298 static int tmem_frontswap_load(unsigned type
, pgoff_t offset
,
301 u64 ind64
= (u64
)offset
;
302 u32 ind
= (u32
)offset
;
303 unsigned long pfn
= page_to_pfn(page
);
304 int pool
= tmem_frontswap_poolid
;
311 ret
= xen_tmem_get_page(pool
, oswiz(type
, ind
), iswiz(ind
), pfn
);
312 /* translate Xen tmem return values to linux semantics */
319 /* flush a single page from frontswap */
320 static void tmem_frontswap_flush_page(unsigned type
, pgoff_t offset
)
322 u64 ind64
= (u64
)offset
;
323 u32 ind
= (u32
)offset
;
324 int pool
= tmem_frontswap_poolid
;
330 (void) xen_tmem_flush_page(pool
, oswiz(type
, ind
), iswiz(ind
));
333 /* flush all pages from the passed swaptype */
334 static void tmem_frontswap_flush_area(unsigned type
)
336 int pool
= tmem_frontswap_poolid
;
341 for (ind
= SWIZ_MASK
; ind
>= 0; ind
--)
342 (void)xen_tmem_flush_object(pool
, oswiz(type
, ind
));
345 static void tmem_frontswap_init(unsigned ignored
)
347 struct tmem_pool_uuid
private = TMEM_POOL_PRIVATE_UUID
;
349 /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
350 if (tmem_frontswap_poolid
< 0)
351 tmem_frontswap_poolid
=
352 xen_tmem_new_pool(private, TMEM_POOL_PERSIST
, PAGE_SIZE
);
355 static bool __initdata use_frontswap
= true;
357 static int __init
no_frontswap(char *s
)
359 use_frontswap
= false;
362 __setup("nofrontswap", no_frontswap
);
364 static struct frontswap_ops __initdata tmem_frontswap_ops
= {
365 .store
= tmem_frontswap_store
,
366 .load
= tmem_frontswap_load
,
367 .invalidate_page
= tmem_frontswap_flush_page
,
368 .invalidate_area
= tmem_frontswap_flush_area
,
369 .init
= tmem_frontswap_init
373 static int __init
xen_tmem_init(void)
377 #ifdef CONFIG_FRONTSWAP
378 if (tmem_enabled
&& use_frontswap
) {
380 struct frontswap_ops old_ops
=
381 frontswap_register_ops(&tmem_frontswap_ops
);
383 tmem_frontswap_poolid
= -1;
384 if (old_ops
.init
!= NULL
)
385 s
= " (WARNING: frontswap_ops overridden)";
386 printk(KERN_INFO
"frontswap enabled, RAM provided by "
387 "Xen Transcendent Memory\n");
390 #ifdef CONFIG_CLEANCACHE
391 BUG_ON(sizeof(struct cleancache_filekey
) != sizeof(struct tmem_oid
));
392 if (tmem_enabled
&& use_cleancache
) {
394 struct cleancache_ops old_ops
=
395 cleancache_register_ops(&tmem_cleancache_ops
);
396 if (old_ops
.init_fs
!= NULL
)
397 s
= " (WARNING: cleancache_ops overridden)";
398 printk(KERN_INFO
"cleancache enabled, RAM provided by "
399 "Xen Transcendent Memory%s\n", s
);
405 module_init(xen_tmem_init
)