2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
5 * 2012, 2013 Minchan Kim
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the licence that better fits your requirements.
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
18 #include <linux/spinlock.h>
19 #include <linux/zsmalloc.h>
24 * Some arbitrary value. This is just to catch
25 * invalid value for num_devices module parameter.
27 static const unsigned max_num_devices = 32;
29 /*-- Configurable parameters */
32 * Pages that compress to size greater than this are stored
33 * uncompressed in memory.
35 static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
38 * NOTE: max_zpage_size must be less than or equal to:
39 * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
40 * always return failure.
43 /*-- End of configurable params */
45 #define SECTOR_SHIFT 9
46 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
47 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
48 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
49 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
50 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
51 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
52 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
54 /* Flags for zram pages (table[page_no].flags) */
56 /* Page consists entirely of zeros */
62 /*-- Data structures */
64 /* Allocated for each disk page */
67 u16 size; /* object size (excluding header) */
72 atomic64_t compr_data_size; /* compressed size of pages stored */
73 atomic64_t num_reads; /* failed + successful */
74 atomic64_t num_writes; /* --do-- */
75 atomic64_t failed_reads; /* should NEVER! happen */
76 atomic64_t failed_writes; /* can happen when memory is too low */
77 atomic64_t invalid_io; /* non-page-aligned I/O requests */
78 atomic64_t notify_free; /* no. of swap slot free notifications */
79 atomic64_t zero_pages; /* no. of zero filled pages */
80 atomic64_t pages_stored; /* no. of pages currently stored */
84 rwlock_t tb_lock; /* protect table */
86 struct zs_pool *mem_pool;
90 struct zram_meta *meta;
91 struct request_queue *queue;
95 /* Prevent concurrent execution of device init, reset and R/W request */
96 struct rw_semaphore init_lock;
98 * This is the limit on amount of *uncompressed* worth of data
99 * we can store in a disk.
101 u64 disksize; /* bytes */
102 int max_comp_streams;
103 struct zram_stats stats;