Blame view

mm/zswap.c 24.9 KB
2b2811178   Seth Jennings   zswap: add to mm/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  /*
   * zswap.c - zswap driver file
   *
   * zswap is a backend for frontswap that takes pages that are in the process
   * of being swapped out and attempts to compress and store them in a
   * RAM-based memory pool.  This can result in a significant I/O reduction on
   * the swap device and, in the case where decompressing from RAM is faster
   * than reading from the swap device, can also improve workload performance.
   *
   * Copyright (C) 2012  Seth Jennings <sjenning@linux.vnet.ibm.com>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
  */
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  
  #include <linux/module.h>
  #include <linux/cpu.h>
  #include <linux/highmem.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/types.h>
  #include <linux/atomic.h>
  #include <linux/frontswap.h>
  #include <linux/rbtree.h>
  #include <linux/swap.h>
  #include <linux/crypto.h>
  #include <linux/mempool.h>
12d79d64b   Dan Streetman   mm/zpool: update ...
37
  #include <linux/zpool.h>
2b2811178   Seth Jennings   zswap: add to mm/
38
39
40
41
42
43
44
45
46
47
  
  #include <linux/mm_types.h>
  #include <linux/page-flags.h>
  #include <linux/swapops.h>
  #include <linux/writeback.h>
  #include <linux/pagemap.h>
  
  /*********************************
  * statistics
  **********************************/
12d79d64b   Dan Streetman   mm/zpool: update ...
48
49
  /* Total bytes used by the compressed storage */
  static u64 zswap_pool_total_size;
2b2811178   Seth Jennings   zswap: add to mm/
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  /* The number of compressed pages currently stored in zswap */
  static atomic_t zswap_stored_pages = ATOMIC_INIT(0);
  
  /*
   * The statistics below are not protected from concurrent access for
   * performance reasons so they may not be a 100% accurate.  However,
   * they do provide useful information on roughly how many times a
   * certain event is occurring.
  */
  
  /* Pool limit was hit (see zswap_max_pool_percent) */
  static u64 zswap_pool_limit_hit;
  /* Pages written back when pool limit was reached */
  static u64 zswap_written_back_pages;
  /* Store failed due to a reclaim failure after pool limit was reached */
  static u64 zswap_reject_reclaim_fail;
  /* Compressed page was too big for the allocator to (optimally) store */
  static u64 zswap_reject_compress_poor;
  /* Store failed because underlying allocator could not get memory */
  static u64 zswap_reject_alloc_fail;
  /* Store failed because the entry metadata could not be allocated (rare) */
  static u64 zswap_reject_kmemcache_fail;
  /* Duplicate store was encountered (rare) */
  static u64 zswap_duplicate_entry;
  
  /*********************************
  * tunables
  **********************************/
  /* Enable/disable zswap (disabled by default, fixed at boot for now) */
  static bool zswap_enabled __read_mostly;
12ab028be   Dan Streetman   mm/zswap.c: chang...
80
  module_param_named(enabled, zswap_enabled, bool, 0444);
2b2811178   Seth Jennings   zswap: add to mm/
81
82
83
84
  
  /* Compressor to be used by zswap (fixed at boot for now) */
  #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
  static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
12ab028be   Dan Streetman   mm/zswap.c: chang...
85
  module_param_named(compressor, zswap_compressor, charp, 0444);
2b2811178   Seth Jennings   zswap: add to mm/
86
87
88
89
90
  
  /* The maximum percentage of memory that the compressed pool can occupy */
  static unsigned int zswap_max_pool_percent = 20;
  module_param_named(max_pool_percent,
  			zswap_max_pool_percent, uint, 0644);
12d79d64b   Dan Streetman   mm/zpool: update ...
91
92
93
94
95
96
97
  /* Compressed storage to use */
  #define ZSWAP_ZPOOL_DEFAULT "zbud"
  static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
  module_param_named(zpool, zswap_zpool_type, charp, 0444);
  
  /* zpool is shared by all of zswap backend  */
  static struct zpool *zswap_pool;
60105e124   Minchan Kim   mm/zswap: support...
98

