xref: /openbmc/linux/drivers/block/zram/zram_drv.c (revision 33fb626b)
1 /*
2  * Compressed RAM block device
3  *
4  * Copyright (C) 2008, 2009, 2010  Nitin Gupta
5  *               2012, 2013 Minchan Kim
6  *
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  *
13  */
14 
15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
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>
27 #include <linux/slab.h>
28 #include <linux/backing-dev.h>
29 #include <linux/string.h>
30 #include <linux/vmalloc.h>
31 #include <linux/err.h>
32 #include <linux/idr.h>
33 #include <linux/sysfs.h>
34 #include <linux/debugfs.h>
35 #include <linux/cpuhotplug.h>
36 #include <linux/part_stat.h>
37 
38 #include "zram_drv.h"
39 
40 static DEFINE_IDR(zram_index_idr);
41 /* idr index must be protected */
42 static DEFINE_MUTEX(zram_index_mutex);
43 
44 static int zram_major;
45 static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
46 
47 /* Module params (documentation at end) */
48 static unsigned int num_devices = 1;
49 /*
50  * Pages that compress to sizes equals or greater than this are stored
51  * uncompressed in memory.
52  */
53 static size_t huge_class_size;
54 
55 static const struct block_device_operations zram_devops;
56 static const struct block_device_operations zram_wb_devops;
57 
58 static void zram_free_page(struct zram *zram, size_t index);
59 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
60 				u32 index, int offset, struct bio *bio);
61 
62 
63 static int zram_slot_trylock(struct zram *zram, u32 index)
64 {
65 	return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags);
66 }
67 
68 static void zram_slot_lock(struct zram *zram, u32 index)
69 {
70 	bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags);
71 }
72 
73 static void zram_slot_unlock(struct zram *zram, u32 index)
74 {
75 	bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags);
76 }
77 
78 static inline bool init_done(struct zram *zram)
79 {
80 	return zram->disksize;
81 }
82 
83 static inline struct zram *dev_to_zram(struct device *dev)
84 {
85 	return (struct zram *)dev_to_disk(dev)->private_data;
86 }
87 
88 static unsigned long zram_get_handle(struct zram *zram, u32 index)
89 {
90 	return zram->table[index].handle;
91 }
92 
93 static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
94 {
95 	zram->table[index].handle = handle;
96 }
97 
98 /* flag operations require table entry bit_spin_lock() being held */
99 static bool zram_test_flag(struct zram *zram, u32 index,
100 			enum zram_pageflags flag)
101 {
102 	return zram->table[index].flags & BIT(flag);
103 }
104 
105 static void zram_set_flag(struct zram *zram, u32 index,
106 			enum zram_pageflags flag)
107 {
108 	zram->table[index].flags |= BIT(flag);
109 }
110 
111 static void zram_clear_flag(struct zram *zram, u32 index,
112 			enum zram_pageflags flag)
113 {
114 	zram->table[index].flags &= ~BIT(flag);
115 }
116 
117 static inline void zram_set_element(struct zram *zram, u32 index,
118 			unsigned long element)
119 {
120 	zram->table[index].element = element;
121 }
122 
123 static unsigned long zram_get_element(struct zram *zram, u32 index)
124 {
125 	return zram->table[index].element;
126 }
127 
128 static size_t zram_get_obj_size(struct zram *zram, u32 index)
129 {
130 	return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
131 }
132 
133 static void zram_set_obj_size(struct zram *zram,
134 					u32 index, size_t size)
135 {
136 	unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
137 
138 	zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
139 }
140 
141 static inline bool zram_allocated(struct zram *zram, u32 index)
142 {
143 	return zram_get_obj_size(zram, index) ||
144 			zram_test_flag(zram, index, ZRAM_SAME) ||
145 			zram_test_flag(zram, index, ZRAM_WB);
146 }
147 
148 #if PAGE_SIZE != 4096
149 static inline bool is_partial_io(struct bio_vec *bvec)
150 {
151 	return bvec->bv_len != PAGE_SIZE;
152 }
153 #else
154 static inline bool is_partial_io(struct bio_vec *bvec)
155 {
156 	return false;
157 }
158 #endif
159 
160 /*
161  * Check if request is within bounds and aligned on zram logical blocks.
162  */
163 static inline bool valid_io_request(struct zram *zram,
164 		sector_t start, unsigned int size)
165 {
166 	u64 end, bound;
167 
168 	/* unaligned request */
169 	if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
170 		return false;
171 	if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
172 		return false;
173 
174 	end = start + (size >> SECTOR_SHIFT);
175 	bound = zram->disksize >> SECTOR_SHIFT;
176 	/* out of range range */
177 	if (unlikely(start >= bound || end > bound || start > end))
178 		return false;
179 
180 	/* I/O request is valid */
181 	return true;
182 }
183 
184 static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
185 {
186 	*index  += (*offset + bvec->bv_len) / PAGE_SIZE;
187 	*offset = (*offset + bvec->bv_len) % PAGE_SIZE;
188 }
189 
190 static inline void update_used_max(struct zram *zram,
191 					const unsigned long pages)
192 {
193 	unsigned long old_max, cur_max;
194 
195 	old_max = atomic_long_read(&zram->stats.max_used_pages);
196 
197 	do {
198 		cur_max = old_max;
199 		if (pages > cur_max)
200 			old_max = atomic_long_cmpxchg(
201 				&zram->stats.max_used_pages, cur_max, pages);
202 	} while (old_max != cur_max);
203 }
204 
205 static inline void zram_fill_page(void *ptr, unsigned long len,
206 					unsigned long value)
207 {
208 	WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
209 	memset_l(ptr, value, len / sizeof(unsigned long));
210 }
211 
212 static bool page_same_filled(void *ptr, unsigned long *element)
213 {
214 	unsigned long *page;
215 	unsigned long val;
216 	unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
217 
218 	page = (unsigned long *)ptr;
219 	val = page[0];
220 
221 	if (val != page[last_pos])
222 		return false;
223 
224 	for (pos = 1; pos < last_pos; pos++) {
225 		if (val != page[pos])
226 			return false;
227 	}
228 
229 	*element = val;
230 
231 	return true;
232 }
233 
234 static ssize_t initstate_show(struct device *dev,
235 		struct device_attribute *attr, char *buf)
236 {
237 	u32 val;
238 	struct zram *zram = dev_to_zram(dev);
239 
240 	down_read(&zram->init_lock);
241 	val = init_done(zram);
242 	up_read(&zram->init_lock);
243 
244 	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
245 }
246 
247 static ssize_t disksize_show(struct device *dev,
248 		struct device_attribute *attr, char *buf)
249 {
250 	struct zram *zram = dev_to_zram(dev);
251 
252 	return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
253 }
254 
255 static ssize_t mem_limit_store(struct device *dev,
256 		struct device_attribute *attr, const char *buf, size_t len)
257 {
258 	u64 limit;
259 	char *tmp;
260 	struct zram *zram = dev_to_zram(dev);
261 
262 	limit = memparse(buf, &tmp);
263 	if (buf == tmp) /* no chars parsed, invalid input */
264 		return -EINVAL;
265 
266 	down_write(&zram->init_lock);
267 	zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
268 	up_write(&zram->init_lock);
269 
270 	return len;
271 }
272 
273 static ssize_t mem_used_max_store(struct device *dev,
274 		struct device_attribute *attr, const char *buf, size_t len)
275 {
276 	int err;
277 	unsigned long val;
278 	struct zram *zram = dev_to_zram(dev);
279 
280 	err = kstrtoul(buf, 10, &val);
281 	if (err || val != 0)
282 		return -EINVAL;
283 
284 	down_read(&zram->init_lock);
285 	if (init_done(zram)) {
286 		atomic_long_set(&zram->stats.max_used_pages,
287 				zs_get_total_pages(zram->mem_pool));
288 	}
289 	up_read(&zram->init_lock);
290 
291 	return len;
292 }
293 
294 static ssize_t idle_store(struct device *dev,
295 		struct device_attribute *attr, const char *buf, size_t len)
296 {
297 	struct zram *zram = dev_to_zram(dev);
298 	unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
299 	int index;
300 
301 	if (!sysfs_streq(buf, "all"))
302 		return -EINVAL;
303 
304 	down_read(&zram->init_lock);
305 	if (!init_done(zram)) {
306 		up_read(&zram->init_lock);
307 		return -EINVAL;
308 	}
309 
310 	for (index = 0; index < nr_pages; index++) {
311 		/*
312 		 * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
313 		 * See the comment in writeback_store.
314 		 */
315 		zram_slot_lock(zram, index);
316 		if (zram_allocated(zram, index) &&
317 				!zram_test_flag(zram, index, ZRAM_UNDER_WB))
318 			zram_set_flag(zram, index, ZRAM_IDLE);
319 		zram_slot_unlock(zram, index);
320 	}
321 
322 	up_read(&zram->init_lock);
323 
324 	return len;
325 }
326 
327 #ifdef CONFIG_ZRAM_WRITEBACK
328 static ssize_t writeback_limit_enable_store(struct device *dev,
329 		struct device_attribute *attr, const char *buf, size_t len)
330 {
331 	struct zram *zram = dev_to_zram(dev);
332 	u64 val;
333 	ssize_t ret = -EINVAL;
334 
335 	if (kstrtoull(buf, 10, &val))
336 		return ret;
337 
338 	down_read(&zram->init_lock);
339 	spin_lock(&zram->wb_limit_lock);
340 	zram->wb_limit_enable = val;
341 	spin_unlock(&zram->wb_limit_lock);
342 	up_read(&zram->init_lock);
343 	ret = len;
344 
345 	return ret;
346 }
347 
348 static ssize_t writeback_limit_enable_show(struct device *dev,
349 		struct device_attribute *attr, char *buf)
350 {
351 	bool val;
352 	struct zram *zram = dev_to_zram(dev);
353 
354 	down_read(&zram->init_lock);
355 	spin_lock(&zram->wb_limit_lock);
356 	val = zram->wb_limit_enable;
357 	spin_unlock(&zram->wb_limit_lock);
358 	up_read(&zram->init_lock);
359 
360 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
361 }
362 
363 static ssize_t writeback_limit_store(struct device *dev,
364 		struct device_attribute *attr, const char *buf, size_t len)
365 {
366 	struct zram *zram = dev_to_zram(dev);
367 	u64 val;
368 	ssize_t ret = -EINVAL;
369 
370 	if (kstrtoull(buf, 10, &val))
371 		return ret;
372 
373 	down_read(&zram->init_lock);
374 	spin_lock(&zram->wb_limit_lock);
375 	zram->bd_wb_limit = val;
376 	spin_unlock(&zram->wb_limit_lock);
377 	up_read(&zram->init_lock);
378 	ret = len;
379 
380 	return ret;
381 }
382 
383 static ssize_t writeback_limit_show(struct device *dev,
384 		struct device_attribute *attr, char *buf)
385 {
386 	u64 val;
387 	struct zram *zram = dev_to_zram(dev);
388 
389 	down_read(&zram->init_lock);
390 	spin_lock(&zram->wb_limit_lock);
391 	val = zram->bd_wb_limit;
392 	spin_unlock(&zram->wb_limit_lock);
393 	up_read(&zram->init_lock);
394 
395 	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
396 }
397 
398 static void reset_bdev(struct zram *zram)
399 {
400 	struct block_device *bdev;
401 
402 	if (!zram->backing_dev)
403 		return;
404 
405 	bdev = zram->bdev;
406 	if (zram->old_block_size)
407 		set_blocksize(bdev, zram->old_block_size);
408 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
409 	/* hope filp_close flush all of IO */
410 	filp_close(zram->backing_dev, NULL);
411 	zram->backing_dev = NULL;
412 	zram->old_block_size = 0;
413 	zram->bdev = NULL;
414 	zram->disk->fops = &zram_devops;
415 	kvfree(zram->bitmap);
416 	zram->bitmap = NULL;
417 }
418 
419 static ssize_t backing_dev_show(struct device *dev,
420 		struct device_attribute *attr, char *buf)
421 {
422 	struct file *file;
423 	struct zram *zram = dev_to_zram(dev);
424 	char *p;
425 	ssize_t ret;
426 
427 	down_read(&zram->init_lock);
428 	file = zram->backing_dev;
429 	if (!file) {
430 		memcpy(buf, "none\n", 5);
431 		up_read(&zram->init_lock);
432 		return 5;
433 	}
434 
435 	p = file_path(file, buf, PAGE_SIZE - 1);
436 	if (IS_ERR(p)) {
437 		ret = PTR_ERR(p);
438 		goto out;
439 	}
440 
441 	ret = strlen(p);
442 	memmove(buf, p, ret);
443 	buf[ret++] = '\n';
444 out:
445 	up_read(&zram->init_lock);
446 	return ret;
447 }
448 
449 static ssize_t backing_dev_store(struct device *dev,
450 		struct device_attribute *attr, const char *buf, size_t len)
451 {
452 	char *file_name;
453 	size_t sz;
454 	struct file *backing_dev = NULL;
455 	struct inode *inode;
456 	struct address_space *mapping;
457 	unsigned int bitmap_sz, old_block_size = 0;
458 	unsigned long nr_pages, *bitmap = NULL;
459 	struct block_device *bdev = NULL;
460 	int err;
461 	struct zram *zram = dev_to_zram(dev);
462 
463 	file_name = kmalloc(PATH_MAX, GFP_KERNEL);
464 	if (!file_name)
465 		return -ENOMEM;
466 
467 	down_write(&zram->init_lock);
468 	if (init_done(zram)) {
469 		pr_info("Can't setup backing device for initialized device\n");
470 		err = -EBUSY;
471 		goto out;
472 	}
473 
474 	strlcpy(file_name, buf, PATH_MAX);
475 	/* ignore trailing newline */
476 	sz = strlen(file_name);
477 	if (sz > 0 && file_name[sz - 1] == '\n')
478 		file_name[sz - 1] = 0x00;
479 
480 	backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
481 	if (IS_ERR(backing_dev)) {
482 		err = PTR_ERR(backing_dev);
483 		backing_dev = NULL;
484 		goto out;
485 	}
486 
487 	mapping = backing_dev->f_mapping;
488 	inode = mapping->host;
489 
490 	/* Support only block device in this moment */
491 	if (!S_ISBLK(inode->i_mode)) {
492 		err = -ENOTBLK;
493 		goto out;
494 	}
495 
496 	bdev = blkdev_get_by_dev(inode->i_rdev,
497 			FMODE_READ | FMODE_WRITE | FMODE_EXCL, zram);
498 	if (IS_ERR(bdev)) {
499 		err = PTR_ERR(bdev);
500 		bdev = NULL;
501 		goto out;
502 	}
503 
504 	nr_pages = i_size_read(inode) >> PAGE_SHIFT;
505 	bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
506 	bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
507 	if (!bitmap) {
508 		err = -ENOMEM;
509 		goto out;
510 	}
511 
512 	old_block_size = block_size(bdev);
513 	err = set_blocksize(bdev, PAGE_SIZE);
514 	if (err)
515 		goto out;
516 
517 	reset_bdev(zram);
518 
519 	zram->old_block_size = old_block_size;
520 	zram->bdev = bdev;
521 	zram->backing_dev = backing_dev;
522 	zram->bitmap = bitmap;
523 	zram->nr_pages = nr_pages;
524 	/*
525 	 * With writeback feature, zram does asynchronous IO so it's no longer
526 	 * synchronous device so let's remove synchronous io flag. Othewise,
527 	 * upper layer(e.g., swap) could wait IO completion rather than
528 	 * (submit and return), which will cause system sluggish.
529 	 * Furthermore, when the IO function returns(e.g., swap_readpage),
530 	 * upper layer expects IO was done so it could deallocate the page
531 	 * freely but in fact, IO is going on so finally could cause
532 	 * use-after-free when the IO is really done.
533 	 */
534 	zram->disk->fops = &zram_wb_devops;
535 	up_write(&zram->init_lock);
536 
537 	pr_info("setup backing device %s\n", file_name);
538 	kfree(file_name);
539 
540 	return len;
541 out:
542 	if (bitmap)
543 		kvfree(bitmap);
544 
545 	if (bdev)
546 		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
547 
548 	if (backing_dev)
549 		filp_close(backing_dev, NULL);
550 
551 	up_write(&zram->init_lock);
552 
553 	kfree(file_name);
554 
555 	return err;
556 }
557 
558 static unsigned long alloc_block_bdev(struct zram *zram)
559 {
560 	unsigned long blk_idx = 1;
561 retry:
562 	/* skip 0 bit to confuse zram.handle = 0 */
563 	blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
564 	if (blk_idx == zram->nr_pages)
565 		return 0;
566 
567 	if (test_and_set_bit(blk_idx, zram->bitmap))
568 		goto retry;
569 
570 	atomic64_inc(&zram->stats.bd_count);
571 	return blk_idx;
572 }
573 
574 static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
575 {
576 	int was_set;
577 
578 	was_set = test_and_clear_bit(blk_idx, zram->bitmap);
579 	WARN_ON_ONCE(!was_set);
580 	atomic64_dec(&zram->stats.bd_count);
581 }
582 
583 static void zram_page_end_io(struct bio *bio)
584 {
585 	struct page *page = bio_first_page_all(bio);
586 
587 	page_endio(page, op_is_write(bio_op(bio)),
588 			blk_status_to_errno(bio->bi_status));
589 	bio_put(bio);
590 }
591 
592 /*
593  * Returns 1 if the submission is successful.
594  */
595 static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec,
596 			unsigned long entry, struct bio *parent)
597 {
598 	struct bio *bio;
599 
600 	bio = bio_alloc(GFP_ATOMIC, 1);
601 	if (!bio)
602 		return -ENOMEM;
603 
604 	bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
605 	bio_set_dev(bio, zram->bdev);
606 	if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) {
607 		bio_put(bio);
608 		return -EIO;
609 	}
610 
611 	if (!parent) {
612 		bio->bi_opf = REQ_OP_READ;
613 		bio->bi_end_io = zram_page_end_io;
614 	} else {
615 		bio->bi_opf = parent->bi_opf;
616 		bio_chain(bio, parent);
617 	}
618 
619 	submit_bio(bio);
620 	return 1;
621 }
622 
623 #define PAGE_WB_SIG "page_index="
624 
625 #define PAGE_WRITEBACK 0
626 #define HUGE_WRITEBACK 1
627 #define IDLE_WRITEBACK 2
628 
629 
630 static ssize_t writeback_store(struct device *dev,
631 		struct device_attribute *attr, const char *buf, size_t len)
632 {
633 	struct zram *zram = dev_to_zram(dev);
634 	unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
635 	unsigned long index = 0;
636 	struct bio bio;
637 	struct bio_vec bio_vec;
638 	struct page *page;
639 	ssize_t ret = len;
640 	int mode;
641 	unsigned long blk_idx = 0;
642 
643 	if (sysfs_streq(buf, "idle"))
644 		mode = IDLE_WRITEBACK;
645 	else if (sysfs_streq(buf, "huge"))
646 		mode = HUGE_WRITEBACK;
647 	else {
648 		if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1))
649 			return -EINVAL;
650 
651 		ret = kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index);
652 		if (ret || index >= nr_pages)
653 			return -EINVAL;
654 
655 		nr_pages = 1;
656 		mode = PAGE_WRITEBACK;
657 	}
658 
659 	down_read(&zram->init_lock);
660 	if (!init_done(zram)) {
661 		ret = -EINVAL;
662 		goto release_init_lock;
663 	}
664 
665 	if (!zram->backing_dev) {
666 		ret = -ENODEV;
667 		goto release_init_lock;
668 	}
669 
670 	page = alloc_page(GFP_KERNEL);
671 	if (!page) {
672 		ret = -ENOMEM;
673 		goto release_init_lock;
674 	}
675 
676 	while (nr_pages--) {
677 		struct bio_vec bvec;
678 
679 		bvec.bv_page = page;
680 		bvec.bv_len = PAGE_SIZE;
681 		bvec.bv_offset = 0;
682 
683 		spin_lock(&zram->wb_limit_lock);
684 		if (zram->wb_limit_enable && !zram->bd_wb_limit) {
685 			spin_unlock(&zram->wb_limit_lock);
686 			ret = -EIO;
687 			break;
688 		}
689 		spin_unlock(&zram->wb_limit_lock);
690 
691 		if (!blk_idx) {
692 			blk_idx = alloc_block_bdev(zram);
693 			if (!blk_idx) {
694 				ret = -ENOSPC;
695 				break;
696 			}
697 		}
698 
699 		zram_slot_lock(zram, index);
700 		if (!zram_allocated(zram, index))
701 			goto next;
702 
703 		if (zram_test_flag(zram, index, ZRAM_WB) ||
704 				zram_test_flag(zram, index, ZRAM_SAME) ||
705 				zram_test_flag(zram, index, ZRAM_UNDER_WB))
706 			goto next;
707 
708 		if (mode == IDLE_WRITEBACK &&
709 			  !zram_test_flag(zram, index, ZRAM_IDLE))
710 			goto next;
711 		if (mode == HUGE_WRITEBACK &&
712 			  !zram_test_flag(zram, index, ZRAM_HUGE))
713 			goto next;
714 		/*
715 		 * Clearing ZRAM_UNDER_WB is duty of caller.
716 		 * IOW, zram_free_page never clear it.
717 		 */
718 		zram_set_flag(zram, index, ZRAM_UNDER_WB);
719 		/* Need for hugepage writeback racing */
720 		zram_set_flag(zram, index, ZRAM_IDLE);
721 		zram_slot_unlock(zram, index);
722 		if (zram_bvec_read(zram, &bvec, index, 0, NULL)) {
723 			zram_slot_lock(zram, index);
724 			zram_clear_flag(zram, index, ZRAM_UNDER_WB);
725 			zram_clear_flag(zram, index, ZRAM_IDLE);
726 			zram_slot_unlock(zram, index);
727 			continue;
728 		}
729 
730 		bio_init(&bio, &bio_vec, 1);
731 		bio_set_dev(&bio, zram->bdev);
732 		bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
733 		bio.bi_opf = REQ_OP_WRITE | REQ_SYNC;
734 
735 		bio_add_page(&bio, bvec.bv_page, bvec.bv_len,
736 				bvec.bv_offset);
737 		/*
738 		 * XXX: A single page IO would be inefficient for write
739 		 * but it would be not bad as starter.
740 		 */
741 		ret = submit_bio_wait(&bio);
742 		if (ret) {
743 			zram_slot_lock(zram, index);
744 			zram_clear_flag(zram, index, ZRAM_UNDER_WB);
745 			zram_clear_flag(zram, index, ZRAM_IDLE);
746 			zram_slot_unlock(zram, index);
747 			continue;
748 		}
749 
750 		atomic64_inc(&zram->stats.bd_writes);
751 		/*
752 		 * We released zram_slot_lock so need to check if the slot was
753 		 * changed. If there is freeing for the slot, we can catch it
754 		 * easily by zram_allocated.
755 		 * A subtle case is the slot is freed/reallocated/marked as
756 		 * ZRAM_IDLE again. To close the race, idle_store doesn't
757 		 * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
758 		 * Thus, we could close the race by checking ZRAM_IDLE bit.
759 		 */
760 		zram_slot_lock(zram, index);
761 		if (!zram_allocated(zram, index) ||
762 			  !zram_test_flag(zram, index, ZRAM_IDLE)) {
763 			zram_clear_flag(zram, index, ZRAM_UNDER_WB);
764 			zram_clear_flag(zram, index, ZRAM_IDLE);
765 			goto next;
766 		}
767 
768 		zram_free_page(zram, index);
769 		zram_clear_flag(zram, index, ZRAM_UNDER_WB);
770 		zram_set_flag(zram, index, ZRAM_WB);
771 		zram_set_element(zram, index, blk_idx);
772 		blk_idx = 0;
773 		atomic64_inc(&zram->stats.pages_stored);
774 		spin_lock(&zram->wb_limit_lock);
775 		if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
776 			zram->bd_wb_limit -=  1UL << (PAGE_SHIFT - 12);
777 		spin_unlock(&zram->wb_limit_lock);
778 next:
779 		zram_slot_unlock(zram, index);
780 	}
781 
782 	if (blk_idx)
783 		free_block_bdev(zram, blk_idx);
784 	__free_page(page);
785 release_init_lock:
786 	up_read(&zram->init_lock);
787 
788 	return ret;
789 }
790 
791 struct zram_work {
792 	struct work_struct work;
793 	struct zram *zram;
794 	unsigned long entry;
795 	struct bio *bio;
796 	struct bio_vec bvec;
797 };
798 
799 #if PAGE_SIZE != 4096
800 static void zram_sync_read(struct work_struct *work)
801 {
802 	struct zram_work *zw = container_of(work, struct zram_work, work);
803 	struct zram *zram = zw->zram;
804 	unsigned long entry = zw->entry;
805 	struct bio *bio = zw->bio;
806 
807 	read_from_bdev_async(zram, &zw->bvec, entry, bio);
808 }
809 
810 /*
811  * Block layer want one ->submit_bio to be active at a time, so if we use
812  * chained IO with parent IO in same context, it's a deadlock. To avoid that,
813  * use a worker thread context.
814  */
815 static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
816 				unsigned long entry, struct bio *bio)
817 {
818 	struct zram_work work;
819 
820 	work.bvec = *bvec;
821 	work.zram = zram;
822 	work.entry = entry;
823 	work.bio = bio;
824 
825 	INIT_WORK_ONSTACK(&work.work, zram_sync_read);
826 	queue_work(system_unbound_wq, &work.work);
827 	flush_work(&work.work);
828 	destroy_work_on_stack(&work.work);
829 
830 	return 1;
831 }
832 #else
833 static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
834 				unsigned long entry, struct bio *bio)
835 {
836 	WARN_ON(1);
837 	return -EIO;
838 }
839 #endif
840 
841 static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
842 			unsigned long entry, struct bio *parent, bool sync)
843 {
844 	atomic64_inc(&zram->stats.bd_reads);
845 	if (sync)
846 		return read_from_bdev_sync(zram, bvec, entry, parent);
847 	else
848 		return read_from_bdev_async(zram, bvec, entry, parent);
849 }
850 #else
851 static inline void reset_bdev(struct zram *zram) {};
852 static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
853 			unsigned long entry, struct bio *parent, bool sync)
854 {
855 	return -EIO;
856 }
857 
858 static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
859 #endif
860 
861 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
862 
863 static struct dentry *zram_debugfs_root;
864 
865 static void zram_debugfs_create(void)
866 {
867 	zram_debugfs_root = debugfs_create_dir("zram", NULL);
868 }
869 
870 static void zram_debugfs_destroy(void)
871 {
872 	debugfs_remove_recursive(zram_debugfs_root);
873 }
874 
875 static void zram_accessed(struct zram *zram, u32 index)
876 {
877 	zram_clear_flag(zram, index, ZRAM_IDLE);
878 	zram->table[index].ac_time = ktime_get_boottime();
879 }
880 
881 static ssize_t read_block_state(struct file *file, char __user *buf,
882 				size_t count, loff_t *ppos)
883 {
884 	char *kbuf;
885 	ssize_t index, written = 0;
886 	struct zram *zram = file->private_data;
887 	unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
888 	struct timespec64 ts;
889 
890 	kbuf = kvmalloc(count, GFP_KERNEL);
891 	if (!kbuf)
892 		return -ENOMEM;
893 
894 	down_read(&zram->init_lock);
895 	if (!init_done(zram)) {
896 		up_read(&zram->init_lock);
897 		kvfree(kbuf);
898 		return -EINVAL;
899 	}
900 
901 	for (index = *ppos; index < nr_pages; index++) {
902 		int copied;
903 
904 		zram_slot_lock(zram, index);
905 		if (!zram_allocated(zram, index))
906 			goto next;
907 
908 		ts = ktime_to_timespec64(zram->table[index].ac_time);
909 		copied = snprintf(kbuf + written, count,
910 			"%12zd %12lld.%06lu %c%c%c%c\n",
911 			index, (s64)ts.tv_sec,
912 			ts.tv_nsec / NSEC_PER_USEC,
913 			zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
914 			zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
915 			zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
916 			zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.');
917 
918 		if (count < copied) {
919 			zram_slot_unlock(zram, index);
920 			break;
921 		}
922 		written += copied;
923 		count -= copied;
924 next:
925 		zram_slot_unlock(zram, index);
926 		*ppos += 1;
927 	}
928 
929 	up_read(&zram->init_lock);
930 	if (copy_to_user(buf, kbuf, written))
931 		written = -EFAULT;
932 	kvfree(kbuf);
933 
934 	return written;
935 }
936 
937 static const struct file_operations proc_zram_block_state_op = {
938 	.open = simple_open,
939 	.read = read_block_state,
940 	.llseek = default_llseek,
941 };
942 
943 static void zram_debugfs_register(struct zram *zram)
944 {
945 	if (!zram_debugfs_root)
946 		return;
947 
948 	zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
949 						zram_debugfs_root);
950 	debugfs_create_file("block_state", 0400, zram->debugfs_dir,
951 				zram, &proc_zram_block_state_op);
952 }
953 
954 static void zram_debugfs_unregister(struct zram *zram)
955 {
956 	debugfs_remove_recursive(zram->debugfs_dir);
957 }
958 #else
959 static void zram_debugfs_create(void) {};
960 static void zram_debugfs_destroy(void) {};
961 static void zram_accessed(struct zram *zram, u32 index)
962 {
963 	zram_clear_flag(zram, index, ZRAM_IDLE);
964 };
965 static void zram_debugfs_register(struct zram *zram) {};
966 static void zram_debugfs_unregister(struct zram *zram) {};
967 #endif
968 
969 /*
970  * We switched to per-cpu streams and this attr is not needed anymore.
971  * However, we will keep it around for some time, because:
972  * a) we may revert per-cpu streams in the future
973  * b) it's visible to user space and we need to follow our 2 years
974  *    retirement rule; but we already have a number of 'soon to be
975  *    altered' attrs, so max_comp_streams need to wait for the next
976  *    layoff cycle.
977  */
978 static ssize_t max_comp_streams_show(struct device *dev,
979 		struct device_attribute *attr, char *buf)
980 {
981 	return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
982 }
983 
984 static ssize_t max_comp_streams_store(struct device *dev,
985 		struct device_attribute *attr, const char *buf, size_t len)
986 {
987 	return len;
988 }
989 
990 static ssize_t comp_algorithm_show(struct device *dev,
991 		struct device_attribute *attr, char *buf)
992 {
993 	size_t sz;
994 	struct zram *zram = dev_to_zram(dev);
995 
996 	down_read(&zram->init_lock);
997 	sz = zcomp_available_show(zram->compressor, buf);
998 	up_read(&zram->init_lock);
999 
1000 	return sz;
1001 }
1002 
1003 static ssize_t comp_algorithm_store(struct device *dev,
1004 		struct device_attribute *attr, const char *buf, size_t len)
1005 {
1006 	struct zram *zram = dev_to_zram(dev);
1007 	char compressor[ARRAY_SIZE(zram->compressor)];
1008 	size_t sz;
1009 
1010 	strlcpy(compressor, buf, sizeof(compressor));
1011 	/* ignore trailing newline */
1012 	sz = strlen(compressor);
1013 	if (sz > 0 && compressor[sz - 1] == '\n')
1014 		compressor[sz - 1] = 0x00;
1015 
1016 	if (!zcomp_available_algorithm(compressor))
1017 		return -EINVAL;
1018 
1019 	down_write(&zram->init_lock);
1020 	if (init_done(zram)) {
1021 		up_write(&zram->init_lock);
1022 		pr_info("Can't change algorithm for initialized device\n");
1023 		return -EBUSY;
1024 	}
1025 
1026 	strcpy(zram->compressor, compressor);
1027 	up_write(&zram->init_lock);
1028 	return len;
1029 }
1030 
1031 static ssize_t compact_store(struct device *dev,
1032 		struct device_attribute *attr, const char *buf, size_t len)
1033 {
1034 	struct zram *zram = dev_to_zram(dev);
1035 
1036 	down_read(&zram->init_lock);
1037 	if (!init_done(zram)) {
1038 		up_read(&zram->init_lock);
1039 		return -EINVAL;
1040 	}
1041 
1042 	zs_compact(zram->mem_pool);
1043 	up_read(&zram->init_lock);
1044 
1045 	return len;
1046 }
1047 
1048 static ssize_t io_stat_show(struct device *dev,
1049 		struct device_attribute *attr, char *buf)
1050 {
1051 	struct zram *zram = dev_to_zram(dev);
1052 	ssize_t ret;
1053 
1054 	down_read(&zram->init_lock);
1055 	ret = scnprintf(buf, PAGE_SIZE,
1056 			"%8llu %8llu %8llu %8llu\n",
1057 			(u64)atomic64_read(&zram->stats.failed_reads),
1058 			(u64)atomic64_read(&zram->stats.failed_writes),
1059 			(u64)atomic64_read(&zram->stats.invalid_io),
1060 			(u64)atomic64_read(&zram->stats.notify_free));
1061 	up_read(&zram->init_lock);
1062 
1063 	return ret;
1064 }
1065 
1066 static ssize_t mm_stat_show(struct device *dev,
1067 		struct device_attribute *attr, char *buf)
1068 {
1069 	struct zram *zram = dev_to_zram(dev);
1070 	struct zs_pool_stats pool_stats;
1071 	u64 orig_size, mem_used = 0;
1072 	long max_used;
1073 	ssize_t ret;
1074 
1075 	memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
1076 
1077 	down_read(&zram->init_lock);
1078 	if (init_done(zram)) {
1079 		mem_used = zs_get_total_pages(zram->mem_pool);
1080 		zs_pool_stats(zram->mem_pool, &pool_stats);
1081 	}
1082 
1083 	orig_size = atomic64_read(&zram->stats.pages_stored);
1084 	max_used = atomic_long_read(&zram->stats.max_used_pages);
1085 
1086 	ret = scnprintf(buf, PAGE_SIZE,
1087 			"%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
1088 			orig_size << PAGE_SHIFT,
1089 			(u64)atomic64_read(&zram->stats.compr_data_size),
1090 			mem_used << PAGE_SHIFT,
1091 			zram->limit_pages << PAGE_SHIFT,
1092 			max_used << PAGE_SHIFT,
1093 			(u64)atomic64_read(&zram->stats.same_pages),
1094 			pool_stats.pages_compacted,
1095 			(u64)atomic64_read(&zram->stats.huge_pages),
1096 			(u64)atomic64_read(&zram->stats.huge_pages_since));
1097 	up_read(&zram->init_lock);
1098 
1099 	return ret;
1100 }
1101 
1102 #ifdef CONFIG_ZRAM_WRITEBACK
1103 #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
1104 static ssize_t bd_stat_show(struct device *dev,
1105 		struct device_attribute *attr, char *buf)
1106 {
1107 	struct zram *zram = dev_to_zram(dev);
1108 	ssize_t ret;
1109 
1110 	down_read(&zram->init_lock);
1111 	ret = scnprintf(buf, PAGE_SIZE,
1112 		"%8llu %8llu %8llu\n",
1113 			FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
1114 			FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
1115 			FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
1116 	up_read(&zram->init_lock);
1117 
1118 	return ret;
1119 }
1120 #endif
1121 
1122 static ssize_t debug_stat_show(struct device *dev,
1123 		struct device_attribute *attr, char *buf)
1124 {
1125 	int version = 1;
1126 	struct zram *zram = dev_to_zram(dev);
1127 	ssize_t ret;
1128 
1129 	down_read(&zram->init_lock);
1130 	ret = scnprintf(buf, PAGE_SIZE,
1131 			"version: %d\n%8llu %8llu\n",
1132 			version,
1133 			(u64)atomic64_read(&zram->stats.writestall),
1134 			(u64)atomic64_read(&zram->stats.miss_free));
1135 	up_read(&zram->init_lock);
1136 
1137 	return ret;
1138 }
1139 
1140 static DEVICE_ATTR_RO(io_stat);
1141 static DEVICE_ATTR_RO(mm_stat);
1142 #ifdef CONFIG_ZRAM_WRITEBACK
1143 static DEVICE_ATTR_RO(bd_stat);
1144 #endif
1145 static DEVICE_ATTR_RO(debug_stat);
1146 
1147 static void zram_meta_free(struct zram *zram, u64 disksize)
1148 {
1149 	size_t num_pages = disksize >> PAGE_SHIFT;
1150 	size_t index;
1151 
1152 	/* Free all pages that are still in this zram device */
1153 	for (index = 0; index < num_pages; index++)
1154 		zram_free_page(zram, index);
1155 
1156 	zs_destroy_pool(zram->mem_pool);
1157 	vfree(zram->table);
1158 }
1159 
1160 static bool zram_meta_alloc(struct zram *zram, u64 disksize)
1161 {
1162 	size_t num_pages;
1163 
1164 	num_pages = disksize >> PAGE_SHIFT;
1165 	zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
1166 	if (!zram->table)
1167 		return false;
1168 
1169 	zram->mem_pool = zs_create_pool(zram->disk->disk_name);
1170 	if (!zram->mem_pool) {
1171 		vfree(zram->table);
1172 		return false;
1173 	}
1174 
1175 	if (!huge_class_size)
1176 		huge_class_size = zs_huge_class_size(zram->mem_pool);
1177 	return true;
1178 }
1179 
1180 /*
1181  * To protect concurrent access to the same index entry,
1182  * caller should hold this table index entry's bit_spinlock to
1183  * indicate this index entry is accessing.
1184  */
1185 static void zram_free_page(struct zram *zram, size_t index)
1186 {
1187 	unsigned long handle;
1188 
1189 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
1190 	zram->table[index].ac_time = 0;
1191 #endif
1192 	if (zram_test_flag(zram, index, ZRAM_IDLE))
1193 		zram_clear_flag(zram, index, ZRAM_IDLE);
1194 
1195 	if (zram_test_flag(zram, index, ZRAM_HUGE)) {
1196 		zram_clear_flag(zram, index, ZRAM_HUGE);
1197 		atomic64_dec(&zram->stats.huge_pages);
1198 	}
1199 
1200 	if (zram_test_flag(zram, index, ZRAM_WB)) {
1201 		zram_clear_flag(zram, index, ZRAM_WB);
1202 		free_block_bdev(zram, zram_get_element(zram, index));
1203 		goto out;
1204 	}
1205 
1206 	/*
1207 	 * No memory is allocated for same element filled pages.
1208 	 * Simply clear same page flag.
1209 	 */
1210 	if (zram_test_flag(zram, index, ZRAM_SAME)) {
1211 		zram_clear_flag(zram, index, ZRAM_SAME);
1212 		atomic64_dec(&zram->stats.same_pages);
1213 		goto out;
1214 	}
1215 
1216 	handle = zram_get_handle(zram, index);
1217 	if (!handle)
1218 		return;
1219 
1220 	zs_free(zram->mem_pool, handle);
1221 
1222 	atomic64_sub(zram_get_obj_size(zram, index),
1223 			&zram->stats.compr_data_size);
1224 out:
1225 	atomic64_dec(&zram->stats.pages_stored);
1226 	zram_set_handle(zram, index, 0);
1227 	zram_set_obj_size(zram, index, 0);
1228 	WARN_ON_ONCE(zram->table[index].flags &
1229 		~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB));
1230 }
1231 
1232 static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
1233 				struct bio *bio, bool partial_io)
1234 {
1235 	struct zcomp_strm *zstrm;
1236 	unsigned long handle;
1237 	unsigned int size;
1238 	void *src, *dst;
1239 	int ret;
1240 
1241 	zram_slot_lock(zram, index);
1242 	if (zram_test_flag(zram, index, ZRAM_WB)) {
1243 		struct bio_vec bvec;
1244 
1245 		zram_slot_unlock(zram, index);
1246 
1247 		bvec.bv_page = page;
1248 		bvec.bv_len = PAGE_SIZE;
1249 		bvec.bv_offset = 0;
1250 		return read_from_bdev(zram, &bvec,
1251 				zram_get_element(zram, index),
1252 				bio, partial_io);
1253 	}
1254 
1255 	handle = zram_get_handle(zram, index);
1256 	if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
1257 		unsigned long value;
1258 		void *mem;
1259 
1260 		value = handle ? zram_get_element(zram, index) : 0;
1261 		mem = kmap_atomic(page);
1262 		zram_fill_page(mem, PAGE_SIZE, value);
1263 		kunmap_atomic(mem);
1264 		zram_slot_unlock(zram, index);
1265 		return 0;
1266 	}
1267 
1268 	size = zram_get_obj_size(zram, index);
1269 
1270 	if (size != PAGE_SIZE)
1271 		zstrm = zcomp_stream_get(zram->comp);
1272 
1273 	src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
1274 	if (size == PAGE_SIZE) {
1275 		dst = kmap_atomic(page);
1276 		memcpy(dst, src, PAGE_SIZE);
1277 		kunmap_atomic(dst);
1278 		ret = 0;
1279 	} else {
1280 		dst = kmap_atomic(page);
1281 		ret = zcomp_decompress(zstrm, src, size, dst);
1282 		kunmap_atomic(dst);
1283 		zcomp_stream_put(zram->comp);
1284 	}
1285 	zs_unmap_object(zram->mem_pool, handle);
1286 	zram_slot_unlock(zram, index);
1287 
1288 	/* Should NEVER happen. Return bio error if it does. */
1289 	if (WARN_ON(ret))
1290 		pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
1291 
1292 	return ret;
1293 }
1294 
1295 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
1296 				u32 index, int offset, struct bio *bio)
1297 {
1298 	int ret;
1299 	struct page *page;
1300 
1301 	page = bvec->bv_page;
1302 	if (is_partial_io(bvec)) {
1303 		/* Use a temporary buffer to decompress the page */
1304 		page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
1305 		if (!page)
1306 			return -ENOMEM;
1307 	}
1308 
1309 	ret = __zram_bvec_read(zram, page, index, bio, is_partial_io(bvec));
1310 	if (unlikely(ret))
1311 		goto out;
1312 
1313 	if (is_partial_io(bvec)) {
1314 		void *dst = kmap_atomic(bvec->bv_page);
1315 		void *src = kmap_atomic(page);
1316 
1317 		memcpy(dst + bvec->bv_offset, src + offset, bvec->bv_len);
1318 		kunmap_atomic(src);
1319 		kunmap_atomic(dst);
1320 	}
1321 out:
1322 	if (is_partial_io(bvec))
1323 		__free_page(page);
1324 
1325 	return ret;
1326 }
1327 
1328 static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1329 				u32 index, struct bio *bio)
1330 {
1331 	int ret = 0;
1332 	unsigned long alloced_pages;
1333 	unsigned long handle = 0;
1334 	unsigned int comp_len = 0;
1335 	void *src, *dst, *mem;
1336 	struct zcomp_strm *zstrm;
1337 	struct page *page = bvec->bv_page;
1338 	unsigned long element = 0;
1339 	enum zram_pageflags flags = 0;
1340 
1341 	mem = kmap_atomic(page);
1342 	if (page_same_filled(mem, &element)) {
1343 		kunmap_atomic(mem);
1344 		/* Free memory associated with this sector now. */
1345 		flags = ZRAM_SAME;
1346 		atomic64_inc(&zram->stats.same_pages);
1347 		goto out;
1348 	}
1349 	kunmap_atomic(mem);
1350 
1351 compress_again:
1352 	zstrm = zcomp_stream_get(zram->comp);
1353 	src = kmap_atomic(page);
1354 	ret = zcomp_compress(zstrm, src, &comp_len);
1355 	kunmap_atomic(src);
1356 
1357 	if (unlikely(ret)) {
1358 		zcomp_stream_put(zram->comp);
1359 		pr_err("Compression failed! err=%d\n", ret);
1360 		zs_free(zram->mem_pool, handle);
1361 		return ret;
1362 	}
1363 
1364 	if (comp_len >= huge_class_size)
1365 		comp_len = PAGE_SIZE;
1366 	/*
1367 	 * handle allocation has 2 paths:
1368 	 * a) fast path is executed with preemption disabled (for
1369 	 *  per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear,
1370 	 *  since we can't sleep;
1371 	 * b) slow path enables preemption and attempts to allocate
1372 	 *  the page with __GFP_DIRECT_RECLAIM bit set. we have to
1373 	 *  put per-cpu compression stream and, thus, to re-do
1374 	 *  the compression once handle is allocated.
1375 	 *
1376 	 * if we have a 'non-null' handle here then we are coming
1377 	 * from the slow path and handle has already been allocated.
1378 	 */
1379 	if (!handle)
1380 		handle = zs_malloc(zram->mem_pool, comp_len,
1381 				__GFP_KSWAPD_RECLAIM |
1382 				__GFP_NOWARN |
1383 				__GFP_HIGHMEM |
1384 				__GFP_MOVABLE);
1385 	if (!handle) {
1386 		zcomp_stream_put(zram->comp);
1387 		atomic64_inc(&zram->stats.writestall);
1388 		handle = zs_malloc(zram->mem_pool, comp_len,
1389 				GFP_NOIO | __GFP_HIGHMEM |
1390 				__GFP_MOVABLE);
1391 		if (handle)
1392 			goto compress_again;
1393 		return -ENOMEM;
1394 	}
1395 
1396 	alloced_pages = zs_get_total_pages(zram->mem_pool);
1397 	update_used_max(zram, alloced_pages);
1398 
1399 	if (zram->limit_pages && alloced_pages > zram->limit_pages) {
1400 		zcomp_stream_put(zram->comp);
1401 		zs_free(zram->mem_pool, handle);
1402 		return -ENOMEM;
1403 	}
1404 
1405 	dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
1406 
1407 	src = zstrm->buffer;
1408 	if (comp_len == PAGE_SIZE)
1409 		src = kmap_atomic(page);
1410 	memcpy(dst, src, comp_len);
1411 	if (comp_len == PAGE_SIZE)
1412 		kunmap_atomic(src);
1413 
1414 	zcomp_stream_put(zram->comp);
1415 	zs_unmap_object(zram->mem_pool, handle);
1416 	atomic64_add(comp_len, &zram->stats.compr_data_size);
1417 out:
1418 	/*
1419 	 * Free memory associated with this sector
1420 	 * before overwriting unused sectors.
1421 	 */
1422 	zram_slot_lock(zram, index);
1423 	zram_free_page(zram, index);
1424 
1425 	if (comp_len == PAGE_SIZE) {
1426 		zram_set_flag(zram, index, ZRAM_HUGE);
1427 		atomic64_inc(&zram->stats.huge_pages);
1428 		atomic64_inc(&zram->stats.huge_pages_since);
1429 	}
1430 
1431 	if (flags) {
1432 		zram_set_flag(zram, index, flags);
1433 		zram_set_element(zram, index, element);
1434 	}  else {
1435 		zram_set_handle(zram, index, handle);
1436 		zram_set_obj_size(zram, index, comp_len);
1437 	}
1438 	zram_slot_unlock(zram, index);
1439 
1440 	/* Update stats */
1441 	atomic64_inc(&zram->stats.pages_stored);
1442 	return ret;
1443 }
1444 
1445 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1446 				u32 index, int offset, struct bio *bio)
1447 {
1448 	int ret;
1449 	struct page *page = NULL;
1450 	void *src;
1451 	struct bio_vec vec;
1452 
1453 	vec = *bvec;
1454 	if (is_partial_io(bvec)) {
1455 		void *dst;
1456 		/*
1457 		 * This is a partial IO. We need to read the full page
1458 		 * before to write the changes.
1459 		 */
1460 		page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
1461 		if (!page)
1462 			return -ENOMEM;
1463 
1464 		ret = __zram_bvec_read(zram, page, index, bio, true);
1465 		if (ret)
1466 			goto out;
1467 
1468 		src = kmap_atomic(bvec->bv_page);
1469 		dst = kmap_atomic(page);
1470 		memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len);
1471 		kunmap_atomic(dst);
1472 		kunmap_atomic(src);
1473 
1474 		vec.bv_page = page;
1475 		vec.bv_len = PAGE_SIZE;
1476 		vec.bv_offset = 0;
1477 	}
1478 
1479 	ret = __zram_bvec_write(zram, &vec, index, bio);
1480 out:
1481 	if (is_partial_io(bvec))
1482 		__free_page(page);
1483 	return ret;
1484 }
1485 
1486 /*
1487  * zram_bio_discard - handler on discard request
1488  * @index: physical block index in PAGE_SIZE units
1489  * @offset: byte offset within physical block
1490  */
1491 static void zram_bio_discard(struct zram *zram, u32 index,
1492 			     int offset, struct bio *bio)
1493 {
1494 	size_t n = bio->bi_iter.bi_size;
1495 
1496 	/*
1497 	 * zram manages data in physical block size units. Because logical block
1498 	 * size isn't identical with physical block size on some arch, we
1499 	 * could get a discard request pointing to a specific offset within a
1500 	 * certain physical block.  Although we can handle this request by
1501 	 * reading that physiclal block and decompressing and partially zeroing
1502 	 * and re-compressing and then re-storing it, this isn't reasonable
1503 	 * because our intent with a discard request is to save memory.  So
1504 	 * skipping this logical block is appropriate here.
1505 	 */
1506 	if (offset) {
1507 		if (n <= (PAGE_SIZE - offset))
1508 			return;
1509 
1510 		n -= (PAGE_SIZE - offset);
1511 		index++;
1512 	}
1513 
1514 	while (n >= PAGE_SIZE) {
1515 		zram_slot_lock(zram, index);
1516 		zram_free_page(zram, index);
1517 		zram_slot_unlock(zram, index);
1518 		atomic64_inc(&zram->stats.notify_free);
1519 		index++;
1520 		n -= PAGE_SIZE;
1521 	}
1522 }
1523 
1524 /*
1525  * Returns errno if it has some problem. Otherwise return 0 or 1.
1526  * Returns 0 if IO request was done synchronously
1527  * Returns 1 if IO request was successfully submitted.
1528  */
1529 static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
1530 			int offset, unsigned int op, struct bio *bio)
1531 {
1532 	int ret;
1533 
1534 	if (!op_is_write(op)) {
1535 		atomic64_inc(&zram->stats.num_reads);
1536 		ret = zram_bvec_read(zram, bvec, index, offset, bio);
1537 		flush_dcache_page(bvec->bv_page);
1538 	} else {
1539 		atomic64_inc(&zram->stats.num_writes);
1540 		ret = zram_bvec_write(zram, bvec, index, offset, bio);
1541 	}
1542 
1543 	zram_slot_lock(zram, index);
1544 	zram_accessed(zram, index);
1545 	zram_slot_unlock(zram, index);
1546 
1547 	if (unlikely(ret < 0)) {
1548 		if (!op_is_write(op))
1549 			atomic64_inc(&zram->stats.failed_reads);
1550 		else
1551 			atomic64_inc(&zram->stats.failed_writes);
1552 	}
1553 
1554 	return ret;
1555 }
1556 
1557 static void __zram_make_request(struct zram *zram, struct bio *bio)
1558 {
1559 	int offset;
1560 	u32 index;
1561 	struct bio_vec bvec;
1562 	struct bvec_iter iter;
1563 	unsigned long start_time;
1564 
1565 	index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1566 	offset = (bio->bi_iter.bi_sector &
1567 		  (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
1568 
1569 	switch (bio_op(bio)) {
1570 	case REQ_OP_DISCARD:
1571 	case REQ_OP_WRITE_ZEROES:
1572 		zram_bio_discard(zram, index, offset, bio);
1573 		bio_endio(bio);
1574 		return;
1575 	default:
1576 		break;
1577 	}
1578 
1579 	start_time = bio_start_io_acct(bio);
1580 	bio_for_each_segment(bvec, bio, iter) {
1581 		struct bio_vec bv = bvec;
1582 		unsigned int unwritten = bvec.bv_len;
1583 
1584 		do {
1585 			bv.bv_len = min_t(unsigned int, PAGE_SIZE - offset,
1586 							unwritten);
1587 			if (zram_bvec_rw(zram, &bv, index, offset,
1588 					 bio_op(bio), bio) < 0) {
1589 				bio->bi_status = BLK_STS_IOERR;
1590 				break;
1591 			}
1592 
1593 			bv.bv_offset += bv.bv_len;
1594 			unwritten -= bv.bv_len;
1595 
1596 			update_position(&index, &offset, &bv);
1597 		} while (unwritten);
1598 	}
1599 	bio_end_io_acct(bio, start_time);
1600 	bio_endio(bio);
1601 }
1602 
1603 /*
1604  * Handler function for all zram I/O requests.
1605  */
1606 static blk_qc_t zram_submit_bio(struct bio *bio)
1607 {
1608 	struct zram *zram = bio->bi_disk->private_data;
1609 
1610 	if (!valid_io_request(zram, bio->bi_iter.bi_sector,
1611 					bio->bi_iter.bi_size)) {
1612 		atomic64_inc(&zram->stats.invalid_io);
1613 		goto error;
1614 	}
1615 
1616 	__zram_make_request(zram, bio);
1617 	return BLK_QC_T_NONE;
1618 
1619 error:
1620 	bio_io_error(bio);
1621 	return BLK_QC_T_NONE;
1622 }
1623 
1624 static void zram_slot_free_notify(struct block_device *bdev,
1625 				unsigned long index)
1626 {
1627 	struct zram *zram;
1628 
1629 	zram = bdev->bd_disk->private_data;
1630 
1631 	atomic64_inc(&zram->stats.notify_free);
1632 	if (!zram_slot_trylock(zram, index)) {
1633 		atomic64_inc(&zram->stats.miss_free);
1634 		return;
1635 	}
1636 
1637 	zram_free_page(zram, index);
1638 	zram_slot_unlock(zram, index);
1639 }
1640 
1641 static int zram_rw_page(struct block_device *bdev, sector_t sector,
1642 		       struct page *page, unsigned int op)
1643 {
1644 	int offset, ret;
1645 	u32 index;
1646 	struct zram *zram;
1647 	struct bio_vec bv;
1648 	unsigned long start_time;
1649 
1650 	if (PageTransHuge(page))
1651 		return -ENOTSUPP;
1652 	zram = bdev->bd_disk->private_data;
1653 
1654 	if (!valid_io_request(zram, sector, PAGE_SIZE)) {
1655 		atomic64_inc(&zram->stats.invalid_io);
1656 		ret = -EINVAL;
1657 		goto out;
1658 	}
1659 
1660 	index = sector >> SECTORS_PER_PAGE_SHIFT;
1661 	offset = (sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
1662 
1663 	bv.bv_page = page;
1664 	bv.bv_len = PAGE_SIZE;
1665 	bv.bv_offset = 0;
1666 
1667 	start_time = disk_start_io_acct(bdev->bd_disk, SECTORS_PER_PAGE, op);
1668 	ret = zram_bvec_rw(zram, &bv, index, offset, op, NULL);
1669 	disk_end_io_acct(bdev->bd_disk, op, start_time);
1670 out:
1671 	/*
1672 	 * If I/O fails, just return error(ie, non-zero) without
1673 	 * calling page_endio.
1674 	 * It causes resubmit the I/O with bio request by upper functions
1675 	 * of rw_page(e.g., swap_readpage, __swap_writepage) and
1676 	 * bio->bi_end_io does things to handle the error
1677 	 * (e.g., SetPageError, set_page_dirty and extra works).
1678 	 */
1679 	if (unlikely(ret < 0))
1680 		return ret;
1681 
1682 	switch (ret) {
1683 	case 0:
1684 		page_endio(page, op_is_write(op), 0);
1685 		break;
1686 	case 1:
1687 		ret = 0;
1688 		break;
1689 	default:
1690 		WARN_ON(1);
1691 	}
1692 	return ret;
1693 }
1694 
1695 static void zram_reset_device(struct zram *zram)
1696 {
1697 	struct zcomp *comp;
1698 	u64 disksize;
1699 
1700 	down_write(&zram->init_lock);
1701 
1702 	zram->limit_pages = 0;
1703 
1704 	if (!init_done(zram)) {
1705 		up_write(&zram->init_lock);
1706 		return;
1707 	}
1708 
1709 	comp = zram->comp;
1710 	disksize = zram->disksize;
1711 	zram->disksize = 0;
1712 
1713 	set_capacity(zram->disk, 0);
1714 	part_stat_set_all(&zram->disk->part0, 0);
1715 
1716 	up_write(&zram->init_lock);
1717 	/* I/O operation under all of CPU are done so let's free */
1718 	zram_meta_free(zram, disksize);
1719 	memset(&zram->stats, 0, sizeof(zram->stats));
1720 	zcomp_destroy(comp);
1721 	reset_bdev(zram);
1722 }
1723 
1724 static ssize_t disksize_store(struct device *dev,
1725 		struct device_attribute *attr, const char *buf, size_t len)
1726 {
1727 	u64 disksize;
1728 	struct zcomp *comp;
1729 	struct zram *zram = dev_to_zram(dev);
1730 	int err;
1731 
1732 	disksize = memparse(buf, NULL);
1733 	if (!disksize)
1734 		return -EINVAL;
1735 
1736 	down_write(&zram->init_lock);
1737 	if (init_done(zram)) {
1738 		pr_info("Cannot change disksize for initialized device\n");
1739 		err = -EBUSY;
1740 		goto out_unlock;
1741 	}
1742 
1743 	disksize = PAGE_ALIGN(disksize);
1744 	if (!zram_meta_alloc(zram, disksize)) {
1745 		err = -ENOMEM;
1746 		goto out_unlock;
1747 	}
1748 
1749 	comp = zcomp_create(zram->compressor);
1750 	if (IS_ERR(comp)) {
1751 		pr_err("Cannot initialise %s compressing backend\n",
1752 				zram->compressor);
1753 		err = PTR_ERR(comp);
1754 		goto out_free_meta;
1755 	}
1756 
1757 	zram->comp = comp;
1758 	zram->disksize = disksize;
1759 	set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
1760 
1761 	revalidate_disk_size(zram->disk, true);
1762 	up_write(&zram->init_lock);
1763 
1764 	return len;
1765 
1766 out_free_meta:
1767 	zram_meta_free(zram, disksize);
1768 out_unlock:
1769 	up_write(&zram->init_lock);
1770 	return err;
1771 }
1772 
1773 static ssize_t reset_store(struct device *dev,
1774 		struct device_attribute *attr, const char *buf, size_t len)
1775 {
1776 	int ret;
1777 	unsigned short do_reset;
1778 	struct zram *zram;
1779 	struct block_device *bdev;
1780 
1781 	ret = kstrtou16(buf, 10, &do_reset);
1782 	if (ret)
1783 		return ret;
1784 
1785 	if (!do_reset)
1786 		return -EINVAL;
1787 
1788 	zram = dev_to_zram(dev);
1789 	bdev = bdget_disk(zram->disk, 0);
1790 	if (!bdev)
1791 		return -ENOMEM;
1792 
1793 	mutex_lock(&bdev->bd_mutex);
1794 	/* Do not reset an active device or claimed device */
1795 	if (bdev->bd_openers || zram->claim) {
1796 		mutex_unlock(&bdev->bd_mutex);
1797 		bdput(bdev);
1798 		return -EBUSY;
1799 	}
1800 
1801 	/* From now on, anyone can't open /dev/zram[0-9] */
1802 	zram->claim = true;
1803 	mutex_unlock(&bdev->bd_mutex);
1804 
1805 	/* Make sure all the pending I/O are finished */
1806 	fsync_bdev(bdev);
1807 	zram_reset_device(zram);
1808 	revalidate_disk_size(zram->disk, true);
1809 	bdput(bdev);
1810 
1811 	mutex_lock(&bdev->bd_mutex);
1812 	zram->claim = false;
1813 	mutex_unlock(&bdev->bd_mutex);
1814 
1815 	return len;
1816 }
1817 
1818 static int zram_open(struct block_device *bdev, fmode_t mode)
1819 {
1820 	int ret = 0;
1821 	struct zram *zram;
1822 
1823 	WARN_ON(!mutex_is_locked(&bdev->bd_mutex));
1824 
1825 	zram = bdev->bd_disk->private_data;
1826 	/* zram was claimed to reset so open request fails */
1827 	if (zram->claim)
1828 		ret = -EBUSY;
1829 
1830 	return ret;
1831 }
1832 
1833 static const struct block_device_operations zram_devops = {
1834 	.open = zram_open,
1835 	.submit_bio = zram_submit_bio,
1836 	.swap_slot_free_notify = zram_slot_free_notify,
1837 	.rw_page = zram_rw_page,
1838 	.owner = THIS_MODULE
1839 };
1840 
1841 static const struct block_device_operations zram_wb_devops = {
1842 	.open = zram_open,
1843 	.submit_bio = zram_submit_bio,
1844 	.swap_slot_free_notify = zram_slot_free_notify,
1845 	.owner = THIS_MODULE
1846 };
1847 
1848 static DEVICE_ATTR_WO(compact);
1849 static DEVICE_ATTR_RW(disksize);
1850 static DEVICE_ATTR_RO(initstate);
1851 static DEVICE_ATTR_WO(reset);
1852 static DEVICE_ATTR_WO(mem_limit);
1853 static DEVICE_ATTR_WO(mem_used_max);
1854 static DEVICE_ATTR_WO(idle);
1855 static DEVICE_ATTR_RW(max_comp_streams);
1856 static DEVICE_ATTR_RW(comp_algorithm);
1857 #ifdef CONFIG_ZRAM_WRITEBACK
1858 static DEVICE_ATTR_RW(backing_dev);
1859 static DEVICE_ATTR_WO(writeback);
1860 static DEVICE_ATTR_RW(writeback_limit);
1861 static DEVICE_ATTR_RW(writeback_limit_enable);
1862 #endif
1863 
1864 static struct attribute *zram_disk_attrs[] = {
1865 	&dev_attr_disksize.attr,
1866 	&dev_attr_initstate.attr,
1867 	&dev_attr_reset.attr,
1868 	&dev_attr_compact.attr,
1869 	&dev_attr_mem_limit.attr,
1870 	&dev_attr_mem_used_max.attr,
1871 	&dev_attr_idle.attr,
1872 	&dev_attr_max_comp_streams.attr,
1873 	&dev_attr_comp_algorithm.attr,
1874 #ifdef CONFIG_ZRAM_WRITEBACK
1875 	&dev_attr_backing_dev.attr,
1876 	&dev_attr_writeback.attr,
1877 	&dev_attr_writeback_limit.attr,
1878 	&dev_attr_writeback_limit_enable.attr,
1879 #endif
1880 	&dev_attr_io_stat.attr,
1881 	&dev_attr_mm_stat.attr,
1882 #ifdef CONFIG_ZRAM_WRITEBACK
1883 	&dev_attr_bd_stat.attr,
1884 #endif
1885 	&dev_attr_debug_stat.attr,
1886 	NULL,
1887 };
1888 
1889 static const struct attribute_group zram_disk_attr_group = {
1890 	.attrs = zram_disk_attrs,
1891 };
1892 
1893 static const struct attribute_group *zram_disk_attr_groups[] = {
1894 	&zram_disk_attr_group,
1895 	NULL,
1896 };
1897 
1898 /*
1899  * Allocate and initialize new zram device. the function returns
1900  * '>= 0' device_id upon success, and negative value otherwise.
1901  */
1902 static int zram_add(void)
1903 {
1904 	struct zram *zram;
1905 	struct request_queue *queue;
1906 	int ret, device_id;
1907 
1908 	zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
1909 	if (!zram)
1910 		return -ENOMEM;
1911 
1912 	ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
1913 	if (ret < 0)
1914 		goto out_free_dev;
1915 	device_id = ret;
1916 
1917 	init_rwsem(&zram->init_lock);
1918 #ifdef CONFIG_ZRAM_WRITEBACK
1919 	spin_lock_init(&zram->wb_limit_lock);
1920 #endif
1921 	queue = blk_alloc_queue(NUMA_NO_NODE);
1922 	if (!queue) {
1923 		pr_err("Error allocating disk queue for device %d\n",
1924 			device_id);
1925 		ret = -ENOMEM;
1926 		goto out_free_idr;
1927 	}
1928 
1929 	/* gendisk structure */
1930 	zram->disk = alloc_disk(1);
1931 	if (!zram->disk) {
1932 		pr_err("Error allocating disk structure for device %d\n",
1933 			device_id);
1934 		ret = -ENOMEM;
1935 		goto out_free_queue;
1936 	}
1937 
1938 	zram->disk->major = zram_major;
1939 	zram->disk->first_minor = device_id;
1940 	zram->disk->fops = &zram_devops;
1941 	zram->disk->queue = queue;
1942 	zram->disk->private_data = zram;
1943 	snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
1944 
1945 	/* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
1946 	set_capacity(zram->disk, 0);
1947 	/* zram devices sort of resembles non-rotational disks */
1948 	blk_queue_flag_set(QUEUE_FLAG_NONROT, zram->disk->queue);
1949 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
1950 
1951 	/*
1952 	 * To ensure that we always get PAGE_SIZE aligned
1953 	 * and n*PAGE_SIZED sized I/O requests.
1954 	 */
1955 	blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
1956 	blk_queue_logical_block_size(zram->disk->queue,
1957 					ZRAM_LOGICAL_BLOCK_SIZE);
1958 	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1959 	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
1960 	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1961 	blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX);
1962 	blk_queue_flag_set(QUEUE_FLAG_DISCARD, zram->disk->queue);
1963 
1964 	/*
1965 	 * zram_bio_discard() will clear all logical blocks if logical block
1966 	 * size is identical with physical block size(PAGE_SIZE). But if it is
1967 	 * different, we will skip discarding some parts of logical blocks in
1968 	 * the part of the request range which isn't aligned to physical block
1969 	 * size.  So we can't ensure that all discarded logical blocks are
1970 	 * zeroed.
1971 	 */
1972 	if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1973 		blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
1974 
1975 	blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue);
1976 	device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
1977 
1978 	strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
1979 
1980 	zram_debugfs_register(zram);
1981 	pr_info("Added device: %s\n", zram->disk->disk_name);
1982 	return device_id;
1983 
1984 out_free_queue:
1985 	blk_cleanup_queue(queue);
1986 out_free_idr:
1987 	idr_remove(&zram_index_idr, device_id);
1988 out_free_dev:
1989 	kfree(zram);
1990 	return ret;
1991 }
1992 
1993 static int zram_remove(struct zram *zram)
1994 {
1995 	struct block_device *bdev;
1996 
1997 	bdev = bdget_disk(zram->disk, 0);
1998 	if (!bdev)
1999 		return -ENOMEM;
2000 
2001 	mutex_lock(&bdev->bd_mutex);
2002 	if (bdev->bd_openers || zram->claim) {
2003 		mutex_unlock(&bdev->bd_mutex);
2004 		bdput(bdev);
2005 		return -EBUSY;
2006 	}
2007 
2008 	zram->claim = true;
2009 	mutex_unlock(&bdev->bd_mutex);
2010 
2011 	zram_debugfs_unregister(zram);
2012 
2013 	/* Make sure all the pending I/O are finished */
2014 	fsync_bdev(bdev);
2015 	zram_reset_device(zram);
2016 	bdput(bdev);
2017 
2018 	pr_info("Removed device: %s\n", zram->disk->disk_name);
2019 
2020 	del_gendisk(zram->disk);
2021 	blk_cleanup_queue(zram->disk->queue);
2022 	put_disk(zram->disk);
2023 	kfree(zram);
2024 	return 0;
2025 }
2026 
2027 /* zram-control sysfs attributes */
2028 
2029 /*
2030  * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a
2031  * sense that reading from this file does alter the state of your system -- it
2032  * creates a new un-initialized zram device and returns back this device's
2033  * device_id (or an error code if it fails to create a new device).
2034  */
2035 static ssize_t hot_add_show(struct class *class,
2036 			struct class_attribute *attr,
2037 			char *buf)
2038 {
2039 	int ret;
2040 
2041 	mutex_lock(&zram_index_mutex);
2042 	ret = zram_add();
2043 	mutex_unlock(&zram_index_mutex);
2044 
2045 	if (ret < 0)
2046 		return ret;
2047 	return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
2048 }
2049 static struct class_attribute class_attr_hot_add =
2050 	__ATTR(hot_add, 0400, hot_add_show, NULL);
2051 
2052 static ssize_t hot_remove_store(struct class *class,
2053 			struct class_attribute *attr,
2054 			const char *buf,
2055 			size_t count)
2056 {
2057 	struct zram *zram;
2058 	int ret, dev_id;
2059 
2060 	/* dev_id is gendisk->first_minor, which is `int' */
2061 	ret = kstrtoint(buf, 10, &dev_id);
2062 	if (ret)
2063 		return ret;
2064 	if (dev_id < 0)
2065 		return -EINVAL;
2066 
2067 	mutex_lock(&zram_index_mutex);
2068 
2069 	zram = idr_find(&zram_index_idr, dev_id);
2070 	if (zram) {
2071 		ret = zram_remove(zram);
2072 		if (!ret)
2073 			idr_remove(&zram_index_idr, dev_id);
2074 	} else {
2075 		ret = -ENODEV;
2076 	}
2077 
2078 	mutex_unlock(&zram_index_mutex);
2079 	return ret ? ret : count;
2080 }
2081 static CLASS_ATTR_WO(hot_remove);
2082 
2083 static struct attribute *zram_control_class_attrs[] = {
2084 	&class_attr_hot_add.attr,
2085 	&class_attr_hot_remove.attr,
2086 	NULL,
2087 };
2088 ATTRIBUTE_GROUPS(zram_control_class);
2089 
2090 static struct class zram_control_class = {
2091 	.name		= "zram-control",
2092 	.owner		= THIS_MODULE,
2093 	.class_groups	= zram_control_class_groups,
2094 };
2095 
2096 static int zram_remove_cb(int id, void *ptr, void *data)
2097 {
2098 	zram_remove(ptr);
2099 	return 0;
2100 }
2101 
2102 static void destroy_devices(void)
2103 {
2104 	class_unregister(&zram_control_class);
2105 	idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
2106 	zram_debugfs_destroy();
2107 	idr_destroy(&zram_index_idr);
2108 	unregister_blkdev(zram_major, "zram");
2109 	cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2110 }
2111 
2112 static int __init zram_init(void)
2113 {
2114 	int ret;
2115 
2116 	ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
2117 				      zcomp_cpu_up_prepare, zcomp_cpu_dead);
2118 	if (ret < 0)
2119 		return ret;
2120 
2121 	ret = class_register(&zram_control_class);
2122 	if (ret) {
2123 		pr_err("Unable to register zram-control class\n");
2124 		cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2125 		return ret;
2126 	}
2127 
2128 	zram_debugfs_create();
2129 	zram_major = register_blkdev(0, "zram");
2130 	if (zram_major <= 0) {
2131 		pr_err("Unable to get major number\n");
2132 		class_unregister(&zram_control_class);
2133 		cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2134 		return -EBUSY;
2135 	}
2136 
2137 	while (num_devices != 0) {
2138 		mutex_lock(&zram_index_mutex);
2139 		ret = zram_add();
2140 		mutex_unlock(&zram_index_mutex);
2141 		if (ret < 0)
2142 			goto out_error;
2143 		num_devices--;
2144 	}
2145 
2146 	return 0;
2147 
2148 out_error:
2149 	destroy_devices();
2150 	return ret;
2151 }
2152 
2153 static void __exit zram_exit(void)
2154 {
2155 	destroy_devices();
2156 }
2157 
2158 module_init(zram_init);
2159 module_exit(zram_exit);
2160 
2161 module_param(num_devices, uint, 0);
2162 MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
2163 
2164 MODULE_LICENSE("Dual BSD/GPL");
2165 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
2166 MODULE_DESCRIPTION("Compressed RAM Block Device");
2167