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#ifdef CONFIG_ZRAM_DEBUG
19#define DEBUG
20#endif
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/bio.h>
25#include <linux/bitops.h>
26#include <linux/blkdev.h>
27#include <linux/buffer_head.h>
28#include <linux/device.h>
29#include <linux/genhd.h>
30#include <linux/highmem.h>
31#include <linux/slab.h>
32#include <linux/string.h>
33#include <linux/vmalloc.h>
34#include <linux/err.h>
35
36#include "zram_drv.h"
37
38/* Globals */
39static int zram_major;
40static struct zram *zram_devices;
41static const char *default_compressor = "lzo";
42
43/* Module params (documentation at end) */
44static unsigned int num_devices = 1;
45
46static inline void deprecated_attr_warn(const char *name)
47{
48	pr_warn_once("%d (%s) Attribute %s (and others) will be removed. %s\n",
49			task_pid_nr(current),
50			current->comm,
51			name,
52			"See zram documentation.");
53}
54
55#define ZRAM_ATTR_RO(name)						\
56static ssize_t name##_show(struct device *d,		\
57				struct device_attribute *attr, char *b)	\
58{									\
59	struct zram *zram = dev_to_zram(d);				\
60									\
61	deprecated_attr_warn(__stringify(name));			\
62	return scnprintf(b, PAGE_SIZE, "%llu\n",			\
63		(u64)atomic64_read(&zram->stats.name));			\
64}									\
65static DEVICE_ATTR_RO(name);
66
67static inline bool init_done(struct zram *zram)
68{
69	return zram->disksize;
70}
71
72static inline struct zram *dev_to_zram(struct device *dev)
73{
74	return (struct zram *)dev_to_disk(dev)->private_data;
75}
76
77static ssize_t compact_store(struct device *dev,
78		struct device_attribute *attr, const char *buf, size_t len)
79{
80	unsigned long nr_migrated;
81	struct zram *zram = dev_to_zram(dev);
82	struct zram_meta *meta;
83
84	down_read(&zram->init_lock);
85	if (!init_done(zram)) {
86		up_read(&zram->init_lock);
87		return -EINVAL;
88	}
89
90	meta = zram->meta;
91	nr_migrated = zs_compact(meta->mem_pool);
92	atomic64_add(nr_migrated, &zram->stats.num_migrated);
93	up_read(&zram->init_lock);
94
95	return len;
96}
97
98static ssize_t disksize_show(struct device *dev,
99		struct device_attribute *attr, char *buf)
100{
101	struct zram *zram = dev_to_zram(dev);
102
103	return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
104}
105
106static ssize_t initstate_show(struct device *dev,
107		struct device_attribute *attr, char *buf)
108{
109	u32 val;
110	struct zram *zram = dev_to_zram(dev);
111
112	down_read(&zram->init_lock);
113	val = init_done(zram);
114	up_read(&zram->init_lock);
115
116	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
117}
118
119static ssize_t orig_data_size_show(struct device *dev,
120		struct device_attribute *attr, char *buf)
121{
122	struct zram *zram = dev_to_zram(dev);
123
124	deprecated_attr_warn("orig_data_size");
125	return scnprintf(buf, PAGE_SIZE, "%llu\n",
126		(u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
127}
128
129static ssize_t mem_used_total_show(struct device *dev,
130		struct device_attribute *attr, char *buf)
131{
132	u64 val = 0;
133	struct zram *zram = dev_to_zram(dev);
134
135	deprecated_attr_warn("mem_used_total");
136	down_read(&zram->init_lock);
137	if (init_done(zram)) {
138		struct zram_meta *meta = zram->meta;
139		val = zs_get_total_pages(meta->mem_pool);
140	}
141	up_read(&zram->init_lock);
142
143	return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
144}
145
146static ssize_t max_comp_streams_show(struct device *dev,
147		struct device_attribute *attr, char *buf)
148{
149	int val;
150	struct zram *zram = dev_to_zram(dev);
151
152	down_read(&zram->init_lock);
153	val = zram->max_comp_streams;
154	up_read(&zram->init_lock);
155
156	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
157}
158
159static ssize_t mem_limit_show(struct device *dev,
160		struct device_attribute *attr, char *buf)
161{
162	u64 val;
163	struct zram *zram = dev_to_zram(dev);
164
165	deprecated_attr_warn("mem_limit");
166	down_read(&zram->init_lock);
167	val = zram->limit_pages;
168	up_read(&zram->init_lock);
169
170	return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
171}
172
173static ssize_t mem_limit_store(struct device *dev,
174		struct device_attribute *attr, const char *buf, size_t len)
175{
176	u64 limit;
177	char *tmp;
178	struct zram *zram = dev_to_zram(dev);
179
180	limit = memparse(buf, &tmp);
181	if (buf == tmp) /* no chars parsed, invalid input */
182		return -EINVAL;
183
184	down_write(&zram->init_lock);
185	zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
186	up_write(&zram->init_lock);
187
188	return len;
189}
190
191static ssize_t mem_used_max_show(struct device *dev,
192		struct device_attribute *attr, char *buf)
193{
194	u64 val = 0;
195	struct zram *zram = dev_to_zram(dev);
196
197	deprecated_attr_warn("mem_used_max");
198	down_read(&zram->init_lock);
199	if (init_done(zram))
200		val = atomic_long_read(&zram->stats.max_used_pages);
201	up_read(&zram->init_lock);
202
203	return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
204}
205
206static ssize_t mem_used_max_store(struct device *dev,
207		struct device_attribute *attr, const char *buf, size_t len)
208{
209	int err;
210	unsigned long val;
211	struct zram *zram = dev_to_zram(dev);
212
213	err = kstrtoul(buf, 10, &val);
214	if (err || val != 0)
215		return -EINVAL;
216
217	down_read(&zram->init_lock);
218	if (init_done(zram)) {
219		struct zram_meta *meta = zram->meta;
220		atomic_long_set(&zram->stats.max_used_pages,
221				zs_get_total_pages(meta->mem_pool));
222	}
223	up_read(&zram->init_lock);
224
225	return len;
226}
227
228static ssize_t max_comp_streams_store(struct device *dev,
229		struct device_attribute *attr, const char *buf, size_t len)
230{
231	int num;
232	struct zram *zram = dev_to_zram(dev);
233	int ret;
234
235	ret = kstrtoint(buf, 0, &num);
236	if (ret < 0)
237		return ret;
238	if (num < 1)
239		return -EINVAL;
240
241	down_write(&zram->init_lock);
242	if (init_done(zram)) {
243		if (!zcomp_set_max_streams(zram->comp, num)) {
244			pr_info("Cannot change max compression streams\n");
245			ret = -EINVAL;
246			goto out;
247		}
248	}
249
250	zram->max_comp_streams = num;
251	ret = len;
252out:
253	up_write(&zram->init_lock);
254	return ret;
255}
256
257static ssize_t comp_algorithm_show(struct device *dev,
258		struct device_attribute *attr, char *buf)
259{
260	size_t sz;
261	struct zram *zram = dev_to_zram(dev);
262
263	down_read(&zram->init_lock);
264	sz = zcomp_available_show(zram->compressor, buf);
265	up_read(&zram->init_lock);
266
267	return sz;
268}
269
270static ssize_t comp_algorithm_store(struct device *dev,
271		struct device_attribute *attr, const char *buf, size_t len)
272{
273	struct zram *zram = dev_to_zram(dev);
274	down_write(&zram->init_lock);
275	if (init_done(zram)) {
276		up_write(&zram->init_lock);
277		pr_info("Can't change algorithm for initialized device\n");
278		return -EBUSY;
279	}
280	strlcpy(zram->compressor, buf, sizeof(zram->compressor));
281	up_write(&zram->init_lock);
282	return len;
283}
284
285/* flag operations needs meta->tb_lock */
286static int zram_test_flag(struct zram_meta *meta, u32 index,
287			enum zram_pageflags flag)
288{
289	return meta->table[index].value & BIT(flag);
290}
291
292static void zram_set_flag(struct zram_meta *meta, u32 index,
293			enum zram_pageflags flag)
294{
295	meta->table[index].value |= BIT(flag);
296}
297
298static void zram_clear_flag(struct zram_meta *meta, u32 index,
299			enum zram_pageflags flag)
300{
301	meta->table[index].value &= ~BIT(flag);
302}
303
304static size_t zram_get_obj_size(struct zram_meta *meta, u32 index)
305{
306	return meta->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
307}
308
309static void zram_set_obj_size(struct zram_meta *meta,
310					u32 index, size_t size)
311{
312	unsigned long flags = meta->table[index].value >> ZRAM_FLAG_SHIFT;
313
314	meta->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
315}
316
317static inline int is_partial_io(struct bio_vec *bvec)
318{
319	return bvec->bv_len != PAGE_SIZE;
320}
321
322/*
323 * Check if request is within bounds and aligned on zram logical blocks.
324 */
325static inline int valid_io_request(struct zram *zram,
326		sector_t start, unsigned int size)
327{
328	u64 end, bound;
329
330	/* unaligned request */
331	if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
332		return 0;
333	if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
334		return 0;
335
336	end = start + (size >> SECTOR_SHIFT);
337	bound = zram->disksize >> SECTOR_SHIFT;
338	/* out of range range */
339	if (unlikely(start >= bound || end > bound || start > end))
340		return 0;
341
342	/* I/O request is valid */
343	return 1;
344}
345
346static void zram_meta_free(struct zram_meta *meta, u64 disksize)
347{
348	size_t num_pages = disksize >> PAGE_SHIFT;
349	size_t index;
350
351	/* Free all pages that are still in this zram device */
352	for (index = 0; index < num_pages; index++) {
353		unsigned long handle = meta->table[index].handle;
354
355		if (!handle)
356			continue;
357
358		zs_free(meta->mem_pool, handle);
359	}
360
361	zs_destroy_pool(meta->mem_pool);
362	vfree(meta->table);
363	kfree(meta);
364}
365
366static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
367{
368	size_t num_pages;
369	char pool_name[8];
370	struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
371
372	if (!meta)
373		return NULL;
374
375	num_pages = disksize >> PAGE_SHIFT;
376	meta->table = vzalloc(num_pages * sizeof(*meta->table));
377	if (!meta->table) {
378		pr_err("Error allocating zram address table\n");
379		goto out_error;
380	}
381
382	snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);
383	meta->mem_pool = zs_create_pool(pool_name, GFP_NOIO | __GFP_HIGHMEM);
384	if (!meta->mem_pool) {
385		pr_err("Error creating memory pool\n");
386		goto out_error;
387	}
388
389	return meta;
390
391out_error:
392	vfree(meta->table);
393	kfree(meta);
394	return NULL;
395}
396
397static inline bool zram_meta_get(struct zram *zram)
398{
399	if (atomic_inc_not_zero(&zram->refcount))
400		return true;
401	return false;
402}
403
404static inline void zram_meta_put(struct zram *zram)
405{
406	atomic_dec(&zram->refcount);
407}
408
409static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
410{
411	if (*offset + bvec->bv_len >= PAGE_SIZE)
412		(*index)++;
413	*offset = (*offset + bvec->bv_len) % PAGE_SIZE;
414}
415
416static int page_zero_filled(void *ptr)
417{
418	unsigned int pos;
419	unsigned long *page;
420
421	page = (unsigned long *)ptr;
422
423	for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
424		if (page[pos])
425			return 0;
426	}
427
428	return 1;
429}
430
431static void handle_zero_page(struct bio_vec *bvec)
432{
433	struct page *page = bvec->bv_page;
434	void *user_mem;
435
436	user_mem = kmap_atomic(page);
437	if (is_partial_io(bvec))
438		memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
439	else
440		clear_page(user_mem);
441	kunmap_atomic(user_mem);
442
443	flush_dcache_page(page);
444}
445
446
447/*
448 * To protect concurrent access to the same index entry,
449 * caller should hold this table index entry's bit_spinlock to
450 * indicate this index entry is accessing.
451 */
452static void zram_free_page(struct zram *zram, size_t index)
453{
454	struct zram_meta *meta = zram->meta;
455	unsigned long handle = meta->table[index].handle;
456
457	if (unlikely(!handle)) {
458		/*
459		 * No memory is allocated for zero filled pages.
460		 * Simply clear zero page flag.
461		 */
462		if (zram_test_flag(meta, index, ZRAM_ZERO)) {
463			zram_clear_flag(meta, index, ZRAM_ZERO);
464			atomic64_dec(&zram->stats.zero_pages);
465		}
466		return;
467	}
468
469	zs_free(meta->mem_pool, handle);
470
471	atomic64_sub(zram_get_obj_size(meta, index),
472			&zram->stats.compr_data_size);
473	atomic64_dec(&zram->stats.pages_stored);
474
475	meta->table[index].handle = 0;
476	zram_set_obj_size(meta, index, 0);
477}
478
479static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
480{
481	int ret = 0;
482	unsigned char *cmem;
483	struct zram_meta *meta = zram->meta;
484	unsigned long handle;
485	size_t size;
486
487	bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
488	handle = meta->table[index].handle;
489	size = zram_get_obj_size(meta, index);
490
491	if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
492		bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
493		clear_page(mem);
494		return 0;
495	}
496
497	cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
498	if (size == PAGE_SIZE)
499		copy_page(mem, cmem);
500	else
501		ret = zcomp_decompress(zram->comp, cmem, size, mem);
502	zs_unmap_object(meta->mem_pool, handle);
503	bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
504
505	/* Should NEVER happen. Return bio error if it does. */
506	if (unlikely(ret)) {
507		pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
508		return ret;
509	}
510
511	return 0;
512}
513
514static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
515			  u32 index, int offset)
516{
517	int ret;
518	struct page *page;
519	unsigned char *user_mem, *uncmem = NULL;
520	struct zram_meta *meta = zram->meta;
521	page = bvec->bv_page;
522
523	bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
524	if (unlikely(!meta->table[index].handle) ||
525			zram_test_flag(meta, index, ZRAM_ZERO)) {
526		bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
527		handle_zero_page(bvec);
528		return 0;
529	}
530	bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
531
532	if (is_partial_io(bvec))
533		/* Use  a temporary buffer to decompress the page */
534		uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
535
536	user_mem = kmap_atomic(page);
537	if (!is_partial_io(bvec))
538		uncmem = user_mem;
539
540	if (!uncmem) {
541		pr_info("Unable to allocate temp memory\n");
542		ret = -ENOMEM;
543		goto out_cleanup;
544	}
545
546	ret = zram_decompress_page(zram, uncmem, index);
547	/* Should NEVER happen. Return bio error if it does. */
548	if (unlikely(ret))
549		goto out_cleanup;
550
551	if (is_partial_io(bvec))
552		memcpy(user_mem + bvec->bv_offset, uncmem + offset,
553				bvec->bv_len);
554
555	flush_dcache_page(page);
556	ret = 0;
557out_cleanup:
558	kunmap_atomic(user_mem);
559	if (is_partial_io(bvec))
560		kfree(uncmem);
561	return ret;
562}
563
564static inline void update_used_max(struct zram *zram,
565					const unsigned long pages)
566{
567	unsigned long old_max, cur_max;
568
569	old_max = atomic_long_read(&zram->stats.max_used_pages);
570
571	do {
572		cur_max = old_max;
573		if (pages > cur_max)
574			old_max = atomic_long_cmpxchg(
575				&zram->stats.max_used_pages, cur_max, pages);
576	} while (old_max != cur_max);
577}
578
579static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
580			   int offset)
581{
582	int ret = 0;
583	size_t clen;
584	unsigned long handle;
585	struct page *page;
586	unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
587	struct zram_meta *meta = zram->meta;
588	struct zcomp_strm *zstrm;
589	bool locked = false;
590	unsigned long alloced_pages;
591
592	page = bvec->bv_page;
593	if (is_partial_io(bvec)) {
594		/*
595		 * This is a partial IO. We need to read the full page
596		 * before to write the changes.
597		 */
598		uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
599		if (!uncmem) {
600			ret = -ENOMEM;
601			goto out;
602		}
603		ret = zram_decompress_page(zram, uncmem, index);
604		if (ret)
605			goto out;
606	}
607
608	zstrm = zcomp_strm_find(zram->comp);
609	locked = true;
610	user_mem = kmap_atomic(page);
611
612	if (is_partial_io(bvec)) {
613		memcpy(uncmem + offset, user_mem + bvec->bv_offset,
614		       bvec->bv_len);
615		kunmap_atomic(user_mem);
616		user_mem = NULL;
617	} else {
618		uncmem = user_mem;
619	}
620
621	if (page_zero_filled(uncmem)) {
622		if (user_mem)
623			kunmap_atomic(user_mem);
624		/* Free memory associated with this sector now. */
625		bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
626		zram_free_page(zram, index);
627		zram_set_flag(meta, index, ZRAM_ZERO);
628		bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
629
630		atomic64_inc(&zram->stats.zero_pages);
631		ret = 0;
632		goto out;
633	}
634
635	ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
636	if (!is_partial_io(bvec)) {
637		kunmap_atomic(user_mem);
638		user_mem = NULL;
639		uncmem = NULL;
640	}
641
642	if (unlikely(ret)) {
643		pr_err("Compression failed! err=%d\n", ret);
644		goto out;
645	}
646	src = zstrm->buffer;
647	if (unlikely(clen > max_zpage_size)) {
648		clen = PAGE_SIZE;
649		if (is_partial_io(bvec))
650			src = uncmem;
651	}
652
653	handle = zs_malloc(meta->mem_pool, clen);
654	if (!handle) {
655		pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
656			index, clen);
657		ret = -ENOMEM;
658		goto out;
659	}
660
661	alloced_pages = zs_get_total_pages(meta->mem_pool);
662	if (zram->limit_pages && alloced_pages > zram->limit_pages) {
663		zs_free(meta->mem_pool, handle);
664		ret = -ENOMEM;
665		goto out;
666	}
667
668	update_used_max(zram, alloced_pages);
669
670	cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
671
672	if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
673		src = kmap_atomic(page);
674		copy_page(cmem, src);
675		kunmap_atomic(src);
676	} else {
677		memcpy(cmem, src, clen);
678	}
679
680	zcomp_strm_release(zram->comp, zstrm);
681	locked = false;
682	zs_unmap_object(meta->mem_pool, handle);
683
684	/*
685	 * Free memory associated with this sector
686	 * before overwriting unused sectors.
687	 */
688	bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
689	zram_free_page(zram, index);
690
691	meta->table[index].handle = handle;
692	zram_set_obj_size(meta, index, clen);
693	bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
694
695	/* Update stats */
696	atomic64_add(clen, &zram->stats.compr_data_size);
697	atomic64_inc(&zram->stats.pages_stored);
698out:
699	if (locked)
700		zcomp_strm_release(zram->comp, zstrm);
701	if (is_partial_io(bvec))
702		kfree(uncmem);
703	return ret;
704}
705
706static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
707			int offset, int rw)
708{
709	unsigned long start_time = jiffies;
710	int ret;
711
712	generic_start_io_acct(rw, bvec->bv_len >> SECTOR_SHIFT,
713			&zram->disk->part0);
714
715	if (rw == READ) {
716		atomic64_inc(&zram->stats.num_reads);
717		ret = zram_bvec_read(zram, bvec, index, offset);
718	} else {
719		atomic64_inc(&zram->stats.num_writes);
720		ret = zram_bvec_write(zram, bvec, index, offset);
721	}
722
723	generic_end_io_acct(rw, &zram->disk->part0, start_time);
724
725	if (unlikely(ret)) {
726		if (rw == READ)
727			atomic64_inc(&zram->stats.failed_reads);
728		else
729			atomic64_inc(&zram->stats.failed_writes);
730	}
731
732	return ret;
733}
734
735/*
736 * zram_bio_discard - handler on discard request
737 * @index: physical block index in PAGE_SIZE units
738 * @offset: byte offset within physical block
739 */
740static void zram_bio_discard(struct zram *zram, u32 index,
741			     int offset, struct bio *bio)
742{
743	size_t n = bio->bi_iter.bi_size;
744	struct zram_meta *meta = zram->meta;
745
746	/*
747	 * zram manages data in physical block size units. Because logical block
748	 * size isn't identical with physical block size on some arch, we
749	 * could get a discard request pointing to a specific offset within a
750	 * certain physical block.  Although we can handle this request by
751	 * reading that physiclal block and decompressing and partially zeroing
752	 * and re-compressing and then re-storing it, this isn't reasonable
753	 * because our intent with a discard request is to save memory.  So
754	 * skipping this logical block is appropriate here.
755	 */
756	if (offset) {
757		if (n <= (PAGE_SIZE - offset))
758			return;
759
760		n -= (PAGE_SIZE - offset);
761		index++;
762	}
763
764	while (n >= PAGE_SIZE) {
765		bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
766		zram_free_page(zram, index);
767		bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
768		atomic64_inc(&zram->stats.notify_free);
769		index++;
770		n -= PAGE_SIZE;
771	}
772}
773
774static void zram_reset_device(struct zram *zram)
775{
776	struct zram_meta *meta;
777	struct zcomp *comp;
778	u64 disksize;
779
780	down_write(&zram->init_lock);
781
782	zram->limit_pages = 0;
783
784	if (!init_done(zram)) {
785		up_write(&zram->init_lock);
786		return;
787	}
788
789	meta = zram->meta;
790	comp = zram->comp;
791	disksize = zram->disksize;
792	/*
793	 * Refcount will go down to 0 eventually and r/w handler
794	 * cannot handle further I/O so it will bail out by
795	 * check zram_meta_get.
796	 */
797	zram_meta_put(zram);
798	/*
799	 * We want to free zram_meta in process context to avoid
800	 * deadlock between reclaim path and any other locks.
801	 */
802	wait_event(zram->io_done, atomic_read(&zram->refcount) == 0);
803
804	/* Reset stats */
805	memset(&zram->stats, 0, sizeof(zram->stats));
806	zram->disksize = 0;
807	zram->max_comp_streams = 1;
808
809	set_capacity(zram->disk, 0);
810	part_stat_set_all(&zram->disk->part0, 0);
811
812	up_write(&zram->init_lock);
813	/* I/O operation under all of CPU are done so let's free */
814	zram_meta_free(meta, disksize);
815	zcomp_destroy(comp);
816}
817
818static ssize_t disksize_store(struct device *dev,
819		struct device_attribute *attr, const char *buf, size_t len)
820{
821	u64 disksize;
822	struct zcomp *comp;
823	struct zram_meta *meta;
824	struct zram *zram = dev_to_zram(dev);
825	int err;
826
827	disksize = memparse(buf, NULL);
828	if (!disksize)
829		return -EINVAL;
830
831	disksize = PAGE_ALIGN(disksize);
832	meta = zram_meta_alloc(zram->disk->first_minor, disksize);
833	if (!meta)
834		return -ENOMEM;
835
836	comp = zcomp_create(zram->compressor, zram->max_comp_streams);
837	if (IS_ERR(comp)) {
838		pr_info("Cannot initialise %s compressing backend\n",
839				zram->compressor);
840		err = PTR_ERR(comp);
841		goto out_free_meta;
842	}
843
844	down_write(&zram->init_lock);
845	if (init_done(zram)) {
846		pr_info("Cannot change disksize for initialized device\n");
847		err = -EBUSY;
848		goto out_destroy_comp;
849	}
850
851	init_waitqueue_head(&zram->io_done);
852	atomic_set(&zram->refcount, 1);
853	zram->meta = meta;
854	zram->comp = comp;
855	zram->disksize = disksize;
856	set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
857	up_write(&zram->init_lock);
858
859	/*
860	 * Revalidate disk out of the init_lock to avoid lockdep splat.
861	 * It's okay because disk's capacity is protected by init_lock
862	 * so that revalidate_disk always sees up-to-date capacity.
863	 */
864	revalidate_disk(zram->disk);
865
866	return len;
867
868out_destroy_comp:
869	up_write(&zram->init_lock);
870	zcomp_destroy(comp);
871out_free_meta:
872	zram_meta_free(meta, disksize);
873	return err;
874}
875
876static ssize_t reset_store(struct device *dev,
877		struct device_attribute *attr, const char *buf, size_t len)
878{
879	int ret;
880	unsigned short do_reset;
881	struct zram *zram;
882	struct block_device *bdev;
883
884	zram = dev_to_zram(dev);
885	bdev = bdget_disk(zram->disk, 0);
886
887	if (!bdev)
888		return -ENOMEM;
889
890	mutex_lock(&bdev->bd_mutex);
891	/* Do not reset an active device! */
892	if (bdev->bd_openers) {
893		ret = -EBUSY;
894		goto out;
895	}
896
897	ret = kstrtou16(buf, 10, &do_reset);
898	if (ret)
899		goto out;
900
901	if (!do_reset) {
902		ret = -EINVAL;
903		goto out;
904	}
905
906	/* Make sure all pending I/O is finished */
907	fsync_bdev(bdev);
908	zram_reset_device(zram);
909
910	mutex_unlock(&bdev->bd_mutex);
911	revalidate_disk(zram->disk);
912	bdput(bdev);
913
914	return len;
915
916out:
917	mutex_unlock(&bdev->bd_mutex);
918	bdput(bdev);
919	return ret;
920}
921
922static void __zram_make_request(struct zram *zram, struct bio *bio)
923{
924	int offset, rw;
925	u32 index;
926	struct bio_vec bvec;
927	struct bvec_iter iter;
928
929	index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
930	offset = (bio->bi_iter.bi_sector &
931		  (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
932
933	if (unlikely(bio->bi_rw & REQ_DISCARD)) {
934		zram_bio_discard(zram, index, offset, bio);
935		bio_endio(bio, 0);
936		return;
937	}
938
939	rw = bio_data_dir(bio);
940	bio_for_each_segment(bvec, bio, iter) {
941		int max_transfer_size = PAGE_SIZE - offset;
942
943		if (bvec.bv_len > max_transfer_size) {
944			/*
945			 * zram_bvec_rw() can only make operation on a single
946			 * zram page. Split the bio vector.
947			 */
948			struct bio_vec bv;
949
950			bv.bv_page = bvec.bv_page;
951			bv.bv_len = max_transfer_size;
952			bv.bv_offset = bvec.bv_offset;
953
954			if (zram_bvec_rw(zram, &bv, index, offset, rw) < 0)
955				goto out;
956
957			bv.bv_len = bvec.bv_len - max_transfer_size;
958			bv.bv_offset += max_transfer_size;
959			if (zram_bvec_rw(zram, &bv, index + 1, 0, rw) < 0)
960				goto out;
961		} else
962			if (zram_bvec_rw(zram, &bvec, index, offset, rw) < 0)
963				goto out;
964
965		update_position(&index, &offset, &bvec);
966	}
967
968	set_bit(BIO_UPTODATE, &bio->bi_flags);
969	bio_endio(bio, 0);
970	return;
971
972out:
973	bio_io_error(bio);
974}
975
976/*
977 * Handler function for all zram I/O requests.
978 */
979static void zram_make_request(struct request_queue *queue, struct bio *bio)
980{
981	struct zram *zram = queue->queuedata;
982
983	if (unlikely(!zram_meta_get(zram)))
984		goto error;
985
986	if (!valid_io_request(zram, bio->bi_iter.bi_sector,
987					bio->bi_iter.bi_size)) {
988		atomic64_inc(&zram->stats.invalid_io);
989		goto put_zram;
990	}
991
992	__zram_make_request(zram, bio);
993	zram_meta_put(zram);
994	return;
995put_zram:
996	zram_meta_put(zram);
997error:
998	bio_io_error(bio);
999}
1000
1001static void zram_slot_free_notify(struct block_device *bdev,
1002				unsigned long index)
1003{
1004	struct zram *zram;
1005	struct zram_meta *meta;
1006
1007	zram = bdev->bd_disk->private_data;
1008	meta = zram->meta;
1009
1010	bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
1011	zram_free_page(zram, index);
1012	bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
1013	atomic64_inc(&zram->stats.notify_free);
1014}
1015
1016static int zram_rw_page(struct block_device *bdev, sector_t sector,
1017		       struct page *page, int rw)
1018{
1019	int offset, err = -EIO;
1020	u32 index;
1021	struct zram *zram;
1022	struct bio_vec bv;
1023
1024	zram = bdev->bd_disk->private_data;
1025	if (unlikely(!zram_meta_get(zram)))
1026		goto out;
1027
1028	if (!valid_io_request(zram, sector, PAGE_SIZE)) {
1029		atomic64_inc(&zram->stats.invalid_io);
1030		err = -EINVAL;
1031		goto put_zram;
1032	}
1033
1034	index = sector >> SECTORS_PER_PAGE_SHIFT;
1035	offset = sector & (SECTORS_PER_PAGE - 1) << SECTOR_SHIFT;
1036
1037	bv.bv_page = page;
1038	bv.bv_len = PAGE_SIZE;
1039	bv.bv_offset = 0;
1040
1041	err = zram_bvec_rw(zram, &bv, index, offset, rw);
1042put_zram:
1043	zram_meta_put(zram);
1044out:
1045	/*
1046	 * If I/O fails, just return error(ie, non-zero) without
1047	 * calling page_endio.
1048	 * It causes resubmit the I/O with bio request by upper functions
1049	 * of rw_page(e.g., swap_readpage, __swap_writepage) and
1050	 * bio->bi_end_io does things to handle the error
1051	 * (e.g., SetPageError, set_page_dirty and extra works).
1052	 */
1053	if (err == 0)
1054		page_endio(page, rw, 0);
1055	return err;
1056}
1057
1058static const struct block_device_operations zram_devops = {
1059	.swap_slot_free_notify = zram_slot_free_notify,
1060	.rw_page = zram_rw_page,
1061	.owner = THIS_MODULE
1062};
1063
1064static DEVICE_ATTR_WO(compact);
1065static DEVICE_ATTR_RW(disksize);
1066static DEVICE_ATTR_RO(initstate);
1067static DEVICE_ATTR_WO(reset);
1068static DEVICE_ATTR_RO(orig_data_size);
1069static DEVICE_ATTR_RO(mem_used_total);
1070static DEVICE_ATTR_RW(mem_limit);
1071static DEVICE_ATTR_RW(mem_used_max);
1072static DEVICE_ATTR_RW(max_comp_streams);
1073static DEVICE_ATTR_RW(comp_algorithm);
1074
1075static ssize_t io_stat_show(struct device *dev,
1076		struct device_attribute *attr, char *buf)
1077{
1078	struct zram *zram = dev_to_zram(dev);
1079	ssize_t ret;
1080
1081	down_read(&zram->init_lock);
1082	ret = scnprintf(buf, PAGE_SIZE,
1083			"%8llu %8llu %8llu %8llu\n",
1084			(u64)atomic64_read(&zram->stats.failed_reads),
1085			(u64)atomic64_read(&zram->stats.failed_writes),
1086			(u64)atomic64_read(&zram->stats.invalid_io),
1087			(u64)atomic64_read(&zram->stats.notify_free));
1088	up_read(&zram->init_lock);
1089
1090	return ret;
1091}
1092
1093static ssize_t mm_stat_show(struct device *dev,
1094		struct device_attribute *attr, char *buf)
1095{
1096	struct zram *zram = dev_to_zram(dev);
1097	u64 orig_size, mem_used = 0;
1098	long max_used;
1099	ssize_t ret;
1100
1101	down_read(&zram->init_lock);
1102	if (init_done(zram))
1103		mem_used = zs_get_total_pages(zram->meta->mem_pool);
1104
1105	orig_size = atomic64_read(&zram->stats.pages_stored);
1106	max_used = atomic_long_read(&zram->stats.max_used_pages);
1107
1108	ret = scnprintf(buf, PAGE_SIZE,
1109			"%8llu %8llu %8llu %8lu %8ld %8llu %8llu\n",
1110			orig_size << PAGE_SHIFT,
1111			(u64)atomic64_read(&zram->stats.compr_data_size),
1112			mem_used << PAGE_SHIFT,
1113			zram->limit_pages << PAGE_SHIFT,
1114			max_used << PAGE_SHIFT,
1115			(u64)atomic64_read(&zram->stats.zero_pages),
1116			(u64)atomic64_read(&zram->stats.num_migrated));
1117	up_read(&zram->init_lock);
1118
1119	return ret;
1120}
1121
1122static DEVICE_ATTR_RO(io_stat);
1123static DEVICE_ATTR_RO(mm_stat);
1124ZRAM_ATTR_RO(num_reads);
1125ZRAM_ATTR_RO(num_writes);
1126ZRAM_ATTR_RO(failed_reads);
1127ZRAM_ATTR_RO(failed_writes);
1128ZRAM_ATTR_RO(invalid_io);
1129ZRAM_ATTR_RO(notify_free);
1130ZRAM_ATTR_RO(zero_pages);
1131ZRAM_ATTR_RO(compr_data_size);
1132
1133static struct attribute *zram_disk_attrs[] = {
1134	&dev_attr_disksize.attr,
1135	&dev_attr_initstate.attr,
1136	&dev_attr_reset.attr,
1137	&dev_attr_num_reads.attr,
1138	&dev_attr_num_writes.attr,
1139	&dev_attr_failed_reads.attr,
1140	&dev_attr_failed_writes.attr,
1141	&dev_attr_compact.attr,
1142	&dev_attr_invalid_io.attr,
1143	&dev_attr_notify_free.attr,
1144	&dev_attr_zero_pages.attr,
1145	&dev_attr_orig_data_size.attr,
1146	&dev_attr_compr_data_size.attr,
1147	&dev_attr_mem_used_total.attr,
1148	&dev_attr_mem_limit.attr,
1149	&dev_attr_mem_used_max.attr,
1150	&dev_attr_max_comp_streams.attr,
1151	&dev_attr_comp_algorithm.attr,
1152	&dev_attr_io_stat.attr,
1153	&dev_attr_mm_stat.attr,
1154	NULL,
1155};
1156
1157static struct attribute_group zram_disk_attr_group = {
1158	.attrs = zram_disk_attrs,
1159};
1160
1161static int create_device(struct zram *zram, int device_id)
1162{
1163	struct request_queue *queue;
1164	int ret = -ENOMEM;
1165
1166	init_rwsem(&zram->init_lock);
1167
1168	queue = blk_alloc_queue(GFP_KERNEL);
1169	if (!queue) {
1170		pr_err("Error allocating disk queue for device %d\n",
1171			device_id);
1172		goto out;
1173	}
1174
1175	blk_queue_make_request(queue, zram_make_request);
1176
1177	 /* gendisk structure */
1178	zram->disk = alloc_disk(1);
1179	if (!zram->disk) {
1180		pr_warn("Error allocating disk structure for device %d\n",
1181			device_id);
1182		ret = -ENOMEM;
1183		goto out_free_queue;
1184	}
1185
1186	zram->disk->major = zram_major;
1187	zram->disk->first_minor = device_id;
1188	zram->disk->fops = &zram_devops;
1189	zram->disk->queue = queue;
1190	zram->disk->queue->queuedata = zram;
1191	zram->disk->private_data = zram;
1192	snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
1193
1194	/* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
1195	set_capacity(zram->disk, 0);
1196	/* zram devices sort of resembles non-rotational disks */
1197	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
1198	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
1199	/*
1200	 * To ensure that we always get PAGE_SIZE aligned
1201	 * and n*PAGE_SIZED sized I/O requests.
1202	 */
1203	blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
1204	blk_queue_logical_block_size(zram->disk->queue,
1205					ZRAM_LOGICAL_BLOCK_SIZE);
1206	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1207	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
1208	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1209	zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
1210	/*
1211	 * zram_bio_discard() will clear all logical blocks if logical block
1212	 * size is identical with physical block size(PAGE_SIZE). But if it is
1213	 * different, we will skip discarding some parts of logical blocks in
1214	 * the part of the request range which isn't aligned to physical block
1215	 * size.  So we can't ensure that all discarded logical blocks are
1216	 * zeroed.
1217	 */
1218	if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1219		zram->disk->queue->limits.discard_zeroes_data = 1;
1220	else
1221		zram->disk->queue->limits.discard_zeroes_data = 0;
1222	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
1223
1224	add_disk(zram->disk);
1225
1226	ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
1227				&zram_disk_attr_group);
1228	if (ret < 0) {
1229		pr_warn("Error creating sysfs group");
1230		goto out_free_disk;
1231	}
1232	strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
1233	zram->meta = NULL;
1234	zram->max_comp_streams = 1;
1235	return 0;
1236
1237out_free_disk:
1238	del_gendisk(zram->disk);
1239	put_disk(zram->disk);
1240out_free_queue:
1241	blk_cleanup_queue(queue);
1242out:
1243	return ret;
1244}
1245
1246static void destroy_devices(unsigned int nr)
1247{
1248	struct zram *zram;
1249	unsigned int i;
1250
1251	for (i = 0; i < nr; i++) {
1252		zram = &zram_devices[i];
1253		/*
1254		 * Remove sysfs first, so no one will perform a disksize
1255		 * store while we destroy the devices
1256		 */
1257		sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
1258				&zram_disk_attr_group);
1259
1260		zram_reset_device(zram);
1261
1262		blk_cleanup_queue(zram->disk->queue);
1263		del_gendisk(zram->disk);
1264		put_disk(zram->disk);
1265	}
1266
1267	kfree(zram_devices);
1268	unregister_blkdev(zram_major, "zram");
1269	pr_info("Destroyed %u device(s)\n", nr);
1270}
1271
1272static int __init zram_init(void)
1273{
1274	int ret, dev_id;
1275
1276	if (num_devices > max_num_devices) {
1277		pr_warn("Invalid value for num_devices: %u\n",
1278				num_devices);
1279		return -EINVAL;
1280	}
1281
1282	zram_major = register_blkdev(0, "zram");
1283	if (zram_major <= 0) {
1284		pr_warn("Unable to get major number\n");
1285		return -EBUSY;
1286	}
1287
1288	/* Allocate the device array and initialize each one */
1289	zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
1290	if (!zram_devices) {
1291		unregister_blkdev(zram_major, "zram");
1292		return -ENOMEM;
1293	}
1294
1295	for (dev_id = 0; dev_id < num_devices; dev_id++) {
1296		ret = create_device(&zram_devices[dev_id], dev_id);
1297		if (ret)
1298			goto out_error;
1299	}
1300
1301	pr_info("Created %u device(s)\n", num_devices);
1302	return 0;
1303
1304out_error:
1305	destroy_devices(dev_id);
1306	return ret;
1307}
1308
1309static void __exit zram_exit(void)
1310{
1311	destroy_devices(num_devices);
1312}
1313
1314module_init(zram_init);
1315module_exit(zram_exit);
1316
1317module_param(num_devices, uint, 0);
1318MODULE_PARM_DESC(num_devices, "Number of zram devices");
1319
1320MODULE_LICENSE("Dual BSD/GPL");
1321MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
1322MODULE_DESCRIPTION("Compressed RAM Block Device");
1323