]>
Commit | Line | Data |
---|---|---|
5bc20fc5 DM |
1 | /* |
2 | * Xen implementation for transcendent memory (tmem) | |
3 | * | |
afec6e04 | 4 | * Copyright (C) 2009-2011 Oracle Corp. All rights reserved. |
5bc20fc5 DM |
5 | * Author: Dan Magenheimer |
6 | */ | |
7 | ||
10a7a077 | 8 | #include <linux/module.h> |
5bc20fc5 DM |
9 | #include <linux/kernel.h> |
10 | #include <linux/types.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/pagemap.h> | |
13 | #include <linux/cleancache.h> | |
afec6e04 | 14 | #include <linux/frontswap.h> |
afec6e04 | 15 | |
5bc20fc5 DM |
16 | #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> | |
b8b0f559 | 21 | #include <xen/tmem.h> |
5bc20fc5 | 22 | |
0cb401d4 KRW |
23 | #ifndef CONFIG_XEN_TMEM_MODULE |
24 | bool __read_mostly tmem_enabled = false; | |
25 | ||
26 | static 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 | |
35 | static bool disable_cleancache __read_mostly; | |
36 | static bool disable_selfballooning __read_mostly; | |
37 | #ifdef CONFIG_XEN_TMEM_MODULE | |
38 | module_param(disable_cleancache, bool, S_IRUGO); | |
39 | module_param(disable_selfballooning, bool, S_IRUGO); | |
40 | #else | |
41 | static 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 | |
51 | static bool disable_frontswap __read_mostly; | |
0cb401d4 KRW |
52 | #ifdef CONFIG_XEN_TMEM_MODULE |
53 | module_param(disable_frontswap, bool, S_IRUGO); | |
0cb401d4 KRW |
54 | #else |
55 | static int __init no_frontswap(char *s) | |
56 | { | |
57 | disable_frontswap = true; | |
58 | return 1; | |
59 | } | |
60 | __setup("nofrontswap", no_frontswap); | |
61 | #endif | |
e8f9cb03 KRW |
62 | #endif /* CONFIG_FRONTSWAP */ |
63 | ||
23972c63 | 64 | #ifdef CONFIG_XEN_SELFBALLOONING |
e8f9cb03 KRW |
65 | static bool disable_frontswap_selfshrinking __read_mostly; |
66 | #ifdef CONFIG_XEN_TMEM_MODULE | |
67 | module_param(disable_frontswap_selfshrinking, bool, S_IRUGO); | |
e8f9cb03 | 68 | #endif |
23972c63 | 69 | #endif /* CONFIG_XEN_SELFBALLOONING */ |
0cb401d4 | 70 | |
5bc20fc5 DM |
71 | #define TMEM_CONTROL 0 |
72 | #define TMEM_NEW_POOL 1 | |
73 | #define TMEM_DESTROY_POOL 2 | |
74 | #define TMEM_NEW_PAGE 3 | |
75 | #define TMEM_PUT_PAGE 4 | |
76 | #define TMEM_GET_PAGE 5 | |
77 | #define TMEM_FLUSH_PAGE 6 | |
78 | #define TMEM_FLUSH_OBJECT 7 | |
79 | #define TMEM_READ 8 | |
80 | #define TMEM_WRITE 9 | |
81 | #define TMEM_XCHG 10 | |
82 | ||
83 | /* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */ | |
84 | #define TMEM_POOL_PERSIST 1 | |
85 | #define TMEM_POOL_SHARED 2 | |
86 | #define TMEM_POOL_PAGESIZE_SHIFT 4 | |
87 | #define TMEM_VERSION_SHIFT 24 | |
88 | ||
89 | ||
90 | struct tmem_pool_uuid { | |
91 | u64 uuid_lo; | |
92 | u64 uuid_hi; | |
93 | }; | |
94 | ||
95 | struct tmem_oid { | |
96 | u64 oid[3]; | |
97 | }; | |
98 | ||
99 | #define TMEM_POOL_PRIVATE_UUID { 0, 0 } | |
100 | ||
101 | /* flags for tmem_ops.new_pool */ | |
102 | #define TMEM_POOL_PERSIST 1 | |
103 | #define TMEM_POOL_SHARED 2 | |
104 | ||
105 | /* xen tmem foundation ops/hypercalls */ | |
106 | ||
107 | static inline int xen_tmem_op(u32 tmem_cmd, u32 tmem_pool, struct tmem_oid oid, | |
108 | u32 index, unsigned long gmfn, u32 tmem_offset, u32 pfn_offset, u32 len) | |
109 | { | |
110 | struct tmem_op op; | |
111 | int rc = 0; | |
112 | ||
113 | op.cmd = tmem_cmd; | |
114 | op.pool_id = tmem_pool; | |
115 | op.u.gen.oid[0] = oid.oid[0]; | |
116 | op.u.gen.oid[1] = oid.oid[1]; | |
117 | op.u.gen.oid[2] = oid.oid[2]; | |
118 | op.u.gen.index = index; | |
119 | op.u.gen.tmem_offset = tmem_offset; | |
120 | op.u.gen.pfn_offset = pfn_offset; | |
121 | op.u.gen.len = len; | |
122 | set_xen_guest_handle(op.u.gen.gmfn, (void *)gmfn); | |
123 | rc = HYPERVISOR_tmem_op(&op); | |
124 | return rc; | |
125 | } | |
126 | ||
127 | static int xen_tmem_new_pool(struct tmem_pool_uuid uuid, | |
128 | u32 flags, unsigned long pagesize) | |
129 | { | |
130 | struct tmem_op op; | |
131 | int rc = 0, pageshift; | |
132 | ||
133 | for (pageshift = 0; pagesize != 1; pageshift++) | |
134 | pagesize >>= 1; | |
135 | flags |= (pageshift - 12) << TMEM_POOL_PAGESIZE_SHIFT; | |
136 | flags |= TMEM_SPEC_VERSION << TMEM_VERSION_SHIFT; | |
137 | op.cmd = TMEM_NEW_POOL; | |
138 | op.u.new.uuid[0] = uuid.uuid_lo; | |
139 | op.u.new.uuid[1] = uuid.uuid_hi; | |
140 | op.u.new.flags = flags; | |
141 | rc = HYPERVISOR_tmem_op(&op); | |
142 | return rc; | |
143 | } | |
144 | ||
145 | /* xen generic tmem ops */ | |
146 | ||
147 | static int xen_tmem_put_page(u32 pool_id, struct tmem_oid oid, | |
148 | u32 index, unsigned long pfn) | |
149 | { | |
150 | unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn; | |
151 | ||
152 | return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index, | |
153 | gmfn, 0, 0, 0); | |
154 | } | |
155 | ||
156 | static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid, | |
157 | u32 index, unsigned long pfn) | |
158 | { | |
159 | unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn; | |
160 | ||
161 | return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index, | |
162 | gmfn, 0, 0, 0); | |
163 | } | |
164 | ||
165 | static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index) | |
166 | { | |
167 | return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index, | |
168 | 0, 0, 0, 0); | |
169 | } | |
170 | ||
171 | static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid) | |
172 | { | |
173 | return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0); | |
174 | } | |
175 | ||
5bc20fc5 | 176 | |
afec6e04 DM |
177 | #ifdef CONFIG_CLEANCACHE |
178 | static int xen_tmem_destroy_pool(u32 pool_id) | |
179 | { | |
180 | struct tmem_oid oid = { { 0 } }; | |
181 | ||
182 | return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0); | |
183 | } | |
184 | ||
5bc20fc5 DM |
185 | /* cleancache ops */ |
186 | ||
187 | static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key, | |
188 | pgoff_t index, struct page *page) | |
189 | { | |
190 | u32 ind = (u32) index; | |
191 | struct tmem_oid oid = *(struct tmem_oid *)&key; | |
192 | unsigned long pfn = page_to_pfn(page); | |
193 | ||
194 | if (pool < 0) | |
195 | return; | |
196 | if (ind != index) | |
197 | return; | |
198 | mb(); /* ensure page is quiescent; tmem may address it with an alias */ | |
199 | (void)xen_tmem_put_page((u32)pool, oid, ind, pfn); | |
200 | } | |
201 | ||
202 | static int tmem_cleancache_get_page(int pool, struct cleancache_filekey key, | |
203 | pgoff_t index, struct page *page) | |
204 | { | |
205 | u32 ind = (u32) index; | |
206 | struct tmem_oid oid = *(struct tmem_oid *)&key; | |
207 | unsigned long pfn = page_to_pfn(page); | |
208 | int ret; | |
209 | ||
210 | /* translate return values to linux semantics */ | |
211 | if (pool < 0) | |
212 | return -1; | |
213 | if (ind != index) | |
214 | return -1; | |
215 | ret = xen_tmem_get_page((u32)pool, oid, ind, pfn); | |
216 | if (ret == 1) | |
217 | return 0; | |
218 | else | |
219 | return -1; | |
220 | } | |
221 | ||
222 | static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key, | |
223 | pgoff_t index) | |
224 | { | |
225 | u32 ind = (u32) index; | |
226 | struct tmem_oid oid = *(struct tmem_oid *)&key; | |
227 | ||
228 | if (pool < 0) | |
229 | return; | |
230 | if (ind != index) | |
231 | return; | |
232 | (void)xen_tmem_flush_page((u32)pool, oid, ind); | |
233 | } | |
234 | ||
235 | static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key) | |
236 | { | |
237 | struct tmem_oid oid = *(struct tmem_oid *)&key; | |
238 | ||
239 | if (pool < 0) | |
240 | return; | |
241 | (void)xen_tmem_flush_object((u32)pool, oid); | |
242 | } | |
243 | ||
244 | static void tmem_cleancache_flush_fs(int pool) | |
245 | { | |
246 | if (pool < 0) | |
247 | return; | |
248 | (void)xen_tmem_destroy_pool((u32)pool); | |
249 | } | |
250 | ||
251 | static int tmem_cleancache_init_fs(size_t pagesize) | |
252 | { | |
253 | struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID; | |
254 | ||
255 | return xen_tmem_new_pool(uuid_private, 0, pagesize); | |
256 | } | |
257 | ||
258 | static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize) | |
259 | { | |
260 | struct tmem_pool_uuid shared_uuid; | |
261 | ||
262 | shared_uuid.uuid_lo = *(u64 *)uuid; | |
263 | shared_uuid.uuid_hi = *(u64 *)(&uuid[8]); | |
264 | return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize); | |
265 | } | |
266 | ||
833f8662 | 267 | static struct cleancache_ops tmem_cleancache_ops = { |
5bc20fc5 DM |
268 | .put_page = tmem_cleancache_put_page, |
269 | .get_page = tmem_cleancache_get_page, | |
91c6cc9b DM |
270 | .invalidate_page = tmem_cleancache_flush_page, |
271 | .invalidate_inode = tmem_cleancache_flush_inode, | |
272 | .invalidate_fs = tmem_cleancache_flush_fs, | |
5bc20fc5 DM |
273 | .init_shared_fs = tmem_cleancache_init_shared_fs, |
274 | .init_fs = tmem_cleancache_init_fs | |
275 | }; | |
afec6e04 | 276 | #endif |
5bc20fc5 | 277 | |
afec6e04 DM |
278 | #ifdef CONFIG_FRONTSWAP |
279 | /* frontswap tmem operations */ | |
280 | ||
281 | /* a single tmem poolid is used for all frontswap "types" (swapfiles) */ | |
282 | static int tmem_frontswap_poolid; | |
283 | ||
284 | /* | |
285 | * Swizzling increases objects per swaptype, increasing tmem concurrency | |
286 | * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS | |
287 | */ | |
288 | #define SWIZ_BITS 4 | |
289 | #define SWIZ_MASK ((1 << SWIZ_BITS) - 1) | |
290 | #define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK)) | |
291 | #define iswiz(_ind) (_ind >> SWIZ_BITS) | |
292 | ||
293 | static inline struct tmem_oid oswiz(unsigned type, u32 ind) | |
5bc20fc5 | 294 | { |
afec6e04 DM |
295 | struct tmem_oid oid = { .oid = { 0 } }; |
296 | oid.oid[0] = _oswiz(type, ind); | |
297 | return oid; | |
298 | } | |
5bc20fc5 | 299 | |
afec6e04 | 300 | /* returns 0 if the page was successfully put into frontswap, -1 if not */ |
165c8aed | 301 | static int tmem_frontswap_store(unsigned type, pgoff_t offset, |
afec6e04 DM |
302 | struct page *page) |
303 | { | |
304 | u64 ind64 = (u64)offset; | |
305 | u32 ind = (u32)offset; | |
306 | unsigned long pfn = page_to_pfn(page); | |
307 | int pool = tmem_frontswap_poolid; | |
308 | int ret; | |
309 | ||
310 | if (pool < 0) | |
311 | return -1; | |
312 | if (ind64 != ind) | |
313 | return -1; | |
314 | mb(); /* ensure page is quiescent; tmem may address it with an alias */ | |
315 | ret = xen_tmem_put_page(pool, oswiz(type, ind), iswiz(ind), pfn); | |
316 | /* translate Xen tmem return values to linux semantics */ | |
317 | if (ret == 1) | |
318 | return 0; | |
319 | else | |
320 | return -1; | |
321 | } | |
322 | ||
323 | /* | |
324 | * returns 0 if the page was successfully gotten from frontswap, -1 if | |
325 | * was not present (should never happen!) | |
326 | */ | |
165c8aed | 327 | static int tmem_frontswap_load(unsigned type, pgoff_t offset, |
afec6e04 DM |
328 | struct page *page) |
329 | { | |
330 | u64 ind64 = (u64)offset; | |
331 | u32 ind = (u32)offset; | |
332 | unsigned long pfn = page_to_pfn(page); | |
333 | int pool = tmem_frontswap_poolid; | |
334 | int ret; | |
335 | ||
336 | if (pool < 0) | |
337 | return -1; | |
338 | if (ind64 != ind) | |
339 | return -1; | |
340 | ret = xen_tmem_get_page(pool, oswiz(type, ind), iswiz(ind), pfn); | |
341 | /* translate Xen tmem return values to linux semantics */ | |
342 | if (ret == 1) | |
343 | return 0; | |
344 | else | |
345 | return -1; | |
346 | } | |
347 | ||
348 | /* flush a single page from frontswap */ | |
349 | static void tmem_frontswap_flush_page(unsigned type, pgoff_t offset) | |
350 | { | |
351 | u64 ind64 = (u64)offset; | |
352 | u32 ind = (u32)offset; | |
353 | int pool = tmem_frontswap_poolid; | |
354 | ||
355 | if (pool < 0) | |
356 | return; | |
357 | if (ind64 != ind) | |
358 | return; | |
359 | (void) xen_tmem_flush_page(pool, oswiz(type, ind), iswiz(ind)); | |
360 | } | |
361 | ||
362 | /* flush all pages from the passed swaptype */ | |
363 | static void tmem_frontswap_flush_area(unsigned type) | |
364 | { | |
365 | int pool = tmem_frontswap_poolid; | |
366 | int ind; | |
367 | ||
368 | if (pool < 0) | |
369 | return; | |
370 | for (ind = SWIZ_MASK; ind >= 0; ind--) | |
371 | (void)xen_tmem_flush_object(pool, oswiz(type, ind)); | |
372 | } | |
373 | ||
374 | static void tmem_frontswap_init(unsigned ignored) | |
375 | { | |
376 | struct tmem_pool_uuid private = TMEM_POOL_PRIVATE_UUID; | |
377 | ||
378 | /* a single tmem poolid is used for all frontswap "types" (swapfiles) */ | |
379 | if (tmem_frontswap_poolid < 0) | |
380 | tmem_frontswap_poolid = | |
381 | xen_tmem_new_pool(private, TMEM_POOL_PERSIST, PAGE_SIZE); | |
382 | } | |
383 | ||
1e01c968 | 384 | static struct frontswap_ops tmem_frontswap_ops = { |
165c8aed KRW |
385 | .store = tmem_frontswap_store, |
386 | .load = tmem_frontswap_load, | |
91c6cc9b DM |
387 | .invalidate_page = tmem_frontswap_flush_page, |
388 | .invalidate_area = tmem_frontswap_flush_area, | |
afec6e04 DM |
389 | .init = tmem_frontswap_init |
390 | }; | |
391 | #endif | |
392 | ||
10a7a077 | 393 | static int xen_tmem_init(void) |
afec6e04 | 394 | { |
5bc20fc5 DM |
395 | if (!xen_domain()) |
396 | return 0; | |
afec6e04 | 397 | #ifdef CONFIG_FRONTSWAP |
10a7a077 | 398 | if (tmem_enabled && !disable_frontswap) { |
afec6e04 | 399 | char *s = ""; |
1e01c968 | 400 | struct frontswap_ops *old_ops = |
afec6e04 DM |
401 | frontswap_register_ops(&tmem_frontswap_ops); |
402 | ||
403 | tmem_frontswap_poolid = -1; | |
f42158fe KRW |
404 | if (IS_ERR(old_ops) || old_ops) { |
405 | if (IS_ERR(old_ops)) | |
406 | return PTR_ERR(old_ops); | |
afec6e04 | 407 | s = " (WARNING: frontswap_ops overridden)"; |
f42158fe | 408 | } |
afec6e04 | 409 | printk(KERN_INFO "frontswap enabled, RAM provided by " |
22230c18 | 410 | "Xen Transcendent Memory%s\n", s); |
afec6e04 DM |
411 | } |
412 | #endif | |
5bc20fc5 DM |
413 | #ifdef CONFIG_CLEANCACHE |
414 | BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid)); | |
10a7a077 | 415 | if (tmem_enabled && !disable_cleancache) { |
5bc20fc5 | 416 | char *s = ""; |
833f8662 | 417 | struct cleancache_ops *old_ops = |
afec6e04 | 418 | cleancache_register_ops(&tmem_cleancache_ops); |
833f8662 | 419 | if (old_ops) |
5bc20fc5 DM |
420 | s = " (WARNING: cleancache_ops overridden)"; |
421 | printk(KERN_INFO "cleancache enabled, RAM provided by " | |
422 | "Xen Transcendent Memory%s\n", s); | |
423 | } | |
10a7a077 DM |
424 | #endif |
425 | #ifdef CONFIG_XEN_SELFBALLOONING | |
426 | xen_selfballoon_init(!disable_selfballooning, | |
427 | !disable_frontswap_selfshrinking); | |
5bc20fc5 DM |
428 | #endif |
429 | return 0; | |
430 | } | |
431 | ||
432 | module_init(xen_tmem_init) | |
10a7a077 DM |
433 | MODULE_LICENSE("GPL"); |
434 | MODULE_AUTHOR("Dan Magenheimer <dan.magenheimer@oracle.com>"); | |
435 | MODULE_DESCRIPTION("Shim to Xen transcendent memory"); |