blob: 2ad0b5bce44be89494d9cb5f75da19b9e459cd10 [file] [log] [blame]
Nitin Gupta306b0c92009-09-22 10:26:53 +05301/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05302 * Compressed RAM block device
Nitin Gupta306b0c92009-09-22 10:26:53 +05303 *
Nitin Gupta1130ebb2010-01-28 21:21:35 +05304 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
Minchan Kim7bfb3de2014-01-30 15:45:55 -08005 * 2012, 2013 Minchan Kim
Nitin Gupta306b0c92009-09-22 10:26:53 +05306 *
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 Gupta306b0c92009-09-22 10:26:53 +053013 */
14
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053015#define KMSG_COMPONENT "zram"
Nitin Gupta306b0c92009-09-22 10:26:53 +053016#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
Robert Jenningsb1f5b812011-01-28 08:59:26 -060018#ifdef CONFIG_ZRAM_DEBUG
19#define DEBUG
20#endif
21
Nitin Gupta306b0c92009-09-22 10:26:53 +053022#include <linux/module.h>
23#include <linux/kernel.h>
Randy Dunlap8946a082010-06-23 20:27:09 -070024#include <linux/bio.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053025#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 Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053032#include <linux/string.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053033#include <linux/vmalloc.h>
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -070034#include <linux/err.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053035
Nitin Gupta16a4bfb2010-06-01 13:31:24 +053036#include "zram_drv.h"
Nitin Gupta306b0c92009-09-22 10:26:53 +053037
38/* Globals */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053039static int zram_major;
Jiang Liu0f0e3ba2013-06-07 00:07:29 +080040static struct zram *zram_devices;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -070041static const char *default_compressor = "lzo";
Nitin Gupta306b0c92009-09-22 10:26:53 +053042
Nitin Gupta306b0c92009-09-22 10:26:53 +053043/* Module params (documentation at end) */
Davidlohr Buesoca3d70b2013-01-01 21:24:13 -080044static unsigned int num_devices = 1;
Nitin Gupta33863c22010-08-09 22:56:47 +053045
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070046#define ZRAM_ATTR_RO(name) \
47static ssize_t zram_attr_##name##_show(struct device *d, \
48 struct device_attribute *attr, char *b) \
49{ \
50 struct zram *zram = dev_to_zram(d); \
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070051 return scnprintf(b, PAGE_SIZE, "%llu\n", \
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070052 (u64)atomic64_read(&zram->stats.name)); \
53} \
54static struct device_attribute dev_attr_##name = \
55 __ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL);
56
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -070057static inline int init_done(struct zram *zram)
58{
59 return zram->meta != NULL;
60}
61
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030062static inline struct zram *dev_to_zram(struct device *dev)
63{
64 return (struct zram *)dev_to_disk(dev)->private_data;
65}
66
67static ssize_t disksize_show(struct device *dev,
68 struct device_attribute *attr, char *buf)
69{
70 struct zram *zram = dev_to_zram(dev);
71
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070072 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030073}
74
75static ssize_t initstate_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
77{
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070078 u32 val;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030079 struct zram *zram = dev_to_zram(dev);
80
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070081 down_read(&zram->init_lock);
82 val = init_done(zram);
83 up_read(&zram->init_lock);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030084
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070085 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030086}
87
88static ssize_t orig_data_size_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
90{
91 struct zram *zram = dev_to_zram(dev);
92
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070093 return scnprintf(buf, PAGE_SIZE, "%llu\n",
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070094 (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030095}
96
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030097static ssize_t mem_used_total_show(struct device *dev,
98 struct device_attribute *attr, char *buf)
99{
100 u64 val = 0;
101 struct zram *zram = dev_to_zram(dev);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300102
103 down_read(&zram->init_lock);
Weijie Yang5a99e952014-10-29 14:50:57 -0700104 if (init_done(zram)) {
105 struct zram_meta *meta = zram->meta;
Minchan Kim722cdc12014-10-09 15:29:50 -0700106 val = zs_get_total_pages(meta->mem_pool);
Weijie Yang5a99e952014-10-29 14:50:57 -0700107 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300108 up_read(&zram->init_lock);
109
Minchan Kim722cdc12014-10-09 15:29:50 -0700110 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300111}
112
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700113static ssize_t max_comp_streams_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
115{
116 int val;
117 struct zram *zram = dev_to_zram(dev);
118
119 down_read(&zram->init_lock);
120 val = zram->max_comp_streams;
121 up_read(&zram->init_lock);
122
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -0700123 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700124}
125
Minchan Kim9ada9da2014-10-09 15:29:53 -0700126static ssize_t mem_limit_show(struct device *dev,
127 struct device_attribute *attr, char *buf)
128{
129 u64 val;
130 struct zram *zram = dev_to_zram(dev);
131
132 down_read(&zram->init_lock);
133 val = zram->limit_pages;
134 up_read(&zram->init_lock);
135
136 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
137}
138
139static ssize_t mem_limit_store(struct device *dev,
140 struct device_attribute *attr, const char *buf, size_t len)
141{
142 u64 limit;
143 char *tmp;
144 struct zram *zram = dev_to_zram(dev);
145
146 limit = memparse(buf, &tmp);
147 if (buf == tmp) /* no chars parsed, invalid input */
148 return -EINVAL;
149
150 down_write(&zram->init_lock);
151 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
152 up_write(&zram->init_lock);
153
154 return len;
155}
156
Minchan Kim461a8ee2014-10-09 15:29:55 -0700157static ssize_t mem_used_max_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
160 u64 val = 0;
161 struct zram *zram = dev_to_zram(dev);
162
163 down_read(&zram->init_lock);
164 if (init_done(zram))
165 val = atomic_long_read(&zram->stats.max_used_pages);
166 up_read(&zram->init_lock);
167
168 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
169}
170
171static ssize_t mem_used_max_store(struct device *dev,
172 struct device_attribute *attr, const char *buf, size_t len)
173{
174 int err;
175 unsigned long val;
176 struct zram *zram = dev_to_zram(dev);
Minchan Kim461a8ee2014-10-09 15:29:55 -0700177
178 err = kstrtoul(buf, 10, &val);
179 if (err || val != 0)
180 return -EINVAL;
181
182 down_read(&zram->init_lock);
Weijie Yang5a99e952014-10-29 14:50:57 -0700183 if (init_done(zram)) {
184 struct zram_meta *meta = zram->meta;
Minchan Kim461a8ee2014-10-09 15:29:55 -0700185 atomic_long_set(&zram->stats.max_used_pages,
186 zs_get_total_pages(meta->mem_pool));
Weijie Yang5a99e952014-10-29 14:50:57 -0700187 }
Minchan Kim461a8ee2014-10-09 15:29:55 -0700188 up_read(&zram->init_lock);
189
190 return len;
191}
192
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700193static ssize_t max_comp_streams_store(struct device *dev,
194 struct device_attribute *attr, const char *buf, size_t len)
195{
196 int num;
197 struct zram *zram = dev_to_zram(dev);
Minchan Kim60a726e2014-04-07 15:38:21 -0700198 int ret;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700199
Minchan Kim60a726e2014-04-07 15:38:21 -0700200 ret = kstrtoint(buf, 0, &num);
201 if (ret < 0)
202 return ret;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700203 if (num < 1)
204 return -EINVAL;
Minchan Kim60a726e2014-04-07 15:38:21 -0700205
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700206 down_write(&zram->init_lock);
207 if (init_done(zram)) {
Minchan Kim60a726e2014-04-07 15:38:21 -0700208 if (!zcomp_set_max_streams(zram->comp, num)) {
Sergey Senozhatskyfe8eb122014-04-07 15:38:15 -0700209 pr_info("Cannot change max compression streams\n");
Minchan Kim60a726e2014-04-07 15:38:21 -0700210 ret = -EINVAL;
211 goto out;
212 }
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700213 }
Minchan Kim60a726e2014-04-07 15:38:21 -0700214
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700215 zram->max_comp_streams = num;
Minchan Kim60a726e2014-04-07 15:38:21 -0700216 ret = len;
217out:
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700218 up_write(&zram->init_lock);
Minchan Kim60a726e2014-04-07 15:38:21 -0700219 return ret;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700220}
221
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700222static ssize_t comp_algorithm_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 size_t sz;
226 struct zram *zram = dev_to_zram(dev);
227
228 down_read(&zram->init_lock);
229 sz = zcomp_available_show(zram->compressor, buf);
230 up_read(&zram->init_lock);
231
232 return sz;
233}
234
235static ssize_t comp_algorithm_store(struct device *dev,
236 struct device_attribute *attr, const char *buf, size_t len)
237{
238 struct zram *zram = dev_to_zram(dev);
239 down_write(&zram->init_lock);
240 if (init_done(zram)) {
241 up_write(&zram->init_lock);
242 pr_info("Can't change algorithm for initialized device\n");
243 return -EBUSY;
244 }
245 strlcpy(zram->compressor, buf, sizeof(zram->compressor));
246 up_write(&zram->init_lock);
247 return len;
248}
249
Minchan Kim92967472014-01-30 15:46:03 -0800250/* flag operations needs meta->tb_lock */
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900251static int zram_test_flag(struct zram_meta *meta, u32 index,
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530252 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530253{
Weijie Yangd2d5e762014-08-06 16:08:31 -0700254 return meta->table[index].value & BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530255}
256
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900257static void zram_set_flag(struct zram_meta *meta, u32 index,
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530258 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530259{
Weijie Yangd2d5e762014-08-06 16:08:31 -0700260 meta->table[index].value |= BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530261}
262
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900263static void zram_clear_flag(struct zram_meta *meta, u32 index,
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530264 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530265{
Weijie Yangd2d5e762014-08-06 16:08:31 -0700266 meta->table[index].value &= ~BIT(flag);
267}
268
269static size_t zram_get_obj_size(struct zram_meta *meta, u32 index)
270{
271 return meta->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
272}
273
274static void zram_set_obj_size(struct zram_meta *meta,
275 u32 index, size_t size)
276{
277 unsigned long flags = meta->table[index].value >> ZRAM_FLAG_SHIFT;
278
279 meta->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530280}
281
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300282static inline int is_partial_io(struct bio_vec *bvec)
283{
284 return bvec->bv_len != PAGE_SIZE;
285}
286
287/*
288 * Check if request is within bounds and aligned on zram logical blocks.
289 */
290static inline int valid_io_request(struct zram *zram, struct bio *bio)
291{
292 u64 start, end, bound;
Kumar Gaurava539c722013-08-08 23:53:24 +0530293
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300294 /* unaligned request */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700295 if (unlikely(bio->bi_iter.bi_sector &
296 (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300297 return 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700298 if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300299 return 0;
300
Kent Overstreet4f024f32013-10-11 15:44:27 -0700301 start = bio->bi_iter.bi_sector;
302 end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300303 bound = zram->disksize >> SECTOR_SHIFT;
304 /* out of range range */
Sergey Senozhatsky75c7caf2013-06-22 17:21:00 +0300305 if (unlikely(start >= bound || end > bound || start > end))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300306 return 0;
307
308 /* I/O request is valid */
309 return 1;
310}
311
312static void zram_meta_free(struct zram_meta *meta)
313{
314 zs_destroy_pool(meta->mem_pool);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300315 vfree(meta->table);
316 kfree(meta);
317}
318
319static struct zram_meta *zram_meta_alloc(u64 disksize)
320{
321 size_t num_pages;
322 struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
323 if (!meta)
324 goto out;
325
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300326 num_pages = disksize >> PAGE_SHIFT;
327 meta->table = vzalloc(num_pages * sizeof(*meta->table));
328 if (!meta->table) {
329 pr_err("Error allocating zram address table\n");
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700330 goto free_meta;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300331 }
332
333 meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
334 if (!meta->mem_pool) {
335 pr_err("Error creating memory pool\n");
336 goto free_table;
337 }
338
339 return meta;
340
341free_table:
342 vfree(meta->table);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300343free_meta:
344 kfree(meta);
345 meta = NULL;
346out:
347 return meta;
348}
349
350static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
351{
352 if (*offset + bvec->bv_len >= PAGE_SIZE)
353 (*index)++;
354 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
355}
356
Nitin Gupta306b0c92009-09-22 10:26:53 +0530357static int page_zero_filled(void *ptr)
358{
359 unsigned int pos;
360 unsigned long *page;
361
362 page = (unsigned long *)ptr;
363
364 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
365 if (page[pos])
366 return 0;
367 }
368
369 return 1;
370}
371
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300372static void handle_zero_page(struct bio_vec *bvec)
373{
374 struct page *page = bvec->bv_page;
375 void *user_mem;
376
377 user_mem = kmap_atomic(page);
378 if (is_partial_io(bvec))
379 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
380 else
381 clear_page(user_mem);
382 kunmap_atomic(user_mem);
383
384 flush_dcache_page(page);
385}
386
Weijie Yangd2d5e762014-08-06 16:08:31 -0700387
388/*
389 * To protect concurrent access to the same index entry,
390 * caller should hold this table index entry's bit_spinlock to
391 * indicate this index entry is accessing.
392 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530393static void zram_free_page(struct zram *zram, size_t index)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530394{
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900395 struct zram_meta *meta = zram->meta;
396 unsigned long handle = meta->table[index].handle;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530397
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600398 if (unlikely(!handle)) {
Nitin Gupta2e882282010-01-28 21:13:41 +0530399 /*
400 * No memory is allocated for zero filled pages.
401 * Simply clear zero page flag.
402 */
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900403 if (zram_test_flag(meta, index, ZRAM_ZERO)) {
404 zram_clear_flag(meta, index, ZRAM_ZERO);
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700405 atomic64_dec(&zram->stats.zero_pages);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530406 }
407 return;
408 }
409
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900410 zs_free(meta->mem_pool, handle);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530411
Weijie Yangd2d5e762014-08-06 16:08:31 -0700412 atomic64_sub(zram_get_obj_size(meta, index),
413 &zram->stats.compr_data_size);
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700414 atomic64_dec(&zram->stats.pages_stored);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530415
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900416 meta->table[index].handle = 0;
Weijie Yangd2d5e762014-08-06 16:08:31 -0700417 zram_set_obj_size(meta, index, 0);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530418}
419
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300420static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530421{
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700422 int ret = 0;
Jerome Marchand924bd882011-06-10 15:28:48 +0200423 unsigned char *cmem;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900424 struct zram_meta *meta = zram->meta;
Minchan Kim92967472014-01-30 15:46:03 -0800425 unsigned long handle;
Minchan Kim023b4092014-08-06 16:08:29 -0700426 size_t size;
Minchan Kim92967472014-01-30 15:46:03 -0800427
Weijie Yangd2d5e762014-08-06 16:08:31 -0700428 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
Minchan Kim92967472014-01-30 15:46:03 -0800429 handle = meta->table[index].handle;
Weijie Yangd2d5e762014-08-06 16:08:31 -0700430 size = zram_get_obj_size(meta, index);
Jerome Marchand924bd882011-06-10 15:28:48 +0200431
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900432 if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
Weijie Yangd2d5e762014-08-06 16:08:31 -0700433 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Jiang Liu42e99bd2013-06-07 00:07:30 +0800434 clear_page(mem);
Jerome Marchand924bd882011-06-10 15:28:48 +0200435 return 0;
436 }
437
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900438 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
Minchan Kim92967472014-01-30 15:46:03 -0800439 if (size == PAGE_SIZE)
Jiang Liu42e99bd2013-06-07 00:07:30 +0800440 copy_page(mem, cmem);
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300441 else
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700442 ret = zcomp_decompress(zram->comp, cmem, size, mem);
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900443 zs_unmap_object(meta->mem_pool, handle);
Weijie Yangd2d5e762014-08-06 16:08:31 -0700444 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Jerome Marchand924bd882011-06-10 15:28:48 +0200445
446 /* Should NEVER happen. Return bio error if it does. */
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700447 if (unlikely(ret)) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200448 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
Jerome Marchand924bd882011-06-10 15:28:48 +0200449 return ret;
450 }
451
452 return 0;
453}
454
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300455static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
456 u32 index, int offset, struct bio *bio)
457{
458 int ret;
459 struct page *page;
460 unsigned char *user_mem, *uncmem = NULL;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900461 struct zram_meta *meta = zram->meta;
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300462 page = bvec->bv_page;
463
Weijie Yangd2d5e762014-08-06 16:08:31 -0700464 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900465 if (unlikely(!meta->table[index].handle) ||
466 zram_test_flag(meta, index, ZRAM_ZERO)) {
Weijie Yangd2d5e762014-08-06 16:08:31 -0700467 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300468 handle_zero_page(bvec);
469 return 0;
470 }
Weijie Yangd2d5e762014-08-06 16:08:31 -0700471 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300472
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300473 if (is_partial_io(bvec))
474 /* Use a temporary buffer to decompress the page */
Minchan Kim7e5a5102013-01-30 11:41:39 +0900475 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
476
477 user_mem = kmap_atomic(page);
478 if (!is_partial_io(bvec))
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300479 uncmem = user_mem;
480
481 if (!uncmem) {
482 pr_info("Unable to allocate temp memory\n");
483 ret = -ENOMEM;
484 goto out_cleanup;
485 }
486
487 ret = zram_decompress_page(zram, uncmem, index);
488 /* Should NEVER happen. Return bio error if it does. */
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700489 if (unlikely(ret))
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300490 goto out_cleanup;
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300491
492 if (is_partial_io(bvec))
493 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
494 bvec->bv_len);
495
496 flush_dcache_page(page);
497 ret = 0;
498out_cleanup:
499 kunmap_atomic(user_mem);
500 if (is_partial_io(bvec))
501 kfree(uncmem);
502 return ret;
503}
504
Minchan Kim461a8ee2014-10-09 15:29:55 -0700505static inline void update_used_max(struct zram *zram,
506 const unsigned long pages)
507{
508 int old_max, cur_max;
509
510 old_max = atomic_long_read(&zram->stats.max_used_pages);
511
512 do {
513 cur_max = old_max;
514 if (pages > cur_max)
515 old_max = atomic_long_cmpxchg(
516 &zram->stats.max_used_pages, cur_max, pages);
517 } while (old_max != cur_max);
518}
519
Jerome Marchand924bd882011-06-10 15:28:48 +0200520static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
521 int offset)
522{
Nitin Gupta397c6062013-01-02 08:53:41 -0800523 int ret = 0;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200524 size_t clen;
Minchan Kimc2344342012-06-08 15:39:25 +0900525 unsigned long handle;
Minchan Kim130f3152012-06-08 15:39:27 +0900526 struct page *page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200527 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900528 struct zram_meta *meta = zram->meta;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700529 struct zcomp_strm *zstrm;
Minchan Kime46e3312014-01-30 15:46:06 -0800530 bool locked = false;
Minchan Kim461a8ee2014-10-09 15:29:55 -0700531 unsigned long alloced_pages;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200532
533 page = bvec->bv_page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200534 if (is_partial_io(bvec)) {
535 /*
536 * This is a partial IO. We need to read the full page
537 * before to write the changes.
538 */
Minchan Kim7e5a5102013-01-30 11:41:39 +0900539 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
Jerome Marchand924bd882011-06-10 15:28:48 +0200540 if (!uncmem) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200541 ret = -ENOMEM;
542 goto out;
543 }
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300544 ret = zram_decompress_page(zram, uncmem, index);
Nitin Gupta397c6062013-01-02 08:53:41 -0800545 if (ret)
Jerome Marchand924bd882011-06-10 15:28:48 +0200546 goto out;
Jerome Marchand924bd882011-06-10 15:28:48 +0200547 }
548
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700549 zstrm = zcomp_strm_find(zram->comp);
Minchan Kime46e3312014-01-30 15:46:06 -0800550 locked = true;
Cong Wangba82fe22011-11-25 23:14:25 +0800551 user_mem = kmap_atomic(page);
Jerome Marchand924bd882011-06-10 15:28:48 +0200552
Nitin Gupta397c6062013-01-02 08:53:41 -0800553 if (is_partial_io(bvec)) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200554 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
555 bvec->bv_len);
Nitin Gupta397c6062013-01-02 08:53:41 -0800556 kunmap_atomic(user_mem);
557 user_mem = NULL;
558 } else {
Jerome Marchand924bd882011-06-10 15:28:48 +0200559 uncmem = user_mem;
Nitin Gupta397c6062013-01-02 08:53:41 -0800560 }
Jerome Marchand924bd882011-06-10 15:28:48 +0200561
562 if (page_zero_filled(uncmem)) {
Cong Wangba82fe22011-11-25 23:14:25 +0800563 kunmap_atomic(user_mem);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900564 /* Free memory associated with this sector now. */
Weijie Yangd2d5e762014-08-06 16:08:31 -0700565 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900566 zram_free_page(zram, index);
Minchan Kim92967472014-01-30 15:46:03 -0800567 zram_set_flag(meta, index, ZRAM_ZERO);
Weijie Yangd2d5e762014-08-06 16:08:31 -0700568 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900569
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700570 atomic64_inc(&zram->stats.zero_pages);
Jerome Marchand924bd882011-06-10 15:28:48 +0200571 ret = 0;
572 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200573 }
574
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700575 ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
Nitin Gupta397c6062013-01-02 08:53:41 -0800576 if (!is_partial_io(bvec)) {
577 kunmap_atomic(user_mem);
578 user_mem = NULL;
579 uncmem = NULL;
580 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200581
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700582 if (unlikely(ret)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200583 pr_err("Compression failed! err=%d\n", ret);
Jerome Marchand924bd882011-06-10 15:28:48 +0200584 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200585 }
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700586 src = zstrm->buffer;
Nitin Guptac8f2f0d2012-10-10 17:42:18 -0700587 if (unlikely(clen > max_zpage_size)) {
Nitin Guptac8f2f0d2012-10-10 17:42:18 -0700588 clen = PAGE_SIZE;
Nitin Gupta397c6062013-01-02 08:53:41 -0800589 if (is_partial_io(bvec))
590 src = uncmem;
Nitin Guptac8f2f0d2012-10-10 17:42:18 -0700591 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200592
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900593 handle = zs_malloc(meta->mem_pool, clen);
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600594 if (!handle) {
Marlies Ruck596b3dd2013-05-16 14:30:39 -0400595 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
596 index, clen);
Jerome Marchand924bd882011-06-10 15:28:48 +0200597 ret = -ENOMEM;
598 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200599 }
Minchan Kim9ada9da2014-10-09 15:29:53 -0700600
Minchan Kim461a8ee2014-10-09 15:29:55 -0700601 alloced_pages = zs_get_total_pages(meta->mem_pool);
602 if (zram->limit_pages && alloced_pages > zram->limit_pages) {
Minchan Kim9ada9da2014-10-09 15:29:53 -0700603 zs_free(meta->mem_pool, handle);
604 ret = -ENOMEM;
605 goto out;
606 }
607
Minchan Kim461a8ee2014-10-09 15:29:55 -0700608 update_used_max(zram, alloced_pages);
609
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900610 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200611
Jiang Liu42e99bd2013-06-07 00:07:30 +0800612 if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
Nitin Gupta397c6062013-01-02 08:53:41 -0800613 src = kmap_atomic(page);
Jiang Liu42e99bd2013-06-07 00:07:30 +0800614 copy_page(cmem, src);
Nitin Gupta397c6062013-01-02 08:53:41 -0800615 kunmap_atomic(src);
Jiang Liu42e99bd2013-06-07 00:07:30 +0800616 } else {
617 memcpy(cmem, src, clen);
618 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200619
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700620 zcomp_strm_release(zram->comp, zstrm);
621 locked = false;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900622 zs_unmap_object(meta->mem_pool, handle);
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600623
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900624 /*
625 * Free memory associated with this sector
626 * before overwriting unused sectors.
627 */
Weijie Yangd2d5e762014-08-06 16:08:31 -0700628 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900629 zram_free_page(zram, index);
630
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900631 meta->table[index].handle = handle;
Weijie Yangd2d5e762014-08-06 16:08:31 -0700632 zram_set_obj_size(meta, index, clen);
633 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200634
635 /* Update stats */
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700636 atomic64_add(clen, &zram->stats.compr_data_size);
637 atomic64_inc(&zram->stats.pages_stored);
Jerome Marchand924bd882011-06-10 15:28:48 +0200638out:
Minchan Kime46e3312014-01-30 15:46:06 -0800639 if (locked)
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700640 zcomp_strm_release(zram->comp, zstrm);
Nitin Gupta397c6062013-01-02 08:53:41 -0800641 if (is_partial_io(bvec))
642 kfree(uncmem);
Jerome Marchand924bd882011-06-10 15:28:48 +0200643 return ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200644}
645
646static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700647 int offset, struct bio *bio)
Jerome Marchand8c921b22011-06-10 15:28:47 +0200648{
Jerome Marchandc5bde232011-06-10 15:28:49 +0200649 int ret;
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700650 int rw = bio_data_dir(bio);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200651
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700652 if (rw == READ) {
653 atomic64_inc(&zram->stats.num_reads);
Jerome Marchandc5bde232011-06-10 15:28:49 +0200654 ret = zram_bvec_read(zram, bvec, index, offset, bio);
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700655 } else {
656 atomic64_inc(&zram->stats.num_writes);
Jerome Marchandc5bde232011-06-10 15:28:49 +0200657 ret = zram_bvec_write(zram, bvec, index, offset);
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700658 }
Jerome Marchandc5bde232011-06-10 15:28:49 +0200659
Chao Yu0cf1e9d2014-08-29 15:18:37 -0700660 if (unlikely(ret)) {
661 if (rw == READ)
662 atomic64_inc(&zram->stats.failed_reads);
663 else
664 atomic64_inc(&zram->stats.failed_writes);
665 }
666
Jerome Marchandc5bde232011-06-10 15:28:49 +0200667 return ret;
Jerome Marchand924bd882011-06-10 15:28:48 +0200668}
669
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700670/*
671 * zram_bio_discard - handler on discard request
672 * @index: physical block index in PAGE_SIZE units
673 * @offset: byte offset within physical block
674 */
675static void zram_bio_discard(struct zram *zram, u32 index,
676 int offset, struct bio *bio)
677{
678 size_t n = bio->bi_iter.bi_size;
Weijie Yangd2d5e762014-08-06 16:08:31 -0700679 struct zram_meta *meta = zram->meta;
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700680
681 /*
682 * zram manages data in physical block size units. Because logical block
683 * size isn't identical with physical block size on some arch, we
684 * could get a discard request pointing to a specific offset within a
685 * certain physical block. Although we can handle this request by
686 * reading that physiclal block and decompressing and partially zeroing
687 * and re-compressing and then re-storing it, this isn't reasonable
688 * because our intent with a discard request is to save memory. So
689 * skipping this logical block is appropriate here.
690 */
691 if (offset) {
Weijie Yang38515c72014-06-04 16:11:06 -0700692 if (n <= (PAGE_SIZE - offset))
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700693 return;
694
Weijie Yang38515c72014-06-04 16:11:06 -0700695 n -= (PAGE_SIZE - offset);
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700696 index++;
697 }
698
699 while (n >= PAGE_SIZE) {
Weijie Yangd2d5e762014-08-06 16:08:31 -0700700 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700701 zram_free_page(zram, index);
Weijie Yangd2d5e762014-08-06 16:08:31 -0700702 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Sergey Senozhatsky015254d2014-10-09 15:29:57 -0700703 atomic64_inc(&zram->stats.notify_free);
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700704 index++;
705 n -= PAGE_SIZE;
706 }
707}
708
Minchan Kim2b86ab92013-08-12 15:13:55 +0900709static void zram_reset_device(struct zram *zram, bool reset_capacity)
Jerome Marchand924bd882011-06-10 15:28:48 +0200710{
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300711 size_t index;
712 struct zram_meta *meta;
713
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300714 down_write(&zram->init_lock);
Minchan Kim9ada9da2014-10-09 15:29:53 -0700715
716 zram->limit_pages = 0;
717
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -0700718 if (!init_done(zram)) {
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300719 up_write(&zram->init_lock);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300720 return;
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300721 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300722
723 meta = zram->meta;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300724 /* Free all pages that are still in this zram device */
725 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
726 unsigned long handle = meta->table[index].handle;
727 if (!handle)
728 continue;
729
730 zs_free(meta->mem_pool, handle);
731 }
732
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700733 zcomp_destroy(zram->comp);
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700734 zram->max_comp_streams = 1;
735
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300736 zram_meta_free(zram->meta);
737 zram->meta = NULL;
738 /* Reset stats */
739 memset(&zram->stats, 0, sizeof(zram->stats));
740
741 zram->disksize = 0;
Minchan Kimb4c5c602014-07-23 14:00:04 -0700742 if (reset_capacity)
Minchan Kim2b86ab92013-08-12 15:13:55 +0900743 set_capacity(zram->disk, 0);
Minchan Kimb4c5c602014-07-23 14:00:04 -0700744
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300745 up_write(&zram->init_lock);
Minchan Kimb4c5c602014-07-23 14:00:04 -0700746
747 /*
748 * Revalidate disk out of the init_lock to avoid lockdep splat.
749 * It's okay because disk's capacity is protected by init_lock
750 * so that revalidate_disk always sees up-to-date capacity.
751 */
752 if (reset_capacity)
753 revalidate_disk(zram->disk);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300754}
755
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300756static ssize_t disksize_store(struct device *dev,
757 struct device_attribute *attr, const char *buf, size_t len)
758{
759 u64 disksize;
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700760 struct zcomp *comp;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300761 struct zram_meta *meta;
762 struct zram *zram = dev_to_zram(dev);
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700763 int err;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300764
765 disksize = memparse(buf, NULL);
766 if (!disksize)
767 return -EINVAL;
768
769 disksize = PAGE_ALIGN(disksize);
770 meta = zram_meta_alloc(disksize);
Minchan Kimdb5d7112014-03-03 15:38:34 -0800771 if (!meta)
772 return -ENOMEM;
Sergey Senozhatskyb67d1ec2014-04-07 15:38:09 -0700773
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700774 comp = zcomp_create(zram->compressor, zram->max_comp_streams);
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700775 if (IS_ERR(comp)) {
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700776 pr_info("Cannot initialise %s compressing backend\n",
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700777 zram->compressor);
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700778 err = PTR_ERR(comp);
779 goto out_free_meta;
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700780 }
781
782 down_write(&zram->init_lock);
783 if (init_done(zram)) {
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700784 pr_info("Cannot change disksize for initialized device\n");
785 err = -EBUSY;
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700786 goto out_destroy_comp;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300787 }
788
Sergey Senozhatskyb67d1ec2014-04-07 15:38:09 -0700789 zram->meta = meta;
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700790 zram->comp = comp;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300791 zram->disksize = disksize;
792 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300793 up_write(&zram->init_lock);
Minchan Kimb4c5c602014-07-23 14:00:04 -0700794
795 /*
796 * Revalidate disk out of the init_lock to avoid lockdep splat.
797 * It's okay because disk's capacity is protected by init_lock
798 * so that revalidate_disk always sees up-to-date capacity.
799 */
800 revalidate_disk(zram->disk);
801
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300802 return len;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700803
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700804out_destroy_comp:
805 up_write(&zram->init_lock);
806 zcomp_destroy(comp);
807out_free_meta:
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700808 zram_meta_free(meta);
809 return err;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300810}
811
812static ssize_t reset_store(struct device *dev,
813 struct device_attribute *attr, const char *buf, size_t len)
814{
815 int ret;
816 unsigned short do_reset;
817 struct zram *zram;
818 struct block_device *bdev;
819
820 zram = dev_to_zram(dev);
821 bdev = bdget_disk(zram->disk, 0);
822
Rashika Kheria46a51c82013-10-30 18:36:32 +0530823 if (!bdev)
824 return -ENOMEM;
825
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300826 /* Do not reset an active device! */
Rashika Kheria1b672222013-11-10 22:13:53 +0530827 if (bdev->bd_holders) {
828 ret = -EBUSY;
829 goto out;
830 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300831
832 ret = kstrtou16(buf, 10, &do_reset);
833 if (ret)
Rashika Kheria1b672222013-11-10 22:13:53 +0530834 goto out;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300835
Rashika Kheria1b672222013-11-10 22:13:53 +0530836 if (!do_reset) {
837 ret = -EINVAL;
838 goto out;
839 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300840
841 /* Make sure all pending I/O is finished */
Rashika Kheria46a51c82013-10-30 18:36:32 +0530842 fsync_bdev(bdev);
Rashika Kheria1b672222013-11-10 22:13:53 +0530843 bdput(bdev);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300844
Minchan Kim2b86ab92013-08-12 15:13:55 +0900845 zram_reset_device(zram, true);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300846 return len;
Rashika Kheria1b672222013-11-10 22:13:53 +0530847
848out:
849 bdput(bdev);
850 return ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200851}
852
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700853static void __zram_make_request(struct zram *zram, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530854{
Kent Overstreet79886132013-11-23 17:19:00 -0800855 int offset;
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530856 u32 index;
Kent Overstreet79886132013-11-23 17:19:00 -0800857 struct bio_vec bvec;
858 struct bvec_iter iter;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530859
Kent Overstreet4f024f32013-10-11 15:44:27 -0700860 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
861 offset = (bio->bi_iter.bi_sector &
862 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530863
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700864 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
865 zram_bio_discard(zram, index, offset, bio);
866 bio_endio(bio, 0);
867 return;
868 }
869
Kent Overstreet79886132013-11-23 17:19:00 -0800870 bio_for_each_segment(bvec, bio, iter) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200871 int max_transfer_size = PAGE_SIZE - offset;
872
Kent Overstreet79886132013-11-23 17:19:00 -0800873 if (bvec.bv_len > max_transfer_size) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200874 /*
875 * zram_bvec_rw() can only make operation on a single
876 * zram page. Split the bio vector.
877 */
878 struct bio_vec bv;
879
Kent Overstreet79886132013-11-23 17:19:00 -0800880 bv.bv_page = bvec.bv_page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200881 bv.bv_len = max_transfer_size;
Kent Overstreet79886132013-11-23 17:19:00 -0800882 bv.bv_offset = bvec.bv_offset;
Jerome Marchand924bd882011-06-10 15:28:48 +0200883
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700884 if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0)
Jerome Marchand924bd882011-06-10 15:28:48 +0200885 goto out;
886
Kent Overstreet79886132013-11-23 17:19:00 -0800887 bv.bv_len = bvec.bv_len - max_transfer_size;
Jerome Marchand924bd882011-06-10 15:28:48 +0200888 bv.bv_offset += max_transfer_size;
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700889 if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0)
Jerome Marchand924bd882011-06-10 15:28:48 +0200890 goto out;
891 } else
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700892 if (zram_bvec_rw(zram, &bvec, index, offset, bio) < 0)
Jerome Marchand924bd882011-06-10 15:28:48 +0200893 goto out;
894
Kent Overstreet79886132013-11-23 17:19:00 -0800895 update_position(&index, &offset, &bvec);
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530896 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530897
898 set_bit(BIO_UPTODATE, &bio->bi_flags);
899 bio_endio(bio, 0);
Nitin Gupta7d7854b2011-01-22 07:36:15 -0500900 return;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530901
902out:
Nitin Gupta306b0c92009-09-22 10:26:53 +0530903 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530904}
905
Nitin Gupta306b0c92009-09-22 10:26:53 +0530906/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530907 * Handler function for all zram I/O requests.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530908 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200909static void zram_make_request(struct request_queue *queue, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530910{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530911 struct zram *zram = queue->queuedata;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530912
Jerome Marchand0900bea2011-09-06 15:02:11 +0200913 down_read(&zram->init_lock);
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -0700914 if (unlikely(!init_done(zram)))
Minchan Kim3de738c2013-01-30 11:41:41 +0900915 goto error;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200916
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530917 if (!valid_io_request(zram, bio)) {
Jiang Liuda5cc7d2013-06-07 00:07:31 +0800918 atomic64_inc(&zram->stats.invalid_io);
Minchan Kim3de738c2013-01-30 11:41:41 +0900919 goto error;
Jerome Marchand6642a672011-02-17 17:11:49 +0100920 }
921
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700922 __zram_make_request(zram, bio);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200923 up_read(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530924
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700925 return;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200926
Jerome Marchand0900bea2011-09-06 15:02:11 +0200927error:
Minchan Kim3de738c2013-01-30 11:41:41 +0900928 up_read(&zram->init_lock);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200929 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530930}
931
Nitin Gupta2ccbec02011-09-09 19:01:00 -0400932static void zram_slot_free_notify(struct block_device *bdev,
933 unsigned long index)
Nitin Gupta107c1612010-05-17 11:02:44 +0530934{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530935 struct zram *zram;
Minchan Kimf614a9f2014-01-30 15:46:04 -0800936 struct zram_meta *meta;
Nitin Gupta107c1612010-05-17 11:02:44 +0530937
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530938 zram = bdev->bd_disk->private_data;
Minchan Kimf614a9f2014-01-30 15:46:04 -0800939 meta = zram->meta;
940
Weijie Yangd2d5e762014-08-06 16:08:31 -0700941 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
Minchan Kimf614a9f2014-01-30 15:46:04 -0800942 zram_free_page(zram, index);
Weijie Yangd2d5e762014-08-06 16:08:31 -0700943 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
Jiang Liuda5cc7d2013-06-07 00:07:31 +0800944 atomic64_inc(&zram->stats.notify_free);
Nitin Gupta107c1612010-05-17 11:02:44 +0530945}
946
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530947static const struct block_device_operations zram_devops = {
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530948 .swap_slot_free_notify = zram_slot_free_notify,
Nitin Gupta107c1612010-05-17 11:02:44 +0530949 .owner = THIS_MODULE
Nitin Gupta306b0c92009-09-22 10:26:53 +0530950};
951
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300952static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
953 disksize_show, disksize_store);
954static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
955static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300956static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300957static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
Minchan Kim9ada9da2014-10-09 15:29:53 -0700958static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show,
959 mem_limit_store);
Minchan Kim461a8ee2014-10-09 15:29:55 -0700960static DEVICE_ATTR(mem_used_max, S_IRUGO | S_IWUSR, mem_used_max_show,
961 mem_used_max_store);
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700962static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
963 max_comp_streams_show, max_comp_streams_store);
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700964static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
965 comp_algorithm_show, comp_algorithm_store);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300966
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -0700967ZRAM_ATTR_RO(num_reads);
968ZRAM_ATTR_RO(num_writes);
Sergey Senozhatsky64447242014-04-07 15:38:05 -0700969ZRAM_ATTR_RO(failed_reads);
970ZRAM_ATTR_RO(failed_writes);
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -0700971ZRAM_ATTR_RO(invalid_io);
972ZRAM_ATTR_RO(notify_free);
973ZRAM_ATTR_RO(zero_pages);
974ZRAM_ATTR_RO(compr_data_size);
975
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300976static struct attribute *zram_disk_attrs[] = {
977 &dev_attr_disksize.attr,
978 &dev_attr_initstate.attr,
979 &dev_attr_reset.attr,
980 &dev_attr_num_reads.attr,
981 &dev_attr_num_writes.attr,
Sergey Senozhatsky64447242014-04-07 15:38:05 -0700982 &dev_attr_failed_reads.attr,
983 &dev_attr_failed_writes.attr,
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300984 &dev_attr_invalid_io.attr,
985 &dev_attr_notify_free.attr,
986 &dev_attr_zero_pages.attr,
987 &dev_attr_orig_data_size.attr,
988 &dev_attr_compr_data_size.attr,
989 &dev_attr_mem_used_total.attr,
Minchan Kim9ada9da2014-10-09 15:29:53 -0700990 &dev_attr_mem_limit.attr,
Minchan Kim461a8ee2014-10-09 15:29:55 -0700991 &dev_attr_mem_used_max.attr,
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700992 &dev_attr_max_comp_streams.attr,
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700993 &dev_attr_comp_algorithm.attr,
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300994 NULL,
995};
996
997static struct attribute_group zram_disk_attr_group = {
998 .attrs = zram_disk_attrs,
999};
1000
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301001static int create_device(struct zram *zram, int device_id)
Nitin Gupta306b0c92009-09-22 10:26:53 +05301002{
Jiang Liu39a9b8a2013-06-07 00:07:24 +08001003 int ret = -ENOMEM;
Nitin Guptade1a21a2010-01-28 21:13:40 +05301004
Jerome Marchand0900bea2011-09-06 15:02:11 +02001005 init_rwsem(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301006
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301007 zram->queue = blk_alloc_queue(GFP_KERNEL);
1008 if (!zram->queue) {
Nitin Gupta306b0c92009-09-22 10:26:53 +05301009 pr_err("Error allocating disk queue for device %d\n",
1010 device_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +05301011 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301012 }
1013
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301014 blk_queue_make_request(zram->queue, zram_make_request);
1015 zram->queue->queuedata = zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301016
1017 /* gendisk structure */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301018 zram->disk = alloc_disk(1);
1019 if (!zram->disk) {
Sam Hansen94b84352012-06-07 16:03:47 -07001020 pr_warn("Error allocating disk structure for device %d\n",
Nitin Gupta306b0c92009-09-22 10:26:53 +05301021 device_id);
Jiang Liu39a9b8a2013-06-07 00:07:24 +08001022 goto out_free_queue;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301023 }
1024
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301025 zram->disk->major = zram_major;
1026 zram->disk->first_minor = device_id;
1027 zram->disk->fops = &zram_devops;
1028 zram->disk->queue = zram->queue;
1029 zram->disk->private_data = zram;
1030 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301031
Nitin Gupta33863c22010-08-09 22:56:47 +05301032 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301033 set_capacity(zram->disk, 0);
Sergey Senozhatskyb67d1ec2014-04-07 15:38:09 -07001034 /* zram devices sort of resembles non-rotational disks */
1035 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06001036 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
Nitin Guptaa1dd52a2010-06-01 13:31:23 +05301037 /*
1038 * To ensure that we always get PAGE_SIZE aligned
1039 * and n*PAGE_SIZED sized I/O requests.
1040 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301041 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
Robert Jennings7b19b8d2011-01-28 08:58:17 -06001042 blk_queue_logical_block_size(zram->disk->queue,
1043 ZRAM_LOGICAL_BLOCK_SIZE);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301044 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1045 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
Joonsoo Kimf4659d82014-04-07 15:38:24 -07001046 zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1047 zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
1048 /*
1049 * zram_bio_discard() will clear all logical blocks if logical block
1050 * size is identical with physical block size(PAGE_SIZE). But if it is
1051 * different, we will skip discarding some parts of logical blocks in
1052 * the part of the request range which isn't aligned to physical block
1053 * size. So we can't ensure that all discarded logical blocks are
1054 * zeroed.
1055 */
1056 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1057 zram->disk->queue->limits.discard_zeroes_data = 1;
1058 else
1059 zram->disk->queue->limits.discard_zeroes_data = 0;
1060 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
Nitin Gupta5d83d5a2010-01-28 21:13:39 +05301061
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301062 add_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301063
Nitin Gupta33863c22010-08-09 22:56:47 +05301064 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
1065 &zram_disk_attr_group);
1066 if (ret < 0) {
Sam Hansen94b84352012-06-07 16:03:47 -07001067 pr_warn("Error creating sysfs group");
Jiang Liu39a9b8a2013-06-07 00:07:24 +08001068 goto out_free_disk;
Nitin Gupta33863c22010-08-09 22:56:47 +05301069 }
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -07001070 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -07001071 zram->meta = NULL;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -07001072 zram->max_comp_streams = 1;
Jiang Liu39a9b8a2013-06-07 00:07:24 +08001073 return 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +05301074
Jiang Liu39a9b8a2013-06-07 00:07:24 +08001075out_free_disk:
1076 del_gendisk(zram->disk);
1077 put_disk(zram->disk);
1078out_free_queue:
1079 blk_cleanup_queue(zram->queue);
Nitin Guptade1a21a2010-01-28 21:13:40 +05301080out:
1081 return ret;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301082}
1083
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301084static void destroy_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +05301085{
Nitin Gupta33863c22010-08-09 22:56:47 +05301086 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
1087 &zram_disk_attr_group);
Nitin Gupta33863c22010-08-09 22:56:47 +05301088
Rashika Kheria59d3fe52013-10-30 18:43:32 +05301089 del_gendisk(zram->disk);
1090 put_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301091
Rashika Kheria59d3fe52013-10-30 18:43:32 +05301092 blk_cleanup_queue(zram->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301093}
1094
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301095static int __init zram_init(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +05301096{
Nitin Guptade1a21a2010-01-28 21:13:40 +05301097 int ret, dev_id;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301098
Nitin Gupta5fa5a902012-02-12 23:04:45 -05001099 if (num_devices > max_num_devices) {
Sam Hansen94b84352012-06-07 16:03:47 -07001100 pr_warn("Invalid value for num_devices: %u\n",
Nitin Gupta5fa5a902012-02-12 23:04:45 -05001101 num_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +05301102 ret = -EINVAL;
1103 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301104 }
1105
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301106 zram_major = register_blkdev(0, "zram");
1107 if (zram_major <= 0) {
Sam Hansen94b84352012-06-07 16:03:47 -07001108 pr_warn("Unable to get major number\n");
Nitin Guptade1a21a2010-01-28 21:13:40 +05301109 ret = -EBUSY;
1110 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301111 }
1112
Nitin Gupta306b0c92009-09-22 10:26:53 +05301113 /* Allocate the device array and initialize each one */
Nitin Gupta5fa5a902012-02-12 23:04:45 -05001114 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
Noah Watkins43801f62011-07-20 17:05:57 -06001115 if (!zram_devices) {
Nitin Guptade1a21a2010-01-28 21:13:40 +05301116 ret = -ENOMEM;
1117 goto unregister;
1118 }
Nitin Gupta306b0c92009-09-22 10:26:53 +05301119
Nitin Gupta5fa5a902012-02-12 23:04:45 -05001120 for (dev_id = 0; dev_id < num_devices; dev_id++) {
Noah Watkins43801f62011-07-20 17:05:57 -06001121 ret = create_device(&zram_devices[dev_id], dev_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +05301122 if (ret)
Minchan Kim3bf040c2010-01-11 16:15:53 +09001123 goto free_devices;
Nitin Guptade1a21a2010-01-28 21:13:40 +05301124 }
1125
Davidlohr Buesoca3d70b2013-01-01 21:24:13 -08001126 pr_info("Created %u device(s) ...\n", num_devices);
1127
Nitin Gupta306b0c92009-09-22 10:26:53 +05301128 return 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +05301129
Minchan Kim3bf040c2010-01-11 16:15:53 +09001130free_devices:
Nitin Guptade1a21a2010-01-28 21:13:40 +05301131 while (dev_id)
Noah Watkins43801f62011-07-20 17:05:57 -06001132 destroy_device(&zram_devices[--dev_id]);
1133 kfree(zram_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +05301134unregister:
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301135 unregister_blkdev(zram_major, "zram");
Nitin Guptade1a21a2010-01-28 21:13:40 +05301136out:
Nitin Gupta306b0c92009-09-22 10:26:53 +05301137 return ret;
1138}
1139
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301140static void __exit zram_exit(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +05301141{
1142 int i;
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301143 struct zram *zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301144
Nitin Gupta5fa5a902012-02-12 23:04:45 -05001145 for (i = 0; i < num_devices; i++) {
Noah Watkins43801f62011-07-20 17:05:57 -06001146 zram = &zram_devices[i];
Nitin Gupta306b0c92009-09-22 10:26:53 +05301147
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301148 destroy_device(zram);
Minchan Kim2b86ab92013-08-12 15:13:55 +09001149 /*
1150 * Shouldn't access zram->disk after destroy_device
1151 * because destroy_device already released zram->disk.
1152 */
1153 zram_reset_device(zram, false);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301154 }
1155
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301156 unregister_blkdev(zram_major, "zram");
Nitin Gupta306b0c92009-09-22 10:26:53 +05301157
Noah Watkins43801f62011-07-20 17:05:57 -06001158 kfree(zram_devices);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301159 pr_debug("Cleanup done!\n");
1160}
1161
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301162module_init(zram_init);
1163module_exit(zram_exit);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301164
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +03001165module_param(num_devices, uint, 0);
1166MODULE_PARM_DESC(num_devices, "Number of zram devices");
1167
Nitin Gupta306b0c92009-09-22 10:26:53 +05301168MODULE_LICENSE("Dual BSD/GPL");
1169MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301170MODULE_DESCRIPTION("Compressed RAM Block Device");