blob: 40743972eaf7d4d038fe72c0ed12f45caeace023 [file] [log] [blame]
Nitin Gupta306b0c92009-09-22 10:26:53 +05301/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05302 * Compressed RAM block device
Nitin Gupta306b0c92009-09-22 10:26:53 +05303 *
Nitin Gupta1130ebb2010-01-28 21:21:35 +05304 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
Minchan Kim7bfb3de2014-01-30 15:45:55 -08005 * 2012, 2013 Minchan Kim
Nitin Gupta306b0c92009-09-22 10:26:53 +05306 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the licence that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 *
Nitin Gupta306b0c92009-09-22 10:26:53 +053013 */
14
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053015#define KMSG_COMPONENT "zram"
Nitin Gupta306b0c92009-09-22 10:26:53 +053016#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
Robert Jenningsb1f5b812011-01-28 08:59:26 -060018#ifdef CONFIG_ZRAM_DEBUG
19#define DEBUG
20#endif
21
Nitin Gupta306b0c92009-09-22 10:26:53 +053022#include <linux/module.h>
23#include <linux/kernel.h>
Randy Dunlap8946a082010-06-23 20:27:09 -070024#include <linux/bio.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053025#include <linux/bitops.h>
26#include <linux/blkdev.h>
27#include <linux/buffer_head.h>
28#include <linux/device.h>
29#include <linux/genhd.h>
30#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053032#include <linux/string.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053033#include <linux/vmalloc.h>
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -070034#include <linux/err.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053035
Nitin Gupta16a4bfb2010-06-01 13:31:24 +053036#include "zram_drv.h"
Nitin Gupta306b0c92009-09-22 10:26:53 +053037
38/* Globals */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053039static int zram_major;
Jiang Liu0f0e3ba2013-06-07 00:07:29 +080040static struct zram *zram_devices;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -070041static const char *default_compressor = "lzo";
Nitin Gupta306b0c92009-09-22 10:26:53 +053042
Nitin Gupta306b0c92009-09-22 10:26:53 +053043/* Module params (documentation at end) */
Davidlohr Buesoca3d70b2013-01-01 21:24:13 -080044static unsigned int num_devices = 1;
Nitin Gupta33863c22010-08-09 22:56:47 +053045
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070046#define ZRAM_ATTR_RO(name) \
47static ssize_t zram_attr_##name##_show(struct device *d, \
48 struct device_attribute *attr, char *b) \
49{ \
50 struct zram *zram = dev_to_zram(d); \
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070051 return scnprintf(b, PAGE_SIZE, "%llu\n", \
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070052 (u64)atomic64_read(&zram->stats.name)); \
53} \
54static struct device_attribute dev_attr_##name = \
55 __ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL);
56
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -070057static inline int init_done(struct zram *zram)
58{
59 return zram->meta != NULL;
60}
61
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030062static inline struct zram *dev_to_zram(struct device *dev)
63{
64 return (struct zram *)dev_to_disk(dev)->private_data;
65}
66
67static ssize_t disksize_show(struct device *dev,
68 struct device_attribute *attr, char *buf)
69{
70 struct zram *zram = dev_to_zram(dev);
71
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070072 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030073}
74
75static ssize_t initstate_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
77{
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070078 u32 val;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030079 struct zram *zram = dev_to_zram(dev);
80
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -070081 down_read(&zram->init_lock);
82 val = init_done(zram);
83 up_read(&zram->init_lock);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030084
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070085 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030086}
87
88static ssize_t orig_data_size_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
90{
91 struct zram *zram = dev_to_zram(dev);
92
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -070093 return scnprintf(buf, PAGE_SIZE, "%llu\n",
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070094 (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030095}
96
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +030097static ssize_t mem_used_total_show(struct device *dev,
98 struct device_attribute *attr, char *buf)
99{
100 u64 val = 0;
101 struct zram *zram = dev_to_zram(dev);
102 struct zram_meta *meta = zram->meta;
103
104 down_read(&zram->init_lock);
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -0700105 if (init_done(zram))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300106 val = zs_get_total_size_bytes(meta->mem_pool);
107 up_read(&zram->init_lock);
108
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -0700109 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300110}
111
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700112static ssize_t max_comp_streams_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
114{
115 int val;
116 struct zram *zram = dev_to_zram(dev);
117
118 down_read(&zram->init_lock);
119 val = zram->max_comp_streams;
120 up_read(&zram->init_lock);
121
Sergey Senozhatsky56b4e8c2014-04-07 15:38:22 -0700122 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700123}
124
125static ssize_t max_comp_streams_store(struct device *dev,
126 struct device_attribute *attr, const char *buf, size_t len)
127{
128 int num;
129 struct zram *zram = dev_to_zram(dev);
Minchan Kim60a726e2014-04-07 15:38:21 -0700130 int ret;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700131
Minchan Kim60a726e2014-04-07 15:38:21 -0700132 ret = kstrtoint(buf, 0, &num);
133 if (ret < 0)
134 return ret;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700135 if (num < 1)
136 return -EINVAL;
Minchan Kim60a726e2014-04-07 15:38:21 -0700137
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700138 down_write(&zram->init_lock);
139 if (init_done(zram)) {
Minchan Kim60a726e2014-04-07 15:38:21 -0700140 if (!zcomp_set_max_streams(zram->comp, num)) {
Sergey Senozhatskyfe8eb122014-04-07 15:38:15 -0700141 pr_info("Cannot change max compression streams\n");
Minchan Kim60a726e2014-04-07 15:38:21 -0700142 ret = -EINVAL;
143 goto out;
144 }
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700145 }
Minchan Kim60a726e2014-04-07 15:38:21 -0700146
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700147 zram->max_comp_streams = num;
Minchan Kim60a726e2014-04-07 15:38:21 -0700148 ret = len;
149out:
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700150 up_write(&zram->init_lock);
Minchan Kim60a726e2014-04-07 15:38:21 -0700151 return ret;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700152}
153
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700154static ssize_t comp_algorithm_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
157 size_t sz;
158 struct zram *zram = dev_to_zram(dev);
159
160 down_read(&zram->init_lock);
161 sz = zcomp_available_show(zram->compressor, buf);
162 up_read(&zram->init_lock);
163
164 return sz;
165}
166
167static ssize_t comp_algorithm_store(struct device *dev,
168 struct device_attribute *attr, const char *buf, size_t len)
169{
170 struct zram *zram = dev_to_zram(dev);
171 down_write(&zram->init_lock);
172 if (init_done(zram)) {
173 up_write(&zram->init_lock);
174 pr_info("Can't change algorithm for initialized device\n");
175 return -EBUSY;
176 }
177 strlcpy(zram->compressor, buf, sizeof(zram->compressor));
178 up_write(&zram->init_lock);
179 return len;
180}
181
Minchan Kim92967472014-01-30 15:46:03 -0800182/* flag operations needs meta->tb_lock */
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900183static int zram_test_flag(struct zram_meta *meta, u32 index,
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530184 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530185{
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900186 return meta->table[index].flags & BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530187}
188
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900189static void zram_set_flag(struct zram_meta *meta, u32 index,
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530190 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530191{
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900192 meta->table[index].flags |= BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530193}
194
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900195static void zram_clear_flag(struct zram_meta *meta, u32 index,
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530196 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530197{
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900198 meta->table[index].flags &= ~BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530199}
200
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300201static inline int is_partial_io(struct bio_vec *bvec)
202{
203 return bvec->bv_len != PAGE_SIZE;
204}
205
206/*
207 * Check if request is within bounds and aligned on zram logical blocks.
208 */
209static inline int valid_io_request(struct zram *zram, struct bio *bio)
210{
211 u64 start, end, bound;
Kumar Gaurava539c722013-08-08 23:53:24 +0530212
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300213 /* unaligned request */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700214 if (unlikely(bio->bi_iter.bi_sector &
215 (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300216 return 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700217 if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300218 return 0;
219
Kent Overstreet4f024f32013-10-11 15:44:27 -0700220 start = bio->bi_iter.bi_sector;
221 end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300222 bound = zram->disksize >> SECTOR_SHIFT;
223 /* out of range range */
Sergey Senozhatsky75c7caf2013-06-22 17:21:00 +0300224 if (unlikely(start >= bound || end > bound || start > end))
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300225 return 0;
226
227 /* I/O request is valid */
228 return 1;
229}
230
231static void zram_meta_free(struct zram_meta *meta)
232{
233 zs_destroy_pool(meta->mem_pool);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300234 vfree(meta->table);
235 kfree(meta);
236}
237
238static struct zram_meta *zram_meta_alloc(u64 disksize)
239{
240 size_t num_pages;
241 struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
242 if (!meta)
243 goto out;
244
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300245 num_pages = disksize >> PAGE_SHIFT;
246 meta->table = vzalloc(num_pages * sizeof(*meta->table));
247 if (!meta->table) {
248 pr_err("Error allocating zram address table\n");
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700249 goto free_meta;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300250 }
251
252 meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
253 if (!meta->mem_pool) {
254 pr_err("Error creating memory pool\n");
255 goto free_table;
256 }
257
Minchan Kim92967472014-01-30 15:46:03 -0800258 rwlock_init(&meta->tb_lock);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300259 return meta;
260
261free_table:
262 vfree(meta->table);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300263free_meta:
264 kfree(meta);
265 meta = NULL;
266out:
267 return meta;
268}
269
270static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
271{
272 if (*offset + bvec->bv_len >= PAGE_SIZE)
273 (*index)++;
274 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
275}
276
Nitin Gupta306b0c92009-09-22 10:26:53 +0530277static int page_zero_filled(void *ptr)
278{
279 unsigned int pos;
280 unsigned long *page;
281
282 page = (unsigned long *)ptr;
283
284 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
285 if (page[pos])
286 return 0;
287 }
288
289 return 1;
290}
291
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300292static void handle_zero_page(struct bio_vec *bvec)
293{
294 struct page *page = bvec->bv_page;
295 void *user_mem;
296
297 user_mem = kmap_atomic(page);
298 if (is_partial_io(bvec))
299 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
300 else
301 clear_page(user_mem);
302 kunmap_atomic(user_mem);
303
304 flush_dcache_page(page);
305}
306
Minchan Kim92967472014-01-30 15:46:03 -0800307/* NOTE: caller should hold meta->tb_lock with write-side */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530308static void zram_free_page(struct zram *zram, size_t index)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530309{
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900310 struct zram_meta *meta = zram->meta;
311 unsigned long handle = meta->table[index].handle;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530312
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600313 if (unlikely(!handle)) {
Nitin Gupta2e882282010-01-28 21:13:41 +0530314 /*
315 * No memory is allocated for zero filled pages.
316 * Simply clear zero page flag.
317 */
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900318 if (zram_test_flag(meta, index, ZRAM_ZERO)) {
319 zram_clear_flag(meta, index, ZRAM_ZERO);
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700320 atomic64_dec(&zram->stats.zero_pages);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530321 }
322 return;
323 }
324
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900325 zs_free(meta->mem_pool, handle);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530326
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700327 atomic64_sub(meta->table[index].size, &zram->stats.compr_data_size);
328 atomic64_dec(&zram->stats.pages_stored);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530329
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900330 meta->table[index].handle = 0;
331 meta->table[index].size = 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530332}
333
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300334static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530335{
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700336 int ret = 0;
Jerome Marchand924bd882011-06-10 15:28:48 +0200337 unsigned char *cmem;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900338 struct zram_meta *meta = zram->meta;
Minchan Kim92967472014-01-30 15:46:03 -0800339 unsigned long handle;
Minchan Kim023b4092014-08-06 16:08:29 -0700340 size_t size;
Minchan Kim92967472014-01-30 15:46:03 -0800341
342 read_lock(&meta->tb_lock);
343 handle = meta->table[index].handle;
344 size = meta->table[index].size;
Jerome Marchand924bd882011-06-10 15:28:48 +0200345
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900346 if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
Minchan Kim92967472014-01-30 15:46:03 -0800347 read_unlock(&meta->tb_lock);
Jiang Liu42e99bd2013-06-07 00:07:30 +0800348 clear_page(mem);
Jerome Marchand924bd882011-06-10 15:28:48 +0200349 return 0;
350 }
351
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900352 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
Minchan Kim92967472014-01-30 15:46:03 -0800353 if (size == PAGE_SIZE)
Jiang Liu42e99bd2013-06-07 00:07:30 +0800354 copy_page(mem, cmem);
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300355 else
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700356 ret = zcomp_decompress(zram->comp, cmem, size, mem);
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900357 zs_unmap_object(meta->mem_pool, handle);
Minchan Kim92967472014-01-30 15:46:03 -0800358 read_unlock(&meta->tb_lock);
Jerome Marchand924bd882011-06-10 15:28:48 +0200359
360 /* Should NEVER happen. Return bio error if it does. */
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700361 if (unlikely(ret)) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200362 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
Jiang Liuda5cc7d2013-06-07 00:07:31 +0800363 atomic64_inc(&zram->stats.failed_reads);
Jerome Marchand924bd882011-06-10 15:28:48 +0200364 return ret;
365 }
366
367 return 0;
368}
369
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300370static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
371 u32 index, int offset, struct bio *bio)
372{
373 int ret;
374 struct page *page;
375 unsigned char *user_mem, *uncmem = NULL;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900376 struct zram_meta *meta = zram->meta;
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300377 page = bvec->bv_page;
378
Minchan Kim92967472014-01-30 15:46:03 -0800379 read_lock(&meta->tb_lock);
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900380 if (unlikely(!meta->table[index].handle) ||
381 zram_test_flag(meta, index, ZRAM_ZERO)) {
Minchan Kim92967472014-01-30 15:46:03 -0800382 read_unlock(&meta->tb_lock);
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300383 handle_zero_page(bvec);
384 return 0;
385 }
Minchan Kim92967472014-01-30 15:46:03 -0800386 read_unlock(&meta->tb_lock);
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300387
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300388 if (is_partial_io(bvec))
389 /* Use a temporary buffer to decompress the page */
Minchan Kim7e5a5102013-01-30 11:41:39 +0900390 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
391
392 user_mem = kmap_atomic(page);
393 if (!is_partial_io(bvec))
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300394 uncmem = user_mem;
395
396 if (!uncmem) {
397 pr_info("Unable to allocate temp memory\n");
398 ret = -ENOMEM;
399 goto out_cleanup;
400 }
401
402 ret = zram_decompress_page(zram, uncmem, index);
403 /* Should NEVER happen. Return bio error if it does. */
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700404 if (unlikely(ret))
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300405 goto out_cleanup;
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300406
407 if (is_partial_io(bvec))
408 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
409 bvec->bv_len);
410
411 flush_dcache_page(page);
412 ret = 0;
413out_cleanup:
414 kunmap_atomic(user_mem);
415 if (is_partial_io(bvec))
416 kfree(uncmem);
417 return ret;
418}
419
Jerome Marchand924bd882011-06-10 15:28:48 +0200420static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
421 int offset)
422{
Nitin Gupta397c6062013-01-02 08:53:41 -0800423 int ret = 0;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200424 size_t clen;
Minchan Kimc2344342012-06-08 15:39:25 +0900425 unsigned long handle;
Minchan Kim130f3152012-06-08 15:39:27 +0900426 struct page *page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200427 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900428 struct zram_meta *meta = zram->meta;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700429 struct zcomp_strm *zstrm;
Minchan Kime46e3312014-01-30 15:46:06 -0800430 bool locked = false;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200431
432 page = bvec->bv_page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200433 if (is_partial_io(bvec)) {
434 /*
435 * This is a partial IO. We need to read the full page
436 * before to write the changes.
437 */
Minchan Kim7e5a5102013-01-30 11:41:39 +0900438 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
Jerome Marchand924bd882011-06-10 15:28:48 +0200439 if (!uncmem) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200440 ret = -ENOMEM;
441 goto out;
442 }
Sergey Senozhatsky37b51fd2012-10-30 22:40:23 +0300443 ret = zram_decompress_page(zram, uncmem, index);
Nitin Gupta397c6062013-01-02 08:53:41 -0800444 if (ret)
Jerome Marchand924bd882011-06-10 15:28:48 +0200445 goto out;
Jerome Marchand924bd882011-06-10 15:28:48 +0200446 }
447
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700448 zstrm = zcomp_strm_find(zram->comp);
Minchan Kime46e3312014-01-30 15:46:06 -0800449 locked = true;
Cong Wangba82fe22011-11-25 23:14:25 +0800450 user_mem = kmap_atomic(page);
Jerome Marchand924bd882011-06-10 15:28:48 +0200451
Nitin Gupta397c6062013-01-02 08:53:41 -0800452 if (is_partial_io(bvec)) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200453 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
454 bvec->bv_len);
Nitin Gupta397c6062013-01-02 08:53:41 -0800455 kunmap_atomic(user_mem);
456 user_mem = NULL;
457 } else {
Jerome Marchand924bd882011-06-10 15:28:48 +0200458 uncmem = user_mem;
Nitin Gupta397c6062013-01-02 08:53:41 -0800459 }
Jerome Marchand924bd882011-06-10 15:28:48 +0200460
461 if (page_zero_filled(uncmem)) {
Cong Wangba82fe22011-11-25 23:14:25 +0800462 kunmap_atomic(user_mem);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900463 /* Free memory associated with this sector now. */
Minchan Kim92967472014-01-30 15:46:03 -0800464 write_lock(&zram->meta->tb_lock);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900465 zram_free_page(zram, index);
Minchan Kim92967472014-01-30 15:46:03 -0800466 zram_set_flag(meta, index, ZRAM_ZERO);
467 write_unlock(&zram->meta->tb_lock);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900468
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700469 atomic64_inc(&zram->stats.zero_pages);
Jerome Marchand924bd882011-06-10 15:28:48 +0200470 ret = 0;
471 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200472 }
473
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700474 ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
Nitin Gupta397c6062013-01-02 08:53:41 -0800475 if (!is_partial_io(bvec)) {
476 kunmap_atomic(user_mem);
477 user_mem = NULL;
478 uncmem = NULL;
479 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200480
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700481 if (unlikely(ret)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200482 pr_err("Compression failed! err=%d\n", ret);
Jerome Marchand924bd882011-06-10 15:28:48 +0200483 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200484 }
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700485 src = zstrm->buffer;
Nitin Guptac8f2f0d2012-10-10 17:42:18 -0700486 if (unlikely(clen > max_zpage_size)) {
Nitin Guptac8f2f0d2012-10-10 17:42:18 -0700487 clen = PAGE_SIZE;
Nitin Gupta397c6062013-01-02 08:53:41 -0800488 if (is_partial_io(bvec))
489 src = uncmem;
Nitin Guptac8f2f0d2012-10-10 17:42:18 -0700490 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200491
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900492 handle = zs_malloc(meta->mem_pool, clen);
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600493 if (!handle) {
Marlies Ruck596b3dd2013-05-16 14:30:39 -0400494 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
495 index, clen);
Jerome Marchand924bd882011-06-10 15:28:48 +0200496 ret = -ENOMEM;
497 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200498 }
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900499 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200500
Jiang Liu42e99bd2013-06-07 00:07:30 +0800501 if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
Nitin Gupta397c6062013-01-02 08:53:41 -0800502 src = kmap_atomic(page);
Jiang Liu42e99bd2013-06-07 00:07:30 +0800503 copy_page(cmem, src);
Nitin Gupta397c6062013-01-02 08:53:41 -0800504 kunmap_atomic(src);
Jiang Liu42e99bd2013-06-07 00:07:30 +0800505 } else {
506 memcpy(cmem, src, clen);
507 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200508
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700509 zcomp_strm_release(zram->comp, zstrm);
510 locked = false;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900511 zs_unmap_object(meta->mem_pool, handle);
Nitin Guptafd1a30d2012-01-09 16:51:59 -0600512
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900513 /*
514 * Free memory associated with this sector
515 * before overwriting unused sectors.
516 */
Minchan Kim92967472014-01-30 15:46:03 -0800517 write_lock(&zram->meta->tb_lock);
Sunghan Suhf40ac2a2013-07-03 20:10:05 +0900518 zram_free_page(zram, index);
519
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900520 meta->table[index].handle = handle;
521 meta->table[index].size = clen;
Minchan Kim92967472014-01-30 15:46:03 -0800522 write_unlock(&zram->meta->tb_lock);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200523
524 /* Update stats */
Sergey Senozhatsky90a78062014-04-07 15:38:03 -0700525 atomic64_add(clen, &zram->stats.compr_data_size);
526 atomic64_inc(&zram->stats.pages_stored);
Jerome Marchand924bd882011-06-10 15:28:48 +0200527out:
Minchan Kime46e3312014-01-30 15:46:06 -0800528 if (locked)
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700529 zcomp_strm_release(zram->comp, zstrm);
Nitin Gupta397c6062013-01-02 08:53:41 -0800530 if (is_partial_io(bvec))
531 kfree(uncmem);
Jerome Marchand924bd882011-06-10 15:28:48 +0200532 if (ret)
Jiang Liuda5cc7d2013-06-07 00:07:31 +0800533 atomic64_inc(&zram->stats.failed_writes);
Jerome Marchand924bd882011-06-10 15:28:48 +0200534 return ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200535}
536
537static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700538 int offset, struct bio *bio)
Jerome Marchand8c921b22011-06-10 15:28:47 +0200539{
Jerome Marchandc5bde232011-06-10 15:28:49 +0200540 int ret;
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700541 int rw = bio_data_dir(bio);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200542
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700543 if (rw == READ) {
544 atomic64_inc(&zram->stats.num_reads);
Jerome Marchandc5bde232011-06-10 15:28:49 +0200545 ret = zram_bvec_read(zram, bvec, index, offset, bio);
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700546 } else {
547 atomic64_inc(&zram->stats.num_writes);
Jerome Marchandc5bde232011-06-10 15:28:49 +0200548 ret = zram_bvec_write(zram, bvec, index, offset);
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700549 }
Jerome Marchandc5bde232011-06-10 15:28:49 +0200550
551 return ret;
Jerome Marchand924bd882011-06-10 15:28:48 +0200552}
553
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700554/*
555 * zram_bio_discard - handler on discard request
556 * @index: physical block index in PAGE_SIZE units
557 * @offset: byte offset within physical block
558 */
559static void zram_bio_discard(struct zram *zram, u32 index,
560 int offset, struct bio *bio)
561{
562 size_t n = bio->bi_iter.bi_size;
563
564 /*
565 * zram manages data in physical block size units. Because logical block
566 * size isn't identical with physical block size on some arch, we
567 * could get a discard request pointing to a specific offset within a
568 * certain physical block. Although we can handle this request by
569 * reading that physiclal block and decompressing and partially zeroing
570 * and re-compressing and then re-storing it, this isn't reasonable
571 * because our intent with a discard request is to save memory. So
572 * skipping this logical block is appropriate here.
573 */
574 if (offset) {
Weijie Yang38515c72014-06-04 16:11:06 -0700575 if (n <= (PAGE_SIZE - offset))
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700576 return;
577
Weijie Yang38515c72014-06-04 16:11:06 -0700578 n -= (PAGE_SIZE - offset);
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700579 index++;
580 }
581
582 while (n >= PAGE_SIZE) {
583 /*
584 * Discard request can be large so the lock hold times could be
585 * lengthy. So take the lock once per page.
586 */
587 write_lock(&zram->meta->tb_lock);
588 zram_free_page(zram, index);
589 write_unlock(&zram->meta->tb_lock);
590 index++;
591 n -= PAGE_SIZE;
592 }
593}
594
Minchan Kim2b86ab92013-08-12 15:13:55 +0900595static void zram_reset_device(struct zram *zram, bool reset_capacity)
Jerome Marchand924bd882011-06-10 15:28:48 +0200596{
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300597 size_t index;
598 struct zram_meta *meta;
599
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300600 down_write(&zram->init_lock);
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -0700601 if (!init_done(zram)) {
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300602 up_write(&zram->init_lock);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300603 return;
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300604 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300605
606 meta = zram->meta;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300607 /* Free all pages that are still in this zram device */
608 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
609 unsigned long handle = meta->table[index].handle;
610 if (!handle)
611 continue;
612
613 zs_free(meta->mem_pool, handle);
614 }
615
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700616 zcomp_destroy(zram->comp);
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700617 zram->max_comp_streams = 1;
618
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300619 zram_meta_free(zram->meta);
620 zram->meta = NULL;
621 /* Reset stats */
622 memset(&zram->stats, 0, sizeof(zram->stats));
623
624 zram->disksize = 0;
Minchan Kimb4c5c602014-07-23 14:00:04 -0700625 if (reset_capacity)
Minchan Kim2b86ab92013-08-12 15:13:55 +0900626 set_capacity(zram->disk, 0);
Minchan Kimb4c5c602014-07-23 14:00:04 -0700627
Sergey Senozhatsky644d4782013-06-26 15:28:39 +0300628 up_write(&zram->init_lock);
Minchan Kimb4c5c602014-07-23 14:00:04 -0700629
630 /*
631 * Revalidate disk out of the init_lock to avoid lockdep splat.
632 * It's okay because disk's capacity is protected by init_lock
633 * so that revalidate_disk always sees up-to-date capacity.
634 */
635 if (reset_capacity)
636 revalidate_disk(zram->disk);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300637}
638
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300639static ssize_t disksize_store(struct device *dev,
640 struct device_attribute *attr, const char *buf, size_t len)
641{
642 u64 disksize;
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700643 struct zcomp *comp;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300644 struct zram_meta *meta;
645 struct zram *zram = dev_to_zram(dev);
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700646 int err;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300647
648 disksize = memparse(buf, NULL);
649 if (!disksize)
650 return -EINVAL;
651
652 disksize = PAGE_ALIGN(disksize);
653 meta = zram_meta_alloc(disksize);
Minchan Kimdb5d7112014-03-03 15:38:34 -0800654 if (!meta)
655 return -ENOMEM;
Sergey Senozhatskyb67d1ec2014-04-07 15:38:09 -0700656
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700657 comp = zcomp_create(zram->compressor, zram->max_comp_streams);
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700658 if (IS_ERR(comp)) {
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700659 pr_info("Cannot initialise %s compressing backend\n",
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700660 zram->compressor);
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700661 err = PTR_ERR(comp);
662 goto out_free_meta;
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700663 }
664
665 down_write(&zram->init_lock);
666 if (init_done(zram)) {
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700667 pr_info("Cannot change disksize for initialized device\n");
668 err = -EBUSY;
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700669 goto out_destroy_comp;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300670 }
671
Sergey Senozhatskyb67d1ec2014-04-07 15:38:09 -0700672 zram->meta = meta;
Sergey Senozhatskyd61f98c2014-04-07 15:38:19 -0700673 zram->comp = comp;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300674 zram->disksize = disksize;
675 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300676 up_write(&zram->init_lock);
Minchan Kimb4c5c602014-07-23 14:00:04 -0700677
678 /*
679 * Revalidate disk out of the init_lock to avoid lockdep splat.
680 * It's okay because disk's capacity is protected by init_lock
681 * so that revalidate_disk always sees up-to-date capacity.
682 */
683 revalidate_disk(zram->disk);
684
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300685 return len;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700686
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700687out_destroy_comp:
688 up_write(&zram->init_lock);
689 zcomp_destroy(comp);
690out_free_meta:
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700691 zram_meta_free(meta);
692 return err;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300693}
694
695static ssize_t reset_store(struct device *dev,
696 struct device_attribute *attr, const char *buf, size_t len)
697{
698 int ret;
699 unsigned short do_reset;
700 struct zram *zram;
701 struct block_device *bdev;
702
703 zram = dev_to_zram(dev);
704 bdev = bdget_disk(zram->disk, 0);
705
Rashika Kheria46a51c82013-10-30 18:36:32 +0530706 if (!bdev)
707 return -ENOMEM;
708
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300709 /* Do not reset an active device! */
Rashika Kheria1b672222013-11-10 22:13:53 +0530710 if (bdev->bd_holders) {
711 ret = -EBUSY;
712 goto out;
713 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300714
715 ret = kstrtou16(buf, 10, &do_reset);
716 if (ret)
Rashika Kheria1b672222013-11-10 22:13:53 +0530717 goto out;
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300718
Rashika Kheria1b672222013-11-10 22:13:53 +0530719 if (!do_reset) {
720 ret = -EINVAL;
721 goto out;
722 }
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300723
724 /* Make sure all pending I/O is finished */
Rashika Kheria46a51c82013-10-30 18:36:32 +0530725 fsync_bdev(bdev);
Rashika Kheria1b672222013-11-10 22:13:53 +0530726 bdput(bdev);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300727
Minchan Kim2b86ab92013-08-12 15:13:55 +0900728 zram_reset_device(zram, true);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300729 return len;
Rashika Kheria1b672222013-11-10 22:13:53 +0530730
731out:
732 bdput(bdev);
733 return ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200734}
735
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700736static void __zram_make_request(struct zram *zram, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530737{
Kent Overstreet79886132013-11-23 17:19:00 -0800738 int offset;
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530739 u32 index;
Kent Overstreet79886132013-11-23 17:19:00 -0800740 struct bio_vec bvec;
741 struct bvec_iter iter;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530742
Kent Overstreet4f024f32013-10-11 15:44:27 -0700743 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
744 offset = (bio->bi_iter.bi_sector &
745 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530746
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700747 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
748 zram_bio_discard(zram, index, offset, bio);
749 bio_endio(bio, 0);
750 return;
751 }
752
Kent Overstreet79886132013-11-23 17:19:00 -0800753 bio_for_each_segment(bvec, bio, iter) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200754 int max_transfer_size = PAGE_SIZE - offset;
755
Kent Overstreet79886132013-11-23 17:19:00 -0800756 if (bvec.bv_len > max_transfer_size) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200757 /*
758 * zram_bvec_rw() can only make operation on a single
759 * zram page. Split the bio vector.
760 */
761 struct bio_vec bv;
762
Kent Overstreet79886132013-11-23 17:19:00 -0800763 bv.bv_page = bvec.bv_page;
Jerome Marchand924bd882011-06-10 15:28:48 +0200764 bv.bv_len = max_transfer_size;
Kent Overstreet79886132013-11-23 17:19:00 -0800765 bv.bv_offset = bvec.bv_offset;
Jerome Marchand924bd882011-06-10 15:28:48 +0200766
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700767 if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0)
Jerome Marchand924bd882011-06-10 15:28:48 +0200768 goto out;
769
Kent Overstreet79886132013-11-23 17:19:00 -0800770 bv.bv_len = bvec.bv_len - max_transfer_size;
Jerome Marchand924bd882011-06-10 15:28:48 +0200771 bv.bv_offset += max_transfer_size;
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700772 if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0)
Jerome Marchand924bd882011-06-10 15:28:48 +0200773 goto out;
774 } else
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700775 if (zram_bvec_rw(zram, &bvec, index, offset, bio) < 0)
Jerome Marchand924bd882011-06-10 15:28:48 +0200776 goto out;
777
Kent Overstreet79886132013-11-23 17:19:00 -0800778 update_position(&index, &offset, &bvec);
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530779 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530780
781 set_bit(BIO_UPTODATE, &bio->bi_flags);
782 bio_endio(bio, 0);
Nitin Gupta7d7854b2011-01-22 07:36:15 -0500783 return;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530784
785out:
Nitin Gupta306b0c92009-09-22 10:26:53 +0530786 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530787}
788
Nitin Gupta306b0c92009-09-22 10:26:53 +0530789/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530790 * Handler function for all zram I/O requests.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530791 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200792static void zram_make_request(struct request_queue *queue, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530793{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530794 struct zram *zram = queue->queuedata;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530795
Jerome Marchand0900bea2011-09-06 15:02:11 +0200796 down_read(&zram->init_lock);
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -0700797 if (unlikely(!init_done(zram)))
Minchan Kim3de738c2013-01-30 11:41:41 +0900798 goto error;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200799
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530800 if (!valid_io_request(zram, bio)) {
Jiang Liuda5cc7d2013-06-07 00:07:31 +0800801 atomic64_inc(&zram->stats.invalid_io);
Minchan Kim3de738c2013-01-30 11:41:41 +0900802 goto error;
Jerome Marchand6642a672011-02-17 17:11:49 +0100803 }
804
Sergey Senozhatskybe257c62014-04-07 15:38:01 -0700805 __zram_make_request(zram, bio);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200806 up_read(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530807
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700808 return;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200809
Jerome Marchand0900bea2011-09-06 15:02:11 +0200810error:
Minchan Kim3de738c2013-01-30 11:41:41 +0900811 up_read(&zram->init_lock);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200812 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530813}
814
Nitin Gupta2ccbec02011-09-09 19:01:00 -0400815static void zram_slot_free_notify(struct block_device *bdev,
816 unsigned long index)
Nitin Gupta107c1612010-05-17 11:02:44 +0530817{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530818 struct zram *zram;
Minchan Kimf614a9f2014-01-30 15:46:04 -0800819 struct zram_meta *meta;
Nitin Gupta107c1612010-05-17 11:02:44 +0530820
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530821 zram = bdev->bd_disk->private_data;
Minchan Kimf614a9f2014-01-30 15:46:04 -0800822 meta = zram->meta;
823
824 write_lock(&meta->tb_lock);
825 zram_free_page(zram, index);
826 write_unlock(&meta->tb_lock);
Jiang Liuda5cc7d2013-06-07 00:07:31 +0800827 atomic64_inc(&zram->stats.notify_free);
Nitin Gupta107c1612010-05-17 11:02:44 +0530828}
829
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530830static const struct block_device_operations zram_devops = {
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530831 .swap_slot_free_notify = zram_slot_free_notify,
Nitin Gupta107c1612010-05-17 11:02:44 +0530832 .owner = THIS_MODULE
Nitin Gupta306b0c92009-09-22 10:26:53 +0530833};
834
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300835static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
836 disksize_show, disksize_store);
837static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
838static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300839static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300840static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700841static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
842 max_comp_streams_show, max_comp_streams_store);
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700843static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
844 comp_algorithm_show, comp_algorithm_store);
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300845
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -0700846ZRAM_ATTR_RO(num_reads);
847ZRAM_ATTR_RO(num_writes);
Sergey Senozhatsky64447242014-04-07 15:38:05 -0700848ZRAM_ATTR_RO(failed_reads);
849ZRAM_ATTR_RO(failed_writes);
Sergey Senozhatskya68eb3b2014-04-07 15:38:04 -0700850ZRAM_ATTR_RO(invalid_io);
851ZRAM_ATTR_RO(notify_free);
852ZRAM_ATTR_RO(zero_pages);
853ZRAM_ATTR_RO(compr_data_size);
854
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300855static struct attribute *zram_disk_attrs[] = {
856 &dev_attr_disksize.attr,
857 &dev_attr_initstate.attr,
858 &dev_attr_reset.attr,
859 &dev_attr_num_reads.attr,
860 &dev_attr_num_writes.attr,
Sergey Senozhatsky64447242014-04-07 15:38:05 -0700861 &dev_attr_failed_reads.attr,
862 &dev_attr_failed_writes.attr,
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300863 &dev_attr_invalid_io.attr,
864 &dev_attr_notify_free.attr,
865 &dev_attr_zero_pages.attr,
866 &dev_attr_orig_data_size.attr,
867 &dev_attr_compr_data_size.attr,
868 &dev_attr_mem_used_total.attr,
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700869 &dev_attr_max_comp_streams.attr,
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700870 &dev_attr_comp_algorithm.attr,
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +0300871 NULL,
872};
873
874static struct attribute_group zram_disk_attr_group = {
875 .attrs = zram_disk_attrs,
876};
877
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530878static int create_device(struct zram *zram, int device_id)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530879{
Jiang Liu39a9b8a2013-06-07 00:07:24 +0800880 int ret = -ENOMEM;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530881
Jerome Marchand0900bea2011-09-06 15:02:11 +0200882 init_rwsem(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530883
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530884 zram->queue = blk_alloc_queue(GFP_KERNEL);
885 if (!zram->queue) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530886 pr_err("Error allocating disk queue for device %d\n",
887 device_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530888 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530889 }
890
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530891 blk_queue_make_request(zram->queue, zram_make_request);
892 zram->queue->queuedata = zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530893
894 /* gendisk structure */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530895 zram->disk = alloc_disk(1);
896 if (!zram->disk) {
Sam Hansen94b84352012-06-07 16:03:47 -0700897 pr_warn("Error allocating disk structure for device %d\n",
Nitin Gupta306b0c92009-09-22 10:26:53 +0530898 device_id);
Jiang Liu39a9b8a2013-06-07 00:07:24 +0800899 goto out_free_queue;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530900 }
901
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530902 zram->disk->major = zram_major;
903 zram->disk->first_minor = device_id;
904 zram->disk->fops = &zram_devops;
905 zram->disk->queue = zram->queue;
906 zram->disk->private_data = zram;
907 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530908
Nitin Gupta33863c22010-08-09 22:56:47 +0530909 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530910 set_capacity(zram->disk, 0);
Sergey Senozhatskyb67d1ec2014-04-07 15:38:09 -0700911 /* zram devices sort of resembles non-rotational disks */
912 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530913 /*
914 * To ensure that we always get PAGE_SIZE aligned
915 * and n*PAGE_SIZED sized I/O requests.
916 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530917 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
Robert Jennings7b19b8d2011-01-28 08:58:17 -0600918 blk_queue_logical_block_size(zram->disk->queue,
919 ZRAM_LOGICAL_BLOCK_SIZE);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530920 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
921 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
Joonsoo Kimf4659d82014-04-07 15:38:24 -0700922 zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
923 zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
924 /*
925 * zram_bio_discard() will clear all logical blocks if logical block
926 * size is identical with physical block size(PAGE_SIZE). But if it is
927 * different, we will skip discarding some parts of logical blocks in
928 * the part of the request range which isn't aligned to physical block
929 * size. So we can't ensure that all discarded logical blocks are
930 * zeroed.
931 */
932 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
933 zram->disk->queue->limits.discard_zeroes_data = 1;
934 else
935 zram->disk->queue->limits.discard_zeroes_data = 0;
936 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
Nitin Gupta5d83d5a2010-01-28 21:13:39 +0530937
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530938 add_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530939
Nitin Gupta33863c22010-08-09 22:56:47 +0530940 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
941 &zram_disk_attr_group);
942 if (ret < 0) {
Sam Hansen94b84352012-06-07 16:03:47 -0700943 pr_warn("Error creating sysfs group");
Jiang Liu39a9b8a2013-06-07 00:07:24 +0800944 goto out_free_disk;
Nitin Gupta33863c22010-08-09 22:56:47 +0530945 }
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700946 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
Sergey Senozhatskybe2d1d52014-04-07 15:38:00 -0700947 zram->meta = NULL;
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700948 zram->max_comp_streams = 1;
Jiang Liu39a9b8a2013-06-07 00:07:24 +0800949 return 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530950
Jiang Liu39a9b8a2013-06-07 00:07:24 +0800951out_free_disk:
952 del_gendisk(zram->disk);
953 put_disk(zram->disk);
954out_free_queue:
955 blk_cleanup_queue(zram->queue);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530956out:
957 return ret;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530958}
959
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530960static void destroy_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530961{
Nitin Gupta33863c22010-08-09 22:56:47 +0530962 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
963 &zram_disk_attr_group);
Nitin Gupta33863c22010-08-09 22:56:47 +0530964
Rashika Kheria59d3fe52013-10-30 18:43:32 +0530965 del_gendisk(zram->disk);
966 put_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530967
Rashika Kheria59d3fe52013-10-30 18:43:32 +0530968 blk_cleanup_queue(zram->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530969}
970
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530971static int __init zram_init(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530972{
Nitin Guptade1a21a2010-01-28 21:13:40 +0530973 int ret, dev_id;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530974
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500975 if (num_devices > max_num_devices) {
Sam Hansen94b84352012-06-07 16:03:47 -0700976 pr_warn("Invalid value for num_devices: %u\n",
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500977 num_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530978 ret = -EINVAL;
979 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530980 }
981
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530982 zram_major = register_blkdev(0, "zram");
983 if (zram_major <= 0) {
Sam Hansen94b84352012-06-07 16:03:47 -0700984 pr_warn("Unable to get major number\n");
Nitin Guptade1a21a2010-01-28 21:13:40 +0530985 ret = -EBUSY;
986 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530987 }
988
Nitin Gupta306b0c92009-09-22 10:26:53 +0530989 /* Allocate the device array and initialize each one */
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500990 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
Noah Watkins43801f62011-07-20 17:05:57 -0600991 if (!zram_devices) {
Nitin Guptade1a21a2010-01-28 21:13:40 +0530992 ret = -ENOMEM;
993 goto unregister;
994 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530995
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500996 for (dev_id = 0; dev_id < num_devices; dev_id++) {
Noah Watkins43801f62011-07-20 17:05:57 -0600997 ret = create_device(&zram_devices[dev_id], dev_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530998 if (ret)
Minchan Kim3bf040c2010-01-11 16:15:53 +0900999 goto free_devices;
Nitin Guptade1a21a2010-01-28 21:13:40 +05301000 }
1001
Davidlohr Buesoca3d70b2013-01-01 21:24:13 -08001002 pr_info("Created %u device(s) ...\n", num_devices);
1003
Nitin Gupta306b0c92009-09-22 10:26:53 +05301004 return 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +05301005
Minchan Kim3bf040c2010-01-11 16:15:53 +09001006free_devices:
Nitin Guptade1a21a2010-01-28 21:13:40 +05301007 while (dev_id)
Noah Watkins43801f62011-07-20 17:05:57 -06001008 destroy_device(&zram_devices[--dev_id]);
1009 kfree(zram_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +05301010unregister:
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301011 unregister_blkdev(zram_major, "zram");
Nitin Guptade1a21a2010-01-28 21:13:40 +05301012out:
Nitin Gupta306b0c92009-09-22 10:26:53 +05301013 return ret;
1014}
1015
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301016static void __exit zram_exit(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +05301017{
1018 int i;
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301019 struct zram *zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +05301020
Nitin Gupta5fa5a902012-02-12 23:04:45 -05001021 for (i = 0; i < num_devices; i++) {
Noah Watkins43801f62011-07-20 17:05:57 -06001022 zram = &zram_devices[i];
Nitin Gupta306b0c92009-09-22 10:26:53 +05301023
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301024 destroy_device(zram);
Minchan Kim2b86ab92013-08-12 15:13:55 +09001025 /*
1026 * Shouldn't access zram->disk after destroy_device
1027 * because destroy_device already released zram->disk.
1028 */
1029 zram_reset_device(zram, false);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301030 }
1031
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301032 unregister_blkdev(zram_major, "zram");
Nitin Gupta306b0c92009-09-22 10:26:53 +05301033
Noah Watkins43801f62011-07-20 17:05:57 -06001034 kfree(zram_devices);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301035 pr_debug("Cleanup done!\n");
1036}
1037
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301038module_init(zram_init);
1039module_exit(zram_exit);
Nitin Gupta306b0c92009-09-22 10:26:53 +05301040
Sergey Senozhatsky9b3bb7a2013-06-22 03:21:18 +03001041module_param(num_devices, uint, 0);
1042MODULE_PARM_DESC(num_devices, "Number of zram devices");
1043
Nitin Gupta306b0c92009-09-22 10:26:53 +05301044MODULE_LICENSE("Dual BSD/GPL");
1045MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05301046MODULE_DESCRIPTION("Compressed RAM Block Device");