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