tmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #define TMEM_CONTROL 0
  21. #define TMEM_NEW_POOL 1
  22. #define TMEM_DESTROY_POOL 2
  23. #define TMEM_NEW_PAGE 3
  24. #define TMEM_PUT_PAGE 4
  25. #define TMEM_GET_PAGE 5
  26. #define TMEM_FLUSH_PAGE 6
  27. #define TMEM_FLUSH_OBJECT 7
  28. #define TMEM_READ 8
  29. #define TMEM_WRITE 9
  30. #define TMEM_XCHG 10
  31. /* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */
  32. #define TMEM_POOL_PERSIST 1
  33. #define TMEM_POOL_SHARED 2
  34. #define TMEM_POOL_PAGESIZE_SHIFT 4
  35. #define TMEM_VERSION_SHIFT 24
  36. struct tmem_pool_uuid {
  37. u64 uuid_lo;
  38. u64 uuid_hi;
  39. };
  40. struct tmem_oid {
  41. u64 oid[3];
  42. };
  43. #define TMEM_POOL_PRIVATE_UUID { 0, 0 }
  44. /* flags for tmem_ops.new_pool */
  45. #define TMEM_POOL_PERSIST 1
  46. #define TMEM_POOL_SHARED 2
  47. /* xen tmem foundation ops/hypercalls */
  48. static inline int xen_tmem_op(u32 tmem_cmd, u32 tmem_pool, struct tmem_oid oid,
  49. u32 index, unsigned long gmfn, u32 tmem_offset, u32 pfn_offset, u32 len)
  50. {
  51. struct tmem_op op;
  52. int rc = 0;
  53. op.cmd = tmem_cmd;
  54. op.pool_id = tmem_pool;
  55. op.u.gen.oid[0] = oid.oid[0];
  56. op.u.gen.oid[1] = oid.oid[1];
  57. op.u.gen.oid[2] = oid.oid[2];
  58. op.u.gen.index = index;
  59. op.u.gen.tmem_offset = tmem_offset;
  60. op.u.gen.pfn_offset = pfn_offset;
  61. op.u.gen.len = len;
  62. set_xen_guest_handle(op.u.gen.gmfn, (void *)gmfn);
  63. rc = HYPERVISOR_tmem_op(&op);
  64. return rc;
  65. }
  66. static int xen_tmem_new_pool(struct tmem_pool_uuid uuid,
  67. u32 flags, unsigned long pagesize)
  68. {
  69. struct tmem_op op;
  70. int rc = 0, pageshift;
  71. for (pageshift = 0; pagesize != 1; pageshift++)
  72. pagesize >>= 1;
  73. flags |= (pageshift - 12) << TMEM_POOL_PAGESIZE_SHIFT;
  74. flags |= TMEM_SPEC_VERSION << TMEM_VERSION_SHIFT;
  75. op.cmd = TMEM_NEW_POOL;
  76. op.u.new.uuid[0] = uuid.uuid_lo;
  77. op.u.new.uuid[1] = uuid.uuid_hi;
  78. op.u.new.flags = flags;
  79. rc = HYPERVISOR_tmem_op(&op);
  80. return rc;
  81. }
  82. /* xen generic tmem ops */
  83. static int xen_tmem_put_page(u32 pool_id, struct tmem_oid oid,
  84. u32 index, unsigned long pfn)
  85. {
  86. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  87. return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index,
  88. gmfn, 0, 0, 0);
  89. }
  90. static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid,
  91. u32 index, unsigned long pfn)
  92. {
  93. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  94. return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index,
  95. gmfn, 0, 0, 0);
  96. }
  97. static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index)
  98. {
  99. return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index,
  100. 0, 0, 0, 0);
  101. }
  102. static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid)
  103. {
  104. return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0);
  105. }
  106. #ifndef CONFIG_XEN_TMEM_MODULE
  107. bool __read_mostly tmem_enabled = false;
  108. static int __init enable_tmem(char *s)
  109. {
  110. tmem_enabled = true;
  111. return 1;
  112. }
  113. __setup("tmem", enable_tmem);
  114. #endif
  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 disable_cleancache __read_mostly;
  190. static bool disable_selfballooning __read_mostly;
  191. #ifdef CONFIG_XEN_TMEM_MODULE
  192. module_param(disable_cleancache, bool, S_IRUGO);
  193. module_param(disable_selfballooning, bool, S_IRUGO);
  194. #else
  195. static int __init no_cleancache(char *s)
  196. {
  197. disable_cleancache = true;
  198. return 1;
  199. }
  200. __setup("nocleancache", no_cleancache);
  201. #endif
  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. unsigned long pfn = page_to_pfn(page);
  237. int pool = tmem_frontswap_poolid;
  238. int ret;
  239. if (pool < 0)
  240. return -1;
  241. if (ind64 != ind)
  242. return -1;
  243. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  244. ret = xen_tmem_put_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  245. /* translate Xen tmem return values to linux semantics */
  246. if (ret == 1)
  247. return 0;
  248. else
  249. return -1;
  250. }
  251. /*
  252. * returns 0 if the page was successfully gotten from frontswap, -1 if
  253. * was not present (should never happen!)
  254. */
  255. static int tmem_frontswap_load(unsigned type, pgoff_t offset,
  256. struct page *page)
  257. {
  258. u64 ind64 = (u64)offset;
  259. u32 ind = (u32)offset;
  260. unsigned long pfn = page_to_pfn(page);
  261. int pool = tmem_frontswap_poolid;
  262. int ret;
  263. if (pool < 0)
  264. return -1;
  265. if (ind64 != ind)
  266. return -1;
  267. ret = xen_tmem_get_page(pool, oswiz(type, ind), iswiz(ind), pfn);
  268. /* translate Xen tmem return values to linux semantics */
  269. if (ret == 1)
  270. return 0;
  271. else
  272. return -1;
  273. }
  274. /* flush a single page from frontswap */
  275. static void tmem_frontswap_flush_page(unsigned type, pgoff_t offset)
  276. {
  277. u64 ind64 = (u64)offset;
  278. u32 ind = (u32)offset;
  279. int pool = tmem_frontswap_poolid;
  280. if (pool < 0)
  281. return;
  282. if (ind64 != ind)
  283. return;
  284. (void) xen_tmem_flush_page(pool, oswiz(type, ind), iswiz(ind));
  285. }
  286. /* flush all pages from the passed swaptype */
  287. static void tmem_frontswap_flush_area(unsigned type)
  288. {
  289. int pool = tmem_frontswap_poolid;
  290. int ind;
  291. if (pool < 0)
  292. return;
  293. for (ind = SWIZ_MASK; ind >= 0; ind--)
  294. (void)xen_tmem_flush_object(pool, oswiz(type, ind));
  295. }
  296. static void tmem_frontswap_init(unsigned ignored)
  297. {
  298. struct tmem_pool_uuid private = TMEM_POOL_PRIVATE_UUID;
  299. /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
  300. if (tmem_frontswap_poolid < 0)
  301. tmem_frontswap_poolid =
  302. xen_tmem_new_pool(private, TMEM_POOL_PERSIST, PAGE_SIZE);
  303. }
  304. static bool disable_frontswap __read_mostly;
  305. static bool disable_frontswap_selfshrinking __read_mostly;
  306. #ifdef CONFIG_XEN_TMEM_MODULE
  307. module_param(disable_frontswap, bool, S_IRUGO);
  308. module_param(disable_frontswap_selfshrinking, bool, S_IRUGO);
  309. #else
  310. static int __init no_frontswap(char *s)
  311. {
  312. disable_frontswap = true;
  313. return 1;
  314. }
  315. __setup("nofrontswap", no_frontswap);
  316. #endif
  317. static struct frontswap_ops tmem_frontswap_ops = {
  318. .store = tmem_frontswap_store,
  319. .load = tmem_frontswap_load,
  320. .invalidate_page = tmem_frontswap_flush_page,
  321. .invalidate_area = tmem_frontswap_flush_area,
  322. .init = tmem_frontswap_init
  323. };
  324. #else /* CONFIG_FRONTSWAP */
  325. #define disable_frontswap_selfshrinking 1
  326. #endif
  327. static int xen_tmem_init(void)
  328. {
  329. if (!xen_domain())
  330. return 0;
  331. #ifdef CONFIG_FRONTSWAP
  332. if (tmem_enabled && !disable_frontswap) {
  333. char *s = "";
  334. struct frontswap_ops *old_ops =
  335. frontswap_register_ops(&tmem_frontswap_ops);
  336. tmem_frontswap_poolid = -1;
  337. if (IS_ERR(old_ops) || old_ops) {
  338. if (IS_ERR(old_ops))
  339. return PTR_ERR(old_ops);
  340. s = " (WARNING: frontswap_ops overridden)";
  341. }
  342. printk(KERN_INFO "frontswap enabled, RAM provided by "
  343. "Xen Transcendent Memory%s\n", s);
  344. }
  345. #endif
  346. #ifdef CONFIG_CLEANCACHE
  347. BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
  348. if (tmem_enabled && !disable_cleancache) {
  349. char *s = "";
  350. struct cleancache_ops *old_ops =
  351. cleancache_register_ops(&tmem_cleancache_ops);
  352. if (old_ops)
  353. s = " (WARNING: cleancache_ops overridden)";
  354. printk(KERN_INFO "cleancache enabled, RAM provided by "
  355. "Xen Transcendent Memory%s\n", s);
  356. }
  357. #endif
  358. #ifdef CONFIG_XEN_SELFBALLOONING
  359. xen_selfballoon_init(!disable_selfballooning,
  360. !disable_frontswap_selfshrinking);
  361. #endif
  362. return 0;
  363. }
  364. module_init(xen_tmem_init)
  365. MODULE_LICENSE("GPL");
  366. MODULE_AUTHOR("Dan Magenheimer <dan.magenheimer@oracle.com>");
  367. MODULE_DESCRIPTION("Shim to Xen transcendent memory");