tmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /* 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. #include <xen/tmem.h>
  23. #define TMEM_CONTROL 0
  24. #define TMEM_NEW_POOL 1
  25. #define TMEM_DESTROY_POOL 2
  26. #define TMEM_NEW_PAGE 3
  27. #define TMEM_PUT_PAGE 4
  28. #define TMEM_GET_PAGE 5
  29. #define TMEM_FLUSH_PAGE 6
  30. #define TMEM_FLUSH_OBJECT 7
  31. #define TMEM_READ 8
  32. #define TMEM_WRITE 9
  33. #define TMEM_XCHG 10
  34. /* Bits for HYPERVISOR_tmem_op(TMEM_NEW_POOL) */
  35. #define TMEM_POOL_PERSIST 1
  36. #define TMEM_POOL_SHARED 2
  37. #define TMEM_POOL_PAGESIZE_SHIFT 4
  38. #define TMEM_VERSION_SHIFT 24
  39. struct tmem_pool_uuid {
  40. u64 uuid_lo;
  41. u64 uuid_hi;
  42. };
  43. struct tmem_oid {
  44. u64 oid[3];
  45. };
  46. #define TMEM_POOL_PRIVATE_UUID { 0, 0 }
  47. /* flags for tmem_ops.new_pool */
  48. #define TMEM_POOL_PERSIST 1
  49. #define TMEM_POOL_SHARED 2
  50. /* xen tmem foundation ops/hypercalls */
  51. static inline int xen_tmem_op(u32 tmem_cmd, u32 tmem_pool, struct tmem_oid oid,
  52. u32 index, unsigned long gmfn, u32 tmem_offset, u32 pfn_offset, u32 len)
  53. {
  54. struct tmem_op op;
  55. int rc = 0;
  56. op.cmd = tmem_cmd;
  57. op.pool_id = tmem_pool;
  58. op.u.gen.oid[0] = oid.oid[0];
  59. op.u.gen.oid[1] = oid.oid[1];
  60. op.u.gen.oid[2] = oid.oid[2];
  61. op.u.gen.index = index;
  62. op.u.gen.tmem_offset = tmem_offset;
  63. op.u.gen.pfn_offset = pfn_offset;
  64. op.u.gen.len = len;
  65. set_xen_guest_handle(op.u.gen.gmfn, (void *)gmfn);
  66. rc = HYPERVISOR_tmem_op(&op);
  67. return rc;
  68. }
  69. static int xen_tmem_new_pool(struct tmem_pool_uuid uuid,
  70. u32 flags, unsigned long pagesize)
  71. {
  72. struct tmem_op op;
  73. int rc = 0, pageshift;
  74. for (pageshift = 0; pagesize != 1; pageshift++)
  75. pagesize >>= 1;
  76. flags |= (pageshift - 12) << TMEM_POOL_PAGESIZE_SHIFT;
  77. flags |= TMEM_SPEC_VERSION << TMEM_VERSION_SHIFT;
  78. op.cmd = TMEM_NEW_POOL;
  79. op.u.new.uuid[0] = uuid.uuid_lo;
  80. op.u.new.uuid[1] = uuid.uuid_hi;
  81. op.u.new.flags = flags;
  82. rc = HYPERVISOR_tmem_op(&op);
  83. return rc;
  84. }
  85. /* xen generic tmem ops */
  86. static int xen_tmem_put_page(u32 pool_id, struct tmem_oid oid,
  87. u32 index, unsigned long pfn)
  88. {
  89. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  90. return xen_tmem_op(TMEM_PUT_PAGE, pool_id, oid, index,
  91. gmfn, 0, 0, 0);
  92. }
  93. static int xen_tmem_get_page(u32 pool_id, struct tmem_oid oid,
  94. u32 index, unsigned long pfn)
  95. {
  96. unsigned long gmfn = xen_pv_domain() ? pfn_to_mfn(pfn) : pfn;
  97. return xen_tmem_op(TMEM_GET_PAGE, pool_id, oid, index,
  98. gmfn, 0, 0, 0);
  99. }
  100. static int xen_tmem_flush_page(u32 pool_id, struct tmem_oid oid, u32 index)
  101. {
  102. return xen_tmem_op(TMEM_FLUSH_PAGE, pool_id, oid, index,
  103. 0, 0, 0, 0);
  104. }
  105. static int xen_tmem_flush_object(u32 pool_id, struct tmem_oid oid)
  106. {
  107. return xen_tmem_op(TMEM_FLUSH_OBJECT, pool_id, oid, 0, 0, 0, 0, 0);
  108. }
  109. #ifndef CONFIG_XEN_TMEM_MODULE
  110. bool __read_mostly tmem_enabled = false;
  111. static int __init enable_tmem(char *s)
  112. {
  113. tmem_enabled = true;
  114. return 1;
  115. }
  116. __setup("tmem", enable_tmem);
  117. #endif
  118. #ifdef CONFIG_CLEANCACHE
  119. static int xen_tmem_destroy_pool(u32 pool_id)
  120. {
  121. struct tmem_oid oid = { { 0 } };
  122. return xen_tmem_op(TMEM_DESTROY_POOL, pool_id, oid, 0, 0, 0, 0, 0);
  123. }
  124. /* cleancache ops */
  125. static void tmem_cleancache_put_page(int pool, struct cleancache_filekey key,
  126. pgoff_t index, struct page *page)
  127. {
  128. u32 ind = (u32) index;
  129. struct tmem_oid oid = *(struct tmem_oid *)&key;
  130. unsigned long pfn = page_to_pfn(page);
  131. if (pool < 0)
  132. return;
  133. if (ind != index)
  134. return;
  135. mb(); /* ensure page is quiescent; tmem may address it with an alias */
  136. (void)xen_tmem_put_page((u32)pool, oid, ind, pfn);
  137. }
  138. static int tmem_cleancache_get_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. int ret;
  145. /* translate return values to linux semantics */
  146. if (pool < 0)
  147. return -1;
  148. if (ind != index)
  149. return -1;
  150. ret = xen_tmem_get_page((u32)pool, oid, ind, pfn);
  151. if (ret == 1)
  152. return 0;
  153. else
  154. return -1;
  155. }
  156. static void tmem_cleancache_flush_page(int pool, struct cleancache_filekey key,
  157. pgoff_t index)
  158. {
  159. u32 ind = (u32) index;
  160. struct tmem_oid oid = *(struct tmem_oid *)&key;
  161. if (pool < 0)
  162. return;
  163. if (ind != index)
  164. return;
  165. (void)xen_tmem_flush_page((u32)pool, oid, ind);
  166. }
  167. static void tmem_cleancache_flush_inode(int pool, struct cleancache_filekey key)
  168. {
  169. struct tmem_oid oid = *(struct tmem_oid *)&key;
  170. if (pool < 0)
  171. return;
  172. (void)xen_tmem_flush_object((u32)pool, oid);
  173. }
  174. static void tmem_cleancache_flush_fs(int pool)
  175. {
  176. if (pool < 0)
  177. return;
  178. (void)xen_tmem_destroy_pool((u32)pool);
  179. }
  180. static int tmem_cleancache_init_fs(size_t pagesize)
  181. {
  182. struct tmem_pool_uuid uuid_private = TMEM_POOL_PRIVATE_UUID;
  183. return xen_tmem_new_pool(uuid_private, 0, pagesize);
  184. }
  185. static int tmem_cleancache_init_shared_fs(char *uuid, size_t pagesize)
  186. {
  187. struct tmem_pool_uuid shared_uuid;
  188. shared_uuid.uuid_lo = *(u64 *)uuid;
  189. shared_uuid.uuid_hi = *(u64 *)(&uuid[8]);
  190. return xen_tmem_new_pool(shared_uuid, TMEM_POOL_SHARED, pagesize);
  191. }
  192. static bool disable_cleancache __read_mostly;
  193. static bool disable_selfballooning __read_mostly;
  194. #ifdef CONFIG_XEN_TMEM_MODULE
  195. module_param(disable_cleancache, bool, S_IRUGO);
  196. module_param(disable_selfballooning, bool, S_IRUGO);
  197. #else
  198. static int __init no_cleancache(char *s)
  199. {
  200. disable_cleancache = true;
  201. return 1;
  202. }
  203. __setup("nocleancache", no_cleancache);
  204. #endif
  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 bool disable_frontswap __read_mostly;
  308. static bool disable_frontswap_selfshrinking __read_mostly;
  309. #ifdef CONFIG_XEN_TMEM_MODULE
  310. module_param(disable_frontswap, bool, S_IRUGO);
  311. module_param(disable_frontswap_selfshrinking, bool, S_IRUGO);
  312. #else
  313. static int __init no_frontswap(char *s)
  314. {
  315. disable_frontswap = true;
  316. return 1;
  317. }
  318. __setup("nofrontswap", no_frontswap);
  319. #endif
  320. static struct frontswap_ops tmem_frontswap_ops = {
  321. .store = tmem_frontswap_store,
  322. .load = tmem_frontswap_load,
  323. .invalidate_page = tmem_frontswap_flush_page,
  324. .invalidate_area = tmem_frontswap_flush_area,
  325. .init = tmem_frontswap_init
  326. };
  327. #else /* CONFIG_FRONTSWAP */
  328. #define disable_frontswap_selfshrinking 1
  329. #endif
  330. static int xen_tmem_init(void)
  331. {
  332. if (!xen_domain())
  333. return 0;
  334. #ifdef CONFIG_FRONTSWAP
  335. if (tmem_enabled && !disable_frontswap) {
  336. char *s = "";
  337. struct frontswap_ops *old_ops =
  338. frontswap_register_ops(&tmem_frontswap_ops);
  339. tmem_frontswap_poolid = -1;
  340. if (IS_ERR(old_ops) || old_ops) {
  341. if (IS_ERR(old_ops))
  342. return PTR_ERR(old_ops);
  343. s = " (WARNING: frontswap_ops overridden)";
  344. }
  345. printk(KERN_INFO "frontswap enabled, RAM provided by "
  346. "Xen Transcendent Memory%s\n", s);
  347. }
  348. #endif
  349. #ifdef CONFIG_CLEANCACHE
  350. BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid));
  351. if (tmem_enabled && !disable_cleancache) {
  352. char *s = "";
  353. struct cleancache_ops *old_ops =
  354. cleancache_register_ops(&tmem_cleancache_ops);
  355. if (old_ops)
  356. s = " (WARNING: cleancache_ops overridden)";
  357. printk(KERN_INFO "cleancache enabled, RAM provided by "
  358. "Xen Transcendent Memory%s\n", s);
  359. }
  360. #endif
  361. #ifdef CONFIG_XEN_SELFBALLOONING
  362. xen_selfballoon_init(!disable_selfballooning,
  363. !disable_frontswap_selfshrinking);
  364. #endif
  365. return 0;
  366. }
  367. module_init(xen_tmem_init)
  368. MODULE_LICENSE("GPL");
  369. MODULE_AUTHOR("Dan Magenheimer <dan.magenheimer@oracle.com>");
  370. MODULE_DESCRIPTION("Shim to Xen transcendent memory");