blob: 706cb625aae90f6652a3ef744843e3500fc07e49 [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
Nitin Gupta306b0c92009-09-22 10:26:53 +05305 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 *
12 * Project home: http://compcache.googlecode.com
13 */
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/lzo.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053033#include <linux/string.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053034#include <linux/vmalloc.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;
Noah Watkins43801f62011-07-20 17:05:57 -060040struct zram *zram_devices;
Nitin Gupta306b0c92009-09-22 10:26:53 +053041
Nitin Gupta306b0c92009-09-22 10:26:53 +053042/* Module params (documentation at end) */
Nitin Gupta5fa5a902012-02-12 23:04:45 -050043static unsigned int num_devices;
Nitin Gupta33863c22010-08-09 22:56:47 +053044
45static void zram_stat_inc(u32 *v)
46{
47 *v = *v + 1;
48}
49
50static void zram_stat_dec(u32 *v)
51{
52 *v = *v - 1;
53}
54
55static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc)
56{
57 spin_lock(&zram->stat64_lock);
58 *v = *v + inc;
59 spin_unlock(&zram->stat64_lock);
60}
61
62static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec)
63{
64 spin_lock(&zram->stat64_lock);
65 *v = *v - dec;
66 spin_unlock(&zram->stat64_lock);
67}
68
69static void zram_stat64_inc(struct zram *zram, u64 *v)
70{
71 zram_stat64_add(zram, v, 1);
72}
Nitin Gupta306b0c92009-09-22 10:26:53 +053073
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053074static int zram_test_flag(struct zram *zram, u32 index,
75 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +053076{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053077 return zram->table[index].flags & BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +053078}
79
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053080static void zram_set_flag(struct zram *zram, u32 index,
81 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +053082{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053083 zram->table[index].flags |= BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +053084}
85
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053086static void zram_clear_flag(struct zram *zram, u32 index,
87 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +053088{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053089 zram->table[index].flags &= ~BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +053090}
91
92static int page_zero_filled(void *ptr)
93{
94 unsigned int pos;
95 unsigned long *page;
96
97 page = (unsigned long *)ptr;
98
99 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
100 if (page[pos])
101 return 0;
102 }
103
104 return 1;
105}
106
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530107static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530108{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530109 if (!zram->disksize) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530110 pr_info(
111 "disk size not provided. You can use disksize_kb module "
112 "param to specify size.\nUsing default: (%u%% of RAM).\n",
113 default_disksize_perc_ram
114 );
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530115 zram->disksize = default_disksize_perc_ram *
Nitin Gupta306b0c92009-09-22 10:26:53 +0530116 (totalram_bytes / 100);
117 }
118
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530119 if (zram->disksize > 2 * (totalram_bytes)) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530120 pr_info(
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530121 "There is little point creating a zram of greater than "
Nitin Gupta306b0c92009-09-22 10:26:53 +0530122 "twice the size of memory since we expect a 2:1 compression "
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530123 "ratio. Note that zram uses about 0.1%% of the size of "
124 "the disk when not in use so a huge zram is "
Nitin Gupta306b0c92009-09-22 10:26:53 +0530125 "wasteful.\n"
126 "\tMemory Size: %zu kB\n"
Nitin Gupta33863c22010-08-09 22:56:47 +0530127 "\tSize you selected: %llu kB\n"
Nitin Gupta306b0c92009-09-22 10:26:53 +0530128 "Continuing anyway ...\n",
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530129 totalram_bytes >> 10, zram->disksize
Nitin Gupta306b0c92009-09-22 10:26:53 +0530130 );
131 }
132
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530133 zram->disksize &= PAGE_MASK;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530134}
135
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530136static void zram_free_page(struct zram *zram, size_t index)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530137{
Minchan Kimc2344342012-06-08 15:39:25 +0900138 unsigned long handle = zram->table[index].handle;
Minchan Kim130f3152012-06-08 15:39:27 +0900139 u16 size = zram->table[index].size;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530140
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600141 if (unlikely(!handle)) {
Nitin Gupta2e882282010-01-28 21:13:41 +0530142 /*
143 * No memory is allocated for zero filled pages.
144 * Simply clear zero page flag.
145 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530146 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
147 zram_clear_flag(zram, index, ZRAM_ZERO);
148 zram_stat_dec(&zram->stats.pages_zero);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530149 }
150 return;
151 }
152
Minchan Kim130f3152012-06-08 15:39:27 +0900153 if (unlikely(size > max_zpage_size))
154 zram_stat_dec(&zram->stats.bad_compress);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530155
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600156 zs_free(zram->mem_pool, handle);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530157
Minchan Kim130f3152012-06-08 15:39:27 +0900158 if (size <= PAGE_SIZE / 2)
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530159 zram_stat_dec(&zram->stats.good_compress);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530160
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600161 zram_stat64_sub(zram, &zram->stats.compr_size,
162 zram->table[index].size);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530163 zram_stat_dec(&zram->stats.pages_stored);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530164
Minchan Kimc2344342012-06-08 15:39:25 +0900165 zram->table[index].handle = 0;
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600166 zram->table[index].size = 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530167}
168
Jerome Marchand924bd882011-06-10 15:28:48 +0200169static void handle_zero_page(struct bio_vec *bvec)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530170{
Jerome Marchand924bd882011-06-10 15:28:48 +0200171 struct page *page = bvec->bv_page;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530172 void *user_mem;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530173
Cong Wangba82fe22011-11-25 23:14:25 +0800174 user_mem = kmap_atomic(page);
Jerome Marchand924bd882011-06-10 15:28:48 +0200175 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
Cong Wangba82fe22011-11-25 23:14:25 +0800176 kunmap_atomic(user_mem);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530177
Nitin Gupta30fb8a72009-12-12 11:44:46 +0530178 flush_dcache_page(page);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530179}
180
Jerome Marchand924bd882011-06-10 15:28:48 +0200181static inline int is_partial_io(struct bio_vec *bvec)
182{
183 return bvec->bv_len != PAGE_SIZE;
184}
185
Jerome Marchand8c921b22011-06-10 15:28:47 +0200186static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
Jerome Marchand924bd882011-06-10 15:28:48 +0200187 u32 index, int offset, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530188{
Jerome Marchand8c921b22011-06-10 15:28:47 +0200189 int ret;
190 size_t clen;
191 struct page *page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200192 unsigned char *user_mem, *cmem, *uncmem = NULL;
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530193
Jerome Marchand8c921b22011-06-10 15:28:47 +0200194 page = bvec->bv_page;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530195
Jerome Marchand8c921b22011-06-10 15:28:47 +0200196 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200197 handle_zero_page(bvec);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200198 return 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530199 }
200
Jerome Marchand8c921b22011-06-10 15:28:47 +0200201 /* Requested page is not present in compressed area */
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600202 if (unlikely(!zram->table[index].handle)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200203 pr_debug("Read before write: sector=%lu, size=%u",
204 (ulong)(bio->bi_sector), bio->bi_size);
Jerome Marchand924bd882011-06-10 15:28:48 +0200205 handle_zero_page(bvec);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200206 return 0;
207 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530208
Jerome Marchand924bd882011-06-10 15:28:48 +0200209 if (is_partial_io(bvec)) {
210 /* Use a temporary buffer to decompress the page */
211 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
212 if (!uncmem) {
213 pr_info("Error allocating temp memory!\n");
214 return -ENOMEM;
215 }
216 }
217
Cong Wangba82fe22011-11-25 23:14:25 +0800218 user_mem = kmap_atomic(page);
Jerome Marchand924bd882011-06-10 15:28:48 +0200219 if (!is_partial_io(bvec))
220 uncmem = user_mem;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200221 clen = PAGE_SIZE;
222
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600223 cmem = zs_map_object(zram->mem_pool, zram->table[index].handle);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200224
Minchan Kim130f3152012-06-08 15:39:27 +0900225 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
Jerome Marchand924bd882011-06-10 15:28:48 +0200226 uncmem, &clen);
227
228 if (is_partial_io(bvec)) {
229 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
230 bvec->bv_len);
231 kfree(uncmem);
232 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200233
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600234 zs_unmap_object(zram->mem_pool, zram->table[index].handle);
Cong Wangba82fe22011-11-25 23:14:25 +0800235 kunmap_atomic(user_mem);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200236
237 /* Should NEVER happen. Return bio error if it does. */
238 if (unlikely(ret != LZO_E_OK)) {
239 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
240 zram_stat64_inc(zram, &zram->stats.failed_reads);
241 return ret;
242 }
243
244 flush_dcache_page(page);
245
246 return 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530247}
248
Jerome Marchand924bd882011-06-10 15:28:48 +0200249static int zram_read_before_write(struct zram *zram, char *mem, u32 index)
Jerome Marchand8c921b22011-06-10 15:28:47 +0200250{
251 int ret;
Jerome Marchand924bd882011-06-10 15:28:48 +0200252 size_t clen = PAGE_SIZE;
Jerome Marchand924bd882011-06-10 15:28:48 +0200253 unsigned char *cmem;
Minchan Kim374a6912012-06-08 15:39:26 +0900254 unsigned long handle = zram->table[index].handle;
Jerome Marchand924bd882011-06-10 15:28:48 +0200255
Minchan Kim374a6912012-06-08 15:39:26 +0900256 if (zram_test_flag(zram, index, ZRAM_ZERO) || !handle) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200257 memset(mem, 0, PAGE_SIZE);
258 return 0;
259 }
260
Minchan Kim374a6912012-06-08 15:39:26 +0900261 cmem = zs_map_object(zram->mem_pool, handle);
Minchan Kim130f3152012-06-08 15:39:27 +0900262 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
Jerome Marchand924bd882011-06-10 15:28:48 +0200263 mem, &clen);
Minchan Kim374a6912012-06-08 15:39:26 +0900264 zs_unmap_object(zram->mem_pool, handle);
Jerome Marchand924bd882011-06-10 15:28:48 +0200265
266 /* Should NEVER happen. Return bio error if it does. */
267 if (unlikely(ret != LZO_E_OK)) {
268 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
269 zram_stat64_inc(zram, &zram->stats.failed_reads);
270 return ret;
271 }
272
273 return 0;
274}
275
276static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
277 int offset)
278{
279 int ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200280 size_t clen;
Minchan Kimc2344342012-06-08 15:39:25 +0900281 unsigned long handle;
Minchan Kim130f3152012-06-08 15:39:27 +0900282 struct page *page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200283 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200284
285 page = bvec->bv_page;
286 src = zram->compress_buffer;
287
Jerome Marchand924bd882011-06-10 15:28:48 +0200288 if (is_partial_io(bvec)) {
289 /*
290 * This is a partial IO. We need to read the full page
291 * before to write the changes.
292 */
293 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
294 if (!uncmem) {
295 pr_info("Error allocating temp memory!\n");
296 ret = -ENOMEM;
297 goto out;
298 }
299 ret = zram_read_before_write(zram, uncmem, index);
300 if (ret) {
301 kfree(uncmem);
302 goto out;
303 }
304 }
305
Jerome Marchand8c921b22011-06-10 15:28:47 +0200306 /*
307 * System overwrites unused sectors. Free memory associated
308 * with this sector now.
309 */
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600310 if (zram->table[index].handle ||
Jerome Marchand8c921b22011-06-10 15:28:47 +0200311 zram_test_flag(zram, index, ZRAM_ZERO))
312 zram_free_page(zram, index);
313
Cong Wangba82fe22011-11-25 23:14:25 +0800314 user_mem = kmap_atomic(page);
Jerome Marchand924bd882011-06-10 15:28:48 +0200315
316 if (is_partial_io(bvec))
317 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
318 bvec->bv_len);
319 else
320 uncmem = user_mem;
321
322 if (page_zero_filled(uncmem)) {
Cong Wangba82fe22011-11-25 23:14:25 +0800323 kunmap_atomic(user_mem);
Jerome Marchand924bd882011-06-10 15:28:48 +0200324 if (is_partial_io(bvec))
325 kfree(uncmem);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200326 zram_stat_inc(&zram->stats.pages_zero);
327 zram_set_flag(zram, index, ZRAM_ZERO);
Jerome Marchand924bd882011-06-10 15:28:48 +0200328 ret = 0;
329 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200330 }
331
Jerome Marchand924bd882011-06-10 15:28:48 +0200332 ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
Jerome Marchand8c921b22011-06-10 15:28:47 +0200333 zram->compress_workmem);
334
Cong Wangba82fe22011-11-25 23:14:25 +0800335 kunmap_atomic(user_mem);
Jerome Marchand924bd882011-06-10 15:28:48 +0200336 if (is_partial_io(bvec))
337 kfree(uncmem);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200338
339 if (unlikely(ret != LZO_E_OK)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200340 pr_err("Compression failed! err=%d\n", ret);
Jerome Marchand924bd882011-06-10 15:28:48 +0200341 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200342 }
343
Minchan Kim130f3152012-06-08 15:39:27 +0900344 if (unlikely(clen > max_zpage_size))
345 zram_stat_inc(&zram->stats.bad_compress);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200346
Minchan Kim130f3152012-06-08 15:39:27 +0900347 handle = zs_malloc(zram->mem_pool, clen);
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600348 if (!handle) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200349 pr_info("Error allocating memory for compressed "
350 "page: %u, size=%zu\n", index, clen);
Jerome Marchand924bd882011-06-10 15:28:48 +0200351 ret = -ENOMEM;
352 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200353 }
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600354 cmem = zs_map_object(zram->mem_pool, handle);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200355
Jerome Marchand8c921b22011-06-10 15:28:47 +0200356 memcpy(cmem, src, clen);
357
Minchan Kim130f3152012-06-08 15:39:27 +0900358 zs_unmap_object(zram->mem_pool, handle);
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600359
360 zram->table[index].handle = handle;
361 zram->table[index].size = clen;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200362
363 /* Update stats */
364 zram_stat64_add(zram, &zram->stats.compr_size, clen);
365 zram_stat_inc(&zram->stats.pages_stored);
366 if (clen <= PAGE_SIZE / 2)
367 zram_stat_inc(&zram->stats.good_compress);
368
Jerome Marchand8c921b22011-06-10 15:28:47 +0200369 return 0;
Jerome Marchand924bd882011-06-10 15:28:48 +0200370
371out:
372 if (ret)
373 zram_stat64_inc(zram, &zram->stats.failed_writes);
374 return ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200375}
376
377static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Jerome Marchand924bd882011-06-10 15:28:48 +0200378 int offset, struct bio *bio, int rw)
Jerome Marchand8c921b22011-06-10 15:28:47 +0200379{
Jerome Marchandc5bde232011-06-10 15:28:49 +0200380 int ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200381
Jerome Marchandc5bde232011-06-10 15:28:49 +0200382 if (rw == READ) {
383 down_read(&zram->lock);
384 ret = zram_bvec_read(zram, bvec, index, offset, bio);
385 up_read(&zram->lock);
386 } else {
387 down_write(&zram->lock);
388 ret = zram_bvec_write(zram, bvec, index, offset);
389 up_write(&zram->lock);
390 }
391
392 return ret;
Jerome Marchand924bd882011-06-10 15:28:48 +0200393}
394
395static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
396{
397 if (*offset + bvec->bv_len >= PAGE_SIZE)
398 (*index)++;
399 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200400}
401
402static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530403{
Jerome Marchand924bd882011-06-10 15:28:48 +0200404 int i, offset;
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530405 u32 index;
406 struct bio_vec *bvec;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530407
Jerome Marchand8c921b22011-06-10 15:28:47 +0200408 switch (rw) {
409 case READ:
410 zram_stat64_inc(zram, &zram->stats.num_reads);
411 break;
412 case WRITE:
413 zram_stat64_inc(zram, &zram->stats.num_writes);
414 break;
415 }
416
Nitin Gupta306b0c92009-09-22 10:26:53 +0530417 index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
Jerome Marchand924bd882011-06-10 15:28:48 +0200418 offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530419
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530420 bio_for_each_segment(bvec, bio, i) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200421 int max_transfer_size = PAGE_SIZE - offset;
422
423 if (bvec->bv_len > max_transfer_size) {
424 /*
425 * zram_bvec_rw() can only make operation on a single
426 * zram page. Split the bio vector.
427 */
428 struct bio_vec bv;
429
430 bv.bv_page = bvec->bv_page;
431 bv.bv_len = max_transfer_size;
432 bv.bv_offset = bvec->bv_offset;
433
434 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
435 goto out;
436
437 bv.bv_len = bvec->bv_len - max_transfer_size;
438 bv.bv_offset += max_transfer_size;
439 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
440 goto out;
441 } else
442 if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
443 < 0)
444 goto out;
445
446 update_position(&index, &offset, bvec);
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530447 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530448
449 set_bit(BIO_UPTODATE, &bio->bi_flags);
450 bio_endio(bio, 0);
Nitin Gupta7d7854b2011-01-22 07:36:15 -0500451 return;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530452
453out:
Nitin Gupta306b0c92009-09-22 10:26:53 +0530454 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530455}
456
Nitin Gupta306b0c92009-09-22 10:26:53 +0530457/*
Jerome Marchand924bd882011-06-10 15:28:48 +0200458 * Check if request is within bounds and aligned on zram logical blocks.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530459 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530460static inline int valid_io_request(struct zram *zram, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530461{
462 if (unlikely(
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530463 (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
Jerome Marchand924bd882011-06-10 15:28:48 +0200464 (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
465 (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530466
467 return 0;
468 }
469
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530470 /* I/O request is valid */
Nitin Gupta306b0c92009-09-22 10:26:53 +0530471 return 1;
472}
473
474/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530475 * Handler function for all zram I/O requests.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530476 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200477static void zram_make_request(struct request_queue *queue, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530478{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530479 struct zram *zram = queue->queuedata;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530480
Jerome Marchand0900bea2011-09-06 15:02:11 +0200481 if (unlikely(!zram->init_done) && zram_init_device(zram))
482 goto error;
483
484 down_read(&zram->init_lock);
485 if (unlikely(!zram->init_done))
486 goto error_unlock;
487
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530488 if (!valid_io_request(zram, bio)) {
489 zram_stat64_inc(zram, &zram->stats.invalid_io);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200490 goto error_unlock;
Jerome Marchand6642a672011-02-17 17:11:49 +0100491 }
492
Jerome Marchand8c921b22011-06-10 15:28:47 +0200493 __zram_make_request(zram, bio, bio_data_dir(bio));
Jerome Marchand0900bea2011-09-06 15:02:11 +0200494 up_read(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530495
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700496 return;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200497
498error_unlock:
499 up_read(&zram->init_lock);
500error:
501 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530502}
503
Jerome Marchand0900bea2011-09-06 15:02:11 +0200504void __zram_reset_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530505{
Nitin Gupta97a06382010-05-13 14:24:21 +0530506 size_t index;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530507
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530508 zram->init_done = 0;
Nitin Gupta7eef7532010-01-28 21:13:38 +0530509
Nitin Gupta306b0c92009-09-22 10:26:53 +0530510 /* Free various per-device buffers */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530511 kfree(zram->compress_workmem);
512 free_pages((unsigned long)zram->compress_buffer, 1);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530513
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530514 zram->compress_workmem = NULL;
515 zram->compress_buffer = NULL;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530516
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530517 /* Free all pages that are still in this zram device */
518 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
Minchan Kimc2344342012-06-08 15:39:25 +0900519 unsigned long handle = zram->table[index].handle;
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600520 if (!handle)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530521 continue;
522
Minchan Kim130f3152012-06-08 15:39:27 +0900523 zs_free(zram->mem_pool, handle);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530524 }
525
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530526 vfree(zram->table);
527 zram->table = NULL;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530528
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600529 zs_destroy_pool(zram->mem_pool);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530530 zram->mem_pool = NULL;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530531
Nitin Gupta306b0c92009-09-22 10:26:53 +0530532 /* Reset stats */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530533 memset(&zram->stats, 0, sizeof(zram->stats));
Nitin Gupta306b0c92009-09-22 10:26:53 +0530534
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530535 zram->disksize = 0;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200536}
537
538void zram_reset_device(struct zram *zram)
539{
540 down_write(&zram->init_lock);
541 __zram_reset_device(zram);
542 up_write(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530543}
544
Nitin Gupta33863c22010-08-09 22:56:47 +0530545int zram_init_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530546{
547 int ret;
548 size_t num_pages;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530549
Jerome Marchand0900bea2011-09-06 15:02:11 +0200550 down_write(&zram->init_lock);
Nitin Gupta484875a2010-08-09 22:56:48 +0530551
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530552 if (zram->init_done) {
Jerome Marchand0900bea2011-09-06 15:02:11 +0200553 up_write(&zram->init_lock);
Nitin Gupta484875a2010-08-09 22:56:48 +0530554 return 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530555 }
556
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530557 zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530558
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530559 zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
560 if (!zram->compress_workmem) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530561 pr_err("Error allocating compressor working memory!\n");
562 ret = -ENOMEM;
Jerome Marchand5a18c532011-09-06 15:02:12 +0200563 goto fail_no_table;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530564 }
565
Jerome Marchandfb927282011-11-30 14:16:16 +0100566 zram->compress_buffer =
567 (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530568 if (!zram->compress_buffer) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530569 pr_err("Error allocating compressor buffer space\n");
570 ret = -ENOMEM;
Jerome Marchand5a18c532011-09-06 15:02:12 +0200571 goto fail_no_table;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530572 }
573
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530574 num_pages = zram->disksize >> PAGE_SHIFT;
Joe Perches5b84cc72010-11-04 20:07:59 -0700575 zram->table = vzalloc(num_pages * sizeof(*zram->table));
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530576 if (!zram->table) {
577 pr_err("Error allocating zram address table\n");
Nitin Gupta306b0c92009-09-22 10:26:53 +0530578 ret = -ENOMEM;
Jerome Marchand5a18c532011-09-06 15:02:12 +0200579 goto fail_no_table;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530580 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530581
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530582 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530583
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530584 /* zram devices sort of resembles non-rotational disks */
585 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530586
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600587 zram->mem_pool = zs_create_pool("zram", GFP_NOIO | __GFP_HIGHMEM);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530588 if (!zram->mem_pool) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530589 pr_err("Error creating memory pool\n");
590 ret = -ENOMEM;
591 goto fail;
592 }
593
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530594 zram->init_done = 1;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200595 up_write(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530596
597 pr_debug("Initialization done!\n");
598 return 0;
599
Jerome Marchand5a18c532011-09-06 15:02:12 +0200600fail_no_table:
601 /* To prevent accessing table entries during cleanup */
602 zram->disksize = 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530603fail:
Jerome Marchand0900bea2011-09-06 15:02:11 +0200604 __zram_reset_device(zram);
605 up_write(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530606 pr_err("Initialization failed: err=%d\n", ret);
607 return ret;
608}
609
Nitin Gupta2ccbec02011-09-09 19:01:00 -0400610static void zram_slot_free_notify(struct block_device *bdev,
611 unsigned long index)
Nitin Gupta107c1612010-05-17 11:02:44 +0530612{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530613 struct zram *zram;
Nitin Gupta107c1612010-05-17 11:02:44 +0530614
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530615 zram = bdev->bd_disk->private_data;
616 zram_free_page(zram, index);
617 zram_stat64_inc(zram, &zram->stats.notify_free);
Nitin Gupta107c1612010-05-17 11:02:44 +0530618}
619
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530620static const struct block_device_operations zram_devops = {
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530621 .swap_slot_free_notify = zram_slot_free_notify,
Nitin Gupta107c1612010-05-17 11:02:44 +0530622 .owner = THIS_MODULE
Nitin Gupta306b0c92009-09-22 10:26:53 +0530623};
624
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530625static int create_device(struct zram *zram, int device_id)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530626{
Nitin Guptade1a21a2010-01-28 21:13:40 +0530627 int ret = 0;
628
Jerome Marchandc5bde232011-06-10 15:28:49 +0200629 init_rwsem(&zram->lock);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200630 init_rwsem(&zram->init_lock);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530631 spin_lock_init(&zram->stat64_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530632
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530633 zram->queue = blk_alloc_queue(GFP_KERNEL);
634 if (!zram->queue) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530635 pr_err("Error allocating disk queue for device %d\n",
636 device_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530637 ret = -ENOMEM;
638 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530639 }
640
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530641 blk_queue_make_request(zram->queue, zram_make_request);
642 zram->queue->queuedata = zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530643
644 /* gendisk structure */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530645 zram->disk = alloc_disk(1);
646 if (!zram->disk) {
647 blk_cleanup_queue(zram->queue);
Sam Hansen94b84352012-06-07 16:03:47 -0700648 pr_warn("Error allocating disk structure for device %d\n",
Nitin Gupta306b0c92009-09-22 10:26:53 +0530649 device_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530650 ret = -ENOMEM;
651 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530652 }
653
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530654 zram->disk->major = zram_major;
655 zram->disk->first_minor = device_id;
656 zram->disk->fops = &zram_devops;
657 zram->disk->queue = zram->queue;
658 zram->disk->private_data = zram;
659 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530660
Nitin Gupta33863c22010-08-09 22:56:47 +0530661 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530662 set_capacity(zram->disk, 0);
Nitin Gupta5d83d5a2010-01-28 21:13:39 +0530663
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530664 /*
665 * To ensure that we always get PAGE_SIZE aligned
666 * and n*PAGE_SIZED sized I/O requests.
667 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530668 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
Robert Jennings7b19b8d2011-01-28 08:58:17 -0600669 blk_queue_logical_block_size(zram->disk->queue,
670 ZRAM_LOGICAL_BLOCK_SIZE);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530671 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
672 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
Nitin Gupta5d83d5a2010-01-28 21:13:39 +0530673
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530674 add_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530675
Nitin Gupta33863c22010-08-09 22:56:47 +0530676 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
677 &zram_disk_attr_group);
678 if (ret < 0) {
Sam Hansen94b84352012-06-07 16:03:47 -0700679 pr_warn("Error creating sysfs group");
Nitin Gupta33863c22010-08-09 22:56:47 +0530680 goto out;
681 }
Nitin Gupta33863c22010-08-09 22:56:47 +0530682
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530683 zram->init_done = 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530684
685out:
686 return ret;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530687}
688
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530689static void destroy_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530690{
Nitin Gupta33863c22010-08-09 22:56:47 +0530691 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
692 &zram_disk_attr_group);
Nitin Gupta33863c22010-08-09 22:56:47 +0530693
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530694 if (zram->disk) {
695 del_gendisk(zram->disk);
696 put_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530697 }
698
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530699 if (zram->queue)
700 blk_cleanup_queue(zram->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530701}
702
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500703unsigned int zram_get_num_devices(void)
704{
705 return num_devices;
706}
707
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530708static int __init zram_init(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530709{
Nitin Guptade1a21a2010-01-28 21:13:40 +0530710 int ret, dev_id;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530711
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500712 if (num_devices > max_num_devices) {
Sam Hansen94b84352012-06-07 16:03:47 -0700713 pr_warn("Invalid value for num_devices: %u\n",
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500714 num_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530715 ret = -EINVAL;
716 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530717 }
718
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530719 zram_major = register_blkdev(0, "zram");
720 if (zram_major <= 0) {
Sam Hansen94b84352012-06-07 16:03:47 -0700721 pr_warn("Unable to get major number\n");
Nitin Guptade1a21a2010-01-28 21:13:40 +0530722 ret = -EBUSY;
723 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530724 }
725
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500726 if (!num_devices) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530727 pr_info("num_devices not specified. Using default: 1\n");
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500728 num_devices = 1;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530729 }
730
731 /* Allocate the device array and initialize each one */
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500732 pr_info("Creating %u devices ...\n", num_devices);
733 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
Noah Watkins43801f62011-07-20 17:05:57 -0600734 if (!zram_devices) {
Nitin Guptade1a21a2010-01-28 21:13:40 +0530735 ret = -ENOMEM;
736 goto unregister;
737 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530738
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500739 for (dev_id = 0; dev_id < num_devices; dev_id++) {
Noah Watkins43801f62011-07-20 17:05:57 -0600740 ret = create_device(&zram_devices[dev_id], dev_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530741 if (ret)
Minchan Kim3bf040c2010-01-11 16:15:53 +0900742 goto free_devices;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530743 }
744
Nitin Gupta306b0c92009-09-22 10:26:53 +0530745 return 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530746
Minchan Kim3bf040c2010-01-11 16:15:53 +0900747free_devices:
Nitin Guptade1a21a2010-01-28 21:13:40 +0530748 while (dev_id)
Noah Watkins43801f62011-07-20 17:05:57 -0600749 destroy_device(&zram_devices[--dev_id]);
750 kfree(zram_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530751unregister:
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530752 unregister_blkdev(zram_major, "zram");
Nitin Guptade1a21a2010-01-28 21:13:40 +0530753out:
Nitin Gupta306b0c92009-09-22 10:26:53 +0530754 return ret;
755}
756
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530757static void __exit zram_exit(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530758{
759 int i;
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530760 struct zram *zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530761
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500762 for (i = 0; i < num_devices; i++) {
Noah Watkins43801f62011-07-20 17:05:57 -0600763 zram = &zram_devices[i];
Nitin Gupta306b0c92009-09-22 10:26:53 +0530764
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530765 destroy_device(zram);
766 if (zram->init_done)
Nitin Gupta33863c22010-08-09 22:56:47 +0530767 zram_reset_device(zram);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530768 }
769
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530770 unregister_blkdev(zram_major, "zram");
Nitin Gupta306b0c92009-09-22 10:26:53 +0530771
Noah Watkins43801f62011-07-20 17:05:57 -0600772 kfree(zram_devices);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530773 pr_debug("Cleanup done!\n");
774}
775
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500776module_param(num_devices, uint, 0);
777MODULE_PARM_DESC(num_devices, "Number of zram devices");
Nitin Gupta306b0c92009-09-22 10:26:53 +0530778
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530779module_init(zram_init);
780module_exit(zram_exit);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530781
782MODULE_LICENSE("Dual BSD/GPL");
783MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530784MODULE_DESCRIPTION("Compressed RAM Block Device");