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