1
2
3
4
5
6
7
8
9
10
11
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
37#include "zram_drv.h"
38
39static DEFINE_IDR(zram_index_idr);
40
41static DEFINE_MUTEX(zram_index_mutex);
42
43static int zram_major;
44static const char *default_compressor = "lzo";
45
46
47static unsigned int num_devices = 1;
48
49
50
51
52static size_t huge_class_size;
53
54static void zram_free_page(struct zram *zram, size_t index);
55
56static void zram_slot_lock(struct zram *zram, u32 index)
57{
58 bit_spin_lock(ZRAM_LOCK, &zram->table[index].value);
59}
60
61static void zram_slot_unlock(struct zram *zram, u32 index)
62{
63 bit_spin_unlock(ZRAM_LOCK, &zram->table[index].value);
64}
65
66static inline bool init_done(struct zram *zram)
67{
68 return zram->disksize;
69}
70
71static inline bool zram_allocated(struct zram *zram, u32 index)
72{
73
74 return (zram->table[index].value >> (ZRAM_FLAG_SHIFT + 1)) ||
75 zram->table[index].handle;
76}
77
78static inline struct zram *dev_to_zram(struct device *dev)
79{
80 return (struct zram *)dev_to_disk(dev)->private_data;
81}
82
83static unsigned long zram_get_handle(struct zram *zram, u32 index)
84{
85 return zram->table[index].handle;
86}
87
88static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
89{
90 zram->table[index].handle = handle;
91}
92
93
94static bool zram_test_flag(struct zram *zram, u32 index,
95 enum zram_pageflags flag)
96{
97 return zram->table[index].value & BIT(flag);
98}
99
100static void zram_set_flag(struct zram *zram, u32 index,
101 enum zram_pageflags flag)
102{
103 zram->table[index].value |= BIT(flag);
104}
105
106static void zram_clear_flag(struct zram *zram, u32 index,
107 enum zram_pageflags flag)
108{
109 zram->table[index].value &= ~BIT(flag);
110}
111
112static inline void zram_set_element(struct zram *zram, u32 index,
113 unsigned long element)
114{
115 zram->table[index].element = element;
116}
117
118static unsigned long zram_get_element(struct zram *zram, u32 index)
119{
120 return zram->table[index].element;
121}
122
123static size_t zram_get_obj_size(struct zram *zram, u32 index)
124{
125 return zram->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
126}
127
128static void zram_set_obj_size(struct zram *zram,
129 u32 index, size_t size)
130{
131 unsigned long flags = zram->table[index].value >> ZRAM_FLAG_SHIFT;
132
133 zram->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
134}
135
136#if PAGE_SIZE != 4096
137static inline bool is_partial_io(struct bio_vec *bvec)
138{
139 return bvec->bv_len != PAGE_SIZE;
140}
141#else
142static inline bool is_partial_io(struct bio_vec *bvec)
143{
144 return false;
145}
146#endif
147
148
149
150
151static inline bool valid_io_request(struct zram *zram,
152 sector_t start, unsigned int size)
153{
154 u64 end, bound;
155
156
157 if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
158 return false;
159 if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
160 return false;
161
162 end = start + (size >> SECTOR_SHIFT);
163 bound = zram->disksize >> SECTOR_SHIFT;
164
165 if (unlikely(start >= bound || end > bound || start > end))
166 return false;
167
168
169 return true;
170}
171
172static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
173{
174 *index += (*offset + bvec->bv_len) / PAGE_SIZE;
175 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
176}
177
178static inline void update_used_max(struct zram *zram,
179 const unsigned long pages)
180{
181 unsigned long old_max, cur_max;
182
183 old_max = atomic_long_read(&zram->stats.max_used_pages);
184
185 do {
186 cur_max = old_max;
187 if (pages > cur_max)
188 old_max = atomic_long_cmpxchg(
189 &zram->stats.max_used_pages, cur_max, pages);
190 } while (old_max != cur_max);
191}
192
193static inline void zram_fill_page(void *ptr, unsigned long len,
194 unsigned long value)
195{
196 WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
197 memset_l(ptr, value, len / sizeof(unsigned long));
198}
199
200static bool page_same_filled(void *ptr, unsigned long *element)
201{
202 unsigned int pos;
203 unsigned long *page;
204 unsigned long val;
205
206 page = (unsigned long *)ptr;
207 val = page[0];
208
209 for (pos = 1; pos < PAGE_SIZE / sizeof(*page); pos++) {
210 if (val != page[pos])
211 return false;
212 }
213
214 *element = val;
215
216 return true;
217}
218
219static ssize_t initstate_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 u32 val;
223 struct zram *zram = dev_to_zram(dev);
224
225 down_read(&zram->init_lock);
226 val = init_done(zram);
227 up_read(&zram->init_lock);
228
229 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
230}
231
232static ssize_t disksize_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234{
235 struct zram *zram = dev_to_zram(dev);
236
237 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
238}
239
240static ssize_t mem_limit_store(struct device *dev,
241 struct device_attribute *attr, const char *buf, size_t len)
242{
243 u64 limit;
244 char *tmp;
245 struct zram *zram = dev_to_zram(dev);
246
247 limit = memparse(buf, &tmp);
248 if (buf == tmp)
249 return -EINVAL;
250
251 down_write(&zram->init_lock);
252 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
253 up_write(&zram->init_lock);
254
255 return len;
256}
257
258static ssize_t mem_used_max_store(struct device *dev,
259 struct device_attribute *attr, const char *buf, size_t len)
260{
261 int err;
262 unsigned long val;
263 struct zram *zram = dev_to_zram(dev);
264
265 err = kstrtoul(buf, 10, &val);
266 if (err || val != 0)
267 return -EINVAL;
268
269 down_read(&zram->init_lock);
270 if (init_done(zram)) {
271 atomic_long_set(&zram->stats.max_used_pages,
272 zs_get_total_pages(zram->mem_pool));
273 }
274 up_read(&zram->init_lock);
275
276 return len;
277}
278
279#ifdef CONFIG_ZRAM_WRITEBACK
280static bool zram_wb_enabled(struct zram *zram)
281{
282 return zram->backing_dev;
283}
284
285static void reset_bdev(struct zram *zram)
286{
287 struct block_device *bdev;
288
289 if (!zram_wb_enabled(zram))
290 return;
291
292 bdev = zram->bdev;
293 if (zram->old_block_size)
294 set_blocksize(bdev, zram->old_block_size);
295 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
296
297 filp_close(zram->backing_dev, NULL);
298 zram->backing_dev = NULL;
299 zram->old_block_size = 0;
300 zram->bdev = NULL;
301 zram->disk->queue->backing_dev_info->capabilities |=
302 BDI_CAP_SYNCHRONOUS_IO;
303 kvfree(zram->bitmap);
304 zram->bitmap = NULL;
305}
306
307static ssize_t backing_dev_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
309{
310 struct zram *zram = dev_to_zram(dev);
311 struct file *file = zram->backing_dev;
312 char *p;
313 ssize_t ret;
314
315 down_read(&zram->init_lock);
316 if (!zram_wb_enabled(zram)) {
317 memcpy(buf, "none\n", 5);
318 up_read(&zram->init_lock);
319 return 5;
320 }
321
322 p = file_path(file, buf, PAGE_SIZE - 1);
323 if (IS_ERR(p)) {
324 ret = PTR_ERR(p);
325 goto out;
326 }
327
328 ret = strlen(p);
329 memmove(buf, p, ret);
330 buf[ret++] = '\n';
331out:
332 up_read(&zram->init_lock);
333 return ret;
334}
335
336static ssize_t backing_dev_store(struct device *dev,
337 struct device_attribute *attr, const char *buf, size_t len)
338{
339 char *file_name;
340 struct file *backing_dev = NULL;
341 struct inode *inode;
342 struct address_space *mapping;
343 unsigned int bitmap_sz, old_block_size = 0;
344 unsigned long nr_pages, *bitmap = NULL;
345 struct block_device *bdev = NULL;
346 int err;
347 struct zram *zram = dev_to_zram(dev);
348
349 file_name = kmalloc(PATH_MAX, GFP_KERNEL);
350 if (!file_name)
351 return -ENOMEM;
352
353 down_write(&zram->init_lock);
354 if (init_done(zram)) {
355 pr_info("Can't setup backing device for initialized device\n");
356 err = -EBUSY;
357 goto out;
358 }
359
360 strlcpy(file_name, buf, len);
361
362 backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
363 if (IS_ERR(backing_dev)) {
364 err = PTR_ERR(backing_dev);
365 backing_dev = NULL;
366 goto out;
367 }
368
369 mapping = backing_dev->f_mapping;
370 inode = mapping->host;
371
372
373 if (!S_ISBLK(inode->i_mode)) {
374 err = -ENOTBLK;
375 goto out;
376 }
377
378 bdev = bdgrab(I_BDEV(inode));
379 err = blkdev_get(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL, zram);
380 if (err < 0)
381 goto out;
382
383 nr_pages = i_size_read(inode) >> PAGE_SHIFT;
384 bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
385 bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
386 if (!bitmap) {
387 err = -ENOMEM;
388 goto out;
389 }
390
391 old_block_size = block_size(bdev);
392 err = set_blocksize(bdev, PAGE_SIZE);
393 if (err)
394 goto out;
395
396 reset_bdev(zram);
397 spin_lock_init(&zram->bitmap_lock);
398
399 zram->old_block_size = old_block_size;
400 zram->bdev = bdev;
401 zram->backing_dev = backing_dev;
402 zram->bitmap = bitmap;
403 zram->nr_pages = nr_pages;
404
405
406
407
408
409
410
411
412
413
414 zram->disk->queue->backing_dev_info->capabilities &=
415 ~BDI_CAP_SYNCHRONOUS_IO;
416 up_write(&zram->init_lock);
417
418 pr_info("setup backing device %s\n", file_name);
419 kfree(file_name);
420
421 return len;
422out:
423 if (bitmap)
424 kvfree(bitmap);
425
426 if (bdev)
427 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
428
429 if (backing_dev)
430 filp_close(backing_dev, NULL);
431
432 up_write(&zram->init_lock);
433
434 kfree(file_name);
435
436 return err;
437}
438
439static unsigned long get_entry_bdev(struct zram *zram)
440{
441 unsigned long entry;
442
443 spin_lock(&zram->bitmap_lock);
444
445 entry = find_next_zero_bit(zram->bitmap, zram->nr_pages, 1);
446 if (entry == zram->nr_pages) {
447 spin_unlock(&zram->bitmap_lock);
448 return 0;
449 }
450
451 set_bit(entry, zram->bitmap);
452 spin_unlock(&zram->bitmap_lock);
453
454 return entry;
455}
456
457static void put_entry_bdev(struct zram *zram, unsigned long entry)
458{
459 int was_set;
460
461 spin_lock(&zram->bitmap_lock);
462 was_set = test_and_clear_bit(entry, zram->bitmap);
463 spin_unlock(&zram->bitmap_lock);
464 WARN_ON_ONCE(!was_set);
465}
466
467static void zram_page_end_io(struct bio *bio)
468{
469 struct page *page = bio_first_page_all(bio);
470
471 page_endio(page, op_is_write(bio_op(bio)),
472 blk_status_to_errno(bio->bi_status));
473 bio_put(bio);
474}
475
476
477
478
479static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec,
480 unsigned long entry, struct bio *parent)
481{
482 struct bio *bio;
483
484 bio = bio_alloc(GFP_ATOMIC, 1);
485 if (!bio)
486 return -ENOMEM;
487
488 bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
489 bio_set_dev(bio, zram->bdev);
490 if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) {
491 bio_put(bio);
492 return -EIO;
493 }
494
495 if (!parent) {
496 bio->bi_opf = REQ_OP_READ;
497 bio->bi_end_io = zram_page_end_io;
498 } else {
499 bio->bi_opf = parent->bi_opf;
500 bio_chain(bio, parent);
501 }
502
503 submit_bio(bio);
504 return 1;
505}
506
507struct zram_work {
508 struct work_struct work;
509 struct zram *zram;
510 unsigned long entry;
511 struct bio *bio;
512};
513
514#if PAGE_SIZE != 4096
515static void zram_sync_read(struct work_struct *work)
516{
517 struct bio_vec bvec;
518 struct zram_work *zw = container_of(work, struct zram_work, work);
519 struct zram *zram = zw->zram;
520 unsigned long entry = zw->entry;
521 struct bio *bio = zw->bio;
522
523 read_from_bdev_async(zram, &bvec, entry, bio);
524}
525
526
527
528
529
530
531static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
532 unsigned long entry, struct bio *bio)
533{
534 struct zram_work work;
535
536 work.zram = zram;
537 work.entry = entry;
538 work.bio = bio;
539
540 INIT_WORK_ONSTACK(&work.work, zram_sync_read);
541 queue_work(system_unbound_wq, &work.work);
542 flush_work(&work.work);
543 destroy_work_on_stack(&work.work);
544
545 return 1;
546}
547#else
548static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
549 unsigned long entry, struct bio *bio)
550{
551 WARN_ON(1);
552 return -EIO;
553}
554#endif
555
556static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
557 unsigned long entry, struct bio *parent, bool sync)
558{
559 if (sync)
560 return read_from_bdev_sync(zram, bvec, entry, parent);
561 else
562 return read_from_bdev_async(zram, bvec, entry, parent);
563}
564
565static int write_to_bdev(struct zram *zram, struct bio_vec *bvec,
566 u32 index, struct bio *parent,
567 unsigned long *pentry)
568{
569 struct bio *bio;
570 unsigned long entry;
571
572 bio = bio_alloc(GFP_ATOMIC, 1);
573 if (!bio)
574 return -ENOMEM;
575
576 entry = get_entry_bdev(zram);
577 if (!entry) {
578 bio_put(bio);
579 return -ENOSPC;
580 }
581
582 bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
583 bio_set_dev(bio, zram->bdev);
584 if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len,
585 bvec->bv_offset)) {
586 bio_put(bio);
587 put_entry_bdev(zram, entry);
588 return -EIO;
589 }
590
591 if (!parent) {
592 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
593 bio->bi_end_io = zram_page_end_io;
594 } else {
595 bio->bi_opf = parent->bi_opf;
596 bio_chain(bio, parent);
597 }
598
599 submit_bio(bio);
600 *pentry = entry;
601
602 return 0;
603}
604
605static void zram_wb_clear(struct zram *zram, u32 index)
606{
607 unsigned long entry;
608
609 zram_clear_flag(zram, index, ZRAM_WB);
610 entry = zram_get_element(zram, index);
611 zram_set_element(zram, index, 0);
612 put_entry_bdev(zram, entry);
613}
614
615#else
616static bool zram_wb_enabled(struct zram *zram) { return false; }
617static inline void reset_bdev(struct zram *zram) {};
618static int write_to_bdev(struct zram *zram, struct bio_vec *bvec,
619 u32 index, struct bio *parent,
620 unsigned long *pentry)
621
622{
623 return -EIO;
624}
625
626static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
627 unsigned long entry, struct bio *parent, bool sync)
628{
629 return -EIO;
630}
631static void zram_wb_clear(struct zram *zram, u32 index) {}
632#endif
633
634#ifdef CONFIG_ZRAM_MEMORY_TRACKING
635
636static struct dentry *zram_debugfs_root;
637
638static void zram_debugfs_create(void)
639{
640 zram_debugfs_root = debugfs_create_dir("zram", NULL);
641}
642
643static void zram_debugfs_destroy(void)
644{
645 debugfs_remove_recursive(zram_debugfs_root);
646}
647
648static void zram_accessed(struct zram *zram, u32 index)
649{
650 zram->table[index].ac_time = ktime_get_boottime();
651}
652
653static void zram_reset_access(struct zram *zram, u32 index)
654{
655 zram->table[index].ac_time = 0;
656}
657
658static ssize_t read_block_state(struct file *file, char __user *buf,
659 size_t count, loff_t *ppos)
660{
661 char *kbuf;
662 ssize_t index, written = 0;
663 struct zram *zram = file->private_data;
664 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
665 struct timespec64 ts;
666
667 kbuf = kvmalloc(count, GFP_KERNEL);
668 if (!kbuf)
669 return -ENOMEM;
670
671 down_read(&zram->init_lock);
672 if (!init_done(zram)) {
673 up_read(&zram->init_lock);
674 kvfree(kbuf);
675 return -EINVAL;
676 }
677
678 for (index = *ppos; index < nr_pages; index++) {
679 int copied;
680
681 zram_slot_lock(zram, index);
682 if (!zram_allocated(zram, index))
683 goto next;
684
685 ts = ktime_to_timespec64(zram->table[index].ac_time);
686 copied = snprintf(kbuf + written, count,
687 "%12zd %12lld.%06lu %c%c%c\n",
688 index, (s64)ts.tv_sec,
689 ts.tv_nsec / NSEC_PER_USEC,
690 zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
691 zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
692 zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.');
693
694 if (count < copied) {
695 zram_slot_unlock(zram, index);
696 break;
697 }
698 written += copied;
699 count -= copied;
700next:
701 zram_slot_unlock(zram, index);
702 *ppos += 1;
703 }
704
705 up_read(&zram->init_lock);
706 if (copy_to_user(buf, kbuf, written))
707 written = -EFAULT;
708 kvfree(kbuf);
709
710 return written;
711}
712
713static const struct file_operations proc_zram_block_state_op = {
714 .open = simple_open,
715 .read = read_block_state,
716 .llseek = default_llseek,
717};
718
719static void zram_debugfs_register(struct zram *zram)
720{
721 if (!zram_debugfs_root)
722 return;
723
724 zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
725 zram_debugfs_root);
726 debugfs_create_file("block_state", 0400, zram->debugfs_dir,
727 zram, &proc_zram_block_state_op);
728}
729
730static void zram_debugfs_unregister(struct zram *zram)
731{
732 debugfs_remove_recursive(zram->debugfs_dir);
733}
734#else
735static void zram_debugfs_create(void) {};
736static void zram_debugfs_destroy(void) {};
737static void zram_accessed(struct zram *zram, u32 index) {};
738static void zram_reset_access(struct zram *zram, u32 index) {};
739static void zram_debugfs_register(struct zram *zram) {};
740static void zram_debugfs_unregister(struct zram *zram) {};
741#endif
742
743
744
745
746
747
748
749
750
751
752static ssize_t max_comp_streams_show(struct device *dev,
753 struct device_attribute *attr, char *buf)
754{
755 return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
756}
757
758static ssize_t max_comp_streams_store(struct device *dev,
759 struct device_attribute *attr, const char *buf, size_t len)
760{
761 return len;
762}
763
764static ssize_t comp_algorithm_show(struct device *dev,
765 struct device_attribute *attr, char *buf)
766{
767 size_t sz;
768 struct zram *zram = dev_to_zram(dev);
769
770 down_read(&zram->init_lock);
771 sz = zcomp_available_show(zram->compressor, buf);
772 up_read(&zram->init_lock);
773
774 return sz;
775}
776
777static ssize_t comp_algorithm_store(struct device *dev,
778 struct device_attribute *attr, const char *buf, size_t len)
779{
780 struct zram *zram = dev_to_zram(dev);
781 char compressor[ARRAY_SIZE(zram->compressor)];
782 size_t sz;
783
784 strlcpy(compressor, buf, sizeof(compressor));
785
786 sz = strlen(compressor);
787 if (sz > 0 && compressor[sz - 1] == '\n')
788 compressor[sz - 1] = 0x00;
789
790 if (!zcomp_available_algorithm(compressor))
791 return -EINVAL;
792
793 down_write(&zram->init_lock);
794 if (init_done(zram)) {
795 up_write(&zram->init_lock);
796 pr_info("Can't change algorithm for initialized device\n");
797 return -EBUSY;
798 }
799
800 strcpy(zram->compressor, compressor);
801 up_write(&zram->init_lock);
802 return len;
803}
804
805static ssize_t compact_store(struct device *dev,
806 struct device_attribute *attr, const char *buf, size_t len)
807{
808 struct zram *zram = dev_to_zram(dev);
809
810 down_read(&zram->init_lock);
811 if (!init_done(zram)) {
812 up_read(&zram->init_lock);
813 return -EINVAL;
814 }
815
816 zs_compact(zram->mem_pool);
817 up_read(&zram->init_lock);
818
819 return len;
820}
821
822static ssize_t io_stat_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
824{
825 struct zram *zram = dev_to_zram(dev);
826 ssize_t ret;
827
828 down_read(&zram->init_lock);
829 ret = scnprintf(buf, PAGE_SIZE,
830 "%8llu %8llu %8llu %8llu\n",
831 (u64)atomic64_read(&zram->stats.failed_reads),
832 (u64)atomic64_read(&zram->stats.failed_writes),
833 (u64)atomic64_read(&zram->stats.invalid_io),
834 (u64)atomic64_read(&zram->stats.notify_free));
835 up_read(&zram->init_lock);
836
837 return ret;
838}
839
840static ssize_t mm_stat_show(struct device *dev,
841 struct device_attribute *attr, char *buf)
842{
843 struct zram *zram = dev_to_zram(dev);
844 struct zs_pool_stats pool_stats;
845 u64 orig_size, mem_used = 0;
846 long max_used;
847 ssize_t ret;
848
849 memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
850
851 down_read(&zram->init_lock);
852 if (init_done(zram)) {
853 mem_used = zs_get_total_pages(zram->mem_pool);
854 zs_pool_stats(zram->mem_pool, &pool_stats);
855 }
856
857 orig_size = atomic64_read(&zram->stats.pages_stored);
858 max_used = atomic_long_read(&zram->stats.max_used_pages);
859
860 ret = scnprintf(buf, PAGE_SIZE,
861 "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu\n",
862 orig_size << PAGE_SHIFT,
863 (u64)atomic64_read(&zram->stats.compr_data_size),
864 mem_used << PAGE_SHIFT,
865 zram->limit_pages << PAGE_SHIFT,
866 max_used << PAGE_SHIFT,
867 (u64)atomic64_read(&zram->stats.same_pages),
868 pool_stats.pages_compacted,
869 (u64)atomic64_read(&zram->stats.huge_pages));
870 up_read(&zram->init_lock);
871
872 return ret;
873}
874
875static ssize_t debug_stat_show(struct device *dev,
876 struct device_attribute *attr, char *buf)
877{
878 int version = 1;
879 struct zram *zram = dev_to_zram(dev);
880 ssize_t ret;
881
882 down_read(&zram->init_lock);
883 ret = scnprintf(buf, PAGE_SIZE,
884 "version: %d\n%8llu\n",
885 version,
886 (u64)atomic64_read(&zram->stats.writestall));
887 up_read(&zram->init_lock);
888
889 return ret;
890}
891
892static DEVICE_ATTR_RO(io_stat);
893static DEVICE_ATTR_RO(mm_stat);
894static DEVICE_ATTR_RO(debug_stat);
895
896static void zram_meta_free(struct zram *zram, u64 disksize)
897{
898 size_t num_pages = disksize >> PAGE_SHIFT;
899 size_t index;
900
901
902 for (index = 0; index < num_pages; index++)
903 zram_free_page(zram, index);
904
905 zs_destroy_pool(zram->mem_pool);
906 vfree(zram->table);
907}
908
909static bool zram_meta_alloc(struct zram *zram, u64 disksize)
910{
911 size_t num_pages;
912
913 num_pages = disksize >> PAGE_SHIFT;
914 zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
915 if (!zram->table)
916 return false;
917
918 zram->mem_pool = zs_create_pool(zram->disk->disk_name);
919 if (!zram->mem_pool) {
920 vfree(zram->table);
921 return false;
922 }
923
924 if (!huge_class_size)
925 huge_class_size = zs_huge_class_size(zram->mem_pool);
926 return true;
927}
928
929
930
931
932
933
934static void zram_free_page(struct zram *zram, size_t index)
935{
936 unsigned long handle;
937
938 zram_reset_access(zram, index);
939
940 if (zram_test_flag(zram, index, ZRAM_HUGE)) {
941 zram_clear_flag(zram, index, ZRAM_HUGE);
942 atomic64_dec(&zram->stats.huge_pages);
943 }
944
945 if (zram_wb_enabled(zram) && zram_test_flag(zram, index, ZRAM_WB)) {
946 zram_wb_clear(zram, index);
947 atomic64_dec(&zram->stats.pages_stored);
948 return;
949 }
950
951
952
953
954
955 if (zram_test_flag(zram, index, ZRAM_SAME)) {
956 zram_clear_flag(zram, index, ZRAM_SAME);
957 zram_set_element(zram, index, 0);
958 atomic64_dec(&zram->stats.same_pages);
959 atomic64_dec(&zram->stats.pages_stored);
960 return;
961 }
962
963 handle = zram_get_handle(zram, index);
964 if (!handle)
965 return;
966
967 zs_free(zram->mem_pool, handle);
968
969 atomic64_sub(zram_get_obj_size(zram, index),
970 &zram->stats.compr_data_size);
971 atomic64_dec(&zram->stats.pages_stored);
972
973 zram_set_handle(zram, index, 0);
974 zram_set_obj_size(zram, index, 0);
975}
976
977static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
978 struct bio *bio, bool partial_io)
979{
980 int ret;
981 unsigned long handle;
982 unsigned int size;
983 void *src, *dst;
984
985 if (zram_wb_enabled(zram)) {
986 zram_slot_lock(zram, index);
987 if (zram_test_flag(zram, index, ZRAM_WB)) {
988 struct bio_vec bvec;
989
990 zram_slot_unlock(zram, index);
991
992 bvec.bv_page = page;
993 bvec.bv_len = PAGE_SIZE;
994 bvec.bv_offset = 0;
995 return read_from_bdev(zram, &bvec,
996 zram_get_element(zram, index),
997 bio, partial_io);
998 }
999 zram_slot_unlock(zram, index);
1000 }
1001
1002 zram_slot_lock(zram, index);
1003 handle = zram_get_handle(zram, index);
1004 if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
1005 unsigned long value;
1006 void *mem;
1007
1008 value = handle ? zram_get_element(zram, index) : 0;
1009 mem = kmap_atomic(page);
1010 zram_fill_page(mem, PAGE_SIZE, value);
1011 kunmap_atomic(mem);
1012 zram_slot_unlock(zram, index);
1013 return 0;
1014 }
1015
1016 size = zram_get_obj_size(zram, index);
1017
1018 src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
1019 if (size == PAGE_SIZE) {
1020 dst = kmap_atomic(page);
1021 memcpy(dst, src, PAGE_SIZE);
1022 kunmap_atomic(dst);
1023 ret = 0;
1024 } else {
1025 struct zcomp_strm *zstrm = zcomp_stream_get(zram->comp);
1026
1027 dst = kmap_atomic(page);
1028 ret = zcomp_decompress(zstrm, src, size, dst);
1029 kunmap_atomic(dst);
1030 zcomp_stream_put(zram->comp);
1031 }
1032 zs_unmap_object(zram->mem_pool, handle);
1033 zram_slot_unlock(zram, index);
1034
1035
1036 if (unlikely(ret))
1037 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
1038
1039 return ret;
1040}
1041
1042static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
1043 u32 index, int offset, struct bio *bio)
1044{
1045 int ret;
1046 struct page *page;
1047
1048 page = bvec->bv_page;
1049 if (is_partial_io(bvec)) {
1050
1051 page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
1052 if (!page)
1053 return -ENOMEM;
1054 }
1055
1056 ret = __zram_bvec_read(zram, page, index, bio, is_partial_io(bvec));
1057 if (unlikely(ret))
1058 goto out;
1059
1060 if (is_partial_io(bvec)) {
1061 void *dst = kmap_atomic(bvec->bv_page);
1062 void *src = kmap_atomic(page);
1063
1064 memcpy(dst + bvec->bv_offset, src + offset, bvec->bv_len);
1065 kunmap_atomic(src);
1066 kunmap_atomic(dst);
1067 }
1068out:
1069 if (is_partial_io(bvec))
1070 __free_page(page);
1071
1072 return ret;
1073}
1074
1075static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1076 u32 index, struct bio *bio)
1077{
1078 int ret = 0;
1079 unsigned long alloced_pages;
1080 unsigned long handle = 0;
1081 unsigned int comp_len = 0;
1082 void *src, *dst, *mem;
1083 struct zcomp_strm *zstrm;
1084 struct page *page = bvec->bv_page;
1085 unsigned long element = 0;
1086 enum zram_pageflags flags = 0;
1087 bool allow_wb = true;
1088
1089 mem = kmap_atomic(page);
1090 if (page_same_filled(mem, &element)) {
1091 kunmap_atomic(mem);
1092
1093 flags = ZRAM_SAME;
1094 atomic64_inc(&zram->stats.same_pages);
1095 goto out;
1096 }
1097 kunmap_atomic(mem);
1098
1099compress_again:
1100 zstrm = zcomp_stream_get(zram->comp);
1101 src = kmap_atomic(page);
1102 ret = zcomp_compress(zstrm, src, &comp_len);
1103 kunmap_atomic(src);
1104
1105 if (unlikely(ret)) {
1106 zcomp_stream_put(zram->comp);
1107 pr_err("Compression failed! err=%d\n", ret);
1108 zs_free(zram->mem_pool, handle);
1109 return ret;
1110 }
1111
1112 if (unlikely(comp_len >= huge_class_size)) {
1113 comp_len = PAGE_SIZE;
1114 if (zram_wb_enabled(zram) && allow_wb) {
1115 zcomp_stream_put(zram->comp);
1116 ret = write_to_bdev(zram, bvec, index, bio, &element);
1117 if (!ret) {
1118 flags = ZRAM_WB;
1119 ret = 1;
1120 goto out;
1121 }
1122 allow_wb = false;
1123 goto compress_again;
1124 }
1125 }
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 if (!handle)
1141 handle = zs_malloc(zram->mem_pool, comp_len,
1142 __GFP_KSWAPD_RECLAIM |
1143 __GFP_NOWARN |
1144 __GFP_HIGHMEM |
1145 __GFP_MOVABLE);
1146 if (!handle) {
1147 zcomp_stream_put(zram->comp);
1148 atomic64_inc(&zram->stats.writestall);
1149 handle = zs_malloc(zram->mem_pool, comp_len,
1150 GFP_NOIO | __GFP_HIGHMEM |
1151 __GFP_MOVABLE);
1152 if (handle)
1153 goto compress_again;
1154 return -ENOMEM;
1155 }
1156
1157 alloced_pages = zs_get_total_pages(zram->mem_pool);
1158 update_used_max(zram, alloced_pages);
1159
1160 if (zram->limit_pages && alloced_pages > zram->limit_pages) {
1161 zcomp_stream_put(zram->comp);
1162 zs_free(zram->mem_pool, handle);
1163 return -ENOMEM;
1164 }
1165
1166 dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
1167
1168 src = zstrm->buffer;
1169 if (comp_len == PAGE_SIZE)
1170 src = kmap_atomic(page);
1171 memcpy(dst, src, comp_len);
1172 if (comp_len == PAGE_SIZE)
1173 kunmap_atomic(src);
1174
1175 zcomp_stream_put(zram->comp);
1176 zs_unmap_object(zram->mem_pool, handle);
1177 atomic64_add(comp_len, &zram->stats.compr_data_size);
1178out:
1179
1180
1181
1182
1183 zram_slot_lock(zram, index);
1184 zram_free_page(zram, index);
1185
1186 if (comp_len == PAGE_SIZE) {
1187 zram_set_flag(zram, index, ZRAM_HUGE);
1188 atomic64_inc(&zram->stats.huge_pages);
1189 }
1190
1191 if (flags) {
1192 zram_set_flag(zram, index, flags);
1193 zram_set_element(zram, index, element);
1194 } else {
1195 zram_set_handle(zram, index, handle);
1196 zram_set_obj_size(zram, index, comp_len);
1197 }
1198 zram_slot_unlock(zram, index);
1199
1200
1201 atomic64_inc(&zram->stats.pages_stored);
1202 return ret;
1203}
1204
1205static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1206 u32 index, int offset, struct bio *bio)
1207{
1208 int ret;
1209 struct page *page = NULL;
1210 void *src;
1211 struct bio_vec vec;
1212
1213 vec = *bvec;
1214 if (is_partial_io(bvec)) {
1215 void *dst;
1216
1217
1218
1219
1220 page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
1221 if (!page)
1222 return -ENOMEM;
1223
1224 ret = __zram_bvec_read(zram, page, index, bio, true);
1225 if (ret)
1226 goto out;
1227
1228 src = kmap_atomic(bvec->bv_page);
1229 dst = kmap_atomic(page);
1230 memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len);
1231 kunmap_atomic(dst);
1232 kunmap_atomic(src);
1233
1234 vec.bv_page = page;
1235 vec.bv_len = PAGE_SIZE;
1236 vec.bv_offset = 0;
1237 }
1238
1239 ret = __zram_bvec_write(zram, &vec, index, bio);
1240out:
1241 if (is_partial_io(bvec))
1242 __free_page(page);
1243 return ret;
1244}
1245
1246
1247
1248
1249
1250
1251static void zram_bio_discard(struct zram *zram, u32 index,
1252 int offset, struct bio *bio)
1253{
1254 size_t n = bio->bi_iter.bi_size;
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 if (offset) {
1267 if (n <= (PAGE_SIZE - offset))
1268 return;
1269
1270 n -= (PAGE_SIZE - offset);
1271 index++;
1272 }
1273
1274 while (n >= PAGE_SIZE) {
1275 zram_slot_lock(zram, index);
1276 zram_free_page(zram, index);
1277 zram_slot_unlock(zram, index);
1278 atomic64_inc(&zram->stats.notify_free);
1279 index++;
1280 n -= PAGE_SIZE;
1281 }
1282}
1283
1284
1285
1286
1287
1288
1289static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
1290 int offset, unsigned int op, struct bio *bio)
1291{
1292 unsigned long start_time = jiffies;
1293 struct request_queue *q = zram->disk->queue;
1294 int ret;
1295
1296 generic_start_io_acct(q, op, bvec->bv_len >> SECTOR_SHIFT,
1297 &zram->disk->part0);
1298
1299 if (!op_is_write(op)) {
1300 atomic64_inc(&zram->stats.num_reads);
1301 ret = zram_bvec_read(zram, bvec, index, offset, bio);
1302 flush_dcache_page(bvec->bv_page);
1303 } else {
1304 atomic64_inc(&zram->stats.num_writes);
1305 ret = zram_bvec_write(zram, bvec, index, offset, bio);
1306 }
1307
1308 generic_end_io_acct(q, op, &zram->disk->part0, start_time);
1309
1310 zram_slot_lock(zram, index);
1311 zram_accessed(zram, index);
1312 zram_slot_unlock(zram, index);
1313
1314 if (unlikely(ret < 0)) {
1315 if (!op_is_write(op))
1316 atomic64_inc(&zram->stats.failed_reads);
1317 else
1318 atomic64_inc(&zram->stats.failed_writes);
1319 }
1320
1321 return ret;
1322}
1323
1324static void __zram_make_request(struct zram *zram, struct bio *bio)
1325{
1326 int offset;
1327 u32 index;
1328 struct bio_vec bvec;
1329 struct bvec_iter iter;
1330
1331 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1332 offset = (bio->bi_iter.bi_sector &
1333 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
1334
1335 switch (bio_op(bio)) {
1336 case REQ_OP_DISCARD:
1337 case REQ_OP_WRITE_ZEROES:
1338 zram_bio_discard(zram, index, offset, bio);
1339 bio_endio(bio);
1340 return;
1341 default:
1342 break;
1343 }
1344
1345 bio_for_each_segment(bvec, bio, iter) {
1346 struct bio_vec bv = bvec;
1347 unsigned int unwritten = bvec.bv_len;
1348
1349 do {
1350 bv.bv_len = min_t(unsigned int, PAGE_SIZE - offset,
1351 unwritten);
1352 if (zram_bvec_rw(zram, &bv, index, offset,
1353 bio_op(bio), bio) < 0)
1354 goto out;
1355
1356 bv.bv_offset += bv.bv_len;
1357 unwritten -= bv.bv_len;
1358
1359 update_position(&index, &offset, &bv);
1360 } while (unwritten);
1361 }
1362
1363 bio_endio(bio);
1364 return;
1365
1366out:
1367 bio_io_error(bio);
1368}
1369
1370
1371
1372
1373static blk_qc_t zram_make_request(struct request_queue *queue, struct bio *bio)
1374{
1375 struct zram *zram = queue->queuedata;
1376
1377 if (!valid_io_request(zram, bio->bi_iter.bi_sector,
1378 bio->bi_iter.bi_size)) {
1379 atomic64_inc(&zram->stats.invalid_io);
1380 goto error;
1381 }
1382
1383 __zram_make_request(zram, bio);
1384 return BLK_QC_T_NONE;
1385
1386error:
1387 bio_io_error(bio);
1388 return BLK_QC_T_NONE;
1389}
1390
1391static void zram_slot_free_notify(struct block_device *bdev,
1392 unsigned long index)
1393{
1394 struct zram *zram;
1395
1396 zram = bdev->bd_disk->private_data;
1397
1398 zram_slot_lock(zram, index);
1399 zram_free_page(zram, index);
1400 zram_slot_unlock(zram, index);
1401 atomic64_inc(&zram->stats.notify_free);
1402}
1403
1404static int zram_rw_page(struct block_device *bdev, sector_t sector,
1405 struct page *page, unsigned int op)
1406{
1407 int offset, ret;
1408 u32 index;
1409 struct zram *zram;
1410 struct bio_vec bv;
1411
1412 if (PageTransHuge(page))
1413 return -ENOTSUPP;
1414 zram = bdev->bd_disk->private_data;
1415
1416 if (!valid_io_request(zram, sector, PAGE_SIZE)) {
1417 atomic64_inc(&zram->stats.invalid_io);
1418 ret = -EINVAL;
1419 goto out;
1420 }
1421
1422 index = sector >> SECTORS_PER_PAGE_SHIFT;
1423 offset = (sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
1424
1425 bv.bv_page = page;
1426 bv.bv_len = PAGE_SIZE;
1427 bv.bv_offset = 0;
1428
1429 ret = zram_bvec_rw(zram, &bv, index, offset, op, NULL);
1430out:
1431
1432
1433
1434
1435
1436
1437
1438
1439 if (unlikely(ret < 0))
1440 return ret;
1441
1442 switch (ret) {
1443 case 0:
1444 page_endio(page, op_is_write(op), 0);
1445 break;
1446 case 1:
1447 ret = 0;
1448 break;
1449 default:
1450 WARN_ON(1);
1451 }
1452 return ret;
1453}
1454
1455static void zram_reset_device(struct zram *zram)
1456{
1457 struct zcomp *comp;
1458 u64 disksize;
1459
1460 down_write(&zram->init_lock);
1461
1462 zram->limit_pages = 0;
1463
1464 if (!init_done(zram)) {
1465 up_write(&zram->init_lock);
1466 return;
1467 }
1468
1469 comp = zram->comp;
1470 disksize = zram->disksize;
1471 zram->disksize = 0;
1472
1473 set_capacity(zram->disk, 0);
1474 part_stat_set_all(&zram->disk->part0, 0);
1475
1476 up_write(&zram->init_lock);
1477
1478 zram_meta_free(zram, disksize);
1479 memset(&zram->stats, 0, sizeof(zram->stats));
1480 zcomp_destroy(comp);
1481 reset_bdev(zram);
1482}
1483
1484static ssize_t disksize_store(struct device *dev,
1485 struct device_attribute *attr, const char *buf, size_t len)
1486{
1487 u64 disksize;
1488 struct zcomp *comp;
1489 struct zram *zram = dev_to_zram(dev);
1490 int err;
1491
1492 disksize = memparse(buf, NULL);
1493 if (!disksize)
1494 return -EINVAL;
1495
1496 down_write(&zram->init_lock);
1497 if (init_done(zram)) {
1498 pr_info("Cannot change disksize for initialized device\n");
1499 err = -EBUSY;
1500 goto out_unlock;
1501 }
1502
1503 disksize = PAGE_ALIGN(disksize);
1504 if (!zram_meta_alloc(zram, disksize)) {
1505 err = -ENOMEM;
1506 goto out_unlock;
1507 }
1508
1509 comp = zcomp_create(zram->compressor);
1510 if (IS_ERR(comp)) {
1511 pr_err("Cannot initialise %s compressing backend\n",
1512 zram->compressor);
1513 err = PTR_ERR(comp);
1514 goto out_free_meta;
1515 }
1516
1517 zram->comp = comp;
1518 zram->disksize = disksize;
1519 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
1520
1521 revalidate_disk(zram->disk);
1522 up_write(&zram->init_lock);
1523
1524 return len;
1525
1526out_free_meta:
1527 zram_meta_free(zram, disksize);
1528out_unlock:
1529 up_write(&zram->init_lock);
1530 return err;
1531}
1532
1533static ssize_t reset_store(struct device *dev,
1534 struct device_attribute *attr, const char *buf, size_t len)
1535{
1536 int ret;
1537 unsigned short do_reset;
1538 struct zram *zram;
1539 struct block_device *bdev;
1540
1541 ret = kstrtou16(buf, 10, &do_reset);
1542 if (ret)
1543 return ret;
1544
1545 if (!do_reset)
1546 return -EINVAL;
1547
1548 zram = dev_to_zram(dev);
1549 bdev = bdget_disk(zram->disk, 0);
1550 if (!bdev)
1551 return -ENOMEM;
1552
1553 mutex_lock(&bdev->bd_mutex);
1554
1555 if (bdev->bd_openers || zram->claim) {
1556 mutex_unlock(&bdev->bd_mutex);
1557 bdput(bdev);
1558 return -EBUSY;
1559 }
1560
1561
1562 zram->claim = true;
1563 mutex_unlock(&bdev->bd_mutex);
1564
1565
1566 fsync_bdev(bdev);
1567 zram_reset_device(zram);
1568 revalidate_disk(zram->disk);
1569 bdput(bdev);
1570
1571 mutex_lock(&bdev->bd_mutex);
1572 zram->claim = false;
1573 mutex_unlock(&bdev->bd_mutex);
1574
1575 return len;
1576}
1577
1578static int zram_open(struct block_device *bdev, fmode_t mode)
1579{
1580 int ret = 0;
1581 struct zram *zram;
1582
1583 WARN_ON(!mutex_is_locked(&bdev->bd_mutex));
1584
1585 zram = bdev->bd_disk->private_data;
1586
1587 if (zram->claim)
1588 ret = -EBUSY;
1589
1590 return ret;
1591}
1592
1593static const struct block_device_operations zram_devops = {
1594 .open = zram_open,
1595 .swap_slot_free_notify = zram_slot_free_notify,
1596 .rw_page = zram_rw_page,
1597 .owner = THIS_MODULE
1598};
1599
1600static DEVICE_ATTR_WO(compact);
1601static DEVICE_ATTR_RW(disksize);
1602static DEVICE_ATTR_RO(initstate);
1603static DEVICE_ATTR_WO(reset);
1604static DEVICE_ATTR_WO(mem_limit);
1605static DEVICE_ATTR_WO(mem_used_max);
1606static DEVICE_ATTR_RW(max_comp_streams);
1607static DEVICE_ATTR_RW(comp_algorithm);
1608#ifdef CONFIG_ZRAM_WRITEBACK
1609static DEVICE_ATTR_RW(backing_dev);
1610#endif
1611
1612static struct attribute *zram_disk_attrs[] = {
1613 &dev_attr_disksize.attr,
1614 &dev_attr_initstate.attr,
1615 &dev_attr_reset.attr,
1616 &dev_attr_compact.attr,
1617 &dev_attr_mem_limit.attr,
1618 &dev_attr_mem_used_max.attr,
1619 &dev_attr_max_comp_streams.attr,
1620 &dev_attr_comp_algorithm.attr,
1621#ifdef CONFIG_ZRAM_WRITEBACK
1622 &dev_attr_backing_dev.attr,
1623#endif
1624 &dev_attr_io_stat.attr,
1625 &dev_attr_mm_stat.attr,
1626 &dev_attr_debug_stat.attr,
1627 NULL,
1628};
1629
1630static const struct attribute_group zram_disk_attr_group = {
1631 .attrs = zram_disk_attrs,
1632};
1633
1634
1635
1636
1637
1638static int zram_add(void)
1639{
1640 struct zram *zram;
1641 struct request_queue *queue;
1642 int ret, device_id;
1643
1644 zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
1645 if (!zram)
1646 return -ENOMEM;
1647
1648 ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
1649 if (ret < 0)
1650 goto out_free_dev;
1651 device_id = ret;
1652
1653 init_rwsem(&zram->init_lock);
1654
1655 queue = blk_alloc_queue(GFP_KERNEL);
1656 if (!queue) {
1657 pr_err("Error allocating disk queue for device %d\n",
1658 device_id);
1659 ret = -ENOMEM;
1660 goto out_free_idr;
1661 }
1662
1663 blk_queue_make_request(queue, zram_make_request);
1664
1665
1666 zram->disk = alloc_disk(1);
1667 if (!zram->disk) {
1668 pr_err("Error allocating disk structure for device %d\n",
1669 device_id);
1670 ret = -ENOMEM;
1671 goto out_free_queue;
1672 }
1673
1674 zram->disk->major = zram_major;
1675 zram->disk->first_minor = device_id;
1676 zram->disk->fops = &zram_devops;
1677 zram->disk->queue = queue;
1678 zram->disk->queue->queuedata = zram;
1679 zram->disk->private_data = zram;
1680 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
1681
1682
1683 set_capacity(zram->disk, 0);
1684
1685 blk_queue_flag_set(QUEUE_FLAG_NONROT, zram->disk->queue);
1686 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
1687
1688
1689
1690
1691
1692 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
1693 blk_queue_logical_block_size(zram->disk->queue,
1694 ZRAM_LOGICAL_BLOCK_SIZE);
1695 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1696 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
1697 zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1698 blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX);
1699 blk_queue_flag_set(QUEUE_FLAG_DISCARD, zram->disk->queue);
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1710 blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
1711
1712 zram->disk->queue->backing_dev_info->capabilities |=
1713 (BDI_CAP_STABLE_WRITES | BDI_CAP_SYNCHRONOUS_IO);
1714 add_disk(zram->disk);
1715
1716 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
1717 &zram_disk_attr_group);
1718 if (ret < 0) {
1719 pr_err("Error creating sysfs group for device %d\n",
1720 device_id);
1721 goto out_free_disk;
1722 }
1723 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
1724
1725 zram_debugfs_register(zram);
1726 pr_info("Added device: %s\n", zram->disk->disk_name);
1727 return device_id;
1728
1729out_free_disk:
1730 del_gendisk(zram->disk);
1731 put_disk(zram->disk);
1732out_free_queue:
1733 blk_cleanup_queue(queue);
1734out_free_idr:
1735 idr_remove(&zram_index_idr, device_id);
1736out_free_dev:
1737 kfree(zram);
1738 return ret;
1739}
1740
1741static int zram_remove(struct zram *zram)
1742{
1743 struct block_device *bdev;
1744
1745 bdev = bdget_disk(zram->disk, 0);
1746 if (!bdev)
1747 return -ENOMEM;
1748
1749 mutex_lock(&bdev->bd_mutex);
1750 if (bdev->bd_openers || zram->claim) {
1751 mutex_unlock(&bdev->bd_mutex);
1752 bdput(bdev);
1753 return -EBUSY;
1754 }
1755
1756 zram->claim = true;
1757 mutex_unlock(&bdev->bd_mutex);
1758
1759 zram_debugfs_unregister(zram);
1760
1761
1762
1763
1764
1765
1766
1767 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
1768 &zram_disk_attr_group);
1769
1770
1771 fsync_bdev(bdev);
1772 zram_reset_device(zram);
1773 bdput(bdev);
1774
1775 pr_info("Removed device: %s\n", zram->disk->disk_name);
1776
1777 del_gendisk(zram->disk);
1778 blk_cleanup_queue(zram->disk->queue);
1779 put_disk(zram->disk);
1780 kfree(zram);
1781 return 0;
1782}
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792static ssize_t hot_add_show(struct class *class,
1793 struct class_attribute *attr,
1794 char *buf)
1795{
1796 int ret;
1797
1798 mutex_lock(&zram_index_mutex);
1799 ret = zram_add();
1800 mutex_unlock(&zram_index_mutex);
1801
1802 if (ret < 0)
1803 return ret;
1804 return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
1805}
1806static CLASS_ATTR_RO(hot_add);
1807
1808static ssize_t hot_remove_store(struct class *class,
1809 struct class_attribute *attr,
1810 const char *buf,
1811 size_t count)
1812{
1813 struct zram *zram;
1814 int ret, dev_id;
1815
1816
1817 ret = kstrtoint(buf, 10, &dev_id);
1818 if (ret)
1819 return ret;
1820 if (dev_id < 0)
1821 return -EINVAL;
1822
1823 mutex_lock(&zram_index_mutex);
1824
1825 zram = idr_find(&zram_index_idr, dev_id);
1826 if (zram) {
1827 ret = zram_remove(zram);
1828 if (!ret)
1829 idr_remove(&zram_index_idr, dev_id);
1830 } else {
1831 ret = -ENODEV;
1832 }
1833
1834 mutex_unlock(&zram_index_mutex);
1835 return ret ? ret : count;
1836}
1837static CLASS_ATTR_WO(hot_remove);
1838
1839static struct attribute *zram_control_class_attrs[] = {
1840 &class_attr_hot_add.attr,
1841 &class_attr_hot_remove.attr,
1842 NULL,
1843};
1844ATTRIBUTE_GROUPS(zram_control_class);
1845
1846static struct class zram_control_class = {
1847 .name = "zram-control",
1848 .owner = THIS_MODULE,
1849 .class_groups = zram_control_class_groups,
1850};
1851
1852static int zram_remove_cb(int id, void *ptr, void *data)
1853{
1854 zram_remove(ptr);
1855 return 0;
1856}
1857
1858static void destroy_devices(void)
1859{
1860 class_unregister(&zram_control_class);
1861 idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
1862 zram_debugfs_destroy();
1863 idr_destroy(&zram_index_idr);
1864 unregister_blkdev(zram_major, "zram");
1865 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
1866}
1867
1868static int __init zram_init(void)
1869{
1870 int ret;
1871
1872 ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
1873 zcomp_cpu_up_prepare, zcomp_cpu_dead);
1874 if (ret < 0)
1875 return ret;
1876
1877 ret = class_register(&zram_control_class);
1878 if (ret) {
1879 pr_err("Unable to register zram-control class\n");
1880 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
1881 return ret;
1882 }
1883
1884 zram_debugfs_create();
1885 zram_major = register_blkdev(0, "zram");
1886 if (zram_major <= 0) {
1887 pr_err("Unable to get major number\n");
1888 class_unregister(&zram_control_class);
1889 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
1890 return -EBUSY;
1891 }
1892
1893 while (num_devices != 0) {
1894 mutex_lock(&zram_index_mutex);
1895 ret = zram_add();
1896 mutex_unlock(&zram_index_mutex);
1897 if (ret < 0)
1898 goto out_error;
1899 num_devices--;
1900 }
1901
1902 return 0;
1903
1904out_error:
1905 destroy_devices();
1906 return ret;
1907}
1908
1909static void __exit zram_exit(void)
1910{
1911 destroy_devices();
1912}
1913
1914module_init(zram_init);
1915module_exit(zram_exit);
1916
1917module_param(num_devices, uint, 0);
1918MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
1919
1920MODULE_LICENSE("Dual BSD/GPL");
1921MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
1922MODULE_DESCRIPTION("Compressed RAM Block Device");
1923