tmem.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Xen implementation for transcendent memory (tmem)
  3. *
  4. * Copyright (C) 2009-2010 Oracle Corp. All rights reserved.
  5. * Author: Dan Magenheimer
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/init.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/cleancache.h>
  12. #include <xen/xen.h>
  13. #include <xen/interface/xen.h>
  14. #include <asm/xen/hypercall.h>
  15. #include <asm/xen/page.h>
  16. #include <asm/xen/hypervisor.h>
  17. #define TMEM_CONTROL 0
  18. #define TMEM_NEW_POOL 1
  19. #define TMEM_DESTROY_POOL 2
  20. #define TMEM_NEW_PAGE 3
  21. #define TMEM_PUT_PAGE 4
  22. #define TMEM_GET_PAGE 5
  23. #define TMEM_FLUSH_PAGE 6
  24. #define TMEM_FLUSH_OBJECT 7
  25. #define TMEM_READ 8
  26. #define TMEM_WRITE 9
  27. #define TMEM_XCHG 10
  28. /* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */
  29. #define TMEM_POOL_PERSIST 1
  30. #define TMEM_POOL_SHARED 2
  31. #define TMEM_POOL_PAGESIZE_SHIFT 4
  32. #define TMEM_VERSION_SHIFT 24
  33. struct tmem_pool_uuid {
  34. u64 uuid_lo;
  35. u64 uuid_hi;
  36. };
  37. struct tmem_oid {
  38. u64 oid[3];
  39. };
  40. #define TMEM_POOL_PRIVATE_UUID { 0, 0 }
  41. /* flags for tmem_ops.new_pool */
  42. #define TMEM_POOL_PERSIST 1
  43. #define TMEM_POOL_SHARED 2
  44. /* xen tmem foundation ops/hypercalls */
  45. static inline int xen_tmem_op(u32 tmem_cmd, u32 tmem_pool, struct tmem_oid oid,
  46. u32 index, unsigned long gmfn, u32 tmem_offset, u32 pfn_offset, u32 len)
  47. {
  48. struct tmem_op op;
  49. int rc = 0;
  50. op.cmd = tmem_cmd;
  51. op.pool_id = tmem_pool;
  52. op.u.gen.oid[0] = oid.oid[0];
  53. op.u.gen.oid[1] = oid.oid[1];
  54. op.u.gen.oid[2] = oid.oid[2];
  55. op.u.gen.index = index;
  56. op.u.gen.tmem_offset = tmem_offset;
  57. op.u.gen.pfn_offset = pfn_offset;
  58. op.u.gen.len = len;
  59. set_xen_guest_handle(op.u.gen.gmfn, (void *)gmfn);
  60. rc = HYPERVISOR_tmem_op(&op);
  61. return rc;
  62. }
  63. static int xen_tmem_new_pool(struct tmem_pool_uuid uuid,
  64. u32 flags, unsigned long pagesize)
  65. {
  66. struct tmem_op op;
  67. int rc = 0, pageshift;
  68. for (pageshift = 0; pagesize != 1; pageshift++)
  69. pagesize >>= 1;
  70. flags |= (pageshift - 12) << TMEM_POOL_PAGESIZE_SHIFT;
  71. flags |= TMEM_SPEC_VERSION << TMEM_VERSION_SHIFT;
  72. op.cmd = TMEM_NEW_POOL;
  73. op.u.new.uuid[0] = uuid.uuid_lo;
  74. op.u.new.uuid[1] = uuid.uuid_hi;
  75. op.u.new.flags = flags;
  76. rc = HYPERVISOR_tmem_op(&op);
  77. return rc;
  78. }
  79. /* xen generic tmem ops */
  80. static int xen_tmem_put_page(u32 pool_id, struct tmem_oid oid,
  81. u32 index, unsigned long pfn)
  82. {
  83. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  84. return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index,
  85. gmfn, 0, 0, 0);
  86. }
  87. static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid,
  88. u32 index, unsigned long pfn)
  89. {
  90. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  91. return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index,
  92. gmfn, 0, 0, 0);
  93. }
  94. static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index)
  95. {
  96. return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index,
  97. 0, 0, 0, 0);
  98. }
  99. static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid)
  100. {
  101. return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0);
  102. }
  103. static int xen_tmem_destroy_pool(u32 pool_id)
  104. {
  105. struct tmem_oid oid = { { 0 } };
  106. return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0);
  107. }
  108. int tmem_enabled;
  109. static int __init enable_tmem(char *s)
  110. {
  111. tmem_enabled = 1;
  112. return 1;
  113. }
  114. __setup("tmem", enable_tmem);
  115. /* cleancache ops */
  116. static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key,
  117. pgoff_t index, struct page *page)
  118. {
  119. u32 ind = (u32) index;
  120. struct tmem_oid oid = *(struct tmem_oid *)&key;
  121. unsigned long pfn = page_to_pfn(page);
  122. if (pool < 0)
  123. return;
  124. if (ind != index)
  125. return;
  126. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  127. (void)xen_tmem_put_page((u32)pool, oid, ind, pfn);
  128. }
  129. static int tmem_cleancache_get_page(int pool, struct cleancache_filekey key,
  130. pgoff_t index, struct page *page)
  131. {
  132. u32 ind = (u32) index;
  133. struct tmem_oid oid = *(struct tmem_oid *)&key;
  134. unsigned long pfn = page_to_pfn(page);
  135. int ret;
  136. /* translate return values to linux semantics */
  137. if (pool < 0)
  138. return -1;
  139. if (ind != index)
  140. return -1;
  141. ret = xen_tmem_get_page((u32)pool, oid, ind, pfn);
  142. if (ret == 1)
  143. return 0;
  144. else
  145. return -1;
  146. }
  147. static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key,
  148. pgoff_t index)
  149. {
  150. u32 ind = (u32) index;
  151. struct tmem_oid oid = *(struct tmem_oid *)&key;
  152. if (pool < 0)
  153. return;
  154. if (ind != index)
  155. return;
  156. (void)xen_tmem_flush_page((u32)pool, oid, ind);
  157. }
  158. static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key)
  159. {
  160. struct tmem_oid oid = *(struct tmem_oid *)&key;
  161. if (pool < 0)
  162. return;
  163. (void)xen_tmem_flush_object((u32)pool, oid);
  164. }
  165. static void tmem_cleancache_flush_fs(int pool)
  166. {
  167. if (pool < 0)
  168. return;
  169. (void)xen_tmem_destroy_pool((u32)pool);
  170. }
  171. static int tmem_cleancache_init_fs(size_t pagesize)
  172. {
  173. struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID;
  174. return xen_tmem_new_pool(uuid_private, 0, pagesize);
  175. }
  176. static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize)
  177. {
  178. struct tmem_pool_uuid shared_uuid;
  179. shared_uuid.uuid_lo = *(u64 *)uuid;
  180. shared_uuid.uuid_hi = *(u64 *)(&uuid[8]);
  181. return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize);
  182. }
  183. static int use_cleancache = 1;
  184. static int __init no_cleancache(char *s)
  185. {
  186. use_cleancache = 0;
  187. return 1;
  188. }
  189. __setup("nocleancache", no_cleancache);
  190. static struct cleancache_ops tmem_cleancache_ops = {
  191. .put_page = tmem_cleancache_put_page,
  192. .get_page = tmem_cleancache_get_page,
  193. .flush_page = tmem_cleancache_flush_page,
  194. .flush_inode = tmem_cleancache_flush_inode,
  195. .flush_fs = tmem_cleancache_flush_fs,
  196. .init_shared_fs = tmem_cleancache_init_shared_fs,
  197. .init_fs = tmem_cleancache_init_fs
  198. };
  199. static int __init xen_tmem_init(void)
  200. {
  201. struct cleancache_ops old_ops;
  202. if (!xen_domain())
  203. return 0;
  204. #ifdef CONFIG_CLEANCACHE
  205. BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
  206. if (tmem_enabled && use_cleancache) {
  207. char *s = "";
  208. old_ops = cleancache_register_ops(&tmem_cleancache_ops);
  209. if (old_ops.init_fs != NULL)
  210. s = " (WARNING: cleancache_ops overridden)";
  211. printk(KERN_INFO "cleancache enabled, RAM provided by "
  212. "Xen Transcendent Memory%s\n", s);
  213. }
  214. #endif
  215. return 0;
  216. }
  217. module_init(xen_tmem_init)