Commit | Line | Data |
---|---|---|
306b0c95 | 1 | /* |
f1e3cfff | 2 | * Compressed RAM block device |
306b0c95 | 3 | * |
1130ebba | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
306b0c95 NG |
5 | * |
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 | ||
f1e3cfff | 15 | #define KMSG_COMPONENT "zram" |
306b0c95 NG |
16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
17 | ||
18 | #include <linux/module.h> | |
19 | #include <linux/kernel.h> | |
8946a086 | 20 | #include <linux/bio.h> |
306b0c95 NG |
21 | #include <linux/bitops.h> |
22 | #include <linux/blkdev.h> | |
23 | #include <linux/buffer_head.h> | |
24 | #include <linux/device.h> | |
25 | #include <linux/genhd.h> | |
26 | #include <linux/highmem.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
306b0c95 | 28 | #include <linux/lzo.h> |
306b0c95 | 29 | #include <linux/string.h> |
306b0c95 | 30 | #include <linux/vmalloc.h> |
306b0c95 | 31 | |
16a4bfb9 | 32 | #include "zram_drv.h" |
306b0c95 NG |
33 | |
34 | /* Globals */ | |
f1e3cfff | 35 | static int zram_major; |
33863c21 | 36 | struct zram *devices; |
306b0c95 | 37 | |
306b0c95 | 38 | /* Module params (documentation at end) */ |
33863c21 NG |
39 | unsigned int num_devices; |
40 | ||
41 | static void zram_stat_inc(u32 *v) | |
42 | { | |
43 | *v = *v + 1; | |
44 | } | |
45 | ||
46 | static void zram_stat_dec(u32 *v) | |
47 | { | |
48 | *v = *v - 1; | |
49 | } | |
50 | ||
51 | static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc) | |
52 | { | |
53 | spin_lock(&zram->stat64_lock); | |
54 | *v = *v + inc; | |
55 | spin_unlock(&zram->stat64_lock); | |
56 | } | |
57 | ||
58 | static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec) | |
59 | { | |
60 | spin_lock(&zram->stat64_lock); | |
61 | *v = *v - dec; | |
62 | spin_unlock(&zram->stat64_lock); | |
63 | } | |
64 | ||
65 | static void zram_stat64_inc(struct zram *zram, u64 *v) | |
66 | { | |
67 | zram_stat64_add(zram, v, 1); | |
68 | } | |
306b0c95 | 69 | |
f1e3cfff NG |
70 | static int zram_test_flag(struct zram *zram, u32 index, |
71 | enum zram_pageflags flag) | |
306b0c95 | 72 | { |
f1e3cfff | 73 | return zram->table[index].flags & BIT(flag); |
306b0c95 NG |
74 | } |
75 | ||
f1e3cfff NG |
76 | static void zram_set_flag(struct zram *zram, u32 index, |
77 | enum zram_pageflags flag) | |
306b0c95 | 78 | { |
f1e3cfff | 79 | zram->table[index].flags |= BIT(flag); |
306b0c95 NG |
80 | } |
81 | ||
f1e3cfff NG |
82 | static void zram_clear_flag(struct zram *zram, u32 index, |
83 | enum zram_pageflags flag) | |
306b0c95 | 84 | { |
f1e3cfff | 85 | zram->table[index].flags &= ~BIT(flag); |
306b0c95 NG |
86 | } |
87 | ||
88 | static int page_zero_filled(void *ptr) | |
89 | { | |
90 | unsigned int pos; | |
91 | unsigned long *page; | |
92 | ||
93 | page = (unsigned long *)ptr; | |
94 | ||
95 | for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) { | |
96 | if (page[pos]) | |
97 | return 0; | |
98 | } | |
99 | ||
100 | return 1; | |
101 | } | |
102 | ||
f1e3cfff | 103 | static void zram_set_disksize(struct zram *zram, size_t totalram_bytes) |
306b0c95 | 104 | { |
f1e3cfff | 105 | if (!zram->disksize) { |
306b0c95 NG |
106 | pr_info( |
107 | "disk size not provided. You can use disksize_kb module " | |
108 | "param to specify size.\nUsing default: (%u%% of RAM).\n", | |
109 | default_disksize_perc_ram | |
110 | ); | |
f1e3cfff | 111 | zram->disksize = default_disksize_perc_ram * |
306b0c95 NG |
112 | (totalram_bytes / 100); |
113 | } | |
114 | ||
f1e3cfff | 115 | if (zram->disksize > 2 * (totalram_bytes)) { |
306b0c95 | 116 | pr_info( |
f1e3cfff | 117 | "There is little point creating a zram of greater than " |
306b0c95 | 118 | "twice the size of memory since we expect a 2:1 compression " |
f1e3cfff NG |
119 | "ratio. Note that zram uses about 0.1%% of the size of " |
120 | "the disk when not in use so a huge zram is " | |
306b0c95 NG |
121 | "wasteful.\n" |
122 | "\tMemory Size: %zu kB\n" | |
33863c21 | 123 | "\tSize you selected: %llu kB\n" |
306b0c95 | 124 | "Continuing anyway ...\n", |
f1e3cfff | 125 | totalram_bytes >> 10, zram->disksize |
306b0c95 NG |
126 | ); |
127 | } | |
128 | ||
f1e3cfff | 129 | zram->disksize &= PAGE_MASK; |
306b0c95 NG |
130 | } |
131 | ||
f1e3cfff | 132 | static void zram_free_page(struct zram *zram, size_t index) |
306b0c95 NG |
133 | { |
134 | u32 clen; | |
135 | void *obj; | |
136 | ||
f1e3cfff NG |
137 | struct page *page = zram->table[index].page; |
138 | u32 offset = zram->table[index].offset; | |
306b0c95 NG |
139 | |
140 | if (unlikely(!page)) { | |
2e882281 NG |
141 | /* |
142 | * No memory is allocated for zero filled pages. | |
143 | * Simply clear zero page flag. | |
144 | */ | |
f1e3cfff NG |
145 | if (zram_test_flag(zram, index, ZRAM_ZERO)) { |
146 | zram_clear_flag(zram, index, ZRAM_ZERO); | |
147 | zram_stat_dec(&zram->stats.pages_zero); | |
306b0c95 NG |
148 | } |
149 | return; | |
150 | } | |
151 | ||
f1e3cfff | 152 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) { |
306b0c95 NG |
153 | clen = PAGE_SIZE; |
154 | __free_page(page); | |
f1e3cfff NG |
155 | zram_clear_flag(zram, index, ZRAM_UNCOMPRESSED); |
156 | zram_stat_dec(&zram->stats.pages_expand); | |
306b0c95 NG |
157 | goto out; |
158 | } | |
159 | ||
160 | obj = kmap_atomic(page, KM_USER0) + offset; | |
161 | clen = xv_get_object_size(obj) - sizeof(struct zobj_header); | |
162 | kunmap_atomic(obj, KM_USER0); | |
163 | ||
f1e3cfff | 164 | xv_free(zram->mem_pool, page, offset); |
306b0c95 | 165 | if (clen <= PAGE_SIZE / 2) |
f1e3cfff | 166 | zram_stat_dec(&zram->stats.good_compress); |
306b0c95 NG |
167 | |
168 | out: | |
33863c21 | 169 | zram_stat64_sub(zram, &zram->stats.compr_size, clen); |
f1e3cfff | 170 | zram_stat_dec(&zram->stats.pages_stored); |
306b0c95 | 171 | |
f1e3cfff NG |
172 | zram->table[index].page = NULL; |
173 | zram->table[index].offset = 0; | |
306b0c95 NG |
174 | } |
175 | ||
a1dd52af | 176 | static void handle_zero_page(struct page *page) |
306b0c95 NG |
177 | { |
178 | void *user_mem; | |
306b0c95 NG |
179 | |
180 | user_mem = kmap_atomic(page, KM_USER0); | |
181 | memset(user_mem, 0, PAGE_SIZE); | |
182 | kunmap_atomic(user_mem, KM_USER0); | |
183 | ||
30fb8a71 | 184 | flush_dcache_page(page); |
306b0c95 NG |
185 | } |
186 | ||
f1e3cfff | 187 | static void handle_uncompressed_page(struct zram *zram, |
a1dd52af | 188 | struct page *page, u32 index) |
306b0c95 | 189 | { |
306b0c95 NG |
190 | unsigned char *user_mem, *cmem; |
191 | ||
306b0c95 | 192 | user_mem = kmap_atomic(page, KM_USER0); |
f1e3cfff NG |
193 | cmem = kmap_atomic(zram->table[index].page, KM_USER1) + |
194 | zram->table[index].offset; | |
306b0c95 NG |
195 | |
196 | memcpy(user_mem, cmem, PAGE_SIZE); | |
197 | kunmap_atomic(user_mem, KM_USER0); | |
198 | kunmap_atomic(cmem, KM_USER1); | |
199 | ||
30fb8a71 | 200 | flush_dcache_page(page); |
306b0c95 NG |
201 | } |
202 | ||
7d7854b4 | 203 | static void zram_read(struct zram *zram, struct bio *bio) |
306b0c95 | 204 | { |
a1dd52af NG |
205 | |
206 | int i; | |
306b0c95 | 207 | u32 index; |
a1dd52af | 208 | struct bio_vec *bvec; |
306b0c95 | 209 | |
484875ad | 210 | if (unlikely(!zram->init_done)) { |
1aa32664 | 211 | bio_endio(bio, -ENXIO); |
7d7854b4 | 212 | return; |
484875ad | 213 | } |
306b0c95 | 214 | |
484875ad | 215 | zram_stat64_inc(zram, &zram->stats.num_reads); |
306b0c95 | 216 | index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT; |
484875ad | 217 | |
a1dd52af NG |
218 | bio_for_each_segment(bvec, bio, i) { |
219 | int ret; | |
220 | size_t clen; | |
221 | struct page *page; | |
222 | struct zobj_header *zheader; | |
223 | unsigned char *user_mem, *cmem; | |
306b0c95 | 224 | |
a1dd52af | 225 | page = bvec->bv_page; |
306b0c95 | 226 | |
f1e3cfff | 227 | if (zram_test_flag(zram, index, ZRAM_ZERO)) { |
a1dd52af NG |
228 | handle_zero_page(page); |
229 | continue; | |
230 | } | |
306b0c95 | 231 | |
a1dd52af | 232 | /* Requested page is not present in compressed area */ |
f1e3cfff | 233 | if (unlikely(!zram->table[index].page)) { |
a1dd52af NG |
234 | pr_debug("Read before write: sector=%lu, size=%u", |
235 | (ulong)(bio->bi_sector), bio->bi_size); | |
236 | /* Do nothing */ | |
237 | continue; | |
238 | } | |
306b0c95 | 239 | |
a1dd52af | 240 | /* Page is stored uncompressed since it's incompressible */ |
f1e3cfff NG |
241 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) { |
242 | handle_uncompressed_page(zram, page, index); | |
a1dd52af NG |
243 | continue; |
244 | } | |
306b0c95 | 245 | |
a1dd52af NG |
246 | user_mem = kmap_atomic(page, KM_USER0); |
247 | clen = PAGE_SIZE; | |
306b0c95 | 248 | |
f1e3cfff NG |
249 | cmem = kmap_atomic(zram->table[index].page, KM_USER1) + |
250 | zram->table[index].offset; | |
306b0c95 | 251 | |
a1dd52af NG |
252 | ret = lzo1x_decompress_safe( |
253 | cmem + sizeof(*zheader), | |
254 | xv_get_object_size(cmem) - sizeof(*zheader), | |
255 | user_mem, &clen); | |
306b0c95 | 256 | |
a1dd52af NG |
257 | kunmap_atomic(user_mem, KM_USER0); |
258 | kunmap_atomic(cmem, KM_USER1); | |
306b0c95 | 259 | |
a1dd52af NG |
260 | /* Should NEVER happen. Return bio error if it does. */ |
261 | if (unlikely(ret != LZO_E_OK)) { | |
262 | pr_err("Decompression failed! err=%d, page=%u\n", | |
263 | ret, index); | |
f1e3cfff | 264 | zram_stat64_inc(zram, &zram->stats.failed_reads); |
a1dd52af NG |
265 | goto out; |
266 | } | |
267 | ||
268 | flush_dcache_page(page); | |
269 | index++; | |
270 | } | |
306b0c95 NG |
271 | |
272 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
273 | bio_endio(bio, 0); | |
7d7854b4 | 274 | return; |
306b0c95 NG |
275 | |
276 | out: | |
277 | bio_io_error(bio); | |
306b0c95 NG |
278 | } |
279 | ||
7d7854b4 | 280 | static void zram_write(struct zram *zram, struct bio *bio) |
306b0c95 | 281 | { |
484875ad | 282 | int i, ret; |
a1dd52af NG |
283 | u32 index; |
284 | struct bio_vec *bvec; | |
306b0c95 | 285 | |
484875ad NG |
286 | if (unlikely(!zram->init_done)) { |
287 | ret = zram_init_device(zram); | |
288 | if (ret) | |
289 | goto out; | |
290 | } | |
306b0c95 | 291 | |
484875ad | 292 | zram_stat64_inc(zram, &zram->stats.num_writes); |
306b0c95 NG |
293 | index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT; |
294 | ||
a1dd52af | 295 | bio_for_each_segment(bvec, bio, i) { |
a1dd52af NG |
296 | u32 offset; |
297 | size_t clen; | |
298 | struct zobj_header *zheader; | |
299 | struct page *page, *page_store; | |
300 | unsigned char *user_mem, *cmem, *src; | |
306b0c95 | 301 | |
a1dd52af | 302 | page = bvec->bv_page; |
f1e3cfff | 303 | src = zram->compress_buffer; |
306b0c95 | 304 | |
a1dd52af NG |
305 | /* |
306 | * System overwrites unused sectors. Free memory associated | |
307 | * with this sector now. | |
308 | */ | |
f1e3cfff NG |
309 | if (zram->table[index].page || |
310 | zram_test_flag(zram, index, ZRAM_ZERO)) | |
311 | zram_free_page(zram, index); | |
306b0c95 | 312 | |
f1e3cfff | 313 | mutex_lock(&zram->lock); |
306b0c95 | 314 | |
a1dd52af NG |
315 | user_mem = kmap_atomic(page, KM_USER0); |
316 | if (page_zero_filled(user_mem)) { | |
317 | kunmap_atomic(user_mem, KM_USER0); | |
f1e3cfff NG |
318 | mutex_unlock(&zram->lock); |
319 | zram_stat_inc(&zram->stats.pages_zero); | |
320 | zram_set_flag(zram, index, ZRAM_ZERO); | |
a1dd52af NG |
321 | continue; |
322 | } | |
306b0c95 | 323 | |
a1dd52af | 324 | ret = lzo1x_1_compress(user_mem, PAGE_SIZE, src, &clen, |
f1e3cfff | 325 | zram->compress_workmem); |
306b0c95 | 326 | |
a1dd52af | 327 | kunmap_atomic(user_mem, KM_USER0); |
306b0c95 | 328 | |
a1dd52af | 329 | if (unlikely(ret != LZO_E_OK)) { |
f1e3cfff | 330 | mutex_unlock(&zram->lock); |
a1dd52af | 331 | pr_err("Compression failed! err=%d\n", ret); |
f1e3cfff | 332 | zram_stat64_inc(zram, &zram->stats.failed_writes); |
306b0c95 NG |
333 | goto out; |
334 | } | |
335 | ||
a1dd52af NG |
336 | /* |
337 | * Page is incompressible. Store it as-is (uncompressed) | |
f1e3cfff | 338 | * since we do not want to return too many disk write |
a1dd52af NG |
339 | * errors which has side effect of hanging the system. |
340 | */ | |
341 | if (unlikely(clen > max_zpage_size)) { | |
342 | clen = PAGE_SIZE; | |
343 | page_store = alloc_page(GFP_NOIO | __GFP_HIGHMEM); | |
344 | if (unlikely(!page_store)) { | |
f1e3cfff | 345 | mutex_unlock(&zram->lock); |
a1dd52af NG |
346 | pr_info("Error allocating memory for " |
347 | "incompressible page: %u\n", index); | |
f1e3cfff NG |
348 | zram_stat64_inc(zram, |
349 | &zram->stats.failed_writes); | |
a1dd52af NG |
350 | goto out; |
351 | } | |
352 | ||
353 | offset = 0; | |
f1e3cfff NG |
354 | zram_set_flag(zram, index, ZRAM_UNCOMPRESSED); |
355 | zram_stat_inc(&zram->stats.pages_expand); | |
356 | zram->table[index].page = page_store; | |
a1dd52af NG |
357 | src = kmap_atomic(page, KM_USER0); |
358 | goto memstore; | |
359 | } | |
306b0c95 | 360 | |
f1e3cfff NG |
361 | if (xv_malloc(zram->mem_pool, clen + sizeof(*zheader), |
362 | &zram->table[index].page, &offset, | |
a1dd52af | 363 | GFP_NOIO | __GFP_HIGHMEM)) { |
f1e3cfff | 364 | mutex_unlock(&zram->lock); |
a1dd52af NG |
365 | pr_info("Error allocating memory for compressed " |
366 | "page: %u, size=%zu\n", index, clen); | |
f1e3cfff | 367 | zram_stat64_inc(zram, &zram->stats.failed_writes); |
a1dd52af NG |
368 | goto out; |
369 | } | |
306b0c95 NG |
370 | |
371 | memstore: | |
f1e3cfff | 372 | zram->table[index].offset = offset; |
306b0c95 | 373 | |
f1e3cfff NG |
374 | cmem = kmap_atomic(zram->table[index].page, KM_USER1) + |
375 | zram->table[index].offset; | |
306b0c95 NG |
376 | |
377 | #if 0 | |
a1dd52af | 378 | /* Back-reference needed for memory defragmentation */ |
f1e3cfff | 379 | if (!zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)) { |
a1dd52af NG |
380 | zheader = (struct zobj_header *)cmem; |
381 | zheader->table_idx = index; | |
382 | cmem += sizeof(*zheader); | |
383 | } | |
306b0c95 NG |
384 | #endif |
385 | ||
a1dd52af | 386 | memcpy(cmem, src, clen); |
306b0c95 | 387 | |
a1dd52af | 388 | kunmap_atomic(cmem, KM_USER1); |
f1e3cfff | 389 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) |
a1dd52af | 390 | kunmap_atomic(src, KM_USER0); |
306b0c95 | 391 | |
a1dd52af | 392 | /* Update stats */ |
33863c21 | 393 | zram_stat64_add(zram, &zram->stats.compr_size, clen); |
f1e3cfff | 394 | zram_stat_inc(&zram->stats.pages_stored); |
a1dd52af | 395 | if (clen <= PAGE_SIZE / 2) |
f1e3cfff | 396 | zram_stat_inc(&zram->stats.good_compress); |
306b0c95 | 397 | |
f1e3cfff | 398 | mutex_unlock(&zram->lock); |
a1dd52af NG |
399 | index++; |
400 | } | |
306b0c95 NG |
401 | |
402 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
403 | bio_endio(bio, 0); | |
7d7854b4 | 404 | return; |
306b0c95 NG |
405 | |
406 | out: | |
306b0c95 | 407 | bio_io_error(bio); |
306b0c95 NG |
408 | } |
409 | ||
306b0c95 NG |
410 | /* |
411 | * Check if request is within bounds and page aligned. | |
412 | */ | |
f1e3cfff | 413 | static inline int valid_io_request(struct zram *zram, struct bio *bio) |
306b0c95 NG |
414 | { |
415 | if (unlikely( | |
f1e3cfff | 416 | (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) || |
306b0c95 | 417 | (bio->bi_sector & (SECTORS_PER_PAGE - 1)) || |
a1dd52af | 418 | (bio->bi_size & (PAGE_SIZE - 1)))) { |
306b0c95 NG |
419 | |
420 | return 0; | |
421 | } | |
422 | ||
a1dd52af | 423 | /* I/O request is valid */ |
306b0c95 NG |
424 | return 1; |
425 | } | |
426 | ||
427 | /* | |
f1e3cfff | 428 | * Handler function for all zram I/O requests. |
306b0c95 | 429 | */ |
f1e3cfff | 430 | static int zram_make_request(struct request_queue *queue, struct bio *bio) |
306b0c95 | 431 | { |
f1e3cfff | 432 | struct zram *zram = queue->queuedata; |
306b0c95 | 433 | |
f1e3cfff NG |
434 | if (!valid_io_request(zram, bio)) { |
435 | zram_stat64_inc(zram, &zram->stats.invalid_io); | |
306b0c95 NG |
436 | bio_io_error(bio); |
437 | return 0; | |
438 | } | |
439 | ||
440 | switch (bio_data_dir(bio)) { | |
441 | case READ: | |
7d7854b4 | 442 | zram_read(zram, bio); |
306b0c95 NG |
443 | break; |
444 | ||
445 | case WRITE: | |
7d7854b4 | 446 | zram_write(zram, bio); |
306b0c95 NG |
447 | break; |
448 | } | |
449 | ||
7d7854b4 | 450 | return 0; |
306b0c95 NG |
451 | } |
452 | ||
33863c21 | 453 | void zram_reset_device(struct zram *zram) |
306b0c95 | 454 | { |
97a06382 | 455 | size_t index; |
306b0c95 | 456 | |
484875ad | 457 | mutex_lock(&zram->init_lock); |
f1e3cfff | 458 | zram->init_done = 0; |
7eef7533 | 459 | |
306b0c95 | 460 | /* Free various per-device buffers */ |
f1e3cfff NG |
461 | kfree(zram->compress_workmem); |
462 | free_pages((unsigned long)zram->compress_buffer, 1); | |
306b0c95 | 463 | |
f1e3cfff NG |
464 | zram->compress_workmem = NULL; |
465 | zram->compress_buffer = NULL; | |
306b0c95 | 466 | |
f1e3cfff NG |
467 | /* Free all pages that are still in this zram device */ |
468 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { | |
306b0c95 NG |
469 | struct page *page; |
470 | u16 offset; | |
471 | ||
f1e3cfff NG |
472 | page = zram->table[index].page; |
473 | offset = zram->table[index].offset; | |
306b0c95 NG |
474 | |
475 | if (!page) | |
476 | continue; | |
477 | ||
f1e3cfff | 478 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) |
306b0c95 NG |
479 | __free_page(page); |
480 | else | |
f1e3cfff | 481 | xv_free(zram->mem_pool, page, offset); |
306b0c95 NG |
482 | } |
483 | ||
f1e3cfff NG |
484 | vfree(zram->table); |
485 | zram->table = NULL; | |
306b0c95 | 486 | |
f1e3cfff NG |
487 | xv_destroy_pool(zram->mem_pool); |
488 | zram->mem_pool = NULL; | |
306b0c95 | 489 | |
306b0c95 | 490 | /* Reset stats */ |
f1e3cfff | 491 | memset(&zram->stats, 0, sizeof(zram->stats)); |
306b0c95 | 492 | |
f1e3cfff | 493 | zram->disksize = 0; |
484875ad | 494 | mutex_unlock(&zram->init_lock); |
306b0c95 NG |
495 | } |
496 | ||
33863c21 | 497 | int zram_init_device(struct zram *zram) |
306b0c95 NG |
498 | { |
499 | int ret; | |
500 | size_t num_pages; | |
306b0c95 | 501 | |
484875ad NG |
502 | mutex_lock(&zram->init_lock); |
503 | ||
f1e3cfff | 504 | if (zram->init_done) { |
484875ad NG |
505 | mutex_unlock(&zram->init_lock); |
506 | return 0; | |
306b0c95 NG |
507 | } |
508 | ||
f1e3cfff | 509 | zram_set_disksize(zram, totalram_pages << PAGE_SHIFT); |
306b0c95 | 510 | |
f1e3cfff NG |
511 | zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
512 | if (!zram->compress_workmem) { | |
306b0c95 NG |
513 | pr_err("Error allocating compressor working memory!\n"); |
514 | ret = -ENOMEM; | |
515 | goto fail; | |
516 | } | |
517 | ||
f1e3cfff NG |
518 | zram->compress_buffer = (void *)__get_free_pages(__GFP_ZERO, 1); |
519 | if (!zram->compress_buffer) { | |
306b0c95 NG |
520 | pr_err("Error allocating compressor buffer space\n"); |
521 | ret = -ENOMEM; | |
522 | goto fail; | |
523 | } | |
524 | ||
f1e3cfff | 525 | num_pages = zram->disksize >> PAGE_SHIFT; |
5b84cc78 | 526 | zram->table = vzalloc(num_pages * sizeof(*zram->table)); |
f1e3cfff NG |
527 | if (!zram->table) { |
528 | pr_err("Error allocating zram address table\n"); | |
306b0c95 | 529 | /* To prevent accessing table entries during cleanup */ |
f1e3cfff | 530 | zram->disksize = 0; |
306b0c95 NG |
531 | ret = -ENOMEM; |
532 | goto fail; | |
533 | } | |
306b0c95 | 534 | |
f1e3cfff | 535 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
306b0c95 | 536 | |
f1e3cfff NG |
537 | /* zram devices sort of resembles non-rotational disks */ |
538 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); | |
306b0c95 | 539 | |
f1e3cfff NG |
540 | zram->mem_pool = xv_create_pool(); |
541 | if (!zram->mem_pool) { | |
306b0c95 NG |
542 | pr_err("Error creating memory pool\n"); |
543 | ret = -ENOMEM; | |
544 | goto fail; | |
545 | } | |
546 | ||
f1e3cfff | 547 | zram->init_done = 1; |
484875ad | 548 | mutex_unlock(&zram->init_lock); |
306b0c95 NG |
549 | |
550 | pr_debug("Initialization done!\n"); | |
551 | return 0; | |
552 | ||
553 | fail: | |
484875ad | 554 | mutex_unlock(&zram->init_lock); |
33863c21 | 555 | zram_reset_device(zram); |
306b0c95 NG |
556 | |
557 | pr_err("Initialization failed: err=%d\n", ret); | |
558 | return ret; | |
559 | } | |
560 | ||
f1e3cfff | 561 | void zram_slot_free_notify(struct block_device *bdev, unsigned long index) |
107c161b | 562 | { |
f1e3cfff | 563 | struct zram *zram; |
107c161b | 564 | |
f1e3cfff NG |
565 | zram = bdev->bd_disk->private_data; |
566 | zram_free_page(zram, index); | |
567 | zram_stat64_inc(zram, &zram->stats.notify_free); | |
107c161b NG |
568 | } |
569 | ||
f1e3cfff | 570 | static const struct block_device_operations zram_devops = { |
f1e3cfff | 571 | .swap_slot_free_notify = zram_slot_free_notify, |
107c161b | 572 | .owner = THIS_MODULE |
306b0c95 NG |
573 | }; |
574 | ||
f1e3cfff | 575 | static int create_device(struct zram *zram, int device_id) |
306b0c95 | 576 | { |
de1a21a0 NG |
577 | int ret = 0; |
578 | ||
f1e3cfff | 579 | mutex_init(&zram->lock); |
484875ad | 580 | mutex_init(&zram->init_lock); |
f1e3cfff | 581 | spin_lock_init(&zram->stat64_lock); |
306b0c95 | 582 | |
f1e3cfff NG |
583 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
584 | if (!zram->queue) { | |
306b0c95 NG |
585 | pr_err("Error allocating disk queue for device %d\n", |
586 | device_id); | |
de1a21a0 NG |
587 | ret = -ENOMEM; |
588 | goto out; | |
306b0c95 NG |
589 | } |
590 | ||
f1e3cfff NG |
591 | blk_queue_make_request(zram->queue, zram_make_request); |
592 | zram->queue->queuedata = zram; | |
306b0c95 NG |
593 | |
594 | /* gendisk structure */ | |
f1e3cfff NG |
595 | zram->disk = alloc_disk(1); |
596 | if (!zram->disk) { | |
597 | blk_cleanup_queue(zram->queue); | |
306b0c95 NG |
598 | pr_warning("Error allocating disk structure for device %d\n", |
599 | device_id); | |
de1a21a0 NG |
600 | ret = -ENOMEM; |
601 | goto out; | |
306b0c95 NG |
602 | } |
603 | ||
f1e3cfff NG |
604 | zram->disk->major = zram_major; |
605 | zram->disk->first_minor = device_id; | |
606 | zram->disk->fops = &zram_devops; | |
607 | zram->disk->queue = zram->queue; | |
608 | zram->disk->private_data = zram; | |
609 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); | |
306b0c95 | 610 | |
33863c21 | 611 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
f1e3cfff | 612 | set_capacity(zram->disk, 0); |
5d83d5a0 | 613 | |
a1dd52af NG |
614 | /* |
615 | * To ensure that we always get PAGE_SIZE aligned | |
616 | * and n*PAGE_SIZED sized I/O requests. | |
617 | */ | |
f1e3cfff NG |
618 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
619 | blk_queue_logical_block_size(zram->disk->queue, PAGE_SIZE); | |
620 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); | |
621 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); | |
5d83d5a0 | 622 | |
f1e3cfff | 623 | add_disk(zram->disk); |
306b0c95 | 624 | |
33863c21 NG |
625 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
626 | &zram_disk_attr_group); | |
627 | if (ret < 0) { | |
628 | pr_warning("Error creating sysfs group"); | |
629 | goto out; | |
630 | } | |
33863c21 | 631 | |
f1e3cfff | 632 | zram->init_done = 0; |
de1a21a0 NG |
633 | |
634 | out: | |
635 | return ret; | |
306b0c95 NG |
636 | } |
637 | ||
f1e3cfff | 638 | static void destroy_device(struct zram *zram) |
306b0c95 | 639 | { |
33863c21 NG |
640 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
641 | &zram_disk_attr_group); | |
33863c21 | 642 | |
f1e3cfff NG |
643 | if (zram->disk) { |
644 | del_gendisk(zram->disk); | |
645 | put_disk(zram->disk); | |
306b0c95 NG |
646 | } |
647 | ||
f1e3cfff NG |
648 | if (zram->queue) |
649 | blk_cleanup_queue(zram->queue); | |
306b0c95 NG |
650 | } |
651 | ||
f1e3cfff | 652 | static int __init zram_init(void) |
306b0c95 | 653 | { |
de1a21a0 | 654 | int ret, dev_id; |
306b0c95 NG |
655 | |
656 | if (num_devices > max_num_devices) { | |
657 | pr_warning("Invalid value for num_devices: %u\n", | |
658 | num_devices); | |
de1a21a0 NG |
659 | ret = -EINVAL; |
660 | goto out; | |
306b0c95 NG |
661 | } |
662 | ||
f1e3cfff NG |
663 | zram_major = register_blkdev(0, "zram"); |
664 | if (zram_major <= 0) { | |
306b0c95 | 665 | pr_warning("Unable to get major number\n"); |
de1a21a0 NG |
666 | ret = -EBUSY; |
667 | goto out; | |
306b0c95 NG |
668 | } |
669 | ||
670 | if (!num_devices) { | |
671 | pr_info("num_devices not specified. Using default: 1\n"); | |
672 | num_devices = 1; | |
673 | } | |
674 | ||
675 | /* Allocate the device array and initialize each one */ | |
676 | pr_info("Creating %u devices ...\n", num_devices); | |
f1e3cfff | 677 | devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
de1a21a0 NG |
678 | if (!devices) { |
679 | ret = -ENOMEM; | |
680 | goto unregister; | |
681 | } | |
306b0c95 | 682 | |
de1a21a0 NG |
683 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
684 | ret = create_device(&devices[dev_id], dev_id); | |
685 | if (ret) | |
3bf040c7 | 686 | goto free_devices; |
de1a21a0 NG |
687 | } |
688 | ||
306b0c95 | 689 | return 0; |
de1a21a0 | 690 | |
3bf040c7 | 691 | free_devices: |
de1a21a0 NG |
692 | while (dev_id) |
693 | destroy_device(&devices[--dev_id]); | |
273ad8dc | 694 | kfree(devices); |
de1a21a0 | 695 | unregister: |
f1e3cfff | 696 | unregister_blkdev(zram_major, "zram"); |
de1a21a0 | 697 | out: |
306b0c95 NG |
698 | return ret; |
699 | } | |
700 | ||
f1e3cfff | 701 | static void __exit zram_exit(void) |
306b0c95 NG |
702 | { |
703 | int i; | |
f1e3cfff | 704 | struct zram *zram; |
306b0c95 NG |
705 | |
706 | for (i = 0; i < num_devices; i++) { | |
f1e3cfff | 707 | zram = &devices[i]; |
306b0c95 | 708 | |
f1e3cfff NG |
709 | destroy_device(zram); |
710 | if (zram->init_done) | |
33863c21 | 711 | zram_reset_device(zram); |
306b0c95 NG |
712 | } |
713 | ||
f1e3cfff | 714 | unregister_blkdev(zram_major, "zram"); |
306b0c95 NG |
715 | |
716 | kfree(devices); | |
717 | pr_debug("Cleanup done!\n"); | |
718 | } | |
719 | ||
720 | module_param(num_devices, uint, 0); | |
f1e3cfff | 721 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); |
306b0c95 | 722 | |
f1e3cfff NG |
723 | module_init(zram_init); |
724 | module_exit(zram_exit); | |
306b0c95 NG |
725 | |
726 | MODULE_LICENSE("Dual BSD/GPL"); | |
727 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); | |
f1e3cfff | 728 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |