15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 #ifdef CONFIG_ZRAM_DEBUG
22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/bitops.h>
28 #include <linux/device.h>
31 #include <linux/slab.h>
33 #include <linux/string.h>
39 static int zram_major;
45 static void zram_stat_inc(
u32 *
v)
50 static void zram_stat_dec(
u32 *
v)
71 zram_stat64_add(zram, v, 1);
92 static int page_zero_filled(
void *
ptr)
97 page = (
unsigned long *)ptr;
99 for (pos = 0; pos !=
PAGE_SIZE /
sizeof(*page); pos++) {
107 static void zram_set_disksize(
struct zram *
zram,
size_t totalram_bytes)
111 "disk size not provided. You can use disksize_kb module "
112 "param to specify size.\nUsing default: (%u%% of RAM).\n",
113 default_disksize_perc_ram
115 zram->
disksize = default_disksize_perc_ram *
116 (totalram_bytes / 100);
119 if (zram->
disksize > 2 * (totalram_bytes)) {
121 "There is little point creating a zram of greater than "
122 "twice the size of memory since we expect a 2:1 compression "
123 "ratio. Note that zram uses about 0.1%% of the size of "
124 "the disk when not in use so a huge zram is "
126 "\tMemory Size: %zu kB\n"
127 "\tSize you selected: %llu kB\n"
128 "Continuing anyway ...\n",
129 totalram_bytes >> 10, zram->
disksize
136 static void zram_free_page(
struct zram *zram,
size_t index)
146 if (zram_test_flag(zram, index,
ZRAM_ZERO)) {
148 zram_stat_dec(&zram->
stats.pages_zero);
153 if (
unlikely(size > max_zpage_size))
154 zram_stat_dec(&zram->
stats.bad_compress);
159 zram_stat_dec(&zram->
stats.good_compress);
161 zram_stat64_sub(zram, &zram->
stats.compr_size,
162 zram->
table[index].size);
163 zram_stat_dec(&zram->
stats.pages_stored);
169 static void handle_zero_page(
struct bio_vec *bvec)
171 struct page *page = bvec->bv_page;
175 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
181 static inline int is_partial_io(
struct bio_vec *bvec)
186 static int zram_bvec_read(
struct zram *zram,
struct bio_vec *bvec,
192 unsigned char *user_mem, *cmem, *uncmem =
NULL;
194 page = bvec->bv_page;
196 if (zram_test_flag(zram, index,
ZRAM_ZERO)) {
197 handle_zero_page(bvec);
203 pr_debug(
"Read before write: sector=%lu, size=%u",
204 (
ulong)(bio->bi_sector), bio->bi_size);
205 handle_zero_page(bvec);
209 if (is_partial_io(bvec)) {
213 pr_info(
"Error allocating temp memory!\n");
219 if (!is_partial_io(bvec))
234 if (is_partial_io(bvec)) {
235 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
245 pr_err(
"Decompression failed! err=%d, page=%u\n", ret, index);
246 zram_stat64_inc(zram, &zram->
stats.failed_reads);
255 static int zram_read_before_write(
struct zram *zram,
char *
mem,
u32 index)
260 unsigned long handle = zram->
table[
index].handle;
262 if (zram_test_flag(zram, index,
ZRAM_ZERO) || !handle) {
274 pr_err(
"Decompression failed! err=%d, page=%u\n", ret, index);
275 zram_stat64_inc(zram, &zram->
stats.failed_reads);
282 static int zram_bvec_write(
struct zram *zram,
struct bio_vec *bvec,
u32 index,
289 unsigned char *user_mem, *cmem, *
src, *uncmem =
NULL;
291 page = bvec->bv_page;
294 if (is_partial_io(bvec)) {
301 pr_info(
"Error allocating temp memory!\n");
305 ret = zram_read_before_write(zram, uncmem, index);
316 if (zram->
table[index].handle ||
318 zram_free_page(zram, index);
322 if (is_partial_io(bvec))
323 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
328 if (page_zero_filled(uncmem)) {
330 if (is_partial_io(bvec))
332 zram_stat_inc(&zram->
stats.pages_zero);
342 if (is_partial_io(bvec))
346 pr_err(
"Compression failed! err=%d\n", ret);
350 if (
unlikely(clen > max_zpage_size)) {
351 zram_stat_inc(&zram->
stats.bad_compress);
358 pr_info(
"Error allocating memory for compressed "
359 "page: %u, size=%zu\n", index, clen);
373 zram_stat64_add(zram, &zram->
stats.compr_size, clen);
374 zram_stat_inc(&zram->
stats.pages_stored);
376 zram_stat_inc(&zram->
stats.good_compress);
382 zram_stat64_inc(zram, &zram->
stats.failed_writes);
386 static int zram_bvec_rw(
struct zram *zram,
struct bio_vec *bvec,
u32 index,
387 int offset,
struct bio *bio,
int rw)
393 ret = zram_bvec_read(zram, bvec, index, offset, bio);
397 ret = zram_bvec_write(zram, bvec, index, offset);
404 static void update_position(
u32 *index,
int *offset,
struct bio_vec *bvec)
408 *offset = (*offset + bvec->bv_len) %
PAGE_SIZE;
411 static void __zram_make_request(
struct zram *zram,
struct bio *bio,
int rw)
415 struct bio_vec *bvec;
419 zram_stat64_inc(zram, &zram->
stats.num_reads);
422 zram_stat64_inc(zram, &zram->
stats.num_writes);
429 bio_for_each_segment(bvec, bio, i) {
432 if (bvec->bv_len > max_transfer_size) {
439 bv.bv_page = bvec->bv_page;
441 bv.bv_offset = bvec->bv_offset;
443 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
448 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
451 if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
455 update_position(&index, &offset, bvec);
458 set_bit(BIO_UPTODATE, &bio->bi_flags);
469 static inline int valid_io_request(
struct zram *zram,
struct bio *bio)
488 struct zram *zram = queue->queuedata;
497 if (!valid_io_request(zram, bio)) {
498 zram_stat64_inc(zram, &zram->
stats.invalid_io);
502 __zram_make_request(zram, bio, bio_data_dir(bio));
528 unsigned long handle = zram->
table[
index].handle;
566 zram_set_disksize(zram, totalram_pages <<
PAGE_SHIFT);
570 pr_err(
"Error allocating compressor working memory!\n");
578 pr_err(
"Error allocating compressor buffer space\n");
586 pr_err(
"Error allocating zram address table\n");
594 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->
disk->queue);
598 pr_err(
"Error creating memory pool\n");
615 pr_err(
"Initialization failed: err=%d\n", ret);
619 static void zram_slot_free_notify(
struct block_device *bdev,
624 zram = bdev->
bd_disk->private_data;
625 zram_free_page(zram, index);
626 zram_stat64_inc(zram, &zram->
stats.notify_free);
629 static const struct block_device_operations zram_devops = {
630 .swap_slot_free_notify = zram_slot_free_notify,
634 static int create_device(
struct zram *zram,
int device_id)
644 pr_err(
"Error allocating disk queue for device %d\n",
651 zram->
queue->queuedata = zram;
657 pr_warn(
"Error allocating disk structure for device %d\n",
663 zram->
disk->major = zram_major;
665 zram->
disk->fops = &zram_devops;
667 zram->
disk->private_data = zram;
668 snprintf(zram->
disk->disk_name, 16,
"zram%d", device_id);
671 set_capacity(zram->
disk, 0);
688 pr_warn(
"Error creating sysfs group");
698 static void destroy_device(
struct zram *zram)
717 static int __init zram_init(
void)
722 pr_warn(
"Invalid value for num_devices: %u\n",
729 if (zram_major <= 0) {
730 pr_warn(
"Unable to get major number\n");
736 pr_info(
"num_devices not specified. Using default: 1\n");
749 ret = create_device(&zram_devices[dev_id], dev_id);
758 destroy_device(&zram_devices[--dev_id]);
766 static void __exit zram_exit(
void)
772 zram = &zram_devices[
i];
774 destroy_device(zram);