2b2811178   Seth Jennings   zswap: add to mm/
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  /*********************************
  * compression functions
  **********************************/
  /* per-cpu compression transforms */
  static struct crypto_comp * __percpu *zswap_comp_pcpu_tfms;
  
  enum comp_op {
  	ZSWAP_COMPOP_COMPRESS,
  	ZSWAP_COMPOP_DECOMPRESS
  };
  
  static int zswap_comp_op(enum comp_op op, const u8 *src, unsigned int slen,
  				u8 *dst, unsigned int *dlen)
  {
  	struct crypto_comp *tfm;
  	int ret;
  
  	tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, get_cpu());
  	switch (op) {
  	case ZSWAP_COMPOP_COMPRESS:
  		ret = crypto_comp_compress(tfm, src, slen, dst, dlen);
  		break;
  	case ZSWAP_COMPOP_DECOMPRESS:
  		ret = crypto_comp_decompress(tfm, src, slen, dst, dlen);
  		break;
  	default:
  		ret = -EINVAL;
  	}
  
  	put_cpu();
  	return ret;
  }
  
  static int __init zswap_comp_init(void)
  {
  	if (!crypto_has_comp(zswap_compressor, 0, 0)) {
  		pr_info("%s compressor not available
  ", zswap_compressor);
  		/* fall back to default compressor */
  		zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  		if (!crypto_has_comp(zswap_compressor, 0, 0))
  			/* can't even load the default compressor */
  			return -ENODEV;
  	}
  	pr_info("using %s compressor
  ", zswap_compressor);
  
  	/* alloc percpu transforms */
  	zswap_comp_pcpu_tfms = alloc_percpu(struct crypto_comp *);
  	if (!zswap_comp_pcpu_tfms)
  		return -ENOMEM;
  	return 0;
  }
dd01d7d89   Mahendran Ganesh   mm/zswap: add __i...
152
  static void __init zswap_comp_exit(void)
2b2811178   Seth Jennings   zswap: add to mm/
153
154
  {
  	/* free percpu transforms */
442cc432e   Markus Elfring   mm/zswap: delete ...
155
  	free_percpu(zswap_comp_pcpu_tfms);
2b2811178   Seth Jennings   zswap: add to mm/
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  }
  
  /*********************************
  * data structures
  **********************************/
  /*
   * struct zswap_entry
   *
   * This structure contains the metadata for tracking a single compressed
   * page within zswap.
   *
   * rbnode - links the entry into red-black tree for the appropriate swap type
   * refcount - the number of outstanding reference to the entry. This is needed
   *            to protect against premature freeing of the entry by code
6b4525164   SeongJae Park   mm/zswap.c: fix t...
170
   *            concurrent calls to load, invalidate, and writeback.  The lock
2b2811178   Seth Jennings   zswap: add to mm/
171
172
173
174
   *            for the zswap_tree structure that contains the entry must
   *            be held while changing the refcount.  Since the lock must
   *            be held, there is no reason to also make refcount atomic.
   * offset - the swap offset for the entry.  Index into the red-black tree.
12d79d64b   Dan Streetman   mm/zpool: update ...
175
   * handle - zpool allocation handle that stores the compressed page data
2b2811178   Seth Jennings   zswap: add to mm/
176
   * length - the length in bytes of the compressed page data.  Needed during
6b4525164   SeongJae Park   mm/zswap.c: fix t...
177
   *          decompression
2b2811178   Seth Jennings   zswap: add to mm/
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
   */
  struct zswap_entry {
  	struct rb_node rbnode;
  	pgoff_t offset;
  	int refcount;
  	unsigned int length;
  	unsigned long handle;
  };
  
  struct zswap_header {
  	swp_entry_t swpentry;
  };
  
  /*
   * The tree lock in the zswap_tree struct protects a few things:
   * - the rbtree
   * - the refcount field of each entry in the tree
   */
  struct zswap_tree {
  	struct rb_root rbroot;
  	spinlock_t lock;
2b2811178   Seth Jennings   zswap: add to mm/
199
200
201
202
203
204
205
206
  };
  
  static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
  
  /*********************************
  * zswap entry functions
  **********************************/
  static struct kmem_cache *zswap_entry_cache;
dd01d7d89   Mahendran Ganesh   mm/zswap: add __i...
207
  static int __init zswap_entry_cache_create(void)
2b2811178   Seth Jennings   zswap: add to mm/
208
209
  {
  	zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
5d2d42de1   SeongJae Park   mm/zswap.c: remov...
210
  	return zswap_entry_cache == NULL;
2b2811178   Seth Jennings   zswap: add to mm/
211
  }
c119239b1   Fabian Frederick   mm/zswap.c: add _...
212
  static void __init zswap_entry_cache_destroy(void)
2b2811178   Seth Jennings   zswap: add to mm/
213
214
215
216
217
218
219
220
221
222
223
  {
  	kmem_cache_destroy(zswap_entry_cache);
  }
  
  static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
  {
  	struct zswap_entry *entry;
  	entry = kmem_cache_alloc(zswap_entry_cache, gfp);
  	if (!entry)
  		return NULL;
  	entry->refcount = 1;
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
224
  	RB_CLEAR_NODE(&entry->rbnode);
2b2811178   Seth Jennings   zswap: add to mm/
225
226
227
228
229
230
231
  	return entry;
  }
  
  static void zswap_entry_cache_free(struct zswap_entry *entry)
  {
  	kmem_cache_free(zswap_entry_cache, entry);
  }
2b2811178   Seth Jennings   zswap: add to mm/
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  /*********************************
  * rbtree functions
  **********************************/
  static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
  {
  	struct rb_node *node = root->rb_node;
  	struct zswap_entry *entry;
  
  	while (node) {
  		entry = rb_entry(node, struct zswap_entry, rbnode);
  		if (entry->offset > offset)
  			node = node->rb_left;
  		else if (entry->offset < offset)
  			node = node->rb_right;
  		else
  			return entry;
  	}
  	return NULL;
  }
  
  /*
   * In the case that a entry with the same offset is found, a pointer to
   * the existing entry is stored in dupentry and the function returns -EEXIST
   */
  static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
  			struct zswap_entry **dupentry)
  {
  	struct rb_node **link = &root->rb_node, *parent = NULL;
  	struct zswap_entry *myentry;
  
  	while (*link) {
  		parent = *link;
  		myentry = rb_entry(parent, struct zswap_entry, rbnode);
  		if (myentry->offset > entry->offset)
  			link = &(*link)->rb_left;
  		else if (myentry->offset < entry->offset)
  			link = &(*link)->rb_right;
  		else {
  			*dupentry = myentry;
  			return -EEXIST;
  		}
  	}
  	rb_link_node(&entry->rbnode, parent, link);
  	rb_insert_color(&entry->rbnode, root);
  	return 0;
  }
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
278
279
280
281
282
283
284
285
286
  static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
  {
  	if (!RB_EMPTY_NODE(&entry->rbnode)) {
  		rb_erase(&entry->rbnode, root);
  		RB_CLEAR_NODE(&entry->rbnode);
  	}
  }
  
  /*
12d79d64b   Dan Streetman   mm/zpool: update ...
287
   * Carries out the common pattern of freeing and entry's zpool allocation,
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
288
289
   * freeing the entry itself, and decrementing the number of stored pages.
   */
60105e124   Minchan Kim   mm/zswap: support...
290
  static void zswap_free_entry(struct zswap_entry *entry)
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
291
  {
12d79d64b   Dan Streetman   mm/zpool: update ...
292
  	zpool_free(zswap_pool, entry->handle);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
293
294
  	zswap_entry_cache_free(entry);
  	atomic_dec(&zswap_stored_pages);
12d79d64b   Dan Streetman   mm/zpool: update ...
295
  	zswap_pool_total_size = zpool_get_total_size(zswap_pool);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  }
  
  /* caller must hold the tree lock */
  static void zswap_entry_get(struct zswap_entry *entry)
  {
  	entry->refcount++;
  }
  
  /* caller must hold the tree lock
  * remove from the tree and free it, if nobody reference the entry
  */
  static void zswap_entry_put(struct zswap_tree *tree,
  			struct zswap_entry *entry)
  {
  	int refcount = --entry->refcount;
  
  	BUG_ON(refcount < 0);
  	if (refcount == 0) {
  		zswap_rb_erase(&tree->rbroot, entry);
60105e124   Minchan Kim   mm/zswap: support...
315
  		zswap_free_entry(entry);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  	}
  }
  
  /* caller must hold the tree lock */
  static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
  				pgoff_t offset)
  {
  	struct zswap_entry *entry = NULL;
  
  	entry = zswap_rb_search(root, offset);
  	if (entry)
  		zswap_entry_get(entry);
  
  	return entry;
  }
