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