tmem.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Xen implementation for transcendent memory (tmem)
  3. *
  4. * Copyright (C) 2009-2011 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. /* temporary ifdef until include/linux/frontswap.h is upstream */
  13. #ifdef CONFIG_FRONTSWAP
  14. #include <linux/frontswap.h>
  15. #endif
  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>
  21. #define TMEM_CONTROL 0
  22. #define TMEM_NEW_POOL 1
  23. #define TMEM_DESTROY_POOL 2
  24. #define TMEM_NEW_PAGE 3
  25. #define TMEM_PUT_PAGE 4
  26. #define TMEM_GET_PAGE 5
  27. #define TMEM_FLUSH_PAGE 6
  28. #define TMEM_FLUSH_OBJECT 7
  29. #define TMEM_READ 8
  30. #define TMEM_WRITE 9
  31. #define TMEM_XCHG 10
  32. /* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */
  33. #define TMEM_POOL_PERSIST 1
  34. #define TMEM_POOL_SHARED 2
  35. #define TMEM_POOL_PAGESIZE_SHIFT 4
  36. #define TMEM_VERSION_SHIFT 24
  37. struct tmem_pool_uuid {
  38. u64 uuid_lo;
  39. u64 uuid_hi;
  40. };
  41. struct tmem_oid {
  42. u64 oid[3];
  43. };
  44. #define TMEM_POOL_PRIVATE_UUID { 0, 0 }
  45. /* flags for tmem_ops.new_pool */
  46. #define TMEM_POOL_PERSIST 1
  47. #define TMEM_POOL_SHARED 2
  48. /* xen tmem foundation ops/hypercalls */
  49. static inline int xen_tmem_op(u32 tmem_cmd, u32 tmem_pool, struct tmem_oid oid,
  50. u32 index, unsigned long gmfn, u32 tmem_offset, u32 pfn_offset, u32 len)
  51. {
  52. struct tmem_op op;
  53. int rc = 0;
  54. op.cmd = tmem_cmd;
  55. op.pool_id = tmem_pool;
  56. op.u.gen.oid[0] = oid.oid[0];
  57. op.u.gen.oid[1] = oid.oid[1];
  58. op.u.gen.oid[2] = oid.oid[2];
  59. op.u.gen.index = index;
  60. op.u.gen.tmem_offset = tmem_offset;
  61. op.u.gen.pfn_offset = pfn_offset;
  62. op.u.gen.len = len;
  63. set_xen_guest_handle(op.u.gen.gmfn, (void *)gmfn);
  64. rc = HYPERVISOR_tmem_op(&op);
  65. return rc;
  66. }
  67. static int xen_tmem_new_pool(struct tmem_pool_uuid uuid,
  68. u32 flags, unsigned long pagesize)
  69. {
  70. struct tmem_op op;
  71. int rc = 0, pageshift;
  72. for (pageshift = 0; pagesize != 1; pageshift++)
  73. pagesize >>= 1;
  74. flags |= (pageshift - 12) << TMEM_POOL_PAGESIZE_SHIFT;
  75. flags |= TMEM_SPEC_VERSION << TMEM_VERSION_SHIFT;
  76. op.cmd = TMEM_NEW_POOL;
  77. op.u.new.uuid[0] = uuid.uuid_lo;
  78. op.u.new.uuid[1] = uuid.uuid_hi;
  79. op.u.new.flags = flags;
  80. rc = HYPERVISOR_tmem_op(&op);
  81. return rc;
  82. }
  83. /* xen generic tmem ops */
  84. static int xen_tmem_put_page(u32 pool_id, struct tmem_oid oid,
  85. u32 index, unsigned long pfn)
  86. {
  87. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  88. return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index,
  89. gmfn, 0, 0, 0);
  90. }
  91. static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid,
  92. u32 index, unsigned long pfn)
  93. {
  94. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  95. return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index,
  96. gmfn, 0, 0, 0);
  97. }
  98. static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index)
  99. {
  100. return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index,
  101. 0, 0, 0, 0);
  102. }
  103. static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid)
  104. {
  105. return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0);
  106. }
  107. bool __read_mostly tmem_enabled = false;
  108. static int __init enable_tmem(char *s)
  109. {
  110. tmem_enabled = true;
  111. return 1;
  112. }
  113. __setup("tmem", enable_tmem);
  114. #ifdef CONFIG_CLEANCACHE
  115. static int xen_tmem_destroy_pool(u32 pool_id)
  116. {
  117. struct tmem_oid oid = { { 0 } };
  118. return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0);
  119. }
  120. /* cleancache ops */
  121. static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key,
  122. pgoff_t index, struct page *page)
  123. {
  124. u32 ind = (u32) index;
  125. struct tmem_oid oid = *(struct tmem_oid *)&key;
  126. unsigned long pfn = page_to_pfn(page);
  127. if (pool < 0)
  128. return;
  129. if (ind != index)
  130. return;
  131. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  132. (void)xen_tmem_put_page((u32)pool, oid, ind, pfn);
  133. }
  134. static int tmem_cleancache_get_page(int pool, struct cleancache_filekey key,
  135. pgoff_t index, struct page *page)
  136. {
  137. u32 ind = (u32) index;
  138. struct tmem_oid oid = *(struct tmem_oid *)&key;
  139. unsigned long pfn = page_to_pfn(page);
  140. int ret;
  141. /* translate return values to linux semantics */
  142. if (pool < 0)
  143. return -1;
  144. if (ind != index)
  145. return -1;
  146. ret = xen_tmem_get_page((u32)pool, oid, ind, pfn);
  147. if (ret == 1)
  148. return 0;
  149. else
  150. return -1;
  151. }
  152. static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key,
  153. pgoff_t index)
  154. {
  155. u32 ind = (u32) index;
  156. struct tmem_oid oid = *(struct tmem_oid *)&key;
  157. if (pool < 0)
  158. return;
  159. if (ind != index)
  160. return;
  161. (void)xen_tmem_flush_page((u32)pool, oid, ind);
  162. }
  163. static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key)
  164. {
  165. struct tmem_oid oid = *(struct tmem_oid *)&key;
  166. if (pool < 0)
  167. return;
  168. (void)xen_tmem_flush_object((u32)pool, oid);
  169. }
  170. static void tmem_cleancache_flush_fs(int pool)
  171. {
  172. if (pool < 0)
  173. return;
  174. (void)xen_tmem_destroy_pool((u32)pool);
  175. }
  176. static int tmem_cleancache_init_fs(size_t pagesize)
  177. {
  178. struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID;
  179. return xen_tmem_new_pool(uuid_private, 0, pagesize);
  180. }
  181. static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize)
  182. {
  183. struct tmem_pool_uuid shared_uuid;
  184. shared_uuid.uuid_lo = *(u64 *)uuid;
  185. shared_uuid.uuid_hi = *(u64 *)(&uuid[8]);
  186. return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize);
  187. }
  188. static bool __initdata use_cleancache = true;
  189. static int __init no_cleancache(char *s)
  190. {
  191. use_cleancache = false;
  192. return 1;
  193. }
  194. __setup("nocleancache", no_cleancache);
  195. static struct cleancache_ops __initdata tmem_cleancache_ops = {
  196. .put_page = tmem_cleancache_put_page,
  197. .get_page = tmem_cleancache_get_page,
  198. .invalidate_page = tmem_cleancache_flush_page,
  199. .invalidate_inode = tmem_cleancache_flush_inode,
  200. .invalidate_fs = tmem_cleancache_flush_fs,
  201. .init_shared_fs = tmem_cleancache_init_shared_fs,
  202. .init_fs = tmem_cleancache_init_fs
  203. };
  204. #endif
  205. #ifdef CONFIG_FRONTSWAP
  206. /* frontswap tmem operations */
  207. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  208. static int tmem_frontswap_poolid;
  209. /*
  210. * Swizzling increases objects per swaptype, increasing tmem concurrency
  211. * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS
  212. */
  213. #define SWIZ_BITS 4
  214. #define SWIZ_MASK ((1 << SWIZ_BITS) - 1)
  215. #define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK))
  216. #define iswiz(_ind) (_ind >> SWIZ_BITS)
  217. static inline struct tmem_oid oswiz(unsigned type, u32 ind)
  218. {
  219. struct tmem_oid oid = { .oid = { 0 } };
  220. oid.oid[0] = _oswiz(type, ind);
  221. return oid;
  222. }
  223. /* returns 0 if the page was successfully put into frontswap, -1 if not */
  224. static int tmem_frontswap_store(unsigned type, pgoff_t offset,
  225. struct page *page)
  226. {
  227. u64 ind64 = (u64)offset;
  228. u32 ind = (u32)offset;
  229. unsigned long pfn = page_to_pfn(page);
  230. int pool = tmem_frontswap_poolid;
  231. int ret;
  232. if (pool < 0)
  233. return -1;
  234. if (ind64 != ind)
  235. return -1;
  236. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  237. ret = xen_tmem_put_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  238. /* translate Xen tmem return values to linux semantics */
  239. if (ret == 1)
  240. return 0;
  241. else
  242. return -1;
  243. }
  244. /*
  245. * returns 0 if the page was successfully gotten from frontswap, -1 if
  246. * was not present (should never happen!)
  247. */
  248. static int tmem_frontswap_load(unsigned type, pgoff_t offset,
  249. struct page *page)
  250. {
  251. u64 ind64 = (u64)offset;
  252. u32 ind = (u32)offset;
  253. unsigned long pfn = page_to_pfn(page);
  254. int pool = tmem_frontswap_poolid;
  255. int ret;
  256. if (pool < 0)
  257. return -1;
  258. if (ind64 != ind)
  259. return -1;
  260. ret = xen_tmem_get_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  261. /* translate Xen tmem return values to linux semantics */
  262. if (ret == 1)
  263. return 0;
  264. else
  265. return -1;
  266. }
  267. /* flush a single page from frontswap */
  268. static void tmem_frontswap_flush_page(unsigned type, pgoff_t offset)
  269. {
  270. u64 ind64 = (u64)offset;
  271. u32 ind = (u32)offset;
  272. int pool = tmem_frontswap_poolid;
  273. if (pool < 0)
  274. return;
  275. if (ind64 != ind)
  276. return;
  277. (void) xen_tmem_flush_page(pool, oswiz(type, ind), iswiz(ind));
  278. }
  279. /* flush all pages from the passed swaptype */
  280. static void tmem_frontswap_flush_area(unsigned type)
  281. {
  282. int pool = tmem_frontswap_poolid;
  283. int ind;
  284. if (pool < 0)
  285. return;
  286. for (ind = SWIZ_MASK; ind >= 0; ind--)
  287. (void)xen_tmem_flush_object(pool, oswiz(type, ind));
  288. }
  289. static void tmem_frontswap_init(unsigned ignored)
  290. {
  291. struct tmem_pool_uuid private = TMEM_POOL_PRIVATE_UUID;
  292. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  293. if (tmem_frontswap_poolid < 0)
  294. tmem_frontswap_poolid =
  295. xen_tmem_new_pool(private, TMEM_POOL_PERSIST, PAGE_SIZE);
  296. }
  297. static bool __initdata use_frontswap = true;
  298. static int __init no_frontswap(char *s)
  299. {
  300. use_frontswap = false;
  301. return 1;
  302. }
  303. __setup("nofrontswap", no_frontswap);
  304. static struct frontswap_ops __initdata tmem_frontswap_ops = {
  305. .store = tmem_frontswap_store,
  306. .load = tmem_frontswap_load,
  307. .invalidate_page = tmem_frontswap_flush_page,
  308. .invalidate_area = tmem_frontswap_flush_area,
  309. .init = tmem_frontswap_init
  310. };
  311. #endif
  312. static int __init xen_tmem_init(void)
  313. {
  314. if (!xen_domain())
  315. return 0;
  316. #ifdef CONFIG_FRONTSWAP
  317. if (tmem_enabled && use_frontswap) {
  318. char *s = "";
  319. struct frontswap_ops old_ops =
  320. frontswap_register_ops(&tmem_frontswap_ops);
  321. tmem_frontswap_poolid = -1;
  322. if (old_ops.init != NULL)
  323. s = " (WARNING: frontswap_ops overridden)";
  324. printk(KERN_INFO "frontswap enabled, RAM provided by "
  325. "Xen Transcendent Memory\n");
  326. }
  327. #endif
  328. #ifdef CONFIG_CLEANCACHE
  329. BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
  330. if (tmem_enabled && use_cleancache) {
  331. char *s = "";
  332. struct cleancache_ops old_ops =
  333. cleancache_register_ops(&tmem_cleancache_ops);
  334. if (old_ops.init_fs != NULL)
  335. s = " (WARNING: cleancache_ops overridden)";
  336. printk(KERN_INFO "cleancache enabled, RAM provided by "
  337. "Xen Transcendent Memory%s\n", s);
  338. }
  339. #endif
  340. return 0;
  341. }
  342. module_init(xen_tmem_init)