2b2811178   Seth Jennings   zswap: add to mm/
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  /*********************************
  * per-cpu code
  **********************************/
  static DEFINE_PER_CPU(u8 *, zswap_dstmem);
  
  static int __zswap_cpu_notifier(unsigned long action, unsigned long cpu)
  {
  	struct crypto_comp *tfm;
  	u8 *dst;
  
  	switch (action) {
  	case CPU_UP_PREPARE:
  		tfm = crypto_alloc_comp(zswap_compressor, 0, 0);
  		if (IS_ERR(tfm)) {
  			pr_err("can't allocate compressor transform
  ");
  			return NOTIFY_BAD;
  		}
  		*per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm;
72d09633c   Eric Dumazet   mm/zswap: NUMA aw...
350
  		dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
2b2811178   Seth Jennings   zswap: add to mm/
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  		if (!dst) {
  			pr_err("can't allocate compressor buffer
  ");
  			crypto_free_comp(tfm);
  			*per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  			return NOTIFY_BAD;
  		}
  		per_cpu(zswap_dstmem, cpu) = dst;
  		break;
  	case CPU_DEAD:
  	case CPU_UP_CANCELED:
  		tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu);
  		if (tfm) {
  			crypto_free_comp(tfm);
  			*per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  		}
  		dst = per_cpu(zswap_dstmem, cpu);
  		kfree(dst);
  		per_cpu(zswap_dstmem, cpu) = NULL;
  		break;
  	default:
  		break;
  	}
  	return NOTIFY_OK;
  }
  
  static int zswap_cpu_notifier(struct notifier_block *nb,
  				unsigned long action, void *pcpu)
  {
  	unsigned long cpu = (unsigned long)pcpu;
  	return __zswap_cpu_notifier(action, cpu);
  }
  
  static struct notifier_block zswap_cpu_notifier_block = {
  	.notifier_call = zswap_cpu_notifier
  };
dd01d7d89   Mahendran Ganesh   mm/zswap: add __i...
387
  static int __init zswap_cpu_init(void)
2b2811178   Seth Jennings   zswap: add to mm/
388
389
  {
  	unsigned long cpu;
576378249   Srivatsa S. Bhat   mm, zswap: Fix CP...
390
  	cpu_notifier_register_begin();
2b2811178   Seth Jennings   zswap: add to mm/
391
392
393
  	for_each_online_cpu(cpu)
  		if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK)
  			goto cleanup;
576378249   Srivatsa S. Bhat   mm, zswap: Fix CP...
394
395
  	__register_cpu_notifier(&zswap_cpu_notifier_block);
  	cpu_notifier_register_done();
2b2811178   Seth Jennings   zswap: add to mm/
396
397
398
399
400
  	return 0;
  
  cleanup:
  	for_each_online_cpu(cpu)
  		__zswap_cpu_notifier(CPU_UP_CANCELED, cpu);
576378249   Srivatsa S. Bhat   mm, zswap: Fix CP...
401
  	cpu_notifier_register_done();
2b2811178   Seth Jennings   zswap: add to mm/
402
403
404
405
406
407
408
409
  	return -ENOMEM;
  }
  
  /*********************************
  * helpers
  **********************************/
  static bool zswap_is_full(void)
  {
5d2d42de1   SeongJae Park   mm/zswap.c: remov...
410
  	return totalram_pages * zswap_max_pool_percent / 100 <
12d79d64b   Dan Streetman   mm/zpool: update ...
411
  		DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
2b2811178   Seth Jennings   zswap: add to mm/
412
  }
