usb: gadget: f_mtp: Avoid race between mtp_read and mtp_function_disable
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / mm / zswap.c
... / ...
CommitLineData
1/*
2 * zswap.c - zswap driver file
3 *
4 * zswap is a backend for frontswap that takes pages that are in the process
5 * of being swapped out and attempts to compress and store them in a
6 * RAM-based memory pool. This can result in a significant I/O reduction on
7 * the swap device and, in the case where decompressing from RAM is faster
8 * than reading from the swap device, can also improve workload performance.
9 *
10 * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21*/
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/cpu.h>
27#include <linux/highmem.h>
28#include <linux/slab.h>
29#include <linux/spinlock.h>
30#include <linux/types.h>
31#include <linux/atomic.h>
32#include <linux/frontswap.h>
33#include <linux/rbtree.h>
34#include <linux/swap.h>
35#include <linux/crypto.h>
36#include <linux/mempool.h>
37#include <linux/zpool.h>
38
39#include <linux/mm_types.h>
40#include <linux/page-flags.h>
41#include <linux/swapops.h>
42#include <linux/writeback.h>
43#include <linux/pagemap.h>
44#include <linux/sec_debug.h>
45
46#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
47#include <linux/jhash.h>
48#endif
49
50/*********************************
51* statistics
52**********************************/
53/* Total bytes used by the compressed storage */
54static u64 zswap_pool_total_size;
55/* Number of memory pages used by the compressed pool */
56u64 zswap_pool_pages;
57/* The number of compressed pages currently stored in zswap */
58atomic_t zswap_stored_pages = ATOMIC_INIT(0);
59
60#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
61/* The number of swapped out pages which are identified as duplicate
62 to the existing zswap pages. Compression and storing of these
63 pages is avoided */
64static atomic_t zswap_duplicate_pages = ATOMIC_INIT(0);
65#endif
66
67/*
68 * The statistics below are not protected from concurrent access for
69 * performance reasons so they may not be a 100% accurate. However,
70 * they do provide useful information on roughly how many times a
71 * certain event is occurring.
72*/
73
74/* Pool limit was hit (see zswap_max_pool_percent) */
75static u64 zswap_pool_limit_hit;
76/* Pages written back when pool limit was reached */
77static u64 zswap_written_back_pages;
78/* Store failed due to a reclaim failure after pool limit was reached */
79static u64 zswap_reject_reclaim_fail;
80/* Compressed page was too big for the allocator to (optimally) store */
81static u64 zswap_reject_compress_poor;
82/* Store failed because underlying allocator could not get memory */
83static u64 zswap_reject_alloc_fail;
84/* Store failed because the entry metadata could not be allocated (rare) */
85static u64 zswap_reject_kmemcache_fail;
86/* Duplicate store was encountered (rare) */
87static u64 zswap_duplicate_entry;
88
89/* The number of zero pages currently stored in zswap */
90static atomic_t zswap_zero_pages = ATOMIC_INIT(0);
91
92/*********************************
93* tunables
94**********************************/
95
96/* Enable/disable zswap (disabled by default) */
97static bool zswap_enabled = 1;
98static int zswap_enabled_param_set(const char *,
99 const struct kernel_param *);
100static struct kernel_param_ops zswap_enabled_param_ops = {
101 .set = zswap_enabled_param_set,
102 .get = param_get_bool,
103};
104module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
105
106/* Crypto compressor to use */
107#define ZSWAP_COMPRESSOR_DEFAULT "lzo"
108#define ZSWAP_COMPRESSOR "lz4"
109static char *zswap_compressor = ZSWAP_COMPRESSOR;
110static int zswap_compressor_param_set(const char *,
111 const struct kernel_param *);
112static struct kernel_param_ops zswap_compressor_param_ops = {
113 .set = zswap_compressor_param_set,
114 .get = param_get_charp,
115 .free = param_free_charp,
116};
117module_param_cb(compressor, &zswap_compressor_param_ops,
118 &zswap_compressor, 0644);
119
120/* Compressed storage zpool to use */
121#define ZSWAP_ZPOOL_DEFAULT "zsmalloc"
122static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
123static int zswap_zpool_param_set(const char *, const struct kernel_param *);
124static struct kernel_param_ops zswap_zpool_param_ops = {
125 .set = zswap_zpool_param_set,
126 .get = param_get_charp,
127 .free = param_free_charp,
128};
129module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
130
131/* The maximum percentage of memory that the compressed pool can occupy */
132static unsigned int zswap_max_pool_percent = 50;
133module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
134
135/*********************************
136* data structures
137**********************************/
138#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
139/*
140 * struct zswap_handle
141 * This structure contains the metadata for tracking single zpool
142 * allocation.
143 *
144 * rbnode - links the handle into red-black tree
145 * checksum - 32-bit checksum value of the page swapped to zswap
146 * ref_count - number of pages sharing this handle
147 * length - the length in bytes of the compressed page data.
148 * Needed during decompression.
149 * handle - zpool allocation handle that stores the compressed page data
150 */
151struct zswap_handle {
152 struct rb_node rbnode;
153 u32 checksum;
154 u32 ref_count;
155 unsigned int length;
156 unsigned long handle;
157};
158#endif
159
160struct zswap_pool {
161 struct zpool *zpool;
162 struct crypto_comp * __percpu *tfm;
163 struct kref kref;
164 struct list_head list;
165 struct rcu_head rcu_head;
166 struct notifier_block notifier;
167 char tfm_name[CRYPTO_MAX_ALG_NAME];
168};
169
170/*
171 * struct zswap_entry
172 *
173 * This structure contains the metadata for tracking a single compressed
174 * page within zswap.
175 *
176 * #ifndef CONFIG_ZSWAP_SAME_PAGE_SHARING
177 * rbnode - links the entry into red-black tree for the appropriate swap type
178 * offset - the swap offset for the entry. Index into the red-black tree.
179 * refcount - the number of outstanding reference to the entry. This is needed
180 * to protect against premature freeing of the entry by code
181 * concurrent calls to load, invalidate, and writeback. The lock
182 * for the zswap_tree structure that contains the entry must
183 * be held while changing the refcount. Since the lock must
184 * be held, there is no reason to also make refcount atomic.
185 * length - the length in bytes of the compressed page data. Needed during
186 * decompression
187 * pool - the zswap_pool the entry's data is in
188 * handle - zpool allocation handle that stores the compressed page data
189 * zero_flag - the flag indicating the page for the zswap_entry is a zero page.
190 * zswap does not store the page during compression.
191 * It memsets the page with 0 during decompression.
192 * #else
193 * zhandle - pointer to struct zswap_handle where length and handle are moved into.
194 * #endif
195 */
196#ifndef CONFIG_ZSWAP_SAME_PAGE_SHARING
197struct zswap_entry {
198 struct rb_node rbnode;
199 pgoff_t offset;
200 int refcount;
201 unsigned int length;
202 struct zswap_pool *pool;
203 unsigned long handle;
204 unsigned char zero_flag;
205};
206#else
207struct zswap_entry {
208 struct rb_node rbnode;
209 pgoff_t offset;
210 int refcount;
211 struct zswap_pool *pool;
212 struct zswap_handle *zhandle;
213 unsigned char zero_flag;
214};
215#endif
216
217#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
218struct zswap_header {
219 swp_entry_t swpentry;
220};
221#endif
222
223/*
224 * The tree lock in the zswap_tree struct protects a few things:
225 * - the rbtree
226 * - the refcount field of each entry in the tree
227 */
228struct zswap_tree {
229 struct rb_root rbroot;
230#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
231 struct rb_root zhandleroot;
232 void *buffer;
233#endif
234 spinlock_t lock;
235};
236
237static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
238
239/* RCU-protected iteration */
240static LIST_HEAD(zswap_pools);
241/* protects zswap_pools list modification */
242static DEFINE_SPINLOCK(zswap_pools_lock);
243/* pool counter to provide unique names to zpool */
244static atomic_t zswap_pools_count = ATOMIC_INIT(0);
245
246/* used by param callback function */
247static bool zswap_init_started;
248
249/* fatal error during init */
250static bool zswap_init_failed;
251
252/*********************************
253* helpers and fwd declarations
254**********************************/
255
256#define zswap_pool_debug(msg, p) \
257 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
258 zpool_get_type((p)->zpool))
259
260static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
261static int zswap_pool_get(struct zswap_pool *pool);
262static void zswap_pool_put(struct zswap_pool *pool);
263
264static const struct zpool_ops zswap_zpool_ops = {
265 .evict = zswap_writeback_entry
266};
267
268static bool zswap_is_full(void)
269{
270 return totalram_pages * zswap_max_pool_percent / 100 <
271 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
272}
273
274static void zswap_update_total_size(void)
275{
276 struct zswap_pool *pool;
277 u64 total = 0;
278
279 rcu_read_lock();
280
281 list_for_each_entry_rcu(pool, &zswap_pools, list)
282 total += zpool_get_total_size(pool->zpool);
283
284 rcu_read_unlock();
285
286 zswap_pool_total_size = total;
287 zswap_pool_pages = zswap_pool_total_size >> PAGE_SHIFT;
288}
289
290/*********************************
291* zswap entry functions
292**********************************/
293static struct kmem_cache *zswap_entry_cache;
294
295static int __init zswap_entry_cache_create(void)
296{
297 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
298 return zswap_entry_cache == NULL;
299}
300
301static void __init zswap_entry_cache_destroy(void)
302{
303 kmem_cache_destroy(zswap_entry_cache);
304}
305
306static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
307{
308 struct zswap_entry *entry;
309 entry = kmem_cache_alloc(zswap_entry_cache, gfp);
310 if (!entry)
311 return NULL;
312 entry->refcount = 1;
313 entry->zero_flag = 0;
314#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
315 entry->zhandle = NULL;
316#endif
317 RB_CLEAR_NODE(&entry->rbnode);
318 return entry;
319}
320
321static void zswap_entry_cache_free(struct zswap_entry *entry)
322{
323 kmem_cache_free(zswap_entry_cache, entry);
324}
325
326#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
327/*********************************
328* zswap handle functions
329**********************************/
330static struct kmem_cache *zswap_handle_cache;
331
332static int zswap_handle_cache_create(void)
333{
334 zswap_handle_cache = KMEM_CACHE(zswap_handle, 0);
335 return zswap_handle_cache == NULL;
336}
337
338static void __init zswap_handle_cache_destroy(void)
339{
340 kmem_cache_destroy(zswap_handle_cache);
341}
342
343static struct zswap_handle *zswap_handle_cache_alloc(gfp_t gfp)
344{
345 struct zswap_handle *zhandle;
346 zhandle = kmem_cache_alloc(zswap_handle_cache, gfp);
347 if (!zhandle)
348 return NULL;
349 zhandle->ref_count = 1;
350 RB_CLEAR_NODE(&zhandle->rbnode);
351 return zhandle;
352}
353
354static void zswap_handle_cache_free(struct zswap_handle *zhandle)
355{
356 kmem_cache_free(zswap_handle_cache, zhandle);
357}
358#endif
359
360/*********************************
361* rbtree functions
362**********************************/
363static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
364{
365 struct rb_node *node = root->rb_node;
366 struct zswap_entry *entry;
367
368 while (node) {
369 entry = rb_entry(node, struct zswap_entry, rbnode);
370 if (entry->offset > offset)
371 node = node->rb_left;
372 else if (entry->offset < offset)
373 node = node->rb_right;
374 else
375 return entry;
376 }
377 return NULL;
378}
379
380/*
381 * In the case that a entry with the same offset is found, a pointer to
382 * the existing entry is stored in dupentry and the function returns -EEXIST
383 */
384static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
385 struct zswap_entry **dupentry)
386{
387 struct rb_node **link = &root->rb_node, *parent = NULL;
388 struct zswap_entry *myentry;
389
390 while (*link) {
391 parent = *link;
392 myentry = rb_entry(parent, struct zswap_entry, rbnode);
393 if (myentry->offset > entry->offset)
394 link = &(*link)->rb_left;
395 else if (myentry->offset < entry->offset)
396 link = &(*link)->rb_right;
397 else {
398 *dupentry = myentry;
399 return -EEXIST;
400 }
401 }
402 rb_link_node(&entry->rbnode, parent, link);
403 rb_insert_color(&entry->rbnode, root);
404 return 0;
405}
406
407static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
408{
409 if (!RB_EMPTY_NODE(&entry->rbnode)) {
410 rb_erase(&entry->rbnode, root);
411 RB_CLEAR_NODE(&entry->rbnode);
412 }
413}
414
415#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
416static struct zswap_handle *zswap_handle_rb_search(struct rb_root *root,
417 u32 checksum)
418{
419 struct rb_node *node = root->rb_node;
420 struct zswap_handle *zhandle;
421
422 while (node) {
423 zhandle = rb_entry(node, struct zswap_handle, rbnode);
424 if (zhandle->checksum > checksum)
425 node = node->rb_left;
426 else if (zhandle->checksum < checksum)
427 node = node->rb_right;
428 else
429 return zhandle;
430 }
431 return NULL;
432}
433
434/*
435 * In the case that zhandle with the same checksum is found, a pointer to
436 * the existing zhandle is stored in duphandle and the function returns -EEXIST
437 */
438static int zswap_handle_rb_insert(struct rb_root *root,
439 struct zswap_handle *zhandle,
440 struct zswap_handle **duphandle)
441{
442 struct rb_node **link = &root->rb_node, *parent = NULL;
443 struct zswap_handle *myhandle;
444
445 while (*link) {
446 parent = *link;
447 myhandle = rb_entry(parent, struct zswap_handle, rbnode);
448 if (myhandle->checksum > zhandle->checksum)
449 link = &parent->rb_left;
450 else if (myhandle->checksum < zhandle->checksum)
451 link = &parent->rb_right;
452 else {
453 *duphandle = myhandle;
454 return -EEXIST;
455 }
456 }
457 rb_link_node(&zhandle->rbnode, parent, link);
458 rb_insert_color(&zhandle->rbnode, root);
459 return 0;
460}
461
462static void zswap_handle_erase(struct rb_root *root,
463 struct zswap_handle *zhandle)
464{
465 if (!RB_EMPTY_NODE(&zhandle->rbnode)) {
466 rb_erase(&zhandle->rbnode, root);
467 RB_CLEAR_NODE(&zhandle->rbnode);
468 }
469}
470
471static void zswap_free_handle(struct zswap_pool *pool, struct zswap_handle *zhandle)
472{
473 zpool_free(pool->zpool, zhandle->handle);
474 zswap_handle_cache_free(zhandle);
475}
476
477/* This function searches for the same page in the zhandle RB-Tree based on the
478 * checksum value of the new page. If the same page is found the zhandle of that
479 * page is returned.
480 */
481static struct zswap_handle *zswap_same_page_search(struct zswap_pool *pool, struct zswap_tree *tree,
482 u8 *uncmem, u32 checksum)
483{
484 int ret = 0;
485 unsigned int dlen = PAGE_SIZE;
486 u8 *src = NULL, *dst = NULL;
487 struct zswap_handle *myhandle = NULL;
488 struct crypto_comp *tfm;
489
490 myhandle = zswap_handle_rb_search(&tree->zhandleroot, checksum);
491 if (myhandle) {
492 /* Compare memory contents */
493 dst = (u8 *)tree->buffer;
494 src = (u8 *)zpool_map_handle(pool->zpool,
495 myhandle->handle, ZPOOL_MM_RO_NOWAIT);
496 if (!src)
497 return NULL;
498
499 if (myhandle->length == PAGE_SIZE)
500 copy_page(dst, src);
501 else {
502 tfm = *get_cpu_ptr(pool->tfm);
503 ret = crypto_comp_decompress(tfm, src, myhandle->length, dst, &dlen);
504 put_cpu_ptr(pool->tfm);
505 }
506
507 zpool_unmap_handle(pool->zpool, myhandle->handle);
508 BUG_ON(ret);
509
510 ret = memcmp(dst, uncmem, PAGE_SIZE);
511 if (ret)
512 myhandle = NULL;
513 }
514 return myhandle;
515}
516#endif
517
518/*
519 * Carries out the common pattern of freeing and entry's zpool allocation,
520 * freeing the entry itself, and decrementing the number of stored pages.
521 */
522static void zswap_free_entry(struct zswap_entry *entry)
523{
524 if (entry->zero_flag == 1) {
525 atomic_dec(&zswap_zero_pages);
526 goto zeropage_out;
527 }
528#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
529 entry->zhandle->ref_count--;
530 if (!entry->zhandle->ref_count)
531 zswap_free_handle(entry->pool, entry->zhandle);
532 else
533 atomic_dec(&zswap_duplicate_pages);
534#else
535 zpool_free(entry->pool->zpool, entry->handle);
536#endif
537zeropage_out:
538 zswap_pool_put(entry->pool);
539 zswap_entry_cache_free(entry);
540 atomic_dec(&zswap_stored_pages);
541 zswap_update_total_size();
542}
543
544/* caller must hold the tree lock */
545static void zswap_entry_get(struct zswap_entry *entry)
546{
547 entry->refcount++;
548}
549
550/* caller must hold the tree lock
551* remove from the tree and free it, if nobody reference the entry
552*/
553static void zswap_entry_put(struct zswap_tree *tree,
554 struct zswap_entry *entry)
555{
556 int refcount = --entry->refcount;
557
558 BUG_ON(refcount < 0);
559 if (refcount == 0) {
560#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
561 if (entry->zhandle && entry->zhandle->ref_count == 1)
562 zswap_handle_erase(&tree->zhandleroot, entry->zhandle);
563#endif
564 zswap_rb_erase(&tree->rbroot, entry);
565 zswap_free_entry(entry);
566 }
567}
568
569/* caller must hold the tree lock */
570static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
571 pgoff_t offset)
572{
573 struct zswap_entry *entry;
574
575 entry = zswap_rb_search(root, offset);
576 if (entry)
577 zswap_entry_get(entry);
578
579 return entry;
580}
581
582/*********************************
583* per-cpu code
584**********************************/
585static DEFINE_PER_CPU(u8 *, zswap_dstmem);
586
587static int __zswap_cpu_dstmem_notifier(unsigned long action, unsigned long cpu)
588{
589 u8 *dst;
590
591 switch (action) {
592 case CPU_UP_PREPARE:
593 dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
594 if (!dst) {
595 pr_err("can't allocate compressor buffer\n");
596 return NOTIFY_BAD;
597 }
598 per_cpu(zswap_dstmem, cpu) = dst;
599 break;
600 case CPU_DEAD:
601 case CPU_UP_CANCELED:
602 dst = per_cpu(zswap_dstmem, cpu);
603 kfree(dst);
604 per_cpu(zswap_dstmem, cpu) = NULL;
605 break;
606 default:
607 break;
608 }
609 return NOTIFY_OK;
610}
611
612static int zswap_cpu_dstmem_notifier(struct notifier_block *nb,
613 unsigned long action, void *pcpu)
614{
615 return __zswap_cpu_dstmem_notifier(action, (unsigned long)pcpu);
616}
617
618static struct notifier_block zswap_dstmem_notifier = {
619 .notifier_call = zswap_cpu_dstmem_notifier,
620};
621
622static int __init zswap_cpu_dstmem_init(void)
623{
624 unsigned long cpu;
625
626 cpu_notifier_register_begin();
627 for_each_online_cpu(cpu)
628 if (__zswap_cpu_dstmem_notifier(CPU_UP_PREPARE, cpu) ==
629 NOTIFY_BAD)
630 goto cleanup;
631 __register_cpu_notifier(&zswap_dstmem_notifier);
632 cpu_notifier_register_done();
633 return 0;
634
635cleanup:
636 for_each_online_cpu(cpu)
637 __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED, cpu);
638 cpu_notifier_register_done();
639 return -ENOMEM;
640}
641
642static void zswap_cpu_dstmem_destroy(void)
643{
644 unsigned long cpu;
645
646 cpu_notifier_register_begin();
647 for_each_online_cpu(cpu)
648 __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED, cpu);
649 __unregister_cpu_notifier(&zswap_dstmem_notifier);
650 cpu_notifier_register_done();
651}
652
653static int __zswap_cpu_comp_notifier(struct zswap_pool *pool,
654 unsigned long action, unsigned long cpu)
655{
656 struct crypto_comp *tfm;
657
658 switch (action) {
659 case CPU_UP_PREPARE:
660 if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu)))
661 break;
662 tfm = crypto_alloc_comp(pool->tfm_name, 0, 0);
663 if (IS_ERR_OR_NULL(tfm)) {
664 pr_err("could not alloc crypto comp %s : %ld\n",
665 pool->tfm_name, PTR_ERR(tfm));
666 return NOTIFY_BAD;
667 }
668 *per_cpu_ptr(pool->tfm, cpu) = tfm;
669 break;
670 case CPU_DEAD:
671 case CPU_UP_CANCELED:
672 tfm = *per_cpu_ptr(pool->tfm, cpu);
673 if (!IS_ERR_OR_NULL(tfm))
674 crypto_free_comp(tfm);
675 *per_cpu_ptr(pool->tfm, cpu) = NULL;
676 break;
677 default:
678 break;
679 }
680 return NOTIFY_OK;
681}
682
683static int zswap_cpu_comp_notifier(struct notifier_block *nb,
684 unsigned long action, void *pcpu)
685{
686 unsigned long cpu = (unsigned long)pcpu;
687 struct zswap_pool *pool = container_of(nb, typeof(*pool), notifier);
688
689 return __zswap_cpu_comp_notifier(pool, action, cpu);
690}
691
692static int zswap_cpu_comp_init(struct zswap_pool *pool)
693{
694 unsigned long cpu;
695
696 memset(&pool->notifier, 0, sizeof(pool->notifier));
697 pool->notifier.notifier_call = zswap_cpu_comp_notifier;
698
699 cpu_notifier_register_begin();
700 for_each_online_cpu(cpu)
701 if (__zswap_cpu_comp_notifier(pool, CPU_UP_PREPARE, cpu) ==
702 NOTIFY_BAD)
703 goto cleanup;
704 __register_cpu_notifier(&pool->notifier);
705 cpu_notifier_register_done();
706 return 0;
707
708cleanup:
709 for_each_online_cpu(cpu)
710 __zswap_cpu_comp_notifier(pool, CPU_UP_CANCELED, cpu);
711 cpu_notifier_register_done();
712 return -ENOMEM;
713}
714
715static void zswap_cpu_comp_destroy(struct zswap_pool *pool)
716{
717 unsigned long cpu;
718
719 cpu_notifier_register_begin();
720 for_each_online_cpu(cpu)
721 __zswap_cpu_comp_notifier(pool, CPU_UP_CANCELED, cpu);
722 __unregister_cpu_notifier(&pool->notifier);
723 cpu_notifier_register_done();
724}
725
726/*********************************
727* pool functions
728**********************************/
729
730static struct zswap_pool *__zswap_pool_current(void)
731{
732 struct zswap_pool *pool;
733
734 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
735 WARN_ON(!pool);
736
737 return pool;
738}
739
740static struct zswap_pool *zswap_pool_current(void)
741{
742 assert_spin_locked(&zswap_pools_lock);
743
744 return __zswap_pool_current();
745}
746
747static struct zswap_pool *zswap_pool_current_get(void)
748{
749 struct zswap_pool *pool;
750
751 rcu_read_lock();
752
753 pool = __zswap_pool_current();
754 if (!pool || !zswap_pool_get(pool))
755 pool = NULL;
756
757 rcu_read_unlock();
758
759 return pool;
760}
761
762#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
763static struct zswap_pool *zswap_pool_last_get(void)
764{
765 struct zswap_pool *pool, *last = NULL;
766
767 rcu_read_lock();
768
769 list_for_each_entry_rcu(pool, &zswap_pools, list)
770 last = pool;
771 if (!WARN_ON(!last) && !zswap_pool_get(last))
772 last = NULL;
773
774 rcu_read_unlock();
775
776 return last;
777}
778#endif
779
780/* type and compressor must be null-terminated */
781static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
782{
783 struct zswap_pool *pool;
784
785 assert_spin_locked(&zswap_pools_lock);
786
787 list_for_each_entry_rcu(pool, &zswap_pools, list) {
788 if (strcmp(pool->tfm_name, compressor))
789 continue;
790 if (strcmp(zpool_get_type(pool->zpool), type))
791 continue;
792 /* if we can't get it, it's about to be destroyed */
793 if (!zswap_pool_get(pool))
794 continue;
795 return pool;
796 }
797
798 return NULL;
799}
800
801static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
802{
803 struct zswap_pool *pool;
804 char name[38]; /* 'zswap' + 32 char (max) num + \0 */
805#ifdef CONFIG_ZSWAP_MIGRATION_SUPPORT
806 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM | __GFP_HIGHMEM | __GFP_MOVABLE;
807#else
808 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM | __GFP_HIGHMEM;
809#endif
810
811 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
812 if (!pool) {
813 pr_err("pool alloc failed\n");
814 return NULL;
815 }
816
817 /* unique name for each pool specifically required by zsmalloc */
818 snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
819
820 pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
821 if (!pool->zpool) {
822 pr_err("%s zpool not available\n", type);
823 goto error;
824 }
825 pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
826
827 strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
828 pool->tfm = alloc_percpu(struct crypto_comp *);
829 if (!pool->tfm) {
830 pr_err("percpu alloc failed\n");
831 goto error;
832 }
833
834 if (zswap_cpu_comp_init(pool))
835 goto error;
836 pr_debug("using %s compressor\n", pool->tfm_name);
837
838 /* being the current pool takes 1 ref; this func expects the
839 * caller to always add the new pool as the current pool
840 */
841 kref_init(&pool->kref);
842 INIT_LIST_HEAD(&pool->list);
843
844 zswap_pool_debug("created", pool);
845
846 return pool;
847
848error:
849 free_percpu(pool->tfm);
850 if (pool->zpool)
851 zpool_destroy_pool(pool->zpool);
852 kfree(pool);
853 return NULL;
854}
855
856static __init struct zswap_pool *__zswap_pool_create_fallback(void)
857{
858 if (!crypto_has_comp(zswap_compressor, 0, 0)) {
859 if (!strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) {
860 pr_err("default compressor %s not available\n",
861 zswap_compressor);
862 return NULL;
863 }
864 pr_err("compressor %s not available, using default %s\n",
865 zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT);
866 param_free_charp(&zswap_compressor);
867 zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
868 }
869 if (!zpool_has_pool(zswap_zpool_type)) {
870 if (!strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
871 pr_err("default zpool %s not available\n",
872 zswap_zpool_type);
873 return NULL;
874 }
875 pr_err("zpool %s not available, using default %s\n",
876 zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT);
877 param_free_charp(&zswap_zpool_type);
878 zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
879 }
880
881 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
882}
883
884static void zswap_pool_destroy(struct zswap_pool *pool)
885{
886 zswap_pool_debug("destroying", pool);
887
888 zswap_cpu_comp_destroy(pool);
889 free_percpu(pool->tfm);
890 zpool_destroy_pool(pool->zpool);
891 kfree(pool);
892}
893
894static int __must_check zswap_pool_get(struct zswap_pool *pool)
895{
896 return kref_get_unless_zero(&pool->kref);
897}
898
899static void __zswap_pool_release(struct rcu_head *head)
900{
901 struct zswap_pool *pool = container_of(head, typeof(*pool), rcu_head);
902
903 /* nobody should have been able to get a kref... */
904 WARN_ON(kref_get_unless_zero(&pool->kref));
905
906 /* pool is now off zswap_pools list and has no references. */
907 zswap_pool_destroy(pool);
908}
909
910static void __zswap_pool_empty(struct kref *kref)
911{
912 struct zswap_pool *pool;
913
914 pool = container_of(kref, typeof(*pool), kref);
915
916 spin_lock(&zswap_pools_lock);
917
918 WARN_ON(pool == zswap_pool_current());
919
920 list_del_rcu(&pool->list);
921 call_rcu(&pool->rcu_head, __zswap_pool_release);
922
923 spin_unlock(&zswap_pools_lock);
924}
925
926static void zswap_pool_put(struct zswap_pool *pool)
927{
928 kref_put(&pool->kref, __zswap_pool_empty);
929}
930
931/*********************************
932* param callbacks
933**********************************/
934
935/* val must be a null-terminated string */
936static int __zswap_param_set(const char *val, const struct kernel_param *kp,
937 char *type, char *compressor)
938{
939 struct zswap_pool *pool, *put_pool = NULL;
940 char *s = strstrip((char *)val);
941 int ret;
942
943 if (zswap_init_failed) {
944 pr_err("can't set param, initialization failed\n");
945 return -ENODEV;
946 }
947
948 /* no change required */
949 if (!strcmp(s, *(char **)kp->arg))
950 return 0;
951
952 /* if this is load-time (pre-init) param setting,
953 * don't create a pool; that's done during init.
954 */
955 if (!zswap_init_started)
956 return param_set_charp(s, kp);
957
958 if (!type) {
959 if (!zpool_has_pool(s)) {
960 pr_err("zpool %s not available\n", s);
961 return -ENOENT;
962 }
963 type = s;
964 } else if (!compressor) {
965 if (!crypto_has_comp(s, 0, 0)) {
966 pr_err("compressor %s not available\n", s);
967 return -ENOENT;
968 }
969 compressor = s;
970 } else {
971 WARN_ON(1);
972 return -EINVAL;
973 }
974
975 spin_lock(&zswap_pools_lock);
976
977 pool = zswap_pool_find_get(type, compressor);
978 if (pool) {
979 zswap_pool_debug("using existing", pool);
980 list_del_rcu(&pool->list);
981 } else {
982 spin_unlock(&zswap_pools_lock);
983 pool = zswap_pool_create(type, compressor);
984 spin_lock(&zswap_pools_lock);
985 }
986
987 if (pool)
988 ret = param_set_charp(s, kp);
989 else
990 ret = -EINVAL;
991
992 if (!ret) {
993 put_pool = zswap_pool_current();
994 list_add_rcu(&pool->list, &zswap_pools);
995 } else if (pool) {
996 /* add the possibly pre-existing pool to the end of the pools
997 * list; if it's new (and empty) then it'll be removed and
998 * destroyed by the put after we drop the lock
999 */
1000 list_add_tail_rcu(&pool->list, &zswap_pools);
1001 put_pool = pool;
1002 }
1003
1004 spin_unlock(&zswap_pools_lock);
1005
1006 /* drop the ref from either the old current pool,
1007 * or the new pool we failed to add
1008 */
1009 if (put_pool)
1010 zswap_pool_put(put_pool);
1011
1012 return ret;
1013}
1014
1015static int zswap_compressor_param_set(const char *val,
1016 const struct kernel_param *kp)
1017{
1018 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
1019}
1020
1021static int zswap_zpool_param_set(const char *val,
1022 const struct kernel_param *kp)
1023{
1024 return __zswap_param_set(val, kp, NULL, zswap_compressor);
1025}
1026
1027static int zswap_enabled_param_set(const char *val,
1028 const struct kernel_param *kp)
1029{
1030 if (zswap_init_failed) {
1031 pr_err("can't enable, initialization failed\n");
1032 return -ENODEV;
1033 }
1034
1035 return param_set_bool(val, kp);
1036}
1037
1038/*********************************
1039* writeback code
1040**********************************/
1041/* return enum for zswap_get_swap_cache_page */
1042enum zswap_get_swap_ret {
1043 ZSWAP_SWAPCACHE_NEW,
1044 ZSWAP_SWAPCACHE_EXIST,
1045 ZSWAP_SWAPCACHE_FAIL,
1046};
1047
1048#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1049/*
1050 * zswap_get_swap_cache_page
1051 *
1052 * This is an adaption of read_swap_cache_async()
1053 *
1054 * This function tries to find a page with the given swap entry
1055 * in the swapper_space address space (the swap cache). If the page
1056 * is found, it is returned in retpage. Otherwise, a page is allocated,
1057 * added to the swap cache, and returned in retpage.
1058 *
1059 * If success, the swap cache page is returned in retpage
1060 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
1061 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
1062 * the new page is added to swapcache and locked
1063 * Returns ZSWAP_SWAPCACHE_FAIL on error
1064 */
1065static int zswap_get_swap_cache_page(swp_entry_t entry,
1066 struct page **retpage)
1067{
1068 bool page_was_allocated;
1069
1070 *retpage = __read_swap_cache_async(entry, GFP_KERNEL,
1071 NULL, 0, &page_was_allocated);
1072 if (page_was_allocated)
1073 return ZSWAP_SWAPCACHE_NEW;
1074 if (!*retpage)
1075 return ZSWAP_SWAPCACHE_FAIL;
1076 return ZSWAP_SWAPCACHE_EXIST;
1077}
1078
1079/*
1080 * Attempts to free an entry by adding a page to the swap cache,
1081 * decompressing the entry data into the page, and issuing a
1082 * bio write to write the page back to the swap device.
1083 *
1084 * This can be thought of as a "resumed writeback" of the page
1085 * to the swap device. We are basically resuming the same swap
1086 * writeback path that was intercepted with the frontswap_store()
1087 * in the first place. After the page has been decompressed into
1088 * the swap cache, the compressed version stored by zswap can be
1089 * freed.
1090 */
1091static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
1092{
1093 struct zswap_header *zhdr;
1094 swp_entry_t swpentry;
1095 struct zswap_tree *tree;
1096 pgoff_t offset;
1097 struct zswap_entry *entry;
1098 struct page *page;
1099 struct crypto_comp *tfm;
1100 u8 *src, *dst;
1101 unsigned int dlen;
1102 int ret;
1103 struct writeback_control wbc = {
1104 .sync_mode = WB_SYNC_NONE,
1105 };
1106
1107 /* extract swpentry from data */
1108 zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
1109 swpentry = zhdr->swpentry; /* here */
1110 zpool_unmap_handle(pool, handle);
1111 tree = zswap_trees[swp_type(swpentry)];
1112 offset = swp_offset(swpentry);
1113
1114 /* find and ref zswap entry */
1115 spin_lock(&tree->lock);
1116 entry = zswap_entry_find_get(&tree->rbroot, offset);
1117 if (!entry) {
1118 /* entry was invalidated */
1119 spin_unlock(&tree->lock);
1120 return 0;
1121 }
1122 spin_unlock(&tree->lock);
1123 BUG_ON(offset != entry->offset);
1124
1125 /* try to allocate swap cache page */
1126 switch (zswap_get_swap_cache_page(swpentry, &page)) {
1127 case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
1128 ret = -ENOMEM;
1129 goto fail;
1130
1131 case ZSWAP_SWAPCACHE_EXIST:
1132 /* page is already in the swap cache, ignore for now */
1133 page_cache_release(page);
1134 ret = -EEXIST;
1135 goto fail;
1136
1137 case ZSWAP_SWAPCACHE_NEW: /* page is locked */
1138 /* decompress */
1139 dlen = PAGE_SIZE;
1140 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
1141 ZPOOL_MM_RO) + sizeof(struct zswap_header);
1142 dst = kmap_atomic(page);
1143 tfm = *get_cpu_ptr(entry->pool->tfm);
1144 ret = crypto_comp_decompress(tfm, src, entry->length,
1145 dst, &dlen);
1146 put_cpu_ptr(entry->pool->tfm);
1147 kunmap_atomic(dst);
1148 zpool_unmap_handle(entry->pool->zpool, entry->handle);
1149 BUG_ON(ret);
1150 BUG_ON(dlen != PAGE_SIZE);
1151
1152 /* page is up to date */
1153 SetPageUptodate(page);
1154 }
1155
1156 /* move it to the tail of the inactive list after end_writeback */
1157 SetPageReclaim(page);
1158
1159 /* start writeback */
1160 __swap_writepage(page, &wbc, end_swap_bio_write);
1161 page_cache_release(page);
1162 zswap_written_back_pages++;
1163
1164 spin_lock(&tree->lock);
1165 /* drop local reference */
1166 zswap_entry_put(tree, entry);
1167
1168 /*
1169 * There are two possible situations for entry here:
1170 * (1) refcount is 1(normal case), entry is valid and on the tree
1171 * (2) refcount is 0, entry is freed and not on the tree
1172 * because invalidate happened during writeback
1173 * search the tree and free the entry if find entry
1174 */
1175 if (entry == zswap_rb_search(&tree->rbroot, offset))
1176 zswap_entry_put(tree, entry);
1177 spin_unlock(&tree->lock);
1178
1179 goto end;
1180
1181 /*
1182 * if we get here due to ZSWAP_SWAPCACHE_EXIST
1183 * a load may happening concurrently
1184 * it is safe and okay to not free the entry
1185 * if we free the entry in the following put
1186 * it it either okay to return !0
1187 */
1188fail:
1189 spin_lock(&tree->lock);
1190 zswap_entry_put(tree, entry);
1191 spin_unlock(&tree->lock);
1192
1193end:
1194 return ret;
1195}
1196
1197static int zswap_shrink(void)
1198{
1199 struct zswap_pool *pool;
1200 int ret;
1201
1202 pool = zswap_pool_last_get();
1203 if (!pool)
1204 return -ENOENT;
1205
1206 ret = zpool_shrink(pool->zpool, 1, NULL);
1207
1208 zswap_pool_put(pool);
1209
1210 return ret;
1211}
1212#else
1213static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
1214{
1215 return -EINVAL;
1216}
1217
1218static int zswap_shrink(void)
1219{
1220 return -EINVAL;
1221}
1222#endif /* CONFIG_ZSWAP_ENABLE_WRITEBACK */
1223
1224static int page_zero_filled(void *ptr)
1225{
1226 unsigned int pos;
1227 unsigned long *page;
1228
1229 page = (unsigned long *)ptr;
1230
1231 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
1232 if (page[pos])
1233 return 0;
1234 }
1235
1236 return 1;
1237}
1238
1239/*********************************
1240* frontswap hooks
1241**********************************/
1242/* attempts to compress and store an single page */
1243static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1244 struct page *page)
1245{
1246 struct zswap_tree *tree = zswap_trees[type];
1247 struct zswap_entry *entry, *dupentry;
1248 struct crypto_comp *tfm;
1249 int ret;
1250 unsigned int dlen = PAGE_SIZE, len;
1251 unsigned long handle;
1252 char *buf;
1253 u8 *src, *dst;
1254#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1255 struct zswap_header *zhdr;
1256#endif
1257#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1258 struct zswap_handle *zhandle = NULL, *duphandle = NULL;
1259 u32 checksum = 0;
1260#endif
1261
1262 if (!zswap_enabled || !tree) {
1263 ret = -ENODEV;
1264 goto reject;
1265 }
1266
1267 /* if this page got EIO on pageout before, give up immediately */
1268 if (PageError(page)) {
1269 ret = -ENOMEM;
1270 goto reject;
1271 }
1272
1273 /* reclaim space if needed */
1274 if (zswap_is_full()) {
1275 zswap_pool_limit_hit++;
1276 if (zswap_shrink()) {
1277 zswap_reject_reclaim_fail++;
1278 ret = -ENOMEM;
1279 goto reject;
1280 }
1281 }
1282
1283 /* allocate entry */
1284 entry = zswap_entry_cache_alloc(GFP_KERNEL);
1285 if (!entry) {
1286 zswap_reject_kmemcache_fail++;
1287 ret = -ENOMEM;
1288 goto reject;
1289 }
1290
1291 /* if entry is successfully added, it keeps the reference */
1292 entry->pool = zswap_pool_current_get();
1293 if (!entry->pool) {
1294 ret = -EINVAL;
1295 goto freepage;
1296 }
1297
1298 /* compress */
1299 src = kmap_atomic(page);
1300 if (page_zero_filled(src)) {
1301 atomic_inc(&zswap_zero_pages);
1302 entry->zero_flag = 1;
1303 kunmap_atomic(src);
1304
1305#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1306 entry->offset = offset;
1307 goto insert_entry;
1308#else
1309 handle = 0;
1310 dlen = PAGE_SIZE;
1311 goto zeropage_out;
1312#endif
1313 }
1314#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1315 checksum = jhash2((const u32 *)src, PAGE_SIZE / 4, 17);
1316 spin_lock(&tree->lock);
1317 zhandle = zswap_same_page_search(entry->pool, tree, src, checksum);
1318 if (zhandle) {
1319 entry->offset = offset;
1320 entry->zhandle = zhandle;
1321 entry->zhandle->ref_count++;
1322 spin_unlock(&tree->lock);
1323 kunmap_atomic(src);
1324 atomic_inc(&zswap_duplicate_pages);
1325 goto insert_entry;
1326 }
1327 spin_unlock(&tree->lock);
1328#endif
1329
1330 /* compress */
1331 dst = get_cpu_var(zswap_dstmem);
1332 tfm = *get_cpu_ptr(entry->pool->tfm);
1333 ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen);
1334 kunmap_atomic(src);
1335 put_cpu_ptr(entry->pool->tfm);
1336 if (ret) {
1337 ret = -EINVAL;
1338 goto put_dstmem;
1339 }
1340
1341 /* store */
1342 if (dlen > PAGE_SIZE)
1343 dlen = PAGE_SIZE;
1344 len = dlen;
1345#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1346 len += sizeof(struct zswap_header);
1347#endif
1348 ret = zpool_malloc(entry->pool->zpool, len,
1349 __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
1350 &handle);
1351 if (ret == -ENOSPC) {
1352 zswap_reject_compress_poor++;
1353 goto put_dstmem;
1354 }
1355 if (ret) {
1356 zswap_reject_alloc_fail++;
1357 goto put_dstmem;
1358 }
1359#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1360 zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
1361 zhdr->swpentry = swp_entry(type, offset);
1362 buf = (u8 *)(zhdr + 1);
1363 memcpy(buf, dst, dlen);
1364#else
1365 buf = (u8 *)zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
1366 if (dlen == PAGE_SIZE) {
1367 src = kmap_atomic(page);
1368 copy_page(buf, src);
1369 kunmap_atomic(src);
1370 } else
1371 memcpy(buf, dst, dlen);
1372#endif
1373 zpool_unmap_handle(entry->pool->zpool, handle);
1374 put_cpu_var(zswap_dstmem);
1375
1376#ifndef CONFIG_ZSWAP_SAME_PAGE_SHARING
1377zeropage_out:
1378#endif
1379 /* populate entry */
1380 entry->offset = offset;
1381#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1382 zhandle = zswap_handle_cache_alloc(GFP_KERNEL);
1383 if (!zhandle) {
1384 ret = -ENOMEM;
1385 goto freeentry;
1386 }
1387 entry->zhandle = zhandle;
1388 entry->zhandle->handle = handle;
1389 entry->zhandle->length = dlen;
1390 entry->zhandle->checksum = checksum;
1391#else
1392 entry->handle = handle;
1393 entry->length = dlen;
1394#endif
1395
1396#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1397 spin_lock(&tree->lock);
1398 ret = zswap_handle_rb_insert(&tree->zhandleroot,
1399 entry->zhandle, &duphandle);
1400 spin_unlock(&tree->lock);
1401
1402insert_entry:
1403#endif
1404 /* map */
1405 spin_lock(&tree->lock);
1406 do {
1407 ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
1408 if (ret == -EEXIST) {
1409 zswap_duplicate_entry++;
1410 /* remove from rbtree */
1411 zswap_rb_erase(&tree->rbroot, dupentry);
1412 zswap_entry_put(tree, dupentry);
1413 }
1414 } while (ret == -EEXIST);
1415 spin_unlock(&tree->lock);
1416
1417 /* update stats */
1418 atomic_inc(&zswap_stored_pages);
1419 zswap_update_total_size();
1420
1421 return 0;
1422
1423put_dstmem:
1424 put_cpu_var(zswap_dstmem);
1425#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1426freeentry:
1427#endif
1428 zswap_pool_put(entry->pool);
1429freepage:
1430 zswap_entry_cache_free(entry);
1431reject:
1432 return ret;
1433}
1434
1435static void hexdump(char *title, u8 *data, int len)
1436{
1437 int i;
1438
1439 printk("%s: length = %d @ %p\n", title, len, data);
1440 for (i = 0; i < len; i++) {
1441 printk("%02x ", data[i]);
1442 if ((i & 0xf) == 0xf)
1443 printk("\n");
1444 }
1445 printk("\n");
1446}
1447
1448static void check_single_bitflip(struct zswap_entry *entry,
1449 unsigned char *src, size_t src_size, unsigned char *dst)
1450{
1451 struct crypto_comp *tfm;
1452 unsigned int dlen;
1453 int byte_offset, bit_offset, type;
1454 int i, j, ret, success = 0;
1455 char debug_msg[32];
1456
1457 for (i = 0; i < src_size; i++) {
1458 for (j = 0; j < 8; j++) {
1459 unsigned char org_byte = src[i];
1460 unsigned char tmp_byte = src[i];
1461
1462 if (((tmp_byte >> j) & 0x1) == 0x1)
1463 tmp_byte &= ~(1 << j);
1464 else
1465 tmp_byte |= (1 << j);
1466 src[i] = tmp_byte;
1467
1468 dlen = PAGE_SIZE;
1469 tfm = *get_cpu_ptr(entry->pool->tfm);
1470 ret = crypto_comp_decompress(tfm, src, src_size, dst, &dlen);
1471 put_cpu_ptr(entry->pool->tfm);
1472
1473 if (ret == 0) {
1474 if (success == 0) {
1475 type = ((org_byte >> j) & 0x1);
1476 byte_offset = (i / 4) * 4;
1477 bit_offset = ((i % 4) * 8) + j;
1478 }
1479 if (success < 99)
1480 success++;
1481 }
1482 src[i] = org_byte;
1483 }
1484 }
1485
1486 if (success)
1487 sprintf(debug_msg, "%02d/0x%16p/%02d/%d", success, src + byte_offset, bit_offset, type);
1488 else
1489 sprintf(debug_msg, "%02d/0x%16p", success, src);
1490
1491 sec_debug_set_extra_info_zswap(debug_msg);
1492
1493 return;
1494}
1495
1496/*
1497 * returns 0 if the page was successfully decompressed
1498 * return -1 on entry not found or error
1499*/
1500static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1501 struct page *page)
1502{
1503 struct zswap_tree *tree = zswap_trees[type];
1504 struct zswap_entry *entry;
1505 struct crypto_comp *tfm;
1506 u8 *src, *dst;
1507 unsigned int dlen;
1508 int ret = 0;
1509
1510 /* find */
1511 spin_lock(&tree->lock);
1512 entry = zswap_entry_find_get(&tree->rbroot, offset);
1513 if (!entry) {
1514 /* entry was written back */
1515 spin_unlock(&tree->lock);
1516 return -1;
1517 }
1518 spin_unlock(&tree->lock);
1519
1520 if (entry->zero_flag == 1) {
1521 dst = kmap_atomic(page);
1522 memset(dst, 0, PAGE_SIZE);
1523 kunmap_atomic(dst);
1524 goto zeropage_out;
1525 }
1526
1527 /* decompress */
1528 dlen = PAGE_SIZE;
1529#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1530 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->zhandle->handle,
1531 ZPOOL_MM_RO);
1532#else
1533 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
1534 ZPOOL_MM_RO);
1535#endif
1536 dst = kmap_atomic(page);
1537#ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1538 src += sizeof(struct zswap_header);
1539 tfm = *get_cpu_ptr(entry->pool->tfm);
1540 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);
1541 put_cpu_ptr(entry->pool->tfm);
1542#else
1543#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1544 if (entry->zhandle->length == PAGE_SIZE)
1545 copy_page(dst, src);
1546 else {
1547 tfm = *get_cpu_ptr(entry->pool->tfm);
1548 ret = crypto_comp_decompress(tfm, src, entry->zhandle->length, dst, &dlen);
1549 put_cpu_ptr(entry->pool->tfm);
1550 }
1551#else
1552 if (entry->length == PAGE_SIZE)
1553 copy_page(dst, src);
1554 else {
1555 tfm = *get_cpu_ptr(entry->pool->tfm);
1556 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);
1557 put_cpu_ptr(entry->pool->tfm);
1558 }
1559#endif
1560#endif
1561 if (ret) {
1562#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1563 hexdump("src buffer", src, entry->zhandle->length);
1564#else
1565 hexdump("src buffer", src, entry->length);
1566#endif
1567 if (dlen)
1568 hexdump("dest buffer", dst, dlen);
1569 printk("zswap_comp_op returned %d\n", ret);
1570#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1571 check_single_bitflip(entry, src, entry->zhandle->length, dst);
1572#else
1573 check_single_bitflip(entry, src, entry->length, dst);
1574#endif
1575 }
1576
1577 kunmap_atomic(dst);
1578#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1579 zpool_unmap_handle(entry->pool->zpool, entry->zhandle->handle);
1580#else
1581 zpool_unmap_handle(entry->pool->zpool, entry->handle);
1582#endif
1583 BUG_ON(ret);
1584
1585zeropage_out:
1586 spin_lock(&tree->lock);
1587 zswap_entry_put(tree, entry);
1588 spin_unlock(&tree->lock);
1589
1590 return 0;
1591}
1592
1593/* frees an entry in zswap */
1594static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1595{
1596 struct zswap_tree *tree = zswap_trees[type];
1597 struct zswap_entry *entry;
1598
1599 /* find */
1600 spin_lock(&tree->lock);
1601 entry = zswap_rb_search(&tree->rbroot, offset);
1602 if (!entry) {
1603 /* entry was written back */
1604 spin_unlock(&tree->lock);
1605 return;
1606 }
1607
1608 /* remove from rbtree */
1609 zswap_rb_erase(&tree->rbroot, entry);
1610
1611 /* drop the initial reference from entry creation */
1612 zswap_entry_put(tree, entry);
1613
1614 spin_unlock(&tree->lock);
1615}
1616
1617/* frees all zswap entries for the given swap type */
1618static void zswap_frontswap_invalidate_area(unsigned type)
1619{
1620 struct zswap_tree *tree = zswap_trees[type];
1621 struct zswap_entry *entry, *n;
1622
1623 if (!tree)
1624 return;
1625
1626 /* walk the tree and free everything */
1627 spin_lock(&tree->lock);
1628 rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
1629 zswap_free_entry(entry);
1630 tree->rbroot = RB_ROOT;
1631#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1632 tree->zhandleroot = RB_ROOT;
1633#endif
1634 spin_unlock(&tree->lock);
1635#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1636 free_page((unsigned long)tree->buffer);
1637#endif
1638 kfree(tree);
1639 zswap_trees[type] = NULL;
1640}
1641
1642static void zswap_frontswap_init(unsigned type)
1643{
1644 struct zswap_tree *tree;
1645
1646 tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
1647 if (!tree) {
1648 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1649 return;
1650 }
1651#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1652 tree->buffer = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1653 if (!tree->buffer) {
1654 pr_err("zswap: Error allocating compressor buffer\n");
1655 kfree(tree);
1656 return;
1657 }
1658 tree->zhandleroot = RB_ROOT;
1659#endif
1660
1661 tree->rbroot = RB_ROOT;
1662 spin_lock_init(&tree->lock);
1663 zswap_trees[type] = tree;
1664}
1665
1666static struct frontswap_ops zswap_frontswap_ops = {
1667 .store = zswap_frontswap_store,
1668 .load = zswap_frontswap_load,
1669 .invalidate_page = zswap_frontswap_invalidate_page,
1670 .invalidate_area = zswap_frontswap_invalidate_area,
1671 .init = zswap_frontswap_init
1672};
1673
1674/*********************************
1675* debugfs functions
1676**********************************/
1677#ifdef CONFIG_DEBUG_FS
1678#include <linux/debugfs.h>
1679
1680static struct dentry *zswap_debugfs_root;
1681
1682static int __init zswap_debugfs_init(void)
1683{
1684 if (!debugfs_initialized())
1685 return -ENODEV;
1686
1687 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1688 if (!zswap_debugfs_root)
1689 return -ENOMEM;
1690
1691 debugfs_create_u64("pool_limit_hit", S_IRUGO,
1692 zswap_debugfs_root, &zswap_pool_limit_hit);
1693 debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
1694 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1695 debugfs_create_u64("reject_alloc_fail", S_IRUGO,
1696 zswap_debugfs_root, &zswap_reject_alloc_fail);
1697 debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
1698 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1699 debugfs_create_u64("reject_compress_poor", S_IRUGO,
1700 zswap_debugfs_root, &zswap_reject_compress_poor);
1701 debugfs_create_u64("written_back_pages", S_IRUGO,
1702 zswap_debugfs_root, &zswap_written_back_pages);
1703 debugfs_create_u64("duplicate_entry", S_IRUGO,
1704 zswap_debugfs_root, &zswap_duplicate_entry);
1705 debugfs_create_u64("pool_total_size", S_IRUGO,
1706 zswap_debugfs_root, &zswap_pool_total_size);
1707 debugfs_create_u64("pool_pages", S_IRUGO,
1708 zswap_debugfs_root, &zswap_pool_pages);
1709 debugfs_create_atomic_t("stored_pages", S_IRUGO,
1710 zswap_debugfs_root, &zswap_stored_pages);
1711#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1712 debugfs_create_atomic_t("duplicate_pages", S_IRUGO,
1713 zswap_debugfs_root, &zswap_duplicate_pages);
1714#endif
1715 debugfs_create_atomic_t("zero_pages", S_IRUGO,
1716 zswap_debugfs_root, &zswap_zero_pages);
1717
1718 return 0;
1719}
1720
1721static void __exit zswap_debugfs_exit(void)
1722{
1723 debugfs_remove_recursive(zswap_debugfs_root);
1724}
1725#else
1726static int __init zswap_debugfs_init(void)
1727{
1728 return 0;
1729}
1730
1731static void __exit zswap_debugfs_exit(void) { }
1732#endif
1733
1734/*********************************
1735* module init and exit
1736**********************************/
1737static int __init init_zswap(void)
1738{
1739 struct zswap_pool *pool;
1740
1741 zswap_init_started = true;
1742
1743 if (zswap_entry_cache_create()) {
1744 pr_err("entry cache creation failed\n");
1745 goto cache_fail;
1746 }
1747
1748#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1749 if (zswap_handle_cache_create()) {
1750 pr_err("handle cache creation failed\n");
1751 goto handlecachefail;
1752 }
1753#endif
1754 if (zswap_cpu_dstmem_init()) {
1755 pr_err("dstmem alloc failed\n");
1756 goto dstmem_fail;
1757 }
1758
1759 pool = __zswap_pool_create_fallback();
1760 if (!pool) {
1761 pr_err("pool creation failed\n");
1762 goto pool_fail;
1763 }
1764 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1765 zpool_get_type(pool->zpool));
1766
1767 list_add(&pool->list, &zswap_pools);
1768
1769 frontswap_register_ops(&zswap_frontswap_ops);
1770 if (zswap_debugfs_init())
1771 pr_warn("debugfs initialization failed\n");
1772 return 0;
1773
1774pool_fail:
1775 zswap_cpu_dstmem_destroy();
1776dstmem_fail:
1777#ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1778 zswap_handle_cache_destroy();
1779handlecachefail:
1780#endif
1781 zswap_entry_cache_destroy();
1782cache_fail:
1783 /* if built-in, we aren't unloaded on failure; don't allow use */
1784 zswap_init_failed = true;
1785 zswap_enabled = false;
1786 return -ENOMEM;
1787}
1788/* must be late so crypto has time to come up */
1789late_initcall(init_zswap);
1790
1791MODULE_LICENSE("GPL");
1792MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
1793MODULE_DESCRIPTION("Compressed cache for swap pages");