2009-09-21 22:56:53 -06:00
|
|
|
/*
|
2010-06-01 02:01:25 -06:00
|
|
|
* Compressed RAM block device
|
2009-09-21 22:56:53 -06:00
|
|
|
*
|
2010-01-28 08:51:35 -07:00
|
|
|
* Copyright (C) 2008, 2009, 2010 Nitin Gupta
|
2009-09-21 22:56:53 -06:00
|
|
|
*
|
|
|
|
* This code is released using a dual license strategy: BSD/GPL
|
|
|
|
* You can choose the licence that better fits your requirements.
|
|
|
|
*
|
|
|
|
* Released under the terms of 3-clause BSD License
|
|
|
|
* Released under the terms of GNU General Public License Version 2.0
|
|
|
|
*
|
|
|
|
* Project home: http://compcache.googlecode.com
|
|
|
|
*/
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
#ifndef _ZRAM_DRV_H_
|
|
|
|
#define _ZRAM_DRV_H_
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-01-28 08:43:37 -07:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
|
2012-01-09 15:51:59 -07:00
|
|
|
#include "../zsmalloc/zsmalloc.h"
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Some arbitrary value. This is just to catch
|
|
|
|
* invalid value for num_devices module parameter.
|
|
|
|
*/
|
|
|
|
static const unsigned max_num_devices = 32;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stored at beginning of each compressed object.
|
|
|
|
*
|
|
|
|
* It stores back-reference to table entry which points to this
|
2010-05-13 02:54:21 -06:00
|
|
|
* object. This is required to support memory defragmentation.
|
2009-09-21 22:56:53 -06:00
|
|
|
*/
|
|
|
|
struct zobj_header {
|
|
|
|
#if 0
|
|
|
|
u32 table_idx;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-- Configurable parameters */
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
/* Default zram disk size: 25% of total RAM */
|
2009-09-21 22:56:53 -06:00
|
|
|
static const unsigned default_disksize_perc_ram = 25;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pages that compress to size greater than this are stored
|
|
|
|
* uncompressed in memory.
|
|
|
|
*/
|
2011-09-09 17:01:00 -06:00
|
|
|
static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
/*
|
2010-05-13 02:54:21 -06:00
|
|
|
* NOTE: max_zpage_size must be less than or equal to:
|
2012-01-09 15:51:59 -07:00
|
|
|
* ZS_MAX_ALLOC_SIZE - sizeof(struct zobj_header)
|
2010-05-13 02:54:21 -06:00
|
|
|
* otherwise, xv_malloc() would always return failure.
|
2009-09-21 22:56:53 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-- End of configurable params */
|
|
|
|
|
|
|
|
#define SECTOR_SHIFT 9
|
|
|
|
#define SECTOR_SIZE (1 << SECTOR_SHIFT)
|
|
|
|
#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
|
|
|
|
#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
|
2011-06-10 07:28:48 -06:00
|
|
|
#define ZRAM_LOGICAL_BLOCK_SHIFT 12
|
|
|
|
#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
|
|
|
|
#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
|
|
|
|
(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
/* Flags for zram pages (table[page_no].flags) */
|
|
|
|
enum zram_pageflags {
|
2009-09-21 22:56:53 -06:00
|
|
|
/* Page is stored uncompressed */
|
2010-06-01 02:01:25 -06:00
|
|
|
ZRAM_UNCOMPRESSED,
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
/* Page consists entirely of zeros */
|
2010-06-01 02:01:25 -06:00
|
|
|
ZRAM_ZERO,
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
__NR_ZRAM_PAGEFLAGS,
|
2009-09-21 22:56:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-- Data structures */
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
/* Allocated for each disk page */
|
2009-09-21 22:56:53 -06:00
|
|
|
struct table {
|
2012-01-09 15:51:59 -07:00
|
|
|
void *handle;
|
|
|
|
u16 size; /* object size (excluding header) */
|
2009-09-21 22:56:53 -06:00
|
|
|
u8 count; /* object ref count (not yet used) */
|
|
|
|
u8 flags;
|
2010-01-06 05:42:00 -07:00
|
|
|
} __attribute__((aligned(4)));
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
struct zram_stats {
|
2010-08-09 11:26:47 -06:00
|
|
|
u64 compr_size; /* compressed size of pages stored */
|
2009-09-21 22:56:53 -06:00
|
|
|
u64 num_reads; /* failed + successful */
|
|
|
|
u64 num_writes; /* --do-- */
|
2010-01-06 05:42:00 -07:00
|
|
|
u64 failed_reads; /* should NEVER! happen */
|
|
|
|
u64 failed_writes; /* can happen when memory is too low */
|
2010-06-01 02:01:25 -06:00
|
|
|
u64 invalid_io; /* non-page-aligned I/O requests */
|
2010-01-28 08:43:37 -07:00
|
|
|
u64 notify_free; /* no. of swap slot free notifications */
|
2009-09-21 22:56:53 -06:00
|
|
|
u32 pages_zero; /* no. of zero filled pages */
|
|
|
|
u32 pages_stored; /* no. of pages currently stored */
|
|
|
|
u32 good_compress; /* % of pages with compression ratio<=50% */
|
|
|
|
u32 pages_expand; /* % of incompressible pages */
|
|
|
|
};
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
struct zram {
|
2012-01-09 15:51:59 -07:00
|
|
|
struct zs_pool *mem_pool;
|
2009-09-21 22:56:53 -06:00
|
|
|
void *compress_workmem;
|
|
|
|
void *compress_buffer;
|
|
|
|
struct table *table;
|
2010-01-28 08:43:37 -07:00
|
|
|
spinlock_t stat64_lock; /* protect 64-bit stats */
|
2011-06-10 07:28:49 -06:00
|
|
|
struct rw_semaphore lock; /* protect compression buffers and table
|
|
|
|
* against concurrent read and writes */
|
2009-09-21 22:56:53 -06:00
|
|
|
struct request_queue *queue;
|
|
|
|
struct gendisk *disk;
|
|
|
|
int init_done;
|
2011-09-06 07:02:11 -06:00
|
|
|
/* Prevent concurrent execution of device init, reset and R/W request */
|
|
|
|
struct rw_semaphore init_lock;
|
2009-09-21 22:56:53 -06:00
|
|
|
/*
|
2010-06-01 02:01:25 -06:00
|
|
|
* This is the limit on amount of *uncompressed* worth of data
|
|
|
|
* we can store in a disk.
|
2009-09-21 22:56:53 -06:00
|
|
|
*/
|
2010-08-09 11:26:47 -06:00
|
|
|
u64 disksize; /* bytes */
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
struct zram_stats stats;
|
2009-09-21 22:56:53 -06:00
|
|
|
};
|
|
|
|
|
2011-07-20 17:05:57 -06:00
|
|
|
extern struct zram *zram_devices;
|
2012-02-12 21:04:45 -07:00
|
|
|
unsigned int zram_get_num_devices(void);
|
2010-08-09 11:26:47 -06:00
|
|
|
#ifdef CONFIG_SYSFS
|
|
|
|
extern struct attribute_group zram_disk_attr_group;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern int zram_init_device(struct zram *zram);
|
2011-09-06 07:02:11 -06:00
|
|
|
extern void __zram_reset_device(struct zram *zram);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-01-28 08:43:37 -07:00
|
|
|
#endif
|