2b2811178   Seth Jennings   zswap: add to mm/
413
414
415
416
417
418
419
  /*********************************
  * writeback code
  **********************************/
  /* return enum for zswap_get_swap_cache_page */
  enum zswap_get_swap_ret {
  	ZSWAP_SWAPCACHE_NEW,
  	ZSWAP_SWAPCACHE_EXIST,
67d13fe84   Weijie Yang   mm/zswap: bugfix:...
420
  	ZSWAP_SWAPCACHE_FAIL,
2b2811178   Seth Jennings   zswap: add to mm/
421
422
423
424
425
426
427
428
429
430
431
432
433
  };
  
  /*
   * zswap_get_swap_cache_page
   *
   * This is an adaption of read_swap_cache_async()
   *
   * This function tries to find a page with the given swap entry
   * in the swapper_space address space (the swap cache).  If the page
   * is found, it is returned in retpage.  Otherwise, a page is allocated,
   * added to the swap cache, and returned in retpage.
   *
   * If success, the swap cache page is returned in retpage
67d13fe84   Weijie Yang   mm/zswap: bugfix:...
434
435
436
437
   * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
   * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
   *     the new page is added to swapcache and locked
   * Returns ZSWAP_SWAPCACHE_FAIL on error
2b2811178   Seth Jennings   zswap: add to mm/
438
439
440
441
442
   */
  static int zswap_get_swap_cache_page(swp_entry_t entry,
  				struct page **retpage)
  {
  	struct page *found_page, *new_page = NULL;
822518dc5   Sunghan Suh   mm/zswap.c: get s...
443
  	struct address_space *swapper_space = swap_address_space(entry);
2b2811178   Seth Jennings   zswap: add to mm/
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  	int err;
  
  	*retpage = NULL;
  	do {
  		/*
  		 * First check the swap cache.  Since this is normally
  		 * called after lookup_swap_cache() failed, re-calling
  		 * that would confuse statistics.
  		 */
  		found_page = find_get_page(swapper_space, entry.val);
  		if (found_page)
  			break;
  
  		/*
  		 * Get a new page to read into from swap.
  		 */
  		if (!new_page) {
  			new_page = alloc_page(GFP_KERNEL);
  			if (!new_page)
  				break; /* Out of memory */
  		}
  
  		/*
  		 * call radix_tree_preload() while we can wait.
  		 */
  		err = radix_tree_preload(GFP_KERNEL);
  		if (err)
  			break;
  
  		/*
  		 * Swap entry may have been freed since our caller observed it.
  		 */
  		err = swapcache_prepare(entry);
  		if (err == -EEXIST) { /* seems racy */
  			radix_tree_preload_end();
  			continue;
  		}
  		if (err) { /* swp entry is obsolete ? */
  			radix_tree_preload_end();
  			break;
  		}
  
  		/* May fail (-ENOMEM) if radix-tree node allocation failed. */
  		__set_page_locked(new_page);
  		SetPageSwapBacked(new_page);
  		err = __add_to_swap_cache(new_page, entry);
  		if (likely(!err)) {
  			radix_tree_preload_end();
  			lru_cache_add_anon(new_page);
  			*retpage = new_page;
  			return ZSWAP_SWAPCACHE_NEW;
  		}
  		radix_tree_preload_end();
  		ClearPageSwapBacked(new_page);
  		__clear_page_locked(new_page);
  		/*
  		 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  		 * clear SWAP_HAS_CACHE flag.
  		 */
0a31bc97c   Johannes Weiner   mm: memcontrol: r...
503
  		swapcache_free(entry);
2b2811178   Seth Jennings   zswap: add to mm/
504
505
506
507
508
  	} while (err != -ENOMEM);
  
  	if (new_page)
  		page_cache_release(new_page);
  	if (!found_page)
67d13fe84   Weijie Yang   mm/zswap: bugfix:...
509
  		return ZSWAP_SWAPCACHE_FAIL;
2b2811178   Seth Jennings   zswap: add to mm/
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
  	*retpage = found_page;
  	return ZSWAP_SWAPCACHE_EXIST;
  }
  
  /*
   * Attempts to free an entry by adding a page to the swap cache,
   * decompressing the entry data into the page, and issuing a
   * bio write to write the page back to the swap device.
   *
   * This can be thought of as a "resumed writeback" of the page
   * to the swap device.  We are basically resuming the same swap
   * writeback path that was intercepted with the frontswap_store()
   * in the first place.  After the page has been decompressed into
   * the swap cache, the compressed version stored by zswap can be
   * freed.
   */
