2013-07-11 07:05:03 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
2014-08-07 07:08:40 +08:00
|
|
|
#include <linux/zpool.h>
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
#include <linux/mm_types.h>
|
|
|
|
#include <linux/page-flags.h>
|
|
|
|
#include <linux/swapops.h>
|
|
|
|
#include <linux/writeback.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* statistics
|
|
|
|
**********************************/
|
2014-08-07 07:08:40 +08:00
|
|
|
/* Total bytes used by the compressed storage */
|
|
|
|
static u64 zswap_pool_total_size;
|
2013-07-11 07:05:03 +08:00
|
|
|
/* 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;
|
2014-01-24 07:52:48 +08:00
|
|
|
module_param_named(enabled, zswap_enabled, bool, 0444);
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
/* Compressor to be used by zswap (fixed at boot for now) */
|
|
|
|
#define ZSWAP_COMPRESSOR_DEFAULT "lzo"
|
|
|
|
static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
|
2014-01-24 07:52:48 +08:00
|
|
|
module_param_named(compressor, zswap_compressor, charp, 0444);
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2014-08-07 07:08:40 +08:00
|
|
|
/* 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;
|
2014-04-08 06:38:27 +08:00
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
/*********************************
|
|
|
|
* 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\n", 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\n", zswap_compressor);
|
|
|
|
|
|
|
|
/* alloc percpu transforms */
|
|
|
|
zswap_comp_pcpu_tfms = alloc_percpu(struct crypto_comp *);
|
|
|
|
if (!zswap_comp_pcpu_tfms)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 08:57:15 +08:00
|
|
|
static void __init zswap_comp_exit(void)
|
2013-07-11 07:05:03 +08:00
|
|
|
{
|
|
|
|
/* free percpu transforms */
|
|
|
|
if (zswap_comp_pcpu_tfms)
|
|
|
|
free_percpu(zswap_comp_pcpu_tfms);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* 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
|
2014-04-08 06:38:25 +08:00
|
|
|
* concurrent calls to load, invalidate, and writeback. The lock
|
2013-07-11 07:05:03 +08:00
|
|
|
* 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.
|
2014-08-07 07:08:40 +08:00
|
|
|
* handle - zpool allocation handle that stores the compressed page data
|
2013-07-11 07:05:03 +08:00
|
|
|
* length - the length in bytes of the compressed page data. Needed during
|
2014-04-08 06:38:25 +08:00
|
|
|
* decompression
|
2013-07-11 07:05:03 +08:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* zswap entry functions
|
|
|
|
**********************************/
|
|
|
|
static struct kmem_cache *zswap_entry_cache;
|
|
|
|
|
2014-12-13 08:57:15 +08:00
|
|
|
static int __init zswap_entry_cache_create(void)
|
2013-07-11 07:05:03 +08:00
|
|
|
{
|
|
|
|
zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
|
2014-04-08 06:38:28 +08:00
|
|
|
return zswap_entry_cache == NULL;
|
2013-07-11 07:05:03 +08:00
|
|
|
}
|
|
|
|
|
2014-08-09 05:19:35 +08:00
|
|
|
static void __init zswap_entry_cache_destroy(void)
|
2013-07-11 07:05:03 +08:00
|
|
|
{
|
|
|
|
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;
|
2013-11-13 07:08:27 +08:00
|
|
|
RB_CLEAR_NODE(&entry->rbnode);
|
2013-07-11 07:05:03 +08:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zswap_entry_cache_free(struct zswap_entry *entry)
|
|
|
|
{
|
|
|
|
kmem_cache_free(zswap_entry_cache, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-11-13 07:08:27 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-08-07 07:08:40 +08:00
|
|
|
* Carries out the common pattern of freeing and entry's zpool allocation,
|
2013-11-13 07:08:27 +08:00
|
|
|
* freeing the entry itself, and decrementing the number of stored pages.
|
|
|
|
*/
|
2014-04-08 06:38:27 +08:00
|
|
|
static void zswap_free_entry(struct zswap_entry *entry)
|
2013-11-13 07:08:27 +08:00
|
|
|
{
|
2014-08-07 07:08:40 +08:00
|
|
|
zpool_free(zswap_pool, entry->handle);
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_entry_cache_free(entry);
|
|
|
|
atomic_dec(&zswap_stored_pages);
|
2014-08-07 07:08:40 +08:00
|
|
|
zswap_pool_total_size = zpool_get_total_size(zswap_pool);
|
2013-11-13 07:08:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2014-04-08 06:38:27 +08:00
|
|
|
zswap_free_entry(entry);
|
2013-11-13 07:08:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
/*********************************
|
|
|
|
* 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\n");
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
}
|
|
|
|
*per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm;
|
2014-06-05 07:11:11 +08:00
|
|
|
dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
|
2013-07-11 07:05:03 +08:00
|
|
|
if (!dst) {
|
|
|
|
pr_err("can't allocate compressor buffer\n");
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2014-12-13 08:57:15 +08:00
|
|
|
static int __init zswap_cpu_init(void)
|
2013-07-11 07:05:03 +08:00
|
|
|
{
|
|
|
|
unsigned long cpu;
|
|
|
|
|
2014-03-11 04:42:40 +08:00
|
|
|
cpu_notifier_register_begin();
|
2013-07-11 07:05:03 +08:00
|
|
|
for_each_online_cpu(cpu)
|
|
|
|
if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK)
|
|
|
|
goto cleanup;
|
2014-03-11 04:42:40 +08:00
|
|
|
__register_cpu_notifier(&zswap_cpu_notifier_block);
|
|
|
|
cpu_notifier_register_done();
|
2013-07-11 07:05:03 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for_each_online_cpu(cpu)
|
|
|
|
__zswap_cpu_notifier(CPU_UP_CANCELED, cpu);
|
2014-03-11 04:42:40 +08:00
|
|
|
cpu_notifier_register_done();
|
2013-07-11 07:05:03 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* helpers
|
|
|
|
**********************************/
|
|
|
|
static bool zswap_is_full(void)
|
|
|
|
{
|
2014-04-08 06:38:28 +08:00
|
|
|
return totalram_pages * zswap_max_pool_percent / 100 <
|
2014-08-07 07:08:40 +08:00
|
|
|
DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
|
2013-07-11 07:05:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* writeback code
|
|
|
|
**********************************/
|
|
|
|
/* return enum for zswap_get_swap_cache_page */
|
|
|
|
enum zswap_get_swap_ret {
|
|
|
|
ZSWAP_SWAPCACHE_NEW,
|
|
|
|
ZSWAP_SWAPCACHE_EXIST,
|
2013-11-13 07:08:26 +08:00
|
|
|
ZSWAP_SWAPCACHE_FAIL,
|
2013-07-11 07:05:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2013-11-13 07:08:26 +08:00
|
|
|
* 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
|
2013-07-11 07:05:03 +08:00
|
|
|
*/
|
|
|
|
static int zswap_get_swap_cache_page(swp_entry_t entry,
|
|
|
|
struct page **retpage)
|
|
|
|
{
|
|
|
|
struct page *found_page, *new_page = NULL;
|
2013-09-12 05:20:22 +08:00
|
|
|
struct address_space *swapper_space = swap_address_space(entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
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.
|
|
|
|
*/
|
mm: memcontrol: rewrite uncharge API
The memcg uncharging code that is involved towards the end of a page's
lifetime - truncation, reclaim, swapout, migration - is impressively
complicated and fragile.
Because anonymous and file pages were always charged before they had their
page->mapping established, uncharges had to happen when the page type
could still be known from the context; as in unmap for anonymous, page
cache removal for file and shmem pages, and swap cache truncation for swap
pages. However, these operations happen well before the page is actually
freed, and so a lot of synchronization is necessary:
- Charging, uncharging, page migration, and charge migration all need
to take a per-page bit spinlock as they could race with uncharging.
- Swap cache truncation happens during both swap-in and swap-out, and
possibly repeatedly before the page is actually freed. This means
that the memcg swapout code is called from many contexts that make
no sense and it has to figure out the direction from page state to
make sure memory and memory+swap are always correctly charged.
- On page migration, the old page might be unmapped but then reused,
so memcg code has to prevent untimely uncharging in that case.
Because this code - which should be a simple charge transfer - is so
special-cased, it is not reusable for replace_page_cache().
But now that charged pages always have a page->mapping, introduce
mem_cgroup_uncharge(), which is called after the final put_page(), when we
know for sure that nobody is looking at the page anymore.
For page migration, introduce mem_cgroup_migrate(), which is called after
the migration is successful and the new page is fully rmapped. Because
the old page is no longer uncharged after migration, prevent double
charges by decoupling the page's memcg association (PCG_USED and
pc->mem_cgroup) from the page holding an actual charge. The new bits
PCG_MEM and PCG_MEMSW represent the respective charges and are transferred
to the new page during migration.
mem_cgroup_migrate() is suitable for replace_page_cache() as well,
which gets rid of mem_cgroup_replace_page_cache(). However, care
needs to be taken because both the source and the target page can
already be charged and on the LRU when fuse is splicing: grab the page
lock on the charge moving side to prevent changing pc->mem_cgroup of a
page under migration. Also, the lruvecs of both pages change as we
uncharge the old and charge the new during migration, and putback may
race with us, so grab the lru lock and isolate the pages iff on LRU to
prevent races and ensure the pages are on the right lruvec afterward.
Swap accounting is massively simplified: because the page is no longer
uncharged as early as swap cache deletion, a new mem_cgroup_swapout() can
transfer the page's memory+swap charge (PCG_MEMSW) to the swap entry
before the final put_page() in page reclaim.
Finally, page_cgroup changes are now protected by whatever protection the
page itself offers: anonymous pages are charged under the page table lock,
whereas page cache insertions, swapin, and migration hold the page lock.
Uncharging happens under full exclusion with no outstanding references.
Charging and uncharging also ensure that the page is off-LRU, which
serializes against charge migration. Remove the very costly page_cgroup
lock and set pc->flags non-atomically.
[mhocko@suse.cz: mem_cgroup_charge_statistics needs preempt_disable]
[vdavydov@parallels.com: fix flags definition]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Tested-by: Jet Chen <jet.chen@intel.com>
Acked-by: Michal Hocko <mhocko@suse.cz>
Tested-by: Felipe Balbi <balbi@ti.com>
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-09 05:19:22 +08:00
|
|
|
swapcache_free(entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
} while (err != -ENOMEM);
|
|
|
|
|
|
|
|
if (new_page)
|
|
|
|
page_cache_release(new_page);
|
|
|
|
if (!found_page)
|
2013-11-13 07:08:26 +08:00
|
|
|
return ZSWAP_SWAPCACHE_FAIL;
|
2013-07-11 07:05:03 +08:00
|
|
|
*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.
|
|
|
|
*/
|
2014-08-07 07:08:40 +08:00
|
|
|
static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
|
2013-07-11 07:05:03 +08:00
|
|
|
{
|
|
|
|
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;
|
2013-11-13 07:08:27 +08:00
|
|
|
int ret;
|
2013-07-11 07:05:03 +08:00
|
|
|
struct writeback_control wbc = {
|
|
|
|
.sync_mode = WB_SYNC_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* extract swpentry from data */
|
2014-08-07 07:08:40 +08:00
|
|
|
zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
|
2013-07-11 07:05:03 +08:00
|
|
|
swpentry = zhdr->swpentry; /* here */
|
2014-08-07 07:08:40 +08:00
|
|
|
zpool_unmap_handle(pool, handle);
|
2013-07-11 07:05:03 +08:00
|
|
|
tree = zswap_trees[swp_type(swpentry)];
|
|
|
|
offset = swp_offset(swpentry);
|
|
|
|
|
|
|
|
/* find and ref zswap entry */
|
|
|
|
spin_lock(&tree->lock);
|
2013-11-13 07:08:27 +08:00
|
|
|
entry = zswap_entry_find_get(&tree->rbroot, offset);
|
2013-07-11 07:05:03 +08:00
|
|
|
if (!entry) {
|
|
|
|
/* entry was invalidated */
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
BUG_ON(offset != entry->offset);
|
|
|
|
|
|
|
|
/* try to allocate swap cache page */
|
|
|
|
switch (zswap_get_swap_cache_page(swpentry, &page)) {
|
2013-11-13 07:08:26 +08:00
|
|
|
case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
|
2013-07-11 07:05:03 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
|
2013-11-13 07:08:26 +08:00
|
|
|
case ZSWAP_SWAPCACHE_EXIST:
|
2013-07-11 07:05:03 +08:00
|
|
|
/* 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;
|
2014-08-07 07:08:40 +08:00
|
|
|
src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
|
|
|
|
ZPOOL_MM_RO) + sizeof(struct zswap_header);
|
2013-07-11 07:05:03 +08:00
|
|
|
dst = kmap_atomic(page);
|
|
|
|
ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src,
|
|
|
|
entry->length, dst, &dlen);
|
|
|
|
kunmap_atomic(dst);
|
2014-08-07 07:08:40 +08:00
|
|
|
zpool_unmap_handle(zswap_pool, entry->handle);
|
2013-07-11 07:05:03 +08:00
|
|
|
BUG_ON(ret);
|
|
|
|
BUG_ON(dlen != PAGE_SIZE);
|
|
|
|
|
|
|
|
/* page is up to date */
|
|
|
|
SetPageUptodate(page);
|
|
|
|
}
|
|
|
|
|
2013-11-13 07:07:52 +08:00
|
|
|
/* move it to the tail of the inactive list after end_writeback */
|
|
|
|
SetPageReclaim(page);
|
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
/* start writeback */
|
|
|
|
__swap_writepage(page, &wbc, end_swap_bio_write);
|
|
|
|
page_cache_release(page);
|
|
|
|
zswap_written_back_pages++;
|
|
|
|
|
|
|
|
spin_lock(&tree->lock);
|
|
|
|
/* drop local reference */
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_entry_put(tree, entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
/*
|
2013-11-13 07:08:27 +08:00
|
|
|
* 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);
|
2013-07-11 07:05:03 +08:00
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
|
2013-11-13 07:08:27 +08:00
|
|
|
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
|
|
|
|
*/
|
2013-07-11 07:05:03 +08:00
|
|
|
fail:
|
|
|
|
spin_lock(&tree->lock);
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_entry_put(tree, entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
spin_unlock(&tree->lock);
|
2013-11-13 07:08:27 +08:00
|
|
|
|
|
|
|
end:
|
2013-07-11 07:05:03 +08:00
|
|
|
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++;
|
2014-08-07 07:08:40 +08:00
|
|
|
if (zpool_shrink(zswap_pool, 1, NULL)) {
|
2013-07-11 07:05:03 +08:00
|
|
|
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);
|
2014-08-07 07:08:40 +08:00
|
|
|
ret = zpool_malloc(zswap_pool, len, __GFP_NORETRY | __GFP_NOWARN,
|
2013-07-11 07:05:03 +08:00
|
|
|
&handle);
|
|
|
|
if (ret == -ENOSPC) {
|
|
|
|
zswap_reject_compress_poor++;
|
|
|
|
goto freepage;
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
zswap_reject_alloc_fail++;
|
|
|
|
goto freepage;
|
|
|
|
}
|
2014-08-07 07:08:40 +08:00
|
|
|
zhdr = zpool_map_handle(zswap_pool, handle, ZPOOL_MM_RW);
|
2013-07-11 07:05:03 +08:00
|
|
|
zhdr->swpentry = swp_entry(type, offset);
|
|
|
|
buf = (u8 *)(zhdr + 1);
|
|
|
|
memcpy(buf, dst, dlen);
|
2014-08-07 07:08:40 +08:00
|
|
|
zpool_unmap_handle(zswap_pool, handle);
|
2013-07-11 07:05:03 +08:00
|
|
|
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 */
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_rb_erase(&tree->rbroot, dupentry);
|
|
|
|
zswap_entry_put(tree, dupentry);
|
2013-07-11 07:05:03 +08:00
|
|
|
}
|
|
|
|
} while (ret == -EEXIST);
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
|
|
|
|
/* update stats */
|
|
|
|
atomic_inc(&zswap_stored_pages);
|
2014-08-07 07:08:40 +08:00
|
|
|
zswap_pool_total_size = zpool_get_total_size(zswap_pool);
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
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;
|
2013-11-13 07:08:27 +08:00
|
|
|
int ret;
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
/* find */
|
|
|
|
spin_lock(&tree->lock);
|
2013-11-13 07:08:27 +08:00
|
|
|
entry = zswap_entry_find_get(&tree->rbroot, offset);
|
2013-07-11 07:05:03 +08:00
|
|
|
if (!entry) {
|
|
|
|
/* entry was written back */
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
|
|
|
|
/* decompress */
|
|
|
|
dlen = PAGE_SIZE;
|
2014-08-07 07:08:40 +08:00
|
|
|
src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
|
|
|
|
ZPOOL_MM_RO) + sizeof(struct zswap_header);
|
2013-07-11 07:05:03 +08:00
|
|
|
dst = kmap_atomic(page);
|
|
|
|
ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length,
|
|
|
|
dst, &dlen);
|
|
|
|
kunmap_atomic(dst);
|
2014-08-07 07:08:40 +08:00
|
|
|
zpool_unmap_handle(zswap_pool, entry->handle);
|
2013-07-11 07:05:03 +08:00
|
|
|
BUG_ON(ret);
|
|
|
|
|
|
|
|
spin_lock(&tree->lock);
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_entry_put(tree, entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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 */
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_rb_erase(&tree->rbroot, entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
/* drop the initial reference from entry creation */
|
2013-11-13 07:08:27 +08:00
|
|
|
zswap_entry_put(tree, entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
spin_unlock(&tree->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* frees all zswap entries for the given swap type */
|
|
|
|
static void zswap_frontswap_invalidate_area(unsigned type)
|
|
|
|
{
|
|
|
|
struct zswap_tree *tree = zswap_trees[type];
|
2013-09-12 05:25:33 +08:00
|
|
|
struct zswap_entry *entry, *n;
|
2013-07-11 07:05:03 +08:00
|
|
|
|
|
|
|
if (!tree)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* walk the tree and free everything */
|
|
|
|
spin_lock(&tree->lock);
|
2013-11-13 07:08:27 +08:00
|
|
|
rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
|
2014-04-08 06:38:27 +08:00
|
|
|
zswap_free_entry(entry);
|
2013-07-11 07:05:03 +08:00
|
|
|
tree->rbroot = RB_ROOT;
|
|
|
|
spin_unlock(&tree->lock);
|
2013-10-17 04:46:54 +08:00
|
|
|
kfree(tree);
|
|
|
|
zswap_trees[type] = NULL;
|
2013-07-11 07:05:03 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 07:08:40 +08:00
|
|
|
static struct zpool_ops zswap_zpool_ops = {
|
2013-07-11 07:05:03 +08:00
|
|
|
.evict = zswap_writeback_entry
|
|
|
|
};
|
|
|
|
|
|
|
|
static void zswap_frontswap_init(unsigned type)
|
|
|
|
{
|
|
|
|
struct zswap_tree *tree;
|
|
|
|
|
|
|
|
tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
|
2014-04-08 06:38:27 +08:00
|
|
|
if (!tree) {
|
|
|
|
pr_err("alloc failed, zswap disabled for swap type %d\n", type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
tree->rbroot = RB_ROOT;
|
|
|
|
spin_lock_init(&tree->lock);
|
|
|
|
zswap_trees[type] = tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-08-07 07:08:40 +08:00
|
|
|
debugfs_create_u64("pool_total_size", S_IRUGO,
|
|
|
|
zswap_debugfs_root, &zswap_pool_total_size);
|
2013-07-11 07:05:03 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-08-07 07:08:40 +08:00
|
|
|
gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN;
|
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
if (!zswap_enabled)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pr_info("loading zswap\n");
|
2014-04-08 06:38:27 +08:00
|
|
|
|
2014-08-07 07:08:40 +08:00
|
|
|
zswap_pool = zpool_create_pool(zswap_zpool_type, gfp, &zswap_zpool_ops);
|
|
|
|
if (!zswap_pool && strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
|
|
|
|
pr_info("%s zpool not available\n", zswap_zpool_type);
|
|
|
|
zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
|
|
|
|
zswap_pool = zpool_create_pool(zswap_zpool_type, gfp,
|
|
|
|
&zswap_zpool_ops);
|
|
|
|
}
|
2014-04-08 06:38:27 +08:00
|
|
|
if (!zswap_pool) {
|
2014-08-07 07:08:40 +08:00
|
|
|
pr_err("%s zpool not available\n", zswap_zpool_type);
|
|
|
|
pr_err("zpool creation failed\n");
|
2014-04-08 06:38:27 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2014-08-07 07:08:40 +08:00
|
|
|
pr_info("using %s pool\n", zswap_zpool_type);
|
2014-04-08 06:38:27 +08:00
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
if (zswap_entry_cache_create()) {
|
|
|
|
pr_err("entry cache creation failed\n");
|
2014-04-08 06:38:27 +08:00
|
|
|
goto cachefail;
|
2013-07-11 07:05:03 +08:00
|
|
|
}
|
|
|
|
if (zswap_comp_init()) {
|
|
|
|
pr_err("compressor initialization failed\n");
|
|
|
|
goto compfail;
|
|
|
|
}
|
|
|
|
if (zswap_cpu_init()) {
|
|
|
|
pr_err("per-cpu initialization failed\n");
|
|
|
|
goto pcpufail;
|
|
|
|
}
|
2014-04-08 06:38:27 +08:00
|
|
|
|
2013-07-11 07:05:03 +08:00
|
|
|
frontswap_register_ops(&zswap_frontswap_ops);
|
|
|
|
if (zswap_debugfs_init())
|
|
|
|
pr_warn("debugfs initialization failed\n");
|
|
|
|
return 0;
|
|
|
|
pcpufail:
|
|
|
|
zswap_comp_exit();
|
|
|
|
compfail:
|
2014-08-09 05:19:35 +08:00
|
|
|
zswap_entry_cache_destroy();
|
2014-04-08 06:38:27 +08:00
|
|
|
cachefail:
|
2014-08-07 07:08:40 +08:00
|
|
|
zpool_destroy_pool(zswap_pool);
|
2013-07-11 07:05:03 +08:00
|
|
|
error:
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
/* must be late so crypto has time to come up */
|
|
|
|
late_initcall(init_zswap);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2014-11-13 11:08:46 +08:00
|
|
|
MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
|
2013-07-11 07:05:03 +08:00
|
|
|
MODULE_DESCRIPTION("Compressed cache for swap pages");
|