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 | |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 718 | static void zram_reset_device(struct zram *zram, bool reset_capacity) |
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)); |
| 735 | |
| 736 | zram->disksize = 0; |
Minchan Kim | b4c5c60 | 2014-07-23 14:00:04 -0700 | [diff] [blame] | 737 | if (reset_capacity) |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 738 | set_capacity(zram->disk, 0); |
Minchan Kim | b4c5c60 | 2014-07-23 14:00:04 -0700 | [diff] [blame] | 739 | |
Sergey Senozhatsky | 644d478 | 2013-06-26 15:28:39 +0300 | [diff] [blame] | 740 | up_write(&zram->init_lock); |
Minchan Kim | b4c5c60 | 2014-07-23 14:00:04 -0700 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | * Revalidate disk out of the init_lock to avoid lockdep splat. |
| 744 | * It's okay because disk's capacity is protected by init_lock |
| 745 | * so that revalidate_disk always sees up-to-date capacity. |
| 746 | */ |
| 747 | if (reset_capacity) |
| 748 | revalidate_disk(zram->disk); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 749 | } |
| 750 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 751 | static ssize_t disksize_store(struct device *dev, |
| 752 | struct device_attribute *attr, const char *buf, size_t len) |
| 753 | { |
| 754 | u64 disksize; |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 755 | struct zcomp *comp; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 756 | struct zram_meta *meta; |
| 757 | struct zram *zram = dev_to_zram(dev); |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 758 | int err; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 759 | |
| 760 | disksize = memparse(buf, NULL); |
| 761 | if (!disksize) |
| 762 | return -EINVAL; |
| 763 | |
| 764 | disksize = PAGE_ALIGN(disksize); |
| 765 | meta = zram_meta_alloc(disksize); |
Minchan Kim | db5d711 | 2014-03-03 15:38:34 -0800 | [diff] [blame] | 766 | if (!meta) |
| 767 | return -ENOMEM; |
Sergey Senozhatsky | b67d1ec | 2014-04-07 15:38:09 -0700 | [diff] [blame] | 768 | |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 769 | comp = zcomp_create(zram->compressor, zram->max_comp_streams); |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 770 | if (IS_ERR(comp)) { |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 771 | pr_info("Cannot initialise %s compressing backend\n", |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 772 | zram->compressor); |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 773 | err = PTR_ERR(comp); |
| 774 | goto out_free_meta; |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | down_write(&zram->init_lock); |
| 778 | if (init_done(zram)) { |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 779 | pr_info("Cannot change disksize for initialized device\n"); |
| 780 | err = -EBUSY; |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 781 | goto out_destroy_comp; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 782 | } |
| 783 | |
Sergey Senozhatsky | b67d1ec | 2014-04-07 15:38:09 -0700 | [diff] [blame] | 784 | zram->meta = meta; |
Sergey Senozhatsky | d61f98c | 2014-04-07 15:38:19 -0700 | [diff] [blame] | 785 | zram->comp = comp; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 786 | zram->disksize = disksize; |
| 787 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 788 | up_write(&zram->init_lock); |
Minchan Kim | b4c5c60 | 2014-07-23 14:00:04 -0700 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * Revalidate disk out of the init_lock to avoid lockdep splat. |
| 792 | * It's okay because disk's capacity is protected by init_lock |
| 793 | * so that revalidate_disk always sees up-to-date capacity. |
| 794 | */ |
| 795 | revalidate_disk(zram->disk); |
| 796 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 797 | return len; |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 798 | |
Sergey Senozhatsky | fcfa8d9 | 2014-04-07 15:38:20 -0700 | [diff] [blame] | 799 | out_destroy_comp: |
| 800 | up_write(&zram->init_lock); |
| 801 | zcomp_destroy(comp); |
| 802 | out_free_meta: |
Ganesh Mahendran | 1fec117 | 2015-02-12 15:00:33 -0800 | [diff] [blame^] | 803 | zram_meta_free(meta, disksize); |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 804 | return err; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static ssize_t reset_store(struct device *dev, |
| 808 | struct device_attribute *attr, const char *buf, size_t len) |
| 809 | { |
| 810 | int ret; |
| 811 | unsigned short do_reset; |
| 812 | struct zram *zram; |
| 813 | struct block_device *bdev; |
| 814 | |
| 815 | zram = dev_to_zram(dev); |
| 816 | bdev = bdget_disk(zram->disk, 0); |
| 817 | |
Rashika Kheria | 46a51c8 | 2013-10-30 18:36:32 +0530 | [diff] [blame] | 818 | if (!bdev) |
| 819 | return -ENOMEM; |
| 820 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 821 | /* Do not reset an active device! */ |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 822 | if (bdev->bd_holders) { |
| 823 | ret = -EBUSY; |
| 824 | goto out; |
| 825 | } |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 826 | |
| 827 | ret = kstrtou16(buf, 10, &do_reset); |
| 828 | if (ret) |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 829 | goto out; |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 830 | |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 831 | if (!do_reset) { |
| 832 | ret = -EINVAL; |
| 833 | goto out; |
| 834 | } |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 835 | |
| 836 | /* Make sure all pending I/O is finished */ |
Rashika Kheria | 46a51c8 | 2013-10-30 18:36:32 +0530 | [diff] [blame] | 837 | fsync_bdev(bdev); |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 838 | bdput(bdev); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 839 | |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 840 | zram_reset_device(zram, true); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 841 | return len; |
Rashika Kheria | 1b67222 | 2013-11-10 22:13:53 +0530 | [diff] [blame] | 842 | |
| 843 | out: |
| 844 | bdput(bdev); |
| 845 | return ret; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 848 | static void __zram_make_request(struct zram *zram, struct bio *bio) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 849 | { |
karam.lee | b627cff | 2014-12-12 16:56:47 -0800 | [diff] [blame] | 850 | int offset, rw; |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 851 | u32 index; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 852 | struct bio_vec bvec; |
| 853 | struct bvec_iter iter; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 854 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 855 | index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT; |
| 856 | offset = (bio->bi_iter.bi_sector & |
| 857 | (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 858 | |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 859 | if (unlikely(bio->bi_rw & REQ_DISCARD)) { |
| 860 | zram_bio_discard(zram, index, offset, bio); |
| 861 | bio_endio(bio, 0); |
| 862 | return; |
| 863 | } |
| 864 | |
karam.lee | b627cff | 2014-12-12 16:56:47 -0800 | [diff] [blame] | 865 | rw = bio_data_dir(bio); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 866 | bio_for_each_segment(bvec, bio, iter) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 867 | int max_transfer_size = PAGE_SIZE - offset; |
| 868 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 869 | if (bvec.bv_len > max_transfer_size) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 870 | /* |
| 871 | * zram_bvec_rw() can only make operation on a single |
| 872 | * zram page. Split the bio vector. |
| 873 | */ |
| 874 | struct bio_vec bv; |
| 875 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 876 | bv.bv_page = bvec.bv_page; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 877 | bv.bv_len = max_transfer_size; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 878 | bv.bv_offset = bvec.bv_offset; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 879 | |
karam.lee | b627cff | 2014-12-12 16:56:47 -0800 | [diff] [blame] | 880 | if (zram_bvec_rw(zram, &bv, index, offset, rw) < 0) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 881 | goto out; |
| 882 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 883 | bv.bv_len = bvec.bv_len - max_transfer_size; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 884 | bv.bv_offset += max_transfer_size; |
karam.lee | b627cff | 2014-12-12 16:56:47 -0800 | [diff] [blame] | 885 | if (zram_bvec_rw(zram, &bv, index + 1, 0, rw) < 0) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 886 | goto out; |
| 887 | } else |
karam.lee | b627cff | 2014-12-12 16:56:47 -0800 | [diff] [blame] | 888 | if (zram_bvec_rw(zram, &bvec, index, offset, rw) < 0) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 889 | goto out; |
| 890 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 891 | update_position(&index, &offset, &bvec); |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 892 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 893 | |
| 894 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
| 895 | bio_endio(bio, 0); |
Nitin Gupta | 7d7854b | 2011-01-22 07:36:15 -0500 | [diff] [blame] | 896 | return; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 897 | |
| 898 | out: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 899 | bio_io_error(bio); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 900 | } |
| 901 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 902 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 903 | * Handler function for all zram I/O requests. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 904 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 905 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 906 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 907 | struct zram *zram = queue->queuedata; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 908 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 909 | down_read(&zram->init_lock); |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 910 | if (unlikely(!init_done(zram))) |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 911 | goto error; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 912 | |
karam.lee | 54850e7 | 2014-12-12 16:56:50 -0800 | [diff] [blame] | 913 | if (!valid_io_request(zram, bio->bi_iter.bi_sector, |
| 914 | bio->bi_iter.bi_size)) { |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 915 | atomic64_inc(&zram->stats.invalid_io); |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 916 | goto error; |
Jerome Marchand | 6642a67 | 2011-02-17 17:11:49 +0100 | [diff] [blame] | 917 | } |
| 918 | |
Sergey Senozhatsky | be257c6 | 2014-04-07 15:38:01 -0700 | [diff] [blame] | 919 | __zram_make_request(zram, bio); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 920 | up_read(&zram->init_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 921 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 922 | return; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 923 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 924 | error: |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 925 | up_read(&zram->init_lock); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 926 | bio_io_error(bio); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 927 | } |
| 928 | |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 929 | static void zram_slot_free_notify(struct block_device *bdev, |
| 930 | unsigned long index) |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 931 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 932 | struct zram *zram; |
Minchan Kim | f614a9f | 2014-01-30 15:46:04 -0800 | [diff] [blame] | 933 | struct zram_meta *meta; |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 934 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 935 | zram = bdev->bd_disk->private_data; |
Minchan Kim | f614a9f | 2014-01-30 15:46:04 -0800 | [diff] [blame] | 936 | meta = zram->meta; |
| 937 | |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame] | 938 | bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value); |
Minchan Kim | f614a9f | 2014-01-30 15:46:04 -0800 | [diff] [blame] | 939 | zram_free_page(zram, index); |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame] | 940 | bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value); |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 941 | atomic64_inc(&zram->stats.notify_free); |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 942 | } |
| 943 | |
karam.lee | 8c7f010 | 2014-12-12 16:56:53 -0800 | [diff] [blame] | 944 | static int zram_rw_page(struct block_device *bdev, sector_t sector, |
| 945 | struct page *page, int rw) |
| 946 | { |
| 947 | int offset, err; |
| 948 | u32 index; |
| 949 | struct zram *zram; |
| 950 | struct bio_vec bv; |
| 951 | |
| 952 | zram = bdev->bd_disk->private_data; |
| 953 | if (!valid_io_request(zram, sector, PAGE_SIZE)) { |
| 954 | atomic64_inc(&zram->stats.invalid_io); |
| 955 | return -EINVAL; |
| 956 | } |
| 957 | |
| 958 | down_read(&zram->init_lock); |
| 959 | if (unlikely(!init_done(zram))) { |
| 960 | err = -EIO; |
| 961 | goto out_unlock; |
| 962 | } |
| 963 | |
| 964 | index = sector >> SECTORS_PER_PAGE_SHIFT; |
| 965 | offset = sector & (SECTORS_PER_PAGE - 1) << SECTOR_SHIFT; |
| 966 | |
| 967 | bv.bv_page = page; |
| 968 | bv.bv_len = PAGE_SIZE; |
| 969 | bv.bv_offset = 0; |
| 970 | |
| 971 | err = zram_bvec_rw(zram, &bv, index, offset, rw); |
| 972 | out_unlock: |
| 973 | up_read(&zram->init_lock); |
| 974 | /* |
| 975 | * If I/O fails, just return error(ie, non-zero) without |
| 976 | * calling page_endio. |
| 977 | * It causes resubmit the I/O with bio request by upper functions |
| 978 | * of rw_page(e.g., swap_readpage, __swap_writepage) and |
| 979 | * bio->bi_end_io does things to handle the error |
| 980 | * (e.g., SetPageError, set_page_dirty and extra works). |
| 981 | */ |
| 982 | if (err == 0) |
| 983 | page_endio(page, rw, 0); |
| 984 | return err; |
| 985 | } |
| 986 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 987 | static const struct block_device_operations zram_devops = { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 988 | .swap_slot_free_notify = zram_slot_free_notify, |
karam.lee | 8c7f010 | 2014-12-12 16:56:53 -0800 | [diff] [blame] | 989 | .rw_page = zram_rw_page, |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 990 | .owner = THIS_MODULE |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 991 | }; |
| 992 | |
Ganesh Mahendran | 083914e | 2014-12-12 16:57:13 -0800 | [diff] [blame] | 993 | static DEVICE_ATTR_RW(disksize); |
| 994 | static DEVICE_ATTR_RO(initstate); |
| 995 | static DEVICE_ATTR_WO(reset); |
| 996 | static DEVICE_ATTR_RO(orig_data_size); |
| 997 | static DEVICE_ATTR_RO(mem_used_total); |
| 998 | static DEVICE_ATTR_RW(mem_limit); |
| 999 | static DEVICE_ATTR_RW(mem_used_max); |
| 1000 | static DEVICE_ATTR_RW(max_comp_streams); |
| 1001 | static DEVICE_ATTR_RW(comp_algorithm); |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 1002 | |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 1003 | ZRAM_ATTR_RO(num_reads); |
| 1004 | ZRAM_ATTR_RO(num_writes); |
Sergey Senozhatsky | 6444724 | 2014-04-07 15:38:05 -0700 | [diff] [blame] | 1005 | ZRAM_ATTR_RO(failed_reads); |
| 1006 | ZRAM_ATTR_RO(failed_writes); |
Sergey Senozhatsky | a68eb3b | 2014-04-07 15:38:04 -0700 | [diff] [blame] | 1007 | ZRAM_ATTR_RO(invalid_io); |
| 1008 | ZRAM_ATTR_RO(notify_free); |
| 1009 | ZRAM_ATTR_RO(zero_pages); |
| 1010 | ZRAM_ATTR_RO(compr_data_size); |
| 1011 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 1012 | static struct attribute *zram_disk_attrs[] = { |
| 1013 | &dev_attr_disksize.attr, |
| 1014 | &dev_attr_initstate.attr, |
| 1015 | &dev_attr_reset.attr, |
| 1016 | &dev_attr_num_reads.attr, |
| 1017 | &dev_attr_num_writes.attr, |
Sergey Senozhatsky | 6444724 | 2014-04-07 15:38:05 -0700 | [diff] [blame] | 1018 | &dev_attr_failed_reads.attr, |
| 1019 | &dev_attr_failed_writes.attr, |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 1020 | &dev_attr_invalid_io.attr, |
| 1021 | &dev_attr_notify_free.attr, |
| 1022 | &dev_attr_zero_pages.attr, |
| 1023 | &dev_attr_orig_data_size.attr, |
| 1024 | &dev_attr_compr_data_size.attr, |
| 1025 | &dev_attr_mem_used_total.attr, |
Minchan Kim | 9ada9da | 2014-10-09 15:29:53 -0700 | [diff] [blame] | 1026 | &dev_attr_mem_limit.attr, |
Minchan Kim | 461a8ee | 2014-10-09 15:29:55 -0700 | [diff] [blame] | 1027 | &dev_attr_mem_used_max.attr, |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 1028 | &dev_attr_max_comp_streams.attr, |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 1029 | &dev_attr_comp_algorithm.attr, |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 1030 | NULL, |
| 1031 | }; |
| 1032 | |
| 1033 | static struct attribute_group zram_disk_attr_group = { |
| 1034 | .attrs = zram_disk_attrs, |
| 1035 | }; |
| 1036 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1037 | static int create_device(struct zram *zram, int device_id) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1038 | { |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 1039 | int ret = -ENOMEM; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1040 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 1041 | init_rwsem(&zram->init_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1042 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1043 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
| 1044 | if (!zram->queue) { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1045 | pr_err("Error allocating disk queue for device %d\n", |
| 1046 | device_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1047 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1048 | } |
| 1049 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1050 | blk_queue_make_request(zram->queue, zram_make_request); |
| 1051 | zram->queue->queuedata = zram; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1052 | |
| 1053 | /* gendisk structure */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1054 | zram->disk = alloc_disk(1); |
| 1055 | if (!zram->disk) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 1056 | pr_warn("Error allocating disk structure for device %d\n", |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1057 | device_id); |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 1058 | goto out_free_queue; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1059 | } |
| 1060 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1061 | zram->disk->major = zram_major; |
| 1062 | zram->disk->first_minor = device_id; |
| 1063 | zram->disk->fops = &zram_devops; |
| 1064 | zram->disk->queue = zram->queue; |
| 1065 | zram->disk->private_data = zram; |
| 1066 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1067 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 1068 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1069 | set_capacity(zram->disk, 0); |
Sergey Senozhatsky | b67d1ec | 2014-04-07 15:38:09 -0700 | [diff] [blame] | 1070 | /* zram devices sort of resembles non-rotational disks */ |
| 1071 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 1072 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue); |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 1073 | /* |
| 1074 | * To ensure that we always get PAGE_SIZE aligned |
| 1075 | * and n*PAGE_SIZED sized I/O requests. |
| 1076 | */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1077 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
Robert Jennings | 7b19b8d | 2011-01-28 08:58:17 -0600 | [diff] [blame] | 1078 | blk_queue_logical_block_size(zram->disk->queue, |
| 1079 | ZRAM_LOGICAL_BLOCK_SIZE); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1080 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
| 1081 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); |
Joonsoo Kim | f4659d8 | 2014-04-07 15:38:24 -0700 | [diff] [blame] | 1082 | zram->disk->queue->limits.discard_granularity = PAGE_SIZE; |
| 1083 | zram->disk->queue->limits.max_discard_sectors = UINT_MAX; |
| 1084 | /* |
| 1085 | * zram_bio_discard() will clear all logical blocks if logical block |
| 1086 | * size is identical with physical block size(PAGE_SIZE). But if it is |
| 1087 | * different, we will skip discarding some parts of logical blocks in |
| 1088 | * the part of the request range which isn't aligned to physical block |
| 1089 | * size. So we can't ensure that all discarded logical blocks are |
| 1090 | * zeroed. |
| 1091 | */ |
| 1092 | if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE) |
| 1093 | zram->disk->queue->limits.discard_zeroes_data = 1; |
| 1094 | else |
| 1095 | zram->disk->queue->limits.discard_zeroes_data = 0; |
| 1096 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue); |
Nitin Gupta | 5d83d5a | 2010-01-28 21:13:39 +0530 | [diff] [blame] | 1097 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1098 | add_disk(zram->disk); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1099 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 1100 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
| 1101 | &zram_disk_attr_group); |
| 1102 | if (ret < 0) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 1103 | pr_warn("Error creating sysfs group"); |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 1104 | goto out_free_disk; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 1105 | } |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 1106 | strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor)); |
Sergey Senozhatsky | be2d1d5 | 2014-04-07 15:38:00 -0700 | [diff] [blame] | 1107 | zram->meta = NULL; |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 1108 | zram->max_comp_streams = 1; |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 1109 | return 0; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1110 | |
Jiang Liu | 39a9b8a | 2013-06-07 00:07:24 +0800 | [diff] [blame] | 1111 | out_free_disk: |
| 1112 | del_gendisk(zram->disk); |
| 1113 | put_disk(zram->disk); |
| 1114 | out_free_queue: |
| 1115 | blk_cleanup_queue(zram->queue); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1116 | out: |
| 1117 | return ret; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1118 | } |
| 1119 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1120 | static void destroy_device(struct zram *zram) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1121 | { |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 1122 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
| 1123 | &zram_disk_attr_group); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 1124 | |
Rashika Kheria | 59d3fe5 | 2013-10-30 18:43:32 +0530 | [diff] [blame] | 1125 | del_gendisk(zram->disk); |
| 1126 | put_disk(zram->disk); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1127 | |
Rashika Kheria | 59d3fe5 | 2013-10-30 18:43:32 +0530 | [diff] [blame] | 1128 | blk_cleanup_queue(zram->queue); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1129 | } |
| 1130 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1131 | static int __init zram_init(void) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1132 | { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1133 | int ret, dev_id; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1134 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 1135 | if (num_devices > max_num_devices) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 1136 | pr_warn("Invalid value for num_devices: %u\n", |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 1137 | num_devices); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1138 | ret = -EINVAL; |
| 1139 | goto out; |
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 | zram_major = register_blkdev(0, "zram"); |
| 1143 | if (zram_major <= 0) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 1144 | pr_warn("Unable to get major number\n"); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1145 | ret = -EBUSY; |
| 1146 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1147 | } |
| 1148 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1149 | /* Allocate the device array and initialize each one */ |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 1150 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1151 | if (!zram_devices) { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1152 | ret = -ENOMEM; |
| 1153 | goto unregister; |
| 1154 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1155 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 1156 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1157 | ret = create_device(&zram_devices[dev_id], dev_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1158 | if (ret) |
Minchan Kim | 3bf040c | 2010-01-11 16:15:53 +0900 | [diff] [blame] | 1159 | goto free_devices; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1160 | } |
| 1161 | |
Davidlohr Bueso | ca3d70b | 2013-01-01 21:24:13 -0800 | [diff] [blame] | 1162 | pr_info("Created %u device(s) ...\n", num_devices); |
| 1163 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1164 | return 0; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1165 | |
Minchan Kim | 3bf040c | 2010-01-11 16:15:53 +0900 | [diff] [blame] | 1166 | free_devices: |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1167 | while (dev_id) |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1168 | destroy_device(&zram_devices[--dev_id]); |
| 1169 | kfree(zram_devices); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1170 | unregister: |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1171 | unregister_blkdev(zram_major, "zram"); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 1172 | out: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1173 | return ret; |
| 1174 | } |
| 1175 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1176 | static void __exit zram_exit(void) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1177 | { |
| 1178 | int i; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1179 | struct zram *zram; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1180 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 1181 | for (i = 0; i < num_devices; i++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1182 | zram = &zram_devices[i]; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1183 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1184 | destroy_device(zram); |
Minchan Kim | 2b86ab9 | 2013-08-12 15:13:55 +0900 | [diff] [blame] | 1185 | /* |
| 1186 | * Shouldn't access zram->disk after destroy_device |
| 1187 | * because destroy_device already released zram->disk. |
| 1188 | */ |
| 1189 | zram_reset_device(zram, false); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1190 | } |
| 1191 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1192 | unregister_blkdev(zram_major, "zram"); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1193 | |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 1194 | kfree(zram_devices); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1195 | pr_debug("Cleanup done!\n"); |
| 1196 | } |
| 1197 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1198 | module_init(zram_init); |
| 1199 | module_exit(zram_exit); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1200 | |
Sergey Senozhatsky | 9b3bb7a | 2013-06-22 03:21:18 +0300 | [diff] [blame] | 1201 | module_param(num_devices, uint, 0); |
| 1202 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); |
| 1203 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1204 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1205 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 1206 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |