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/rwsem.h>
19 #include <linux/zsmalloc.h>
20 #include <linux/crypto.h>
24 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
25 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
26 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
27 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
28 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
29 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
33 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
34 * object size (excluding header), the higher bits is for
37 * zram is mainly used for memory efficiency so we want to keep memory
38 * footprint small so we can squeeze size and flags into a field.
39 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
40 * the higher bits is for zram_pageflags.
42 #define ZRAM_FLAG_SHIFT 24
44 /* Flags for zram pages (table[page_no].value) */
46 /* zram slot is locked */
47 ZRAM_LOCK = ZRAM_FLAG_SHIFT,
48 ZRAM_SAME, /* Page consists the same element */
49 ZRAM_WB, /* page is stored on backing_device */
50 ZRAM_HUGE, /* Incompressible page */
55 /*-- Data structures */
57 /* Allocated for each disk page */
58 struct zram_table_entry {
61 unsigned long element;
64 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
70 atomic64_t compr_data_size; /* compressed size of pages stored */
71 atomic64_t num_reads; /* failed + successful */
72 atomic64_t num_writes; /* --do-- */
73 atomic64_t failed_reads; /* can happen when memory is too low */
74 atomic64_t failed_writes; /* can happen when memory is too low */
75 atomic64_t invalid_io; /* non-page-aligned I/O requests */
76 atomic64_t notify_free; /* no. of swap slot free notifications */
77 atomic64_t same_pages; /* no. of same element filled pages */
78 atomic64_t huge_pages; /* no. of huge pages */
79 atomic64_t pages_stored; /* no. of pages currently stored */
80 atomic_long_t max_used_pages; /* no. of maximum pages stored */
81 atomic64_t writestall; /* no. of write slow paths */
82 atomic64_t miss_free; /* no. of missed free */
86 struct zram_table_entry *table;
87 struct zs_pool *mem_pool;
90 /* Prevent concurrent execution of device init */
91 struct rw_semaphore init_lock;
93 * the number of pages zram can consume for storing compressed data
95 unsigned long limit_pages;
97 struct zram_stats stats;
99 * This is the limit on amount of *uncompressed* worth of data
100 * we can store in a disk.
102 u64 disksize; /* bytes */
103 char compressor[CRYPTO_MAX_ALG_NAME];
105 * zram is claimed so open request will be failed
107 bool claim; /* Protected by bdev->bd_mutex */
108 #ifdef CONFIG_ZRAM_WRITEBACK
109 struct file *backing_dev;
110 struct block_device *bdev;
111 unsigned int old_block_size;
112 unsigned long *bitmap;
113 unsigned long nr_pages;
115 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
116 struct dentry *debugfs_dir;