12d79d64b   Dan Streetman   mm/zpool: update ...
526
  static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
2b2811178   Seth Jennings   zswap: add to mm/
527
528
529
530
531
532
533
534
535
  {
  	struct zswap_header *zhdr;
  	swp_entry_t swpentry;
  	struct zswap_tree *tree;
  	pgoff_t offset;
  	struct zswap_entry *entry;
  	struct page *page;
  	u8 *src, *dst;
  	unsigned int dlen;
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
536
  	int ret;
2b2811178   Seth Jennings   zswap: add to mm/
537
538
539
540
541
  	struct writeback_control wbc = {
  		.sync_mode = WB_SYNC_NONE,
  	};
  
  	/* extract swpentry from data */
12d79d64b   Dan Streetman   mm/zpool: update ...
542
  	zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
2b2811178   Seth Jennings   zswap: add to mm/
543
  	swpentry = zhdr->swpentry; /* here */
12d79d64b   Dan Streetman   mm/zpool: update ...
544
  	zpool_unmap_handle(pool, handle);
2b2811178   Seth Jennings   zswap: add to mm/
545
546
  	tree = zswap_trees[swp_type(swpentry)];
  	offset = swp_offset(swpentry);
2b2811178   Seth Jennings   zswap: add to mm/
547
548
549
  
  	/* find and ref zswap entry */
  	spin_lock(&tree->lock);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
550
  	entry = zswap_entry_find_get(&tree->rbroot, offset);
2b2811178   Seth Jennings   zswap: add to mm/
551
552
553
554
555
  	if (!entry) {
  		/* entry was invalidated */
  		spin_unlock(&tree->lock);
  		return 0;
  	}
2b2811178   Seth Jennings   zswap: add to mm/
556
557
558
559
560
  	spin_unlock(&tree->lock);
  	BUG_ON(offset != entry->offset);
  
  	/* try to allocate swap cache page */
  	switch (zswap_get_swap_cache_page(swpentry, &page)) {
67d13fe84   Weijie Yang   mm/zswap: bugfix:...
561
  	case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
2b2811178   Seth Jennings   zswap: add to mm/
562
563
  		ret = -ENOMEM;
  		goto fail;
67d13fe84   Weijie Yang   mm/zswap: bugfix:...
564
  	case ZSWAP_SWAPCACHE_EXIST:
2b2811178   Seth Jennings   zswap: add to mm/
565
566
567
568
569
570
571
572
  		/* page is already in the swap cache, ignore for now */
  		page_cache_release(page);
  		ret = -EEXIST;
  		goto fail;
  
  	case ZSWAP_SWAPCACHE_NEW: /* page is locked */
  		/* decompress */
  		dlen = PAGE_SIZE;
12d79d64b   Dan Streetman   mm/zpool: update ...
573
574
  		src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
  				ZPOOL_MM_RO) + sizeof(struct zswap_header);
2b2811178   Seth Jennings   zswap: add to mm/
575
576
577
578
  		dst = kmap_atomic(page);
  		ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src,
  				entry->length, dst, &dlen);
  		kunmap_atomic(dst);
12d79d64b   Dan Streetman   mm/zpool: update ...
579
  		zpool_unmap_handle(zswap_pool, entry->handle);
2b2811178   Seth Jennings   zswap: add to mm/
580
581
582
583
584
585
  		BUG_ON(ret);
  		BUG_ON(dlen != PAGE_SIZE);
  
  		/* page is up to date */
  		SetPageUptodate(page);
  	}
b349acc76   Weijie Yang   mm/zswap: avoid u...
586
587
  	/* move it to the tail of the inactive list after end_writeback */
  	SetPageReclaim(page);
2b2811178   Seth Jennings   zswap: add to mm/
588
589
590
591
592
593
  	/* start writeback */
  	__swap_writepage(page, &wbc, end_swap_bio_write);
  	page_cache_release(page);
  	zswap_written_back_pages++;
  
  	spin_lock(&tree->lock);
2b2811178   Seth Jennings   zswap: add to mm/
594
  	/* drop local reference */
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
595
  	zswap_entry_put(tree, entry);
2b2811178   Seth Jennings   zswap: add to mm/
596
597
  
  	/*
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
598
599
600
601
602
603
604
605
  	* There are two possible situations for entry here:
  	* (1) refcount is 1(normal case),  entry is valid and on the tree
  	* (2) refcount is 0, entry is freed and not on the tree
  	*     because invalidate happened during writeback
  	*  search the tree and free the entry if find entry
  	*/
  	if (entry == zswap_rb_search(&tree->rbroot, offset))
  		zswap_entry_put(tree, entry);
2b2811178   Seth Jennings   zswap: add to mm/
606
  	spin_unlock(&tree->lock);
2b2811178   Seth Jennings   zswap: add to mm/
607

0ab0abcf5   Weijie Yang   mm/zswap: refacto...
608
609
610
611
612
613
614
615
616
  	goto end;
  
  	/*
  	* if we get here due to ZSWAP_SWAPCACHE_EXIST
  	* a load may happening concurrently
  	* it is safe and okay to not free the entry
  	* if we free the entry in the following put
  	* it it either okay to return !0
  	*/
