2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
12 * Project home: http://compcache.googlecode.com
18 #include <linux/spinlock.h>
19 #include <linux/mutex.h>
21 #include "zram_ioctl.h"
25 * Some arbitrary value. This is just to catch
26 * invalid value for num_devices module parameter.
28 static const unsigned max_num_devices = 32;
31 * Stored at beginning of each compressed object.
33 * It stores back-reference to table entry which points to this
34 * object. This is required to support memory defragmentation.
42 /*-- Configurable parameters */
44 /* Default zram disk size: 25% of total RAM */
45 static const unsigned default_disksize_perc_ram = 25;
48 * Pages that compress to size greater than this are stored
49 * uncompressed in memory.
51 static const unsigned max_zpage_size = PAGE_SIZE / 4 * 3;
54 * NOTE: max_zpage_size must be less than or equal to:
55 * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header)
56 * otherwise, xv_malloc() would always return failure.
59 /*-- End of configurable params */
61 #define SECTOR_SHIFT 9
62 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
63 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
64 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
66 /* Flags for zram pages (table[page_no].flags) */
68 /* Page is stored uncompressed */
71 /* Page consists entirely of zeros */
77 /*-- Data structures */
79 /* Allocated for each disk page */
83 u8 count; /* object ref count (not yet used) */
85 } __attribute__((aligned(4)));
89 size_t compr_size; /* compressed size of pages stored -
90 * needed to enforce memlimit */
92 #if defined(CONFIG_ZRAM_STATS)
93 u64 num_reads; /* failed + successful */
94 u64 num_writes; /* --do-- */
95 u64 failed_reads; /* should NEVER! happen */
96 u64 failed_writes; /* can happen when memory is too low */
97 u64 invalid_io; /* non-page-aligned I/O requests */
98 u64 notify_free; /* no. of swap slot free notifications */
99 u32 pages_zero; /* no. of zero filled pages */
100 u32 pages_stored; /* no. of pages currently stored */
101 u32 good_compress; /* % of pages with compression ratio<=50% */
102 u32 pages_expand; /* % of incompressible pages */
107 struct xv_pool *mem_pool;
108 void *compress_workmem;
109 void *compress_buffer;
111 spinlock_t stat64_lock; /* protect 64-bit stats */
112 struct mutex lock; /* protect compression buffers against
113 * concurrent writes */
114 struct request_queue *queue;
115 struct gendisk *disk;
118 * This is the limit on amount of *uncompressed* worth of data
119 * we can store in a disk.
121 size_t disksize; /* bytes */
123 struct zram_stats stats;
128 /* Debugging and Stats */
129 #if defined(CONFIG_ZRAM_STATS)
130 static void zram_stat_inc(u32 *v)
135 static void zram_stat_dec(u32 *v)
140 static void zram_stat64_inc(struct zram *zram, u64 *v)
142 spin_lock(&zram->stat64_lock);
144 spin_unlock(&zram->stat64_lock);
147 static u64 zram_stat64_read(struct zram *zram, u64 *v)
151 spin_lock(&zram->stat64_lock);
153 spin_unlock(&zram->stat64_lock);
158 #define zram_stat_inc(v)
159 #define zram_stat_dec(v)
160 #define zram_stat64_inc(r, v)
161 #define zram_stat64_read(r, v)
162 #endif /* CONFIG_ZRAM_STATS */