tmem.c 10 KB

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