2b2811178   Seth Jennings   zswap: add to mm/
617
618
  fail:
  	spin_lock(&tree->lock);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
619
  	zswap_entry_put(tree, entry);
2b2811178   Seth Jennings   zswap: add to mm/
620
  	spin_unlock(&tree->lock);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
621
622
  
  end:
2b2811178   Seth Jennings   zswap: add to mm/
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  	return ret;
  }
  
  /*********************************
  * frontswap hooks
  **********************************/
  /* attempts to compress and store an single page */
  static int zswap_frontswap_store(unsigned type, pgoff_t offset,
  				struct page *page)
  {
  	struct zswap_tree *tree = zswap_trees[type];
  	struct zswap_entry *entry, *dupentry;
  	int ret;
  	unsigned int dlen = PAGE_SIZE, len;
  	unsigned long handle;
  	char *buf;
  	u8 *src, *dst;
  	struct zswap_header *zhdr;
  
  	if (!tree) {
  		ret = -ENODEV;
  		goto reject;
  	}
  
  	/* reclaim space if needed */
  	if (zswap_is_full()) {
  		zswap_pool_limit_hit++;
12d79d64b   Dan Streetman   mm/zpool: update ...
650
  		if (zpool_shrink(zswap_pool, 1, NULL)) {
2b2811178   Seth Jennings   zswap: add to mm/
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  			zswap_reject_reclaim_fail++;
  			ret = -ENOMEM;
  			goto reject;
  		}
  	}
  
  	/* allocate entry */
  	entry = zswap_entry_cache_alloc(GFP_KERNEL);
  	if (!entry) {
  		zswap_reject_kmemcache_fail++;
  		ret = -ENOMEM;
  		goto reject;
  	}
  
  	/* compress */
  	dst = get_cpu_var(zswap_dstmem);
  	src = kmap_atomic(page);
  	ret = zswap_comp_op(ZSWAP_COMPOP_COMPRESS, src, PAGE_SIZE, dst, &dlen);
  	kunmap_atomic(src);
  	if (ret) {
  		ret = -EINVAL;
  		goto freepage;
  	}
  
  	/* store */
  	len = dlen + sizeof(struct zswap_header);
12d79d64b   Dan Streetman   mm/zpool: update ...
677
  	ret = zpool_malloc(zswap_pool, len, __GFP_NORETRY | __GFP_NOWARN,
2b2811178   Seth Jennings   zswap: add to mm/
678
679
680
681
682
683
684
685
686
  		&handle);
  	if (ret == -ENOSPC) {
  		zswap_reject_compress_poor++;
  		goto freepage;
  	}
  	if (ret) {
  		zswap_reject_alloc_fail++;
  		goto freepage;
  	}
12d79d64b   Dan Streetman   mm/zpool: update ...
687
  	zhdr = zpool_map_handle(zswap_pool, handle, ZPOOL_MM_RW);
2b2811178   Seth Jennings   zswap: add to mm/
688
689
690
  	zhdr->swpentry = swp_entry(type, offset);
  	buf = (u8 *)(zhdr + 1);
  	memcpy(buf, dst, dlen);
12d79d64b   Dan Streetman   mm/zpool: update ...
691
  	zpool_unmap_handle(zswap_pool, handle);
2b2811178   Seth Jennings   zswap: add to mm/
692
693
694
695
696
697
698
699
700
701
702
703
704
705
  	put_cpu_var(zswap_dstmem);
  
  	/* populate entry */
  	entry->offset = offset;
  	entry->handle = handle;
  	entry->length = dlen;
  
  	/* map */
  	spin_lock(&tree->lock);
  	do {
  		ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
  		if (ret == -EEXIST) {
  			zswap_duplicate_entry++;
  			/* remove from rbtree */
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
706
707
  			zswap_rb_erase(&tree->rbroot, dupentry);
  			zswap_entry_put(tree, dupentry);
2b2811178   Seth Jennings   zswap: add to mm/
708
709
710
711
712
713
  		}
  	} while (ret == -EEXIST);
  	spin_unlock(&tree->lock);
  
  	/* update stats */
  	atomic_inc(&zswap_stored_pages);
12d79d64b   Dan Streetman   mm/zpool: update ...
714
  	zswap_pool_total_size = zpool_get_total_size(zswap_pool);
2b2811178   Seth Jennings   zswap: add to mm/
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
  
  	return 0;
  
  freepage:
  	put_cpu_var(zswap_dstmem);
  	zswap_entry_cache_free(entry);
  reject:
  	return ret;
  }
  
  /*
   * returns 0 if the page was successfully decompressed
   * return -1 on entry not found or error
  */
  static int zswap_frontswap_load(unsigned type, pgoff_t offset,
  				struct page *page)
  {
  	struct zswap_tree *tree = zswap_trees[type];
  	struct zswap_entry *entry;
  	u8 *src, *dst;
  	unsigned int dlen;
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
736
  	int ret;
2b2811178   Seth Jennings   zswap: add to mm/
737
738
739
  
  	/* find */
  	spin_lock(&tree->lock);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
740
  	entry = zswap_entry_find_get(&tree->rbroot, offset);
2b2811178   Seth Jennings   zswap: add to mm/
741
742
743
744
745
  	if (!entry) {
  		/* entry was written back */
  		spin_unlock(&tree->lock);
  		return -1;
  	}
2b2811178   Seth Jennings   zswap: add to mm/
746
747
748
749
  	spin_unlock(&tree->lock);
  
  	/* decompress */
  	dlen = PAGE_SIZE;
12d79d64b   Dan Streetman   mm/zpool: update ...
750
751
  	src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
  			ZPOOL_MM_RO) + sizeof(struct zswap_header);
