tmem.c 11 KB

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