tmem.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/module.h>
  12. #include <linux/cleancache.h>
  13. /* temporary ifdef until include/linux/frontswap.h is upstream */
  14. #ifdef CONFIG_FRONTSWAP
  15. #include <linux/frontswap.h>
  16. #endif
  17. #include <xen/xen.h>
  18. #include <xen/interface/xen.h>
  19. #include <asm/xen/hypercall.h>
  20. #include <asm/xen/page.h>
  21. #include <asm/xen/hypervisor.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. int tmem_enabled __read_mostly;
  109. EXPORT_SYMBOL(tmem_enabled);
  110. static int __init enable_tmem(char *s)
  111. {
  112. tmem_enabled = 1;
  113. return 1;
  114. }
  115. __setup("tmem", enable_tmem);
  116. #ifdef CONFIG_CLEANCACHE
  117. static int xen_tmem_destroy_pool(u32 pool_id)
  118. {
  119. struct tmem_oid oid = { { 0 } };
  120. return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0);
  121. }
  122. /* cleancache ops */
  123. static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key,
  124. pgoff_t index, struct page *page)
  125. {
  126. u32 ind = (u32) index;
  127. struct tmem_oid oid = *(struct tmem_oid *)&key;
  128. unsigned long pfn = page_to_pfn(page);
  129. if (pool < 0)
  130. return;
  131. if (ind != index)
  132. return;
  133. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  134. (void)xen_tmem_put_page((u32)pool, oid, ind, pfn);
  135. }
  136. static int tmem_cleancache_get_page(int pool, struct cleancache_filekey key,
  137. pgoff_t index, struct page *page)
  138. {
  139. u32 ind = (u32) index;
  140. struct tmem_oid oid = *(struct tmem_oid *)&key;
  141. unsigned long pfn = page_to_pfn(page);
  142. int ret;
  143. /* translate return values to linux semantics */
  144. if (pool < 0)
  145. return -1;
  146. if (ind != index)
  147. return -1;
  148. ret = xen_tmem_get_page((u32)pool, oid, ind, pfn);
  149. if (ret == 1)
  150. return 0;
  151. else
  152. return -1;
  153. }
  154. static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key,
  155. pgoff_t index)
  156. {
  157. u32 ind = (u32) index;
  158. struct tmem_oid oid = *(struct tmem_oid *)&key;
  159. if (pool < 0)
  160. return;
  161. if (ind != index)
  162. return;
  163. (void)xen_tmem_flush_page((u32)pool, oid, ind);
  164. }
  165. static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key)
  166. {
  167. struct tmem_oid oid = *(struct tmem_oid *)&key;
  168. if (pool < 0)
  169. return;
  170. (void)xen_tmem_flush_object((u32)pool, oid);
  171. }
  172. static void tmem_cleancache_flush_fs(int pool)
  173. {
  174. if (pool < 0)
  175. return;
  176. (void)xen_tmem_destroy_pool((u32)pool);
  177. }
  178. static int tmem_cleancache_init_fs(size_t pagesize)
  179. {
  180. struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID;
  181. return xen_tmem_new_pool(uuid_private, 0, pagesize);
  182. }
  183. static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize)
  184. {
  185. struct tmem_pool_uuid shared_uuid;
  186. shared_uuid.uuid_lo = *(u64 *)uuid;
  187. shared_uuid.uuid_hi = *(u64 *)(&uuid[8]);
  188. return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize);
  189. }
  190. static int use_cleancache = 1;
  191. static int __init no_cleancache(char *s)
  192. {
  193. use_cleancache = 0;
  194. return 1;
  195. }
  196. __setup("nocleancache", no_cleancache);
  197. static struct cleancache_ops tmem_cleancache_ops = {
  198. .put_page = tmem_cleancache_put_page,
  199. .get_page = tmem_cleancache_get_page,
  200. .flush_page = tmem_cleancache_flush_page,
  201. .flush_inode = tmem_cleancache_flush_inode,
  202. .flush_fs = tmem_cleancache_flush_fs,
  203. .init_shared_fs = tmem_cleancache_init_shared_fs,
  204. .init_fs = tmem_cleancache_init_fs
  205. };
  206. #endif
  207. #ifdef CONFIG_FRONTSWAP
  208. /* frontswap tmem operations */
  209. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  210. static int tmem_frontswap_poolid;
  211. /*
  212. * Swizzling increases objects per swaptype, increasing tmem concurrency
  213. * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS
  214. */
  215. #define SWIZ_BITS 4
  216. #define SWIZ_MASK ((1 << SWIZ_BITS) - 1)
  217. #define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK))
  218. #define iswiz(_ind) (_ind >> SWIZ_BITS)
  219. static inline struct tmem_oid oswiz(unsigned type, u32 ind)
  220. {
  221. struct tmem_oid oid = { .oid = { 0 } };
  222. oid.oid[0] = _oswiz(type, ind);
  223. return oid;
  224. }
  225. /* returns 0 if the page was successfully put into frontswap, -1 if not */
  226. static int tmem_frontswap_put_page(unsigned type, pgoff_t offset,
  227. struct page *page)
  228. {
  229. u64 ind64 = (u64)offset;
  230. u32 ind = (u32)offset;
  231. unsigned long pfn = page_to_pfn(page);
  232. int pool = tmem_frontswap_poolid;
  233. int ret;
  234. if (pool < 0)
  235. return -1;
  236. if (ind64 != ind)
  237. return -1;
  238. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  239. ret = xen_tmem_put_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  240. /* translate Xen tmem return values to linux semantics */
  241. if (ret == 1)
  242. return 0;
  243. else
  244. return -1;
  245. }
  246. /*
  247. * returns 0 if the page was successfully gotten from frontswap, -1 if
  248. * was not present (should never happen!)
  249. */
  250. static int tmem_frontswap_get_page(unsigned type, pgoff_t offset,
  251. struct page *page)
  252. {
  253. u64 ind64 = (u64)offset;
  254. u32 ind = (u32)offset;
  255. unsigned long pfn = page_to_pfn(page);
  256. int pool = tmem_frontswap_poolid;
  257. int ret;
  258. if (pool < 0)
  259. return -1;
  260. if (ind64 != ind)
  261. return -1;
  262. ret = xen_tmem_get_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  263. /* translate Xen tmem return values to linux semantics */
  264. if (ret == 1)
  265. return 0;
  266. else
  267. return -1;
  268. }
  269. /* flush a single page from frontswap */
  270. static void tmem_frontswap_flush_page(unsigned type, pgoff_t offset)
  271. {
  272. u64 ind64 = (u64)offset;
  273. u32 ind = (u32)offset;
  274. int pool = tmem_frontswap_poolid;
  275. if (pool < 0)
  276. return;
  277. if (ind64 != ind)
  278. return;
  279. (void) xen_tmem_flush_page(pool, oswiz(type, ind), iswiz(ind));
  280. }
  281. /* flush all pages from the passed swaptype */
  282. static void tmem_frontswap_flush_area(unsigned type)
  283. {
  284. int pool = tmem_frontswap_poolid;
  285. int ind;
  286. if (pool < 0)
  287. return;
  288. for (ind = SWIZ_MASK; ind >= 0; ind--)
  289. (void)xen_tmem_flush_object(pool, oswiz(type, ind));
  290. }
  291. static void tmem_frontswap_init(unsigned ignored)
  292. {
  293. struct tmem_pool_uuid private = TMEM_POOL_PRIVATE_UUID;
  294. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  295. if (tmem_frontswap_poolid < 0)
  296. tmem_frontswap_poolid =
  297. xen_tmem_new_pool(private, TMEM_POOL_PERSIST, PAGE_SIZE);
  298. }
  299. static int __initdata use_frontswap = 1;
  300. static int __init no_frontswap(char *s)
  301. {
  302. use_frontswap = 0;
  303. return 1;
  304. }
  305. __setup("nofrontswap", no_frontswap);
  306. static struct frontswap_ops tmem_frontswap_ops = {
  307. .put_page = tmem_frontswap_put_page,
  308. .get_page = tmem_frontswap_get_page,
  309. .flush_page = tmem_frontswap_flush_page,
  310. .flush_area = tmem_frontswap_flush_area,
  311. .init = tmem_frontswap_init
  312. };
  313. #endif
  314. static int __init xen_tmem_init(void)
  315. {
  316. if (!xen_domain())
  317. return 0;
  318. #ifdef CONFIG_FRONTSWAP
  319. if (tmem_enabled && use_frontswap) {
  320. char *s = "";
  321. struct frontswap_ops old_ops =
  322. frontswap_register_ops(&tmem_frontswap_ops);
  323. tmem_frontswap_poolid = -1;
  324. if (old_ops.init != NULL)
  325. s = " (WARNING: frontswap_ops overridden)";
  326. printk(KERN_INFO "frontswap enabled, RAM provided by "
  327. "Xen Transcendent Memory\n");
  328. }
  329. #endif
  330. #ifdef CONFIG_CLEANCACHE
  331. BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
  332. if (tmem_enabled && use_cleancache) {
  333. char *s = "";
  334. struct cleancache_ops old_ops =
  335. cleancache_register_ops(&tmem_cleancache_ops);
  336. if (old_ops.init_fs != NULL)
  337. s = " (WARNING: cleancache_ops overridden)";
  338. printk(KERN_INFO "cleancache enabled, RAM provided by "
  339. "Xen Transcendent Memory%s\n", s);
  340. }
  341. #endif
  342. return 0;
  343. }
  344. module_init(xen_tmem_init)