2b2811178   Seth Jennings   zswap: add to mm/
752
753
754
755
  	dst = kmap_atomic(page);
  	ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length,
  		dst, &dlen);
  	kunmap_atomic(dst);
12d79d64b   Dan Streetman   mm/zpool: update ...
756
  	zpool_unmap_handle(zswap_pool, entry->handle);
2b2811178   Seth Jennings   zswap: add to mm/
757
758
759
  	BUG_ON(ret);
  
  	spin_lock(&tree->lock);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
760
  	zswap_entry_put(tree, entry);
2b2811178   Seth Jennings   zswap: add to mm/
761
  	spin_unlock(&tree->lock);
2b2811178   Seth Jennings   zswap: add to mm/
762
763
764
765
766
767
768
769
  	return 0;
  }
  
  /* frees an entry in zswap */
  static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
  {
  	struct zswap_tree *tree = zswap_trees[type];
  	struct zswap_entry *entry;
2b2811178   Seth Jennings   zswap: add to mm/
770
771
772
773
774
775
776
777
778
779
780
  
  	/* find */
  	spin_lock(&tree->lock);
  	entry = zswap_rb_search(&tree->rbroot, offset);
  	if (!entry) {
  		/* entry was written back */
  		spin_unlock(&tree->lock);
  		return;
  	}
  
  	/* remove from rbtree */
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
781
  	zswap_rb_erase(&tree->rbroot, entry);
2b2811178   Seth Jennings   zswap: add to mm/
782
783
  
  	/* drop the initial reference from entry creation */
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
784
  	zswap_entry_put(tree, entry);
2b2811178   Seth Jennings   zswap: add to mm/
785
786
  
  	spin_unlock(&tree->lock);
2b2811178   Seth Jennings   zswap: add to mm/
787
788
789
790
791
792
  }
  
  /* frees all zswap entries for the given swap type */
  static void zswap_frontswap_invalidate_area(unsigned type)
  {
  	struct zswap_tree *tree = zswap_trees[type];
0bd42136f   Cody P Schafer   mm/zswap: use pos...
793
  	struct zswap_entry *entry, *n;
2b2811178   Seth Jennings   zswap: add to mm/
794
795
796
797
798
799
  
  	if (!tree)
  		return;
  
  	/* walk the tree and free everything */
  	spin_lock(&tree->lock);
0ab0abcf5   Weijie Yang   mm/zswap: refacto...
800
  	rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
60105e124   Minchan Kim   mm/zswap: support...
801
  		zswap_free_entry(entry);
2b2811178   Seth Jennings   zswap: add to mm/
802
803
  	tree->rbroot = RB_ROOT;
  	spin_unlock(&tree->lock);
aa9bca05a   Weijie Yang   mm/zswap: bugfix:...
804
805
  	kfree(tree);
  	zswap_trees[type] = NULL;
2b2811178   Seth Jennings   zswap: add to mm/
806
  }
12d79d64b   Dan Streetman   mm/zpool: update ...
807
  static struct zpool_ops zswap_zpool_ops = {
2b2811178   Seth Jennings   zswap: add to mm/
808
809
810
811
812
813
814
815
  	.evict = zswap_writeback_entry
  };
  
  static void zswap_frontswap_init(unsigned type)
  {
  	struct zswap_tree *tree;
  
  	tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
60105e124   Minchan Kim   mm/zswap: support...
816
817
818
819
820
  	if (!tree) {
  		pr_err("alloc failed, zswap disabled for swap type %d
  ", type);
  		return;
  	}
2b2811178   Seth Jennings   zswap: add to mm/
821
822
823
  	tree->rbroot = RB_ROOT;
  	spin_lock_init(&tree->lock);
  	zswap_trees[type] = tree;
2b2811178   Seth Jennings   zswap: add to mm/
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
  }
  
  static struct frontswap_ops zswap_frontswap_ops = {
  	.store = zswap_frontswap_store,
  	.load = zswap_frontswap_load,
  	.invalidate_page = zswap_frontswap_invalidate_page,
  	.invalidate_area = zswap_frontswap_invalidate_area,
  	.init = zswap_frontswap_init
  };
  
  /*********************************
  * debugfs functions
  **********************************/
  #ifdef CONFIG_DEBUG_FS
  #include <linux/debugfs.h>
  
  static struct dentry *zswap_debugfs_root;
  
  static int __init zswap_debugfs_init(void)
  {
  	if (!debugfs_initialized())
  		return -ENODEV;
  
  	zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
  	if (!zswap_debugfs_root)
  		return -ENOMEM;
  
  	debugfs_create_u64("pool_limit_hit", S_IRUGO,
  			zswap_debugfs_root, &zswap_pool_limit_hit);
  	debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
  			zswap_debugfs_root, &zswap_reject_reclaim_fail);
  	debugfs_create_u64("reject_alloc_fail", S_IRUGO,
  			zswap_debugfs_root, &zswap_reject_alloc_fail);
  	debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
  			zswap_debugfs_root, &zswap_reject_kmemcache_fail);
  	debugfs_create_u64("reject_compress_poor", S_IRUGO,
  			zswap_debugfs_root, &zswap_reject_compress_poor);
  	debugfs_create_u64("written_back_pages", S_IRUGO,
  			zswap_debugfs_root, &zswap_written_back_pages);
  	debugfs_create_u64("duplicate_entry", S_IRUGO,
  			zswap_debugfs_root, &zswap_duplicate_entry);
