tmem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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, unsigned long pfn)
  111. {
  112. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  113. return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index,
  114. gmfn, 0, 0, 0);
  115. }
  116. static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid,
  117. u32 index, unsigned long pfn)
  118. {
  119. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  120. return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index,
  121. gmfn, 0, 0, 0);
  122. }
  123. static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index)
  124. {
  125. return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index,
  126. 0, 0, 0, 0);
  127. }
  128. static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid)
  129. {
  130. return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0);
  131. }
  132. #ifdef CONFIG_CLEANCACHE
  133. static int xen_tmem_destroy_pool(u32 pool_id)
  134. {
  135. struct tmem_oid oid = { { 0 } };
  136. return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0);
  137. }
  138. /* cleancache ops */
  139. static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key,
  140. pgoff_t index, struct page *page)
  141. {
  142. u32 ind = (u32) index;
  143. struct tmem_oid oid = *(struct tmem_oid *)&key;
  144. unsigned long pfn = page_to_pfn(page);
  145. if (pool < 0)
  146. return;
  147. if (ind != index)
  148. return;
  149. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  150. (void)xen_tmem_put_page((u32)pool, oid, ind, pfn);
  151. }
  152. static int tmem_cleancache_get_page(int pool, struct cleancache_filekey key,
  153. pgoff_t index, struct page *page)
  154. {
  155. u32 ind = (u32) index;
  156. struct tmem_oid oid = *(struct tmem_oid *)&key;
  157. unsigned long pfn = page_to_pfn(page);
  158. int ret;
  159. /* translate return values to linux semantics */
  160. if (pool < 0)
  161. return -1;
  162. if (ind != index)
  163. return -1;
  164. ret = xen_tmem_get_page((u32)pool, oid, ind, pfn);
  165. if (ret == 1)
  166. return 0;
  167. else
  168. return -1;
  169. }
  170. static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key,
  171. pgoff_t index)
  172. {
  173. u32 ind = (u32) index;
  174. struct tmem_oid oid = *(struct tmem_oid *)&key;
  175. if (pool < 0)
  176. return;
  177. if (ind != index)
  178. return;
  179. (void)xen_tmem_flush_page((u32)pool, oid, ind);
  180. }
  181. static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key)
  182. {
  183. struct tmem_oid oid = *(struct tmem_oid *)&key;
  184. if (pool < 0)
  185. return;
  186. (void)xen_tmem_flush_object((u32)pool, oid);
  187. }
  188. static void tmem_cleancache_flush_fs(int pool)
  189. {
  190. if (pool < 0)
  191. return;
  192. (void)xen_tmem_destroy_pool((u32)pool);
  193. }
  194. static int tmem_cleancache_init_fs(size_t pagesize)
  195. {
  196. struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID;
  197. return xen_tmem_new_pool(uuid_private, 0, pagesize);
  198. }
  199. static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize)
  200. {
  201. struct tmem_pool_uuid shared_uuid;
  202. shared_uuid.uuid_lo = *(u64 *)uuid;
  203. shared_uuid.uuid_hi = *(u64 *)(&uuid[8]);
  204. return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize);
  205. }
  206. static struct cleancache_ops tmem_cleancache_ops = {
  207. .put_page = tmem_cleancache_put_page,
  208. .get_page = tmem_cleancache_get_page,
  209. .invalidate_page = tmem_cleancache_flush_page,
  210. .invalidate_inode = tmem_cleancache_flush_inode,
  211. .invalidate_fs = tmem_cleancache_flush_fs,
  212. .init_shared_fs = tmem_cleancache_init_shared_fs,
  213. .init_fs = tmem_cleancache_init_fs
  214. };
  215. #endif
  216. #ifdef CONFIG_FRONTSWAP
  217. /* frontswap tmem operations */
  218. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  219. static int tmem_frontswap_poolid;
  220. /*
  221. * Swizzling increases objects per swaptype, increasing tmem concurrency
  222. * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS
  223. */
  224. #define SWIZ_BITS 4
  225. #define SWIZ_MASK ((1 << SWIZ_BITS) - 1)
  226. #define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK))
  227. #define iswiz(_ind) (_ind >> SWIZ_BITS)
  228. static inline struct tmem_oid oswiz(unsigned type, u32 ind)
  229. {
  230. struct tmem_oid oid = { .oid = { 0 } };
  231. oid.oid[0] = _oswiz(type, ind);
  232. return oid;
  233. }
  234. /* returns 0 if the page was successfully put into frontswap, -1 if not */
  235. static int tmem_frontswap_store(unsigned type, pgoff_t offset,
  236. struct page *page)
  237. {
  238. u64 ind64 = (u64)offset;
  239. u32 ind = (u32)offset;
  240. unsigned long pfn = page_to_pfn(page);
  241. int pool = tmem_frontswap_poolid;
  242. int ret;
  243. if (pool < 0)
  244. return -1;
  245. if (ind64 != ind)
  246. return -1;
  247. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  248. ret = xen_tmem_put_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  249. /* translate Xen tmem return values to linux semantics */
  250. if (ret == 1)
  251. return 0;
  252. else
  253. return -1;
  254. }
  255. /*
  256. * returns 0 if the page was successfully gotten from frontswap, -1 if
  257. * was not present (should never happen!)
  258. */
  259. static int tmem_frontswap_load(unsigned type, pgoff_t offset,
  260. struct page *page)
  261. {
  262. u64 ind64 = (u64)offset;
  263. u32 ind = (u32)offset;
  264. unsigned long pfn = page_to_pfn(page);
  265. int pool = tmem_frontswap_poolid;
  266. int ret;
  267. if (pool < 0)
  268. return -1;
  269. if (ind64 != ind)
  270. return -1;
  271. ret = xen_tmem_get_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  272. /* translate Xen tmem return values to linux semantics */
  273. if (ret == 1)
  274. return 0;
  275. else
  276. return -1;
  277. }
  278. /* flush a single page from frontswap */
  279. static void tmem_frontswap_flush_page(unsigned type, pgoff_t offset)
  280. {
  281. u64 ind64 = (u64)offset;
  282. u32 ind = (u32)offset;
  283. int pool = tmem_frontswap_poolid;
  284. if (pool < 0)
  285. return;
  286. if (ind64 != ind)
  287. return;
  288. (void) xen_tmem_flush_page(pool, oswiz(type, ind), iswiz(ind));
  289. }
  290. /* flush all pages from the passed swaptype */
  291. static void tmem_frontswap_flush_area(unsigned type)
  292. {
  293. int pool = tmem_frontswap_poolid;
  294. int ind;
  295. if (pool < 0)
  296. return;
  297. for (ind = SWIZ_MASK; ind >= 0; ind--)
  298. (void)xen_tmem_flush_object(pool, oswiz(type, ind));
  299. }
  300. static void tmem_frontswap_init(unsigned ignored)
  301. {
  302. struct tmem_pool_uuid private = TMEM_POOL_PRIVATE_UUID;
  303. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  304. if (tmem_frontswap_poolid < 0)
  305. tmem_frontswap_poolid =
  306. xen_tmem_new_pool(private, TMEM_POOL_PERSIST, PAGE_SIZE);
  307. }
  308. static struct frontswap_ops tmem_frontswap_ops = {
  309. .store = tmem_frontswap_store,
  310. .load = tmem_frontswap_load,
  311. .invalidate_page = tmem_frontswap_flush_page,
  312. .invalidate_area = tmem_frontswap_flush_area,
  313. .init = tmem_frontswap_init
  314. };
  315. #endif
  316. static int __init xen_tmem_init(void)
  317. {
  318. if (!xen_domain())
  319. return 0;
  320. #ifdef CONFIG_FRONTSWAP
  321. if (tmem_enabled && frontswap) {
  322. char *s = "";
  323. tmem_frontswap_poolid = -1;
  324. frontswap_register_ops(&tmem_frontswap_ops);
  325. pr_info("frontswap enabled, RAM provided by Xen Transcendent Memory%s\n",
  326. s);
  327. }
  328. #endif
  329. #ifdef CONFIG_CLEANCACHE
  330. BUILD_BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
  331. if (tmem_enabled && cleancache) {
  332. int err;
  333. err = cleancache_register_ops(&tmem_cleancache_ops);
  334. if (err)
  335. pr_warn("xen-tmem: failed to enable cleancache: %d\n",
  336. err);
  337. else
  338. pr_info("cleancache enabled, RAM provided by "
  339. "Xen Transcendent Memory\n");
  340. }
  341. #endif
  342. #ifdef CONFIG_XEN_SELFBALLOONING
  343. /*
  344. * There is no point of driving pages to the swap system if they
  345. * aren't going anywhere in tmem universe.
  346. */
  347. if (!frontswap) {
  348. selfshrinking = false;
  349. selfballooning = false;
  350. }
  351. xen_selfballoon_init(selfballooning, selfshrinking);
  352. #endif
  353. return 0;
  354. }
  355. module_init(xen_tmem_init)
  356. MODULE_LICENSE("GPL");
  357. MODULE_AUTHOR("Dan Magenheimer <dan.magenheimer@oracle.com>");
  358. MODULE_DESCRIPTION("Shim to Xen transcendent memory");