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