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