12d79d64b   Dan Streetman   mm/zpool: update ...
865
866
  	debugfs_create_u64("pool_total_size", S_IRUGO,
  			zswap_debugfs_root, &zswap_pool_total_size);
2b2811178   Seth Jennings   zswap: add to mm/
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
  	debugfs_create_atomic_t("stored_pages", S_IRUGO,
  			zswap_debugfs_root, &zswap_stored_pages);
  
  	return 0;
  }
  
  static void __exit zswap_debugfs_exit(void)
  {
  	debugfs_remove_recursive(zswap_debugfs_root);
  }
  #else
  static int __init zswap_debugfs_init(void)
  {
  	return 0;
  }
  
  static void __exit zswap_debugfs_exit(void) { }
  #endif
  
  /*********************************
  * module init and exit
  **********************************/
  static int __init init_zswap(void)
  {
12d79d64b   Dan Streetman   mm/zpool: update ...
891
  	gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN;
2b2811178   Seth Jennings   zswap: add to mm/
892
893
894
895
896
  	if (!zswap_enabled)
  		return 0;
  
  	pr_info("loading zswap
  ");
60105e124   Minchan Kim   mm/zswap: support...
897

3eba0c6a5   Ganesh Mahendran   mm/zpool: add nam...
898
899
  	zswap_pool = zpool_create_pool(zswap_zpool_type, "zswap", gfp,
  					&zswap_zpool_ops);
12d79d64b   Dan Streetman   mm/zpool: update ...
900
901
902
903
  	if (!zswap_pool && strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
  		pr_info("%s zpool not available
  ", zswap_zpool_type);
  		zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
3eba0c6a5   Ganesh Mahendran   mm/zpool: add nam...
904
  		zswap_pool = zpool_create_pool(zswap_zpool_type, "zswap", gfp,
12d79d64b   Dan Streetman   mm/zpool: update ...
905
906
  					&zswap_zpool_ops);
  	}
60105e124   Minchan Kim   mm/zswap: support...
907
  	if (!zswap_pool) {
12d79d64b   Dan Streetman   mm/zpool: update ...
908
909
910
911
  		pr_err("%s zpool not available
  ", zswap_zpool_type);
  		pr_err("zpool creation failed
  ");
60105e124   Minchan Kim   mm/zswap: support...
912
913
  		goto error;
  	}
12d79d64b   Dan Streetman   mm/zpool: update ...
914
915
  	pr_info("using %s pool
  ", zswap_zpool_type);
60105e124   Minchan Kim   mm/zswap: support...
916

2b2811178   Seth Jennings   zswap: add to mm/
917
918
919
  	if (zswap_entry_cache_create()) {
  		pr_err("entry cache creation failed
  ");
60105e124   Minchan Kim   mm/zswap: support...
920
  		goto cachefail;
2b2811178   Seth Jennings   zswap: add to mm/
921
922
923
924
925
926
927
928
929
930
931
  	}
  	if (zswap_comp_init()) {
  		pr_err("compressor initialization failed
  ");
  		goto compfail;
  	}
  	if (zswap_cpu_init()) {
  		pr_err("per-cpu initialization failed
  ");
  		goto pcpufail;
  	}
60105e124   Minchan Kim   mm/zswap: support...
932

2b2811178   Seth Jennings   zswap: add to mm/
933
934
935
936
937
938
939
940
  	frontswap_register_ops(&zswap_frontswap_ops);
  	if (zswap_debugfs_init())
  		pr_warn("debugfs initialization failed
  ");
  	return 0;
  pcpufail:
  	zswap_comp_exit();
  compfail:
c119239b1   Fabian Frederick   mm/zswap.c: add _...
941
  	zswap_entry_cache_destroy();
60105e124   Minchan Kim   mm/zswap: support...
942
  cachefail:
12d79d64b   Dan Streetman   mm/zpool: update ...
943
  	zpool_destroy_pool(zswap_pool);
2b2811178   Seth Jennings   zswap: add to mm/
944
945
946
947
948
949
950
  error:
  	return -ENOMEM;
  }
  /* must be late so crypto has time to come up */
  late_initcall(init_zswap);
  
  MODULE_LICENSE("GPL");
68386da82   Seth Jennings   zbud, zswap: chan...
951
  MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
2b2811178   Seth Jennings   zswap: add to mm/
952
  MODULE_DESCRIPTION("Compressed cache for swap pages");