Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 2 | * Compressed RAM block device |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 3 | * |
Nitin Gupta | 1130ebb | 2010-01-28 21:21:35 +0530 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
Minchan Kim | 7bfb3de | 2014-01-30 15:45:55 -0800 | [diff] [blame] | 5 | * 2012, 2013 Minchan Kim |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 6 | * |
| 7 | * This code is released using a dual license strategy: BSD/GPL |
| 8 | * You can choose the licence that better fits your requirements. |
| 9 | * |
| 10 | * Released under the terms of 3-clause BSD License |
| 11 | * Released under the terms of GNU General Public License Version 2.0 |
| 12 | * |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 13 | */ |
| 14 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 15 | #define KMSG_COMPONENT "zram" |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 17 | |
Robert Jennings | b1f5b81 | 2011-01-28 08:59:26 -0600 | [diff] [blame] | 18 | #ifdef CONFIG_ZRAM_DEBUG |
| 19 | #define DEBUG |
| 20 | #endif |
| 21 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
Randy Dunlap | 8946a08 | 2010-06-23 20:27:09 -0700 | [diff] [blame] | 24 | #include <linux/bio.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 25 | #include <linux/bitops.h> |
| 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/buffer_head.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/genhd.h> |
| 30 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 32 | #include <linux/string.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 33 | #include <linux/vmalloc.h> |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 34 | #include <linux/err.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 35 | |
Nitin Gupta | 16a4bfb | 2010-06-01 13:31:24 +0530 | [diff] [blame] | 36 | #include "zram_drv.h" |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 37 | |
| 38 | /* Globals */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 39 | static int zram_major; |
Jiang Liu | 0f0e3ba | 2013-06-07 00:07:29 +0800 | [diff] [blame] | 40 | static struct zram *zram_devices; |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 41 | static const char *default_compressor = "lzo"; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 42 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 43 | /* Module params (documentation at end) */ |
Davidlohr Bueso | ca3d70b | 2013-01-01 21:24:13 -0800 | [diff] [blame] | 44 | static unsigned int num_devices = 1; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 45 | |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 46 | #define ZRAM_ATTR_RO(name) \ |
| 47 | static ssize_t zram_attr_##name##_show(struct device *d, \ |
| 48 | struct device_attribute *attr, char *b) \ |
| 49 | { \ |
| 50 | struct zram *zram = dev_to_zram(d); \ |
Sergey Senozhatsky | 56b4e8c | 2014-04-07 15:38:22 -0700 | [diff] [blame] | 51 | return scnprintf(b, PAGE_SIZE, "%llu\n", \ |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 52 | (u64)atomic64_read(&zram->stats.name)); \ |
| 53 | } \ |
| 54 | static struct device_attribute dev_attr_##name = \ |
| 55 | __ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL); |
| 56 | |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 57 | static inline int init_done(struct zram *zram) |
| 58 | { |
| 59 | return zram->meta != NULL; |
| 60 | } |
| 61 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 62 | static inline struct zram *dev_to_zram(struct device *dev) |
| 63 | { |
| 64 | return (struct zram *)dev_to_disk(dev)->private_data; |
| 65 | } |
| 66 | |
| 67 | static ssize_t disksize_show(struct device *dev, |
| 68 | struct device_attribute *attr, char *buf) |
| 69 | { |
| 70 | struct zram *zram = dev_to_zram(dev); |
| 71 | |
Sergey Senozhatsky | 56b4e8c | 2014-04-07 15:38:22 -0700 | [diff] [blame] | 72 | return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static ssize_t initstate_show(struct device *dev, |
| 76 | struct device_attribute *attr, char *buf) |
| 77 | { |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 78 | u32 val; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 79 | struct zram *zram = dev_to_zram(dev); |
| 80 | |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 81 | down_read(&zram->init_lock); |
| 82 | val = init_done(zram); |
| 83 | up_read(&zram->init_lock); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 84 | |
Sergey Senozhatsky | 56b4e8c | 2014-04-07 15:38:22 -0700 | [diff] [blame] | 85 | return scnprintf(buf, PAGE_SIZE, "%u\n", val); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static ssize_t orig_data_size_show(struct device *dev, |
| 89 | struct device_attribute *attr, char *buf) |
| 90 | { |
| 91 | struct zram *zram = dev_to_zram(dev); |
| 92 | |
Sergey Senozhatsky | 56b4e8c | 2014-04-07 15:38:22 -0700 | [diff] [blame] | 93 | return scnprintf(buf, PAGE_SIZE, "%llu\n", |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 94 | (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 97 | static ssize_t mem_used_total_show(struct device *dev, |
| 98 | struct device_attribute *attr, char *buf) |
| 99 | { |
| 100 | u64 val = 0; |
| 101 | struct zram *zram = dev_to_zram(dev); |
| 102 | struct zram_meta *meta = zram->meta; |
| 103 | |
| 104 | down_read(&zram->init_lock); |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 105 | if (init_done(zram)) |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 106 | val = zs_get_total_size_bytes(meta->mem_pool); |
| 107 | up_read(&zram->init_lock); |
| 108 | |
Sergey Senozhatsky | 56b4e8c | 2014-04-07 15:38:22 -0700 | [diff] [blame] | 109 | return scnprintf(buf, PAGE_SIZE, "%llu\n", val); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 110 | } |
| 111 | |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 112 | static ssize_t max_comp_streams_show(struct device *dev, |
| 113 | struct device_attribute *attr, char *buf) |
| 114 | { |
| 115 | int val; |
| 116 | struct zram *zram = dev_to_zram(dev); |
| 117 | |
| 118 | down_read(&zram->init_lock); |
| 119 | val = zram->max_comp_streams; |
| 120 | up_read(&zram->init_lock); |
| 121 | |
Sergey Senozhatsky | 56b4e8c | 2014-04-07 15:38:22 -0700 | [diff] [blame] | 122 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static ssize_t max_comp_streams_store(struct device *dev, |
| 126 | struct device_attribute *attr, const char *buf, size_t len) |
| 127 | { |
| 128 | int num; |
| 129 | struct zram *zram = dev_to_zram(dev); |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 130 | int ret; |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 131 | |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 132 | ret = kstrtoint(buf, 0, &num); |
| 133 | if (ret < 0) |
| 134 | return ret; |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 135 | if (num < 1) |
| 136 | return -EINVAL; |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 137 | |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 138 | down_write(&zram->init_lock); |
| 139 | if (init_done(zram)) { |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 140 | if (!zcomp_set_max_streams(zram->comp, num)) { |
Sergey Senozhatsky | fe8eb12 | 2014-04-07 15:38:15 -0700 | [diff] [blame] | 141 | pr_info("Cannot change max compression streams\n"); |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 142 | ret = -EINVAL; |
| 143 | goto out; |
| 144 | } |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 145 | } |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 146 | |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 147 | zram->max_comp_streams = num; |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 148 | ret = len; |
| 149 | out: |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 150 | up_write(&zram->init_lock); |
Minchan Kim | 60a726e | 2014-04-07 15:38:21 -0700 | [diff] [blame] | 151 | return ret; |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 154 | static ssize_t comp_algorithm_show(struct device *dev, |
| 155 | struct device_attribute *attr, char *buf) |
| 156 | { |
| 157 | size_t sz; |
| 158 | struct zram *zram = dev_to_zram(dev); |
| 159 | |
| 160 | down_read(&zram->init_lock); |
| 161 | sz = zcomp_available_show(zram->compressor, buf); |
| 162 | up_read(&zram->init_lock); |
| 163 | |
| 164 | return sz; |
| 165 | } |
| 166 | |
| 167 | static ssize_t comp_algorithm_store(struct device *dev, |
| 168 | struct device_attribute *attr, const char *buf, size_t len) |
| 169 | { |
| 170 | struct zram *zram = dev_to_zram(dev); |
| 171 | down_write(&zram->init_lock); |
| 172 | if (init_done(zram)) { |
| 173 | up_write(&zram->init_lock); |
| 174 | pr_info("Can't change algorithm for initialized device\n"); |
| 175 | return -EBUSY; |
| 176 | } |
| 177 | strlcpy(zram->compressor, buf, sizeof(zram->compressor)); |
| 178 | up_write(&zram->init_lock); |
| 179 | return len; |
| 180 | } |
| 181 | |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 182 | /* flag operations needs meta->tb_lock */ |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 183 | static int zram_test_flag(struct zram_meta *meta, u32 index, |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 184 | enum zram_pageflags flag) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 185 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 186 | return meta->table[index].flags & BIT(flag); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 187 | } |
| 188 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 189 | static void zram_set_flag(struct zram_meta *meta, u32 index, |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 190 | enum zram_pageflags flag) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 191 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 192 | meta->table[index].flags |= BIT(flag); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 193 | } |
| 194 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 195 | static void zram_clear_flag(struct zram_meta *meta, u32 index, |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 196 | enum zram_pageflags flag) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 197 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 198 | meta->table[index].flags &= ~BIT(flag); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 199 | } |
| 200 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 201 | static inline int is_partial_io(struct bio_vec *bvec) |
| 202 | { |
| 203 | return bvec->bv_len != PAGE_SIZE; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Check if request is within bounds and aligned on zram logical blocks. |
| 208 | */ |
| 209 | static inline int valid_io_request(struct zram *zram, struct bio *bio) |
| 210 | { |
| 211 | u64 start, end, bound; |
Kumar Gaurav | a539c72 | 2013-08-08 23:53:24 +0530 | [diff] [blame] | 212 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 213 | /* unaligned request */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 214 | if (unlikely(bio->bi_iter.bi_sector & |
| 215 | (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1))) |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 216 | return 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 217 | if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1))) |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 218 | return 0; |
| 219 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 220 | start = bio->bi_iter.bi_sector; |
| 221 | end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 222 | bound = zram->disksize >> SECTOR_SHIFT; |
| 223 | /* out of range range */ |
Sergey Senozhatsky | 75c7caf | 2013-06-22 17:21:00 +0300 | [diff] [blame] | 224 | if (unlikely(start >= bound || end > bound || start > end)) |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 225 | return 0; |
| 226 | |
| 227 | /* I/O request is valid */ |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | static void zram_meta_free(struct zram_meta *meta) |
| 232 | { |
| 233 | zs_destroy_pool(meta->mem_pool); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 234 | vfree(meta->table); |
| 235 | kfree(meta); |
| 236 | } |
| 237 | |
| 238 | static struct zram_meta *zram_meta_alloc(u64 disksize) |
| 239 | { |
| 240 | size_t num_pages; |
| 241 | struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL); |
| 242 | if (!meta) |
| 243 | goto out; |
| 244 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 245 | num_pages = disksize >> PAGE_SHIFT; |
| 246 | meta->table = vzalloc(num_pages * sizeof(*meta->table)); |
| 247 | if (!meta->table) { |
| 248 | pr_err("Error allocating zram address table\n"); |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 249 | goto free_meta; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM); |
| 253 | if (!meta->mem_pool) { |
| 254 | pr_err("Error creating memory pool\n"); |
| 255 | goto free_table; |
| 256 | } |
| 257 | |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 258 | rwlock_init(&meta->tb_lock); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 259 | return meta; |
| 260 | |
| 261 | free_table: |
| 262 | vfree(meta->table); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 263 | free_meta: |
| 264 | kfree(meta); |
| 265 | meta = NULL; |
| 266 | out: |
| 267 | return meta; |
| 268 | } |
| 269 | |
| 270 | static void update_position(u32 *index, int *offset, struct bio_vec *bvec) |
| 271 | { |
| 272 | if (*offset + bvec->bv_len >= PAGE_SIZE) |
| 273 | (*index)++; |
| 274 | *offset = (*offset + bvec->bv_len) % PAGE_SIZE; |
| 275 | } |
| 276 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 277 | static int page_zero_filled(void *ptr) |
| 278 | { |
| 279 | unsigned int pos; |
| 280 | unsigned long *page; |
| 281 | |
| 282 | page = (unsigned long *)ptr; |
| 283 | |
| 284 | for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) { |
| 285 | if (page[pos]) |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | return 1; |
| 290 | } |
| 291 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 292 | static void handle_zero_page(struct bio_vec *bvec) |
| 293 | { |
| 294 | struct page *page = bvec->bv_page; |
| 295 | void *user_mem; |
| 296 | |
| 297 | user_mem = kmap_atomic(page); |
| 298 | if (is_partial_io(bvec)) |
| 299 | memset(user_mem + bvec->bv_offset, 0, bvec->bv_len); |
| 300 | else |
| 301 | clear_page(user_mem); |
| 302 | kunmap_atomic(user_mem); |
| 303 | |
| 304 | flush_dcache_page(page); |
| 305 | } |
| 306 | |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 307 | /* NOTE: caller should hold meta->tb_lock with write-side */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 308 | static void zram_free_page(struct zram *zram, size_t index) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 309 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 310 | struct zram_meta *meta = zram->meta; |
| 311 | unsigned long handle = meta->table[index].handle; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 312 | |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 313 | if (unlikely(!handle)) { |
Nitin Gupta | 2e88228 | 2010-01-28 21:13:41 +0530 | [diff] [blame] | 314 | /* |
| 315 | * No memory is allocated for zero filled pages. |
| 316 | * Simply clear zero page flag. |
| 317 | */ |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 318 | if (zram_test_flag(meta, index, ZRAM_ZERO)) { |
| 319 | zram_clear_flag(meta, index, ZRAM_ZERO); |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 320 | atomic64_dec(&zram->stats.zero_pages); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 321 | } |
| 322 | return; |
| 323 | } |
| 324 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 325 | zs_free(meta->mem_pool, handle); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 326 | |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 327 | atomic64_sub(meta->table[index].size, &zram->stats.compr_data_size); |
| 328 | atomic64_dec(&zram->stats.pages_stored); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 329 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 330 | meta->table[index].handle = 0; |
| 331 | meta->table[index].size = 0; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 332 | } |
| 333 | |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 334 | static int zram_decompress_page(struct zram *zram, char *mem, u32 index) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 335 | { |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 336 | int ret = 0; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 337 | unsigned char *cmem; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 338 | struct zram_meta *meta = zram->meta; |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 339 | unsigned long handle; |
| 340 | u16 size; |
| 341 | |
| 342 | read_lock(&meta->tb_lock); |
| 343 | handle = meta->table[index].handle; |
| 344 | size = meta->table[index].size; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 345 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 346 | if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) { |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 347 | read_unlock(&meta->tb_lock); |
Jiang Liu | 42e99bd | 2013-06-07 00:07:30 +0800 | [diff] [blame] | 348 | clear_page(mem); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 352 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO); |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 353 | if (size == PAGE_SIZE) |
Jiang Liu | 42e99bd | 2013-06-07 00:07:30 +0800 | [diff] [blame] | 354 | copy_page(mem, cmem); |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 355 | else |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 356 | ret = zcomp_decompress(zram->comp, cmem, size, mem); |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 357 | zs_unmap_object(meta->mem_pool, handle); |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 358 | read_unlock(&meta->tb_lock); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 359 | |
| 360 | /* Should NEVER happen. Return bio error if it does. */ |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 361 | if (unlikely(ret)) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 362 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 363 | atomic64_inc(&zram->stats.failed_reads); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 370 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
| 371 | u32 index, int offset, struct bio *bio) |
| 372 | { |
| 373 | int ret; |
| 374 | struct page *page; |
| 375 | unsigned char *user_mem, *uncmem = NULL; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 376 | struct zram_meta *meta = zram->meta; |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 377 | page = bvec->bv_page; |
| 378 | |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 379 | read_lock(&meta->tb_lock); |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 380 | if (unlikely(!meta->table[index].handle) || |
| 381 | zram_test_flag(meta, index, ZRAM_ZERO)) { |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 382 | read_unlock(&meta->tb_lock); |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 383 | handle_zero_page(bvec); |
| 384 | return 0; |
| 385 | } |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 386 | read_unlock(&meta->tb_lock); |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 387 | |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 388 | if (is_partial_io(bvec)) |
| 389 | /* Use a temporary buffer to decompress the page */ |
Minchan Kim | 7e5a510 | 2013-01-30 11:41:39 +0900 | [diff] [blame] | 390 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
| 391 | |
| 392 | user_mem = kmap_atomic(page); |
| 393 | if (!is_partial_io(bvec)) |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 394 | uncmem = user_mem; |
| 395 | |
| 396 | if (!uncmem) { |
| 397 | pr_info("Unable to allocate temp memory\n"); |
| 398 | ret = -ENOMEM; |
| 399 | goto out_cleanup; |
| 400 | } |
| 401 | |
| 402 | ret = zram_decompress_page(zram, uncmem, index); |
| 403 | /* Should NEVER happen. Return bio error if it does. */ |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 404 | if (unlikely(ret)) |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 405 | goto out_cleanup; |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 406 | |
| 407 | if (is_partial_io(bvec)) |
| 408 | memcpy(user_mem + bvec->bv_offset, uncmem + offset, |
| 409 | bvec->bv_len); |
| 410 | |
| 411 | flush_dcache_page(page); |
| 412 | ret = 0; |
| 413 | out_cleanup: |
| 414 | kunmap_atomic(user_mem); |
| 415 | if (is_partial_io(bvec)) |
| 416 | kfree(uncmem); |
| 417 | return ret; |
| 418 | } |
| 419 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 420 | static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, |
| 421 | int offset) |
| 422 | { |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 423 | int ret = 0; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 424 | size_t clen; |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 425 | unsigned long handle; |
Minchan Kim | 130f315 | 2012-06-08 15:39:27 +0900 | [diff] [blame] | 426 | struct page *page; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 427 | unsigned char *user_mem, *cmem, *src, *uncmem = NULL; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 428 | struct zram_meta *meta = zram->meta; |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 429 | struct zcomp_strm *zstrm; |
Minchan Kim | e46e331 | 2014-01-30 15:46:06 -0800 | [diff] [blame] | 430 | bool locked = false; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 431 | |
| 432 | page = bvec->bv_page; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 433 | if (is_partial_io(bvec)) { |
| 434 | /* |
| 435 | * This is a partial IO. We need to read the full page |
| 436 | * before to write the changes. |
| 437 | */ |
Minchan Kim | 7e5a510 | 2013-01-30 11:41:39 +0900 | [diff] [blame] | 438 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 439 | if (!uncmem) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 440 | ret = -ENOMEM; |
| 441 | goto out; |
| 442 | } |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 443 | ret = zram_decompress_page(zram, uncmem, index); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 444 | if (ret) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 445 | goto out; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 448 | zstrm = zcomp_strm_find(zram->comp); |
Minchan Kim | e46e331 | 2014-01-30 15:46:06 -0800 | [diff] [blame] | 449 | locked = true; |
Cong Wang | ba82fe2 | 2011-11-25 23:14:25 +0800 | [diff] [blame] | 450 | user_mem = kmap_atomic(page); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 451 | |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 452 | if (is_partial_io(bvec)) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 453 | memcpy(uncmem + offset, user_mem + bvec->bv_offset, |
| 454 | bvec->bv_len); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 455 | kunmap_atomic(user_mem); |
| 456 | user_mem = NULL; |
| 457 | } else { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 458 | uncmem = user_mem; |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 459 | } |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 460 | |
| 461 | if (page_zero_filled(uncmem)) { |
Cong Wang | ba82fe2 | 2011-11-25 23:14:25 +0800 | [diff] [blame] | 462 | kunmap_atomic(user_mem); |
Sunghan Suh | f40ac2a | 2013-07-03 20:10:05 +0900 | [diff] [blame] | 463 | /* Free memory associated with this sector now. */ |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 464 | write_lock(&zram->meta->tb_lock); |
Sunghan Suh | f40ac2a | 2013-07-03 20:10:05 +0900 | [diff] [blame] | 465 | zram_free_page(zram, index); |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 466 | zram_set_flag(meta, index, ZRAM_ZERO); |
| 467 | write_unlock(&zram->meta->tb_lock); |
Sunghan Suh | f40ac2a | 2013-07-03 20:10:05 +0900 | [diff] [blame] | 468 | |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 469 | atomic64_inc(&zram->stats.zero_pages); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 470 | ret = 0; |
| 471 | goto out; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 474 | ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 475 | if (!is_partial_io(bvec)) { |
| 476 | kunmap_atomic(user_mem); |
| 477 | user_mem = NULL; |
| 478 | uncmem = NULL; |
| 479 | } |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 480 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 481 | if (unlikely(ret)) { |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 482 | pr_err("Compression failed! err=%d\n", ret); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 483 | goto out; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 484 | } |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 485 | src = zstrm->buffer; |
Nitin Gupta | c8f2f0d | 2012-10-10 17:42:18 -0700 | [diff] [blame] | 486 | if (unlikely(clen > max_zpage_size)) { |
Nitin Gupta | c8f2f0d | 2012-10-10 17:42:18 -0700 | [diff] [blame] | 487 | clen = PAGE_SIZE; |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 488 | if (is_partial_io(bvec)) |
| 489 | src = uncmem; |
Nitin Gupta | c8f2f0d | 2012-10-10 17:42:18 -0700 | [diff] [blame] | 490 | } |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 491 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 492 | handle = zs_malloc(meta->mem_pool, clen); |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 493 | if (!handle) { |
Marlies Ruck | 596b3dd | 2013-05-16 14:30:39 -0400 | [diff] [blame] | 494 | pr_info("Error allocating memory for compressed page: %u, size=%zu\n", |
| 495 | index, clen); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 496 | ret = -ENOMEM; |
| 497 | goto out; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 498 | } |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 499 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 500 | |
Jiang Liu | 42e99bd | 2013-06-07 00:07:30 +0800 | [diff] [blame] | 501 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) { |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 502 | src = kmap_atomic(page); |
Jiang Liu | 42e99bd | 2013-06-07 00:07:30 +0800 | [diff] [blame] | 503 | copy_page(cmem, src); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 504 | kunmap_atomic(src); |
Jiang Liu | 42e99bd | 2013-06-07 00:07:30 +0800 | [diff] [blame] | 505 | } else { |
| 506 | memcpy(cmem, src, clen); |
| 507 | } |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 508 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 509 | zcomp_strm_release(zram->comp, zstrm); |
| 510 | locked = false; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 511 | zs_unmap_object(meta->mem_pool, handle); |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 512 | |
Sunghan Suh | f40ac2a | 2013-07-03 20:10:05 +0900 | [diff] [blame] | 513 | /* |
| 514 | * Free memory associated with this sector |
| 515 | * before overwriting unused sectors. |
| 516 | */ |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 517 | write_lock(&zram->meta->tb_lock); |
Sunghan Suh | f40ac2a | 2013-07-03 20:10:05 +0900 | [diff] [blame] | 518 | zram_free_page(zram, index); |
| 519 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 520 | meta->table[index].handle = handle; |
| 521 | meta->table[index].size = clen; |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 522 | write_unlock(&zram->meta->tb_lock); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 523 | |
| 524 | /* Update stats */ |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 525 | atomic64_add(clen, &zram->stats.compr_data_size); |
| 526 | atomic64_inc(&zram->stats.pages_stored); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 527 | out: |
Minchan Kim | e46e331 | 2014-01-30 15:46:06 -0800 | [diff] [blame] | 528 | if (locked) |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 529 | zcomp_strm_release(zram->comp, zstrm); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 530 | if (is_partial_io(bvec)) |
| 531 | kfree(uncmem); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 532 | if (ret) |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 533 | atomic64_inc(&zram->stats.failed_writes); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 534 | return ret; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 538 | int offset, struct bio *bio) |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 539 | { |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 540 | int ret; |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 541 | int rw = bio_data_dir(bio); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 542 | |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 543 | if (rw == READ) { |
| 544 | atomic64_inc(&zram->stats.num_reads); |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 545 | ret = zram_bvec_read(zram, bvec, index, offset, bio); |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 546 | } else { |
| 547 | atomic64_inc(&zram->stats.num_writes); |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 548 | ret = zram_bvec_write(zram, bvec, index, offset); |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 549 | } |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 550 | |
| 551 | return ret; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 554 | /* |
| 555 | * zram_bio_discard - handler on discard request |
| 556 | * @index: physical block index in PAGE_SIZE units |
| 557 | * @offset: byte offset within physical block |
| 558 | */ |
| 559 | static void zram_bio_discard(struct zram *zram, u32 index, |
| 560 | int offset, struct bio *bio) |
| 561 | { |
| 562 | size_t n = bio->bi_iter.bi_size; |
| 563 | |
| 564 | /* |
| 565 | * zram manages data in physical block size units. Because logical block |
| 566 | * size isn't identical with physical block size on some arch, we |
| 567 | * could get a discard request pointing to a specific offset within a |
| 568 | * certain physical block. Although we can handle this request by |
| 569 | * reading that physiclal block and decompressing and partially zeroing |
| 570 | * and re-compressing and then re-storing it, this isn't reasonable |
| 571 | * because our intent with a discard request is to save memory. So |
| 572 | * skipping this logical block is appropriate here. |
| 573 | */ |
| 574 | if (offset) { |
Weijie Yang | 38515c7 | 2014-06-04 16:11:06 -0700 | [diff] [blame] | 575 | if (n <= (PAGE_SIZE - offset)) |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 576 | return; |
| 577 | |
Weijie Yang | 38515c7 | 2014-06-04 16:11:06 -0700 | [diff] [blame] | 578 | n -= (PAGE_SIZE - offset); |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 579 | index++; |
| 580 | } |
| 581 | |
| 582 | while (n >= PAGE_SIZE) { |
| 583 | /* |
| 584 | * Discard request can be large so the lock hold times could be |
| 585 | * lengthy. So take the lock once per page. |
| 586 | */ |
| 587 | write_lock(&zram->meta->tb_lock); |
| 588 | zram_free_page(zram, index); |
| 589 | write_unlock(&zram->meta->tb_lock); |
| 590 | index++; |
| 591 | n -= PAGE_SIZE; |
| 592 | } |
| 593 | } |
| 594 | |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 595 | static void zram_reset_device(struct zram *zram, bool reset_capacity) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 596 | { |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 597 | size_t index; |
| 598 | struct zram_meta *meta; |
| 599 | |
Sergey Senozhatsky | 644d478 | 2013-06-26 15:28:39 +0300 | [diff] [blame] | 600 | down_write(&zram->init_lock); |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 601 | if (!init_done(zram)) { |
Sergey Senozhatsky | 644d478 | 2013-06-26 15:28:39 +0300 | [diff] [blame] | 602 | up_write(&zram->init_lock); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 603 | return; |
Sergey Senozhatsky | 644d478 | 2013-06-26 15:28:39 +0300 | [diff] [blame] | 604 | } |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 605 | |
| 606 | meta = zram->meta; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 607 | /* Free all pages that are still in this zram device */ |
| 608 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { |
| 609 | unsigned long handle = meta->table[index].handle; |
| 610 | if (!handle) |
| 611 | continue; |
| 612 | |
| 613 | zs_free(meta->mem_pool, handle); |
| 614 | } |
| 615 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 616 | zcomp_destroy(zram->comp); |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 617 | zram->max_comp_streams = 1; |
| 618 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 619 | zram_meta_free(zram->meta); |
| 620 | zram->meta = NULL; |
| 621 | /* Reset stats */ |
| 622 | memset(&zram->stats, 0, sizeof(zram->stats)); |
| 623 | |
| 624 | zram->disksize = 0; |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 625 | if (reset_capacity) |
| 626 | set_capacity(zram->disk, 0); |
Sergey Senozhatsky | 644d478 | 2013-06-26 15:28:39 +0300 | [diff] [blame] | 627 | up_write(&zram->init_lock); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 628 | } |
| 629 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 630 | static ssize_t disksize_store(struct device *dev, |
| 631 | struct device_attribute *attr, const char *buf, size_t len) |
| 632 | { |
| 633 | u64 disksize; |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 634 | struct zcomp *comp; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 635 | struct zram_meta *meta; |
| 636 | struct zram *zram = dev_to_zram(dev); |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 637 | int err; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 638 | |
| 639 | disksize = memparse(buf, NULL); |
| 640 | if (!disksize) |
| 641 | return -EINVAL; |
| 642 | |
| 643 | disksize = PAGE_ALIGN(disksize); |
| 644 | meta = zram_meta_alloc(disksize); |
Minchan Kim | db5d711 | 2014-03-03 15:38:34 -0800 | [diff] [blame] | 645 | if (!meta) |
| 646 | return -ENOMEM; |
Sergey Senozhatsky | b67d1ec | 2014-04-07 15:38:09 -0700 | [diff] [blame] | 647 | |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 648 | comp = zcomp_create(zram->compressor, zram->max_comp_streams); |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 649 | if (IS_ERR(comp)) { |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 650 | pr_info("Cannot initialise %s compressing backend\n", |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 651 | zram->compressor); |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 652 | err = PTR_ERR(comp); |
| 653 | goto out_free_meta; |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | down_write(&zram->init_lock); |
| 657 | if (init_done(zram)) { |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 658 | pr_info("Cannot change disksize for initialized device\n"); |
| 659 | err = -EBUSY; |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 660 | goto out_destroy_comp; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 661 | } |
| 662 | |
Sergey Senozhatsky | b67d1ec | 2014-04-07 15:38:09 -0700 | [diff] [blame] | 663 | zram->meta = meta; |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 664 | zram->comp = comp; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 665 | zram->disksize = disksize; |
| 666 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 667 | up_write(&zram->init_lock); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 668 | return len; |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 669 | |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 670 | out_destroy_comp: |
| 671 | up_write(&zram->init_lock); |
| 672 | zcomp_destroy(comp); |
| 673 | out_free_meta: |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 674 | zram_meta_free(meta); |
| 675 | return err; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static ssize_t reset_store(struct device *dev, |
| 679 | struct device_attribute *attr, const char *buf, size_t len) |
| 680 | { |
| 681 | int ret; |
| 682 | unsigned short do_reset; |
| 683 | struct zram *zram; |
| 684 | struct block_device *bdev; |
| 685 | |
| 686 | zram = dev_to_zram(dev); |
| 687 | bdev = bdget_disk(zram->disk, 0); |
| 688 | |
Rashika Kheria | 46a51c8 | 2013-10-30 18:36:32 +0530 | [diff] [blame] | 689 | if (!bdev) |
| 690 | return -ENOMEM; |
| 691 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 692 | /* Do not reset an active device! */ |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 693 | if (bdev->bd_holders) { |
| 694 | ret = -EBUSY; |
| 695 | goto out; |
| 696 | } |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 697 | |
| 698 | ret = kstrtou16(buf, 10, &do_reset); |
| 699 | if (ret) |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 700 | goto out; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 701 | |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 702 | if (!do_reset) { |
| 703 | ret = -EINVAL; |
| 704 | goto out; |
| 705 | } |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 706 | |
| 707 | /* Make sure all pending I/O is finished */ |
Rashika Kheria | 46a51c8 | 2013-10-30 18:36:32 +0530 | [diff] [blame] | 708 | fsync_bdev(bdev); |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 709 | bdput(bdev); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 710 | |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 711 | zram_reset_device(zram, true); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 712 | return len; |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 713 | |
| 714 | out: |
| 715 | bdput(bdev); |
| 716 | return ret; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 719 | static void __zram_make_request(struct zram *zram, struct bio *bio) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 720 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 721 | int offset; |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 722 | u32 index; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 723 | struct bio_vec bvec; |
| 724 | struct bvec_iter iter; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 725 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 726 | index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT; |
| 727 | offset = (bio->bi_iter.bi_sector & |
| 728 | (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 729 | |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 730 | if (unlikely(bio->bi_rw & REQ_DISCARD)) { |
| 731 | zram_bio_discard(zram, index, offset, bio); |
| 732 | bio_endio(bio, 0); |
| 733 | return; |
| 734 | } |
| 735 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 736 | bio_for_each_segment(bvec, bio, iter) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 737 | int max_transfer_size = PAGE_SIZE - offset; |
| 738 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 739 | if (bvec.bv_len > max_transfer_size) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 740 | /* |
| 741 | * zram_bvec_rw() can only make operation on a single |
| 742 | * zram page. Split the bio vector. |
| 743 | */ |
| 744 | struct bio_vec bv; |
| 745 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 746 | bv.bv_page = bvec.bv_page; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 747 | bv.bv_len = max_transfer_size; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 748 | bv.bv_offset = bvec.bv_offset; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 749 | |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 750 | if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 751 | goto out; |
| 752 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 753 | bv.bv_len = bvec.bv_len - max_transfer_size; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 754 | bv.bv_offset += max_transfer_size; |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 755 | if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 756 | goto out; |
| 757 | } else |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 758 | if (zram_bvec_rw(zram, &bvec, index, offset, bio) < 0) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 759 | goto out; |
| 760 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 761 | update_position(&index, &offset, &bvec); |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 762 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 763 | |
| 764 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
| 765 | bio_endio(bio, 0); |
Nitin Gupta | 7d7854b | 2011-01-22 07:36:15 -0500 | [diff] [blame] | 766 | return; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 767 | |
| 768 | out: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 769 | bio_io_error(bio); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 770 | } |
| 771 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 772 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 773 | * Handler function for all zram I/O requests. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 774 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 775 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 776 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 777 | struct zram *zram = queue->queuedata; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 778 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 779 | down_read(&zram->init_lock); |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 780 | if (unlikely(!init_done(zram))) |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 781 | goto error; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 782 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 783 | if (!valid_io_request(zram, bio)) { |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 784 | atomic64_inc(&zram->stats.invalid_io); |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 785 | goto error; |
Jerome Marchand | 6642a67 | 2011-02-17 17:11:49 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 788 | __zram_make_request(zram, bio); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 789 | up_read(&zram->init_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 790 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 791 | return; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 792 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 793 | error: |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 794 | up_read(&zram->init_lock); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 795 | bio_io_error(bio); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 796 | } |
| 797 | |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 798 | static void zram_slot_free_notify(struct block_device *bdev, |
| 799 | unsigned long index) |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 800 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 801 | struct zram *zram; |
Minchan Kim | f614a9f | 2014-01-30 15:46:04 -0800 | [diff] [blame] | 802 | struct zram_meta *meta; |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 803 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 804 | zram = bdev->bd_disk->private_data; |
Minchan Kim | f614a9f | 2014-01-30 15:46:04 -0800 | [diff] [blame] | 805 | meta = zram->meta; |
| 806 | |
| 807 | write_lock(&meta->tb_lock); |
| 808 | zram_free_page(zram, index); |
| 809 | write_unlock(&meta->tb_lock); |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 810 | atomic64_inc(&zram->stats.notify_free); |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 811 | } |
| 812 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 813 | static const struct block_device_operations zram_devops = { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 814 | .swap_slot_free_notify = zram_slot_free_notify, |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 815 | .owner = THIS_MODULE |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 816 | }; |
| 817 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 818 | static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR, |
| 819 | disksize_show, disksize_store); |
| 820 | static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL); |
| 821 | static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 822 | static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 823 | static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL); |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 824 | static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR, |
| 825 | max_comp_streams_show, max_comp_streams_store); |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 826 | static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR, |
| 827 | comp_algorithm_show, comp_algorithm_store); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 828 | |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 829 | ZRAM_ATTR_RO(num_reads); |
| 830 | ZRAM_ATTR_RO(num_writes); |
Sergey Senozhatsky | 6444724 | 2014-04-07 15:38:05 -0700 | [diff] [blame] | 831 | ZRAM_ATTR_RO(failed_reads); |
| 832 | ZRAM_ATTR_RO(failed_writes); |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 833 | ZRAM_ATTR_RO(invalid_io); |
| 834 | ZRAM_ATTR_RO(notify_free); |
| 835 | ZRAM_ATTR_RO(zero_pages); |
| 836 | ZRAM_ATTR_RO(compr_data_size); |
| 837 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 838 | static struct attribute *zram_disk_attrs[] = { |
| 839 | &dev_attr_disksize.attr, |
| 840 | &dev_attr_initstate.attr, |
| 841 | &dev_attr_reset.attr, |
| 842 | &dev_attr_num_reads.attr, |
| 843 | &dev_attr_num_writes.attr, |
Sergey Senozhatsky | 6444724 | 2014-04-07 15:38:05 -0700 | [diff] [blame] | 844 | &dev_attr_failed_reads.attr, |
| 845 | &dev_attr_failed_writes.attr, |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 846 | &dev_attr_invalid_io.attr, |
| 847 | &dev_attr_notify_free.attr, |
| 848 | &dev_attr_zero_pages.attr, |
| 849 | &dev_attr_orig_data_size.attr, |
| 850 | &dev_attr_compr_data_size.attr, |
| 851 | &dev_attr_mem_used_total.attr, |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 852 | &dev_attr_max_comp_streams.attr, |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 853 | &dev_attr_comp_algorithm.attr, |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 854 | NULL, |
| 855 | }; |
| 856 | |
| 857 | static struct attribute_group zram_disk_attr_group = { |
| 858 | .attrs = zram_disk_attrs, |
| 859 | }; |
| 860 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 861 | static int create_device(struct zram *zram, int device_id) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 862 | { |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 863 | int ret = -ENOMEM; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 864 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 865 | init_rwsem(&zram->init_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 866 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 867 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
| 868 | if (!zram->queue) { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 869 | pr_err("Error allocating disk queue for device %d\n", |
| 870 | device_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 871 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 872 | } |
| 873 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 874 | blk_queue_make_request(zram->queue, zram_make_request); |
| 875 | zram->queue->queuedata = zram; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 876 | |
| 877 | /* gendisk structure */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 878 | zram->disk = alloc_disk(1); |
| 879 | if (!zram->disk) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 880 | pr_warn("Error allocating disk structure for device %d\n", |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 881 | device_id); |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 882 | goto out_free_queue; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 883 | } |
| 884 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 885 | zram->disk->major = zram_major; |
| 886 | zram->disk->first_minor = device_id; |
| 887 | zram->disk->fops = &zram_devops; |
| 888 | zram->disk->queue = zram->queue; |
| 889 | zram->disk->private_data = zram; |
| 890 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 891 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 892 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 893 | set_capacity(zram->disk, 0); |
Sergey Senozhatsky | b67d1ec | 2014-04-07 15:38:09 -0700 | [diff] [blame] | 894 | /* zram devices sort of resembles non-rotational disks */ |
| 895 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 896 | /* |
| 897 | * To ensure that we always get PAGE_SIZE aligned |
| 898 | * and n*PAGE_SIZED sized I/O requests. |
| 899 | */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 900 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
Robert Jennings | 7b19b8d | 2011-01-28 08:58:17 -0600 | [diff] [blame] | 901 | blk_queue_logical_block_size(zram->disk->queue, |
| 902 | ZRAM_LOGICAL_BLOCK_SIZE); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 903 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
| 904 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 905 | zram->disk->queue->limits.discard_granularity = PAGE_SIZE; |
| 906 | zram->disk->queue->limits.max_discard_sectors = UINT_MAX; |
| 907 | /* |
| 908 | * zram_bio_discard() will clear all logical blocks if logical block |
| 909 | * size is identical with physical block size(PAGE_SIZE). But if it is |
| 910 | * different, we will skip discarding some parts of logical blocks in |
| 911 | * the part of the request range which isn't aligned to physical block |
| 912 | * size. So we can't ensure that all discarded logical blocks are |
| 913 | * zeroed. |
| 914 | */ |
| 915 | if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE) |
| 916 | zram->disk->queue->limits.discard_zeroes_data = 1; |
| 917 | else |
| 918 | zram->disk->queue->limits.discard_zeroes_data = 0; |
| 919 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue); |
Nitin Gupta | 5d83d5a | 2010-01-28 21:13:39 +0530 | [diff] [blame] | 920 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 921 | add_disk(zram->disk); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 922 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 923 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
| 924 | &zram_disk_attr_group); |
| 925 | if (ret < 0) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 926 | pr_warn("Error creating sysfs group"); |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 927 | goto out_free_disk; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 928 | } |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 929 | strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor)); |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 930 | zram->meta = NULL; |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 931 | zram->max_comp_streams = 1; |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 932 | return 0; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 933 | |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 934 | out_free_disk: |
| 935 | del_gendisk(zram->disk); |
| 936 | put_disk(zram->disk); |
| 937 | out_free_queue: |
| 938 | blk_cleanup_queue(zram->queue); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 939 | out: |
| 940 | return ret; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 941 | } |
| 942 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 943 | static void destroy_device(struct zram *zram) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 944 | { |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 945 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
| 946 | &zram_disk_attr_group); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 947 | |
Rashika Kheria | 59d3fe5 | 2013-10-30 18:43:32 +0530 | [diff] [blame] | 948 | del_gendisk(zram->disk); |
| 949 | put_disk(zram->disk); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 950 | |
Rashika Kheria | 59d3fe5 | 2013-10-30 18:43:32 +0530 | [diff] [blame] | 951 | blk_cleanup_queue(zram->queue); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 952 | } |
| 953 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 954 | static int __init zram_init(void) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 955 | { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 956 | int ret, dev_id; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 957 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 958 | if (num_devices > max_num_devices) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 959 | pr_warn("Invalid value for num_devices: %u\n", |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 960 | num_devices); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 961 | ret = -EINVAL; |
| 962 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 963 | } |
| 964 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 965 | zram_major = register_blkdev(0, "zram"); |
| 966 | if (zram_major <= 0) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 967 | pr_warn("Unable to get major number\n"); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 968 | ret = -EBUSY; |
| 969 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 970 | } |
| 971 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 972 | /* Allocate the device array and initialize each one */ |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 973 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 974 | if (!zram_devices) { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 975 | ret = -ENOMEM; |
| 976 | goto unregister; |
| 977 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 978 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 979 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 980 | ret = create_device(&zram_devices[dev_id], dev_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 981 | if (ret) |
Minchan Kim | 3bf040c | 2010-01-11 16:15:53 +0900 | [diff] [blame] | 982 | goto free_devices; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 983 | } |
| 984 | |
Davidlohr Bueso | ca3d70b | 2013-01-01 21:24:13 -0800 | [diff] [blame] | 985 | pr_info("Created %u device(s) ...\n", num_devices); |
| 986 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 987 | return 0; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 988 | |
Minchan Kim | 3bf040c | 2010-01-11 16:15:53 +0900 | [diff] [blame] | 989 | free_devices: |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 990 | while (dev_id) |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 991 | destroy_device(&zram_devices[--dev_id]); |
| 992 | kfree(zram_devices); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 993 | unregister: |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 994 | unregister_blkdev(zram_major, "zram"); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 995 | out: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 996 | return ret; |
| 997 | } |
| 998 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 999 | static void __exit zram_exit(void) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1000 | { |
| 1001 | int i; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1002 | struct zram *zram; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1003 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 1004 | for (i = 0; i < num_devices; i++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1005 | zram = &zram_devices[i]; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1006 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1007 | destroy_device(zram); |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 1008 | /* |
| 1009 | * Shouldn't access zram->disk after destroy_device |
| 1010 | * because destroy_device already released zram->disk. |
| 1011 | */ |
| 1012 | zram_reset_device(zram, false); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1013 | } |
| 1014 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1015 | unregister_blkdev(zram_major, "zram"); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1016 | |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1017 | kfree(zram_devices); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1018 | pr_debug("Cleanup done!\n"); |
| 1019 | } |
| 1020 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1021 | module_init(zram_init); |
| 1022 | module_exit(zram_exit); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1023 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 1024 | module_param(num_devices, uint, 0); |
| 1025 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); |
| 1026 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1027 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1028 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1029 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |