Linux Audio

Check our new training course

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