Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 2 | * Compressed RAM block device |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 3 | * |
Nitin Gupta | 1130ebb | 2010-01-28 21:21:35 +0530 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 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 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 15 | #define KMSG_COMPONENT "zram" |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 17 | |
Robert Jennings | b1f5b81 | 2011-01-28 08:59:26 -0600 | [diff] [blame] | 18 | #ifdef CONFIG_ZRAM_DEBUG |
| 19 | #define DEBUG |
| 20 | #endif |
| 21 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
Randy Dunlap | 8946a08 | 2010-06-23 20:27:09 -0700 | [diff] [blame] | 24 | #include <linux/bio.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 25 | #include <linux/bitops.h> |
| 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/buffer_head.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/genhd.h> |
| 30 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 32 | #include <linux/lzo.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 33 | #include <linux/string.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 34 | #include <linux/vmalloc.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 35 | |
Nitin Gupta | 16a4bfb | 2010-06-01 13:31:24 +0530 | [diff] [blame] | 36 | #include "zram_drv.h" |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 37 | |
| 38 | /* Globals */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 39 | static int zram_major; |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 40 | struct zram *zram_devices; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 41 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 42 | /* Module params (documentation at end) */ |
Davidlohr Bueso | ca3d70b | 2013-01-01 21:24:13 -0800 | [diff] [blame] | 43 | static unsigned int num_devices = 1; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 44 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 45 | static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc) |
| 46 | { |
| 47 | spin_lock(&zram->stat64_lock); |
| 48 | *v = *v + inc; |
| 49 | spin_unlock(&zram->stat64_lock); |
| 50 | } |
| 51 | |
| 52 | static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec) |
| 53 | { |
| 54 | spin_lock(&zram->stat64_lock); |
| 55 | *v = *v - dec; |
| 56 | spin_unlock(&zram->stat64_lock); |
| 57 | } |
| 58 | |
| 59 | static void zram_stat64_inc(struct zram *zram, u64 *v) |
| 60 | { |
| 61 | zram_stat64_add(zram, v, 1); |
| 62 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 63 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 64 | static int zram_test_flag(struct zram_meta *meta, u32 index, |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 65 | enum zram_pageflags flag) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 66 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 67 | return meta->table[index].flags & BIT(flag); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 68 | } |
| 69 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 70 | static void zram_set_flag(struct zram_meta *meta, u32 index, |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 71 | enum zram_pageflags flag) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 72 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 73 | meta->table[index].flags |= BIT(flag); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 76 | static void zram_clear_flag(struct zram_meta *meta, u32 index, |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 77 | enum zram_pageflags flag) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 78 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 79 | meta->table[index].flags &= ~BIT(flag); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int page_zero_filled(void *ptr) |
| 83 | { |
| 84 | unsigned int pos; |
| 85 | unsigned long *page; |
| 86 | |
| 87 | page = (unsigned long *)ptr; |
| 88 | |
| 89 | for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) { |
| 90 | if (page[pos]) |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | return 1; |
| 95 | } |
| 96 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 97 | static void zram_free_page(struct zram *zram, size_t index) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 98 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 99 | struct zram_meta *meta = zram->meta; |
| 100 | unsigned long handle = meta->table[index].handle; |
| 101 | u16 size = meta->table[index].size; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 102 | |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 103 | if (unlikely(!handle)) { |
Nitin Gupta | 2e88228 | 2010-01-28 21:13:41 +0530 | [diff] [blame] | 104 | /* |
| 105 | * No memory is allocated for zero filled pages. |
| 106 | * Simply clear zero page flag. |
| 107 | */ |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 108 | if (zram_test_flag(meta, index, ZRAM_ZERO)) { |
| 109 | zram_clear_flag(meta, index, ZRAM_ZERO); |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 110 | zram->stats.pages_zero--; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 111 | } |
| 112 | return; |
| 113 | } |
| 114 | |
Minchan Kim | 130f315 | 2012-06-08 15:39:27 +0900 | [diff] [blame] | 115 | if (unlikely(size > max_zpage_size)) |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 116 | zram->stats.bad_compress--; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 117 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 118 | zs_free(meta->mem_pool, handle); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 119 | |
Minchan Kim | 130f315 | 2012-06-08 15:39:27 +0900 | [diff] [blame] | 120 | if (size <= PAGE_SIZE / 2) |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 121 | zram->stats.good_compress--; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 122 | |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 123 | zram_stat64_sub(zram, &zram->stats.compr_size, |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 124 | meta->table[index].size); |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 125 | zram->stats.pages_stored--; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 126 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 127 | meta->table[index].handle = 0; |
| 128 | meta->table[index].size = 0; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 129 | } |
| 130 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 131 | static void handle_zero_page(struct bio_vec *bvec) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 132 | { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 133 | struct page *page = bvec->bv_page; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 134 | void *user_mem; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 135 | |
Cong Wang | ba82fe2 | 2011-11-25 23:14:25 +0800 | [diff] [blame] | 136 | user_mem = kmap_atomic(page); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 137 | memset(user_mem + bvec->bv_offset, 0, bvec->bv_len); |
Cong Wang | ba82fe2 | 2011-11-25 23:14:25 +0800 | [diff] [blame] | 138 | kunmap_atomic(user_mem); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 139 | |
Nitin Gupta | 30fb8a7 | 2009-12-12 11:44:46 +0530 | [diff] [blame] | 140 | flush_dcache_page(page); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 141 | } |
| 142 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 143 | static inline int is_partial_io(struct bio_vec *bvec) |
| 144 | { |
| 145 | return bvec->bv_len != PAGE_SIZE; |
| 146 | } |
| 147 | |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 148 | static int zram_decompress_page(struct zram *zram, char *mem, u32 index) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 149 | { |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 150 | int ret = LZO_E_OK; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 151 | size_t clen = PAGE_SIZE; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 152 | unsigned char *cmem; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 153 | struct zram_meta *meta = zram->meta; |
| 154 | unsigned long handle = meta->table[index].handle; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 155 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 156 | if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 157 | memset(mem, 0, PAGE_SIZE); |
| 158 | return 0; |
| 159 | } |
| 160 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 161 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO); |
| 162 | if (meta->table[index].size == PAGE_SIZE) |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 163 | memcpy(mem, cmem, PAGE_SIZE); |
| 164 | else |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 165 | ret = lzo1x_decompress_safe(cmem, meta->table[index].size, |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 166 | mem, &clen); |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 167 | zs_unmap_object(meta->mem_pool, handle); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 168 | |
| 169 | /* Should NEVER happen. Return bio error if it does. */ |
| 170 | if (unlikely(ret != LZO_E_OK)) { |
| 171 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); |
| 172 | zram_stat64_inc(zram, &zram->stats.failed_reads); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 179 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
| 180 | u32 index, int offset, struct bio *bio) |
| 181 | { |
| 182 | int ret; |
| 183 | struct page *page; |
| 184 | unsigned char *user_mem, *uncmem = NULL; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 185 | struct zram_meta *meta = zram->meta; |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 186 | page = bvec->bv_page; |
| 187 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 188 | if (unlikely(!meta->table[index].handle) || |
| 189 | zram_test_flag(meta, index, ZRAM_ZERO)) { |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 190 | handle_zero_page(bvec); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 194 | if (is_partial_io(bvec)) |
| 195 | /* Use a temporary buffer to decompress the page */ |
Minchan Kim | 7e5a510 | 2013-01-30 11:41:39 +0900 | [diff] [blame] | 196 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
| 197 | |
| 198 | user_mem = kmap_atomic(page); |
| 199 | if (!is_partial_io(bvec)) |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 200 | uncmem = user_mem; |
| 201 | |
| 202 | if (!uncmem) { |
| 203 | pr_info("Unable to allocate temp memory\n"); |
| 204 | ret = -ENOMEM; |
| 205 | goto out_cleanup; |
| 206 | } |
| 207 | |
| 208 | ret = zram_decompress_page(zram, uncmem, index); |
| 209 | /* Should NEVER happen. Return bio error if it does. */ |
| 210 | if (unlikely(ret != LZO_E_OK)) { |
| 211 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); |
| 212 | zram_stat64_inc(zram, &zram->stats.failed_reads); |
| 213 | goto out_cleanup; |
| 214 | } |
| 215 | |
| 216 | if (is_partial_io(bvec)) |
| 217 | memcpy(user_mem + bvec->bv_offset, uncmem + offset, |
| 218 | bvec->bv_len); |
| 219 | |
| 220 | flush_dcache_page(page); |
| 221 | ret = 0; |
| 222 | out_cleanup: |
| 223 | kunmap_atomic(user_mem); |
| 224 | if (is_partial_io(bvec)) |
| 225 | kfree(uncmem); |
| 226 | return ret; |
| 227 | } |
| 228 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 229 | static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, |
| 230 | int offset) |
| 231 | { |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 232 | int ret = 0; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 233 | size_t clen; |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 234 | unsigned long handle; |
Minchan Kim | 130f315 | 2012-06-08 15:39:27 +0900 | [diff] [blame] | 235 | struct page *page; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 236 | unsigned char *user_mem, *cmem, *src, *uncmem = NULL; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 237 | struct zram_meta *meta = zram->meta; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 238 | |
| 239 | page = bvec->bv_page; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 240 | src = meta->compress_buffer; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 241 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 242 | if (is_partial_io(bvec)) { |
| 243 | /* |
| 244 | * This is a partial IO. We need to read the full page |
| 245 | * before to write the changes. |
| 246 | */ |
Minchan Kim | 7e5a510 | 2013-01-30 11:41:39 +0900 | [diff] [blame] | 247 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 248 | if (!uncmem) { |
| 249 | pr_info("Error allocating temp memory!\n"); |
| 250 | ret = -ENOMEM; |
| 251 | goto out; |
| 252 | } |
Sergey Senozhatsky | 37b51fd | 2012-10-30 22:40:23 +0300 | [diff] [blame] | 253 | ret = zram_decompress_page(zram, uncmem, index); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 254 | if (ret) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 255 | goto out; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 258 | /* |
| 259 | * System overwrites unused sectors. Free memory associated |
| 260 | * with this sector now. |
| 261 | */ |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 262 | if (meta->table[index].handle || |
| 263 | zram_test_flag(meta, index, ZRAM_ZERO)) |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 264 | zram_free_page(zram, index); |
| 265 | |
Cong Wang | ba82fe2 | 2011-11-25 23:14:25 +0800 | [diff] [blame] | 266 | user_mem = kmap_atomic(page); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 267 | |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 268 | if (is_partial_io(bvec)) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 269 | memcpy(uncmem + offset, user_mem + bvec->bv_offset, |
| 270 | bvec->bv_len); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 271 | kunmap_atomic(user_mem); |
| 272 | user_mem = NULL; |
| 273 | } else { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 274 | uncmem = user_mem; |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 275 | } |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 276 | |
| 277 | if (page_zero_filled(uncmem)) { |
Cong Wang | ba82fe2 | 2011-11-25 23:14:25 +0800 | [diff] [blame] | 278 | kunmap_atomic(user_mem); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 279 | if (is_partial_io(bvec)) |
| 280 | kfree(uncmem); |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 281 | zram->stats.pages_zero++; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 282 | zram_set_flag(meta, index, ZRAM_ZERO); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 283 | ret = 0; |
| 284 | goto out; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 287 | ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen, |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 288 | meta->compress_workmem); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 289 | |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 290 | if (!is_partial_io(bvec)) { |
| 291 | kunmap_atomic(user_mem); |
| 292 | user_mem = NULL; |
| 293 | uncmem = NULL; |
| 294 | } |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 295 | |
| 296 | if (unlikely(ret != LZO_E_OK)) { |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 297 | pr_err("Compression failed! err=%d\n", ret); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 298 | goto out; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Nitin Gupta | c8f2f0d | 2012-10-10 17:42:18 -0700 | [diff] [blame] | 301 | if (unlikely(clen > max_zpage_size)) { |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 302 | zram->stats.bad_compress++; |
Nitin Gupta | c8f2f0d | 2012-10-10 17:42:18 -0700 | [diff] [blame] | 303 | clen = PAGE_SIZE; |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 304 | src = NULL; |
| 305 | if (is_partial_io(bvec)) |
| 306 | src = uncmem; |
Nitin Gupta | c8f2f0d | 2012-10-10 17:42:18 -0700 | [diff] [blame] | 307 | } |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 308 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 309 | handle = zs_malloc(meta->mem_pool, clen); |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 310 | if (!handle) { |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 311 | pr_info("Error allocating memory for compressed " |
| 312 | "page: %u, size=%zu\n", index, clen); |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 313 | ret = -ENOMEM; |
| 314 | goto out; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 315 | } |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 316 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 317 | |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 318 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) |
| 319 | src = kmap_atomic(page); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 320 | memcpy(cmem, src, clen); |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 321 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) |
| 322 | kunmap_atomic(src); |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 323 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 324 | zs_unmap_object(meta->mem_pool, handle); |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 325 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 326 | meta->table[index].handle = handle; |
| 327 | meta->table[index].size = clen; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 328 | |
| 329 | /* Update stats */ |
| 330 | zram_stat64_add(zram, &zram->stats.compr_size, clen); |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 331 | zram->stats.pages_stored++; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 332 | if (clen <= PAGE_SIZE / 2) |
Davidlohr Bueso | d178a07 | 2013-01-01 21:24:29 -0800 | [diff] [blame] | 333 | zram->stats.good_compress++; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 334 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 335 | out: |
Nitin Gupta | 397c606 | 2013-01-02 08:53:41 -0800 | [diff] [blame] | 336 | if (is_partial_io(bvec)) |
| 337 | kfree(uncmem); |
| 338 | |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 339 | if (ret) |
| 340 | zram_stat64_inc(zram, &zram->stats.failed_writes); |
| 341 | return ret; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 345 | int offset, struct bio *bio, int rw) |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 346 | { |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 347 | int ret; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 348 | |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 349 | if (rw == READ) { |
| 350 | down_read(&zram->lock); |
| 351 | ret = zram_bvec_read(zram, bvec, index, offset, bio); |
| 352 | up_read(&zram->lock); |
| 353 | } else { |
| 354 | down_write(&zram->lock); |
| 355 | ret = zram_bvec_write(zram, bvec, index, offset); |
| 356 | up_write(&zram->lock); |
| 357 | } |
| 358 | |
| 359 | return ret; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void update_position(u32 *index, int *offset, struct bio_vec *bvec) |
| 363 | { |
| 364 | if (*offset + bvec->bv_len >= PAGE_SIZE) |
| 365 | (*index)++; |
| 366 | *offset = (*offset + bvec->bv_len) % PAGE_SIZE; |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void __zram_make_request(struct zram *zram, struct bio *bio, int rw) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 370 | { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 371 | int i, offset; |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 372 | u32 index; |
| 373 | struct bio_vec *bvec; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 374 | |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 375 | switch (rw) { |
| 376 | case READ: |
| 377 | zram_stat64_inc(zram, &zram->stats.num_reads); |
| 378 | break; |
| 379 | case WRITE: |
| 380 | zram_stat64_inc(zram, &zram->stats.num_writes); |
| 381 | break; |
| 382 | } |
| 383 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 384 | index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT; |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 385 | offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 386 | |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 387 | bio_for_each_segment(bvec, bio, i) { |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 388 | int max_transfer_size = PAGE_SIZE - offset; |
| 389 | |
| 390 | if (bvec->bv_len > max_transfer_size) { |
| 391 | /* |
| 392 | * zram_bvec_rw() can only make operation on a single |
| 393 | * zram page. Split the bio vector. |
| 394 | */ |
| 395 | struct bio_vec bv; |
| 396 | |
| 397 | bv.bv_page = bvec->bv_page; |
| 398 | bv.bv_len = max_transfer_size; |
| 399 | bv.bv_offset = bvec->bv_offset; |
| 400 | |
| 401 | if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0) |
| 402 | goto out; |
| 403 | |
| 404 | bv.bv_len = bvec->bv_len - max_transfer_size; |
| 405 | bv.bv_offset += max_transfer_size; |
| 406 | if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0) |
| 407 | goto out; |
| 408 | } else |
| 409 | if (zram_bvec_rw(zram, bvec, index, offset, bio, rw) |
| 410 | < 0) |
| 411 | goto out; |
| 412 | |
| 413 | update_position(&index, &offset, bvec); |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 414 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 415 | |
| 416 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
| 417 | bio_endio(bio, 0); |
Nitin Gupta | 7d7854b | 2011-01-22 07:36:15 -0500 | [diff] [blame] | 418 | return; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 419 | |
| 420 | out: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 421 | bio_io_error(bio); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 422 | } |
| 423 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 424 | /* |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 425 | * Check if request is within bounds and aligned on zram logical blocks. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 426 | */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 427 | static inline int valid_io_request(struct zram *zram, struct bio *bio) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 428 | { |
| 429 | if (unlikely( |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 430 | (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) || |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 431 | (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) || |
| 432 | (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 437 | /* I/O request is valid */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 438 | return 1; |
| 439 | } |
| 440 | |
| 441 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 442 | * Handler function for all zram I/O requests. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 443 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 444 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 445 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 446 | struct zram *zram = queue->queuedata; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 447 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 448 | down_read(&zram->init_lock); |
| 449 | if (unlikely(!zram->init_done)) |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 450 | goto error; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 451 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 452 | if (!valid_io_request(zram, bio)) { |
| 453 | zram_stat64_inc(zram, &zram->stats.invalid_io); |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 454 | goto error; |
Jerome Marchand | 6642a67 | 2011-02-17 17:11:49 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Jerome Marchand | 8c921b2 | 2011-06-10 15:28:47 +0200 | [diff] [blame] | 457 | __zram_make_request(zram, bio, bio_data_dir(bio)); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 458 | up_read(&zram->init_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 459 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 460 | return; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 461 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 462 | error: |
Minchan Kim | 3de738c | 2013-01-30 11:41:41 +0900 | [diff] [blame] | 463 | up_read(&zram->init_lock); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 464 | bio_io_error(bio); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 465 | } |
| 466 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 467 | void __zram_reset_device(struct zram *zram) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 468 | { |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 469 | size_t index; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 470 | struct zram_meta *meta; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 471 | |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 472 | if (!zram->init_done) |
| 473 | return; |
| 474 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 475 | meta = zram->meta; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 476 | zram->init_done = 0; |
Nitin Gupta | 7eef753 | 2010-01-28 21:13:38 +0530 | [diff] [blame] | 477 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 478 | /* Free all pages that are still in this zram device */ |
| 479 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 480 | unsigned long handle = meta->table[index].handle; |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 481 | if (!handle) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 482 | continue; |
| 483 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 484 | zs_free(meta->mem_pool, handle); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 485 | } |
| 486 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 487 | zram_meta_free(zram->meta); |
| 488 | zram->meta = NULL; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 489 | /* Reset stats */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 490 | memset(&zram->stats, 0, sizeof(zram->stats)); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 491 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 492 | zram->disksize = 0; |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 493 | set_capacity(zram->disk, 0); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void zram_reset_device(struct zram *zram) |
| 497 | { |
| 498 | down_write(&zram->init_lock); |
| 499 | __zram_reset_device(zram); |
| 500 | up_write(&zram->init_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 501 | } |
| 502 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 503 | void zram_meta_free(struct zram_meta *meta) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 504 | { |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 505 | zs_destroy_pool(meta->mem_pool); |
| 506 | kfree(meta->compress_workmem); |
| 507 | free_pages((unsigned long)meta->compress_buffer, 1); |
| 508 | vfree(meta->table); |
| 509 | kfree(meta); |
| 510 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 511 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 512 | struct zram_meta *zram_meta_alloc(u64 disksize) |
| 513 | { |
| 514 | size_t num_pages; |
| 515 | struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL); |
| 516 | if (!meta) |
| 517 | goto out; |
| 518 | |
| 519 | meta->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 520 | if (!meta->compress_workmem) { |
| 521 | pr_err("Error allocating compressor working memory!\n"); |
| 522 | goto free_meta; |
| 523 | } |
| 524 | |
| 525 | meta->compress_buffer = |
| 526 | (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); |
| 527 | if (!meta->compress_buffer) { |
| 528 | pr_err("Error allocating compressor buffer space\n"); |
| 529 | goto free_workmem; |
| 530 | } |
| 531 | |
| 532 | num_pages = disksize >> PAGE_SHIFT; |
| 533 | meta->table = vzalloc(num_pages * sizeof(*meta->table)); |
| 534 | if (!meta->table) { |
| 535 | pr_err("Error allocating zram address table\n"); |
| 536 | goto free_buffer; |
| 537 | } |
| 538 | |
| 539 | meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM); |
| 540 | if (!meta->mem_pool) { |
| 541 | pr_err("Error creating memory pool\n"); |
| 542 | goto free_table; |
| 543 | } |
| 544 | |
| 545 | return meta; |
| 546 | |
| 547 | free_table: |
| 548 | vfree(meta->table); |
| 549 | free_buffer: |
| 550 | free_pages((unsigned long)meta->compress_buffer, 1); |
| 551 | free_workmem: |
| 552 | kfree(meta->compress_workmem); |
| 553 | free_meta: |
| 554 | kfree(meta); |
| 555 | meta = NULL; |
| 556 | out: |
| 557 | return meta; |
| 558 | } |
| 559 | |
| 560 | void zram_init_device(struct zram *zram, struct zram_meta *meta) |
| 561 | { |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 562 | if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) { |
| 563 | pr_info( |
| 564 | "There is little point creating a zram of greater than " |
| 565 | "twice the size of memory since we expect a 2:1 compression " |
| 566 | "ratio. Note that zram uses about 0.1%% of the size of " |
| 567 | "the disk when not in use so a huge zram is " |
| 568 | "wasteful.\n" |
Minchan Kim | 152bce6 | 2013-02-06 08:45:22 +0900 | [diff] [blame] | 569 | "\tMemory Size: %lu kB\n" |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 570 | "\tSize you selected: %llu kB\n" |
| 571 | "Continuing anyway ...\n", |
| 572 | (totalram_pages << PAGE_SHIFT) >> 10, zram->disksize >> 10 |
| 573 | ); |
| 574 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 575 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 576 | /* zram devices sort of resembles non-rotational disks */ |
| 577 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 578 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame^] | 579 | zram->meta = meta; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 580 | zram->init_done = 1; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 581 | |
| 582 | pr_debug("Initialization done!\n"); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 583 | } |
| 584 | |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 585 | static void zram_slot_free_notify(struct block_device *bdev, |
| 586 | unsigned long index) |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 587 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 588 | struct zram *zram; |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 589 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 590 | zram = bdev->bd_disk->private_data; |
| 591 | zram_free_page(zram, index); |
| 592 | zram_stat64_inc(zram, &zram->stats.notify_free); |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 593 | } |
| 594 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 595 | static const struct block_device_operations zram_devops = { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 596 | .swap_slot_free_notify = zram_slot_free_notify, |
Nitin Gupta | 107c161 | 2010-05-17 11:02:44 +0530 | [diff] [blame] | 597 | .owner = THIS_MODULE |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 598 | }; |
| 599 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 600 | static int create_device(struct zram *zram, int device_id) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 601 | { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 602 | int ret = 0; |
| 603 | |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 604 | init_rwsem(&zram->lock); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 605 | init_rwsem(&zram->init_lock); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 606 | spin_lock_init(&zram->stat64_lock); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 607 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 608 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
| 609 | if (!zram->queue) { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 610 | pr_err("Error allocating disk queue for device %d\n", |
| 611 | device_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 612 | ret = -ENOMEM; |
| 613 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 614 | } |
| 615 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 616 | blk_queue_make_request(zram->queue, zram_make_request); |
| 617 | zram->queue->queuedata = zram; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 618 | |
| 619 | /* gendisk structure */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 620 | zram->disk = alloc_disk(1); |
| 621 | if (!zram->disk) { |
| 622 | blk_cleanup_queue(zram->queue); |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 623 | pr_warn("Error allocating disk structure for device %d\n", |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 624 | device_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 625 | ret = -ENOMEM; |
| 626 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 627 | } |
| 628 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 629 | zram->disk->major = zram_major; |
| 630 | zram->disk->first_minor = device_id; |
| 631 | zram->disk->fops = &zram_devops; |
| 632 | zram->disk->queue = zram->queue; |
| 633 | zram->disk->private_data = zram; |
| 634 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 635 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 636 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 637 | set_capacity(zram->disk, 0); |
Nitin Gupta | 5d83d5a | 2010-01-28 21:13:39 +0530 | [diff] [blame] | 638 | |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 639 | /* |
| 640 | * To ensure that we always get PAGE_SIZE aligned |
| 641 | * and n*PAGE_SIZED sized I/O requests. |
| 642 | */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 643 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
Robert Jennings | 7b19b8d | 2011-01-28 08:58:17 -0600 | [diff] [blame] | 644 | blk_queue_logical_block_size(zram->disk->queue, |
| 645 | ZRAM_LOGICAL_BLOCK_SIZE); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 646 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
| 647 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); |
Nitin Gupta | 5d83d5a | 2010-01-28 21:13:39 +0530 | [diff] [blame] | 648 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 649 | add_disk(zram->disk); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 650 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 651 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
| 652 | &zram_disk_attr_group); |
| 653 | if (ret < 0) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 654 | pr_warn("Error creating sysfs group"); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 655 | goto out; |
| 656 | } |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 657 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 658 | zram->init_done = 0; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 659 | |
| 660 | out: |
| 661 | return ret; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 662 | } |
| 663 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 664 | static void destroy_device(struct zram *zram) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 665 | { |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 666 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
| 667 | &zram_disk_attr_group); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 668 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 669 | if (zram->disk) { |
| 670 | del_gendisk(zram->disk); |
| 671 | put_disk(zram->disk); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 672 | } |
| 673 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 674 | if (zram->queue) |
| 675 | blk_cleanup_queue(zram->queue); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 676 | } |
| 677 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 678 | unsigned int zram_get_num_devices(void) |
| 679 | { |
| 680 | return num_devices; |
| 681 | } |
| 682 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 683 | static int __init zram_init(void) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 684 | { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 685 | int ret, dev_id; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 686 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 687 | if (num_devices > max_num_devices) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 688 | pr_warn("Invalid value for num_devices: %u\n", |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 689 | num_devices); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 690 | ret = -EINVAL; |
| 691 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 692 | } |
| 693 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 694 | zram_major = register_blkdev(0, "zram"); |
| 695 | if (zram_major <= 0) { |
Sam Hansen | 94b8435 | 2012-06-07 16:03:47 -0700 | [diff] [blame] | 696 | pr_warn("Unable to get major number\n"); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 697 | ret = -EBUSY; |
| 698 | goto out; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 699 | } |
| 700 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 701 | /* Allocate the device array and initialize each one */ |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 702 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 703 | if (!zram_devices) { |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 704 | ret = -ENOMEM; |
| 705 | goto unregister; |
| 706 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 707 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 708 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 709 | ret = create_device(&zram_devices[dev_id], dev_id); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 710 | if (ret) |
Minchan Kim | 3bf040c | 2010-01-11 16:15:53 +0900 | [diff] [blame] | 711 | goto free_devices; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 712 | } |
| 713 | |
Davidlohr Bueso | ca3d70b | 2013-01-01 21:24:13 -0800 | [diff] [blame] | 714 | pr_info("Created %u device(s) ...\n", num_devices); |
| 715 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 716 | return 0; |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 717 | |
Minchan Kim | 3bf040c | 2010-01-11 16:15:53 +0900 | [diff] [blame] | 718 | free_devices: |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 719 | while (dev_id) |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 720 | destroy_device(&zram_devices[--dev_id]); |
| 721 | kfree(zram_devices); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 722 | unregister: |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 723 | unregister_blkdev(zram_major, "zram"); |
Nitin Gupta | de1a21a | 2010-01-28 21:13:40 +0530 | [diff] [blame] | 724 | out: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 725 | return ret; |
| 726 | } |
| 727 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 728 | static void __exit zram_exit(void) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 729 | { |
| 730 | int i; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 731 | struct zram *zram; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 732 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 733 | for (i = 0; i < num_devices; i++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 734 | zram = &zram_devices[i]; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 735 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 736 | destroy_device(zram); |
Minchan Kim | 0231c40 | 2013-01-30 11:41:40 +0900 | [diff] [blame] | 737 | zram_reset_device(zram); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 738 | } |
| 739 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 740 | unregister_blkdev(zram_major, "zram"); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 741 | |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 742 | kfree(zram_devices); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 743 | pr_debug("Cleanup done!\n"); |
| 744 | } |
| 745 | |
Nitin Gupta | 5fa5a90 | 2012-02-12 23:04:45 -0500 | [diff] [blame] | 746 | module_param(num_devices, uint, 0); |
| 747 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 748 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 749 | module_init(zram_init); |
| 750 | module_exit(zram_exit); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 751 | |
| 752 | MODULE_LICENSE("Dual BSD/GPL"); |
| 753 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 754 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |