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 #ifndef _ZRAM_DRV_H_
16 #define _ZRAM_DRV_H_
17 
18 #include <linux/rwsem.h>
19 #include <linux/zsmalloc.h>
20 #include <linux/crypto.h>
21 
22 #include "zcomp.h"
23 
24 #define SECTORS_PER_PAGE_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
25 #define SECTORS_PER_PAGE	(1 << SECTORS_PER_PAGE_SHIFT)
26 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
27 #define ZRAM_LOGICAL_BLOCK_SIZE	(1 << ZRAM_LOGICAL_BLOCK_SHIFT)
28 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK	\
29 	(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
30 
31 
32 /*
33  * ZRAM is mainly used for memory efficiency so we want to keep memory
34  * footprint small and thus squeeze size and zram pageflags into a flags
35  * member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding
36  * header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT
37  * bits), the higher bits are for zram_pageflags.
38  *
39  * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.
40  */
41 #define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)
42 
43 /* Only 2 bits are allowed for comp priority index */
44 #define ZRAM_COMP_PRIORITY_MASK	0x3
45 
46 /* Flags for zram pages (table[page_no].flags) */
47 enum zram_pageflags {
48 	/* zram slot is locked */
49 	ZRAM_LOCK = ZRAM_FLAG_SHIFT,
50 	ZRAM_SAME,	/* Page consists the same element */
51 	ZRAM_WB,	/* page is stored on backing_device */
52 	ZRAM_UNDER_WB,	/* page is under writeback */
53 	ZRAM_HUGE,	/* Incompressible page */
54 	ZRAM_IDLE,	/* not accessed page since last idle marking */
55 	ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */
56 
57 	ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */
58 	ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */
59 
60 	__NR_ZRAM_PAGEFLAGS,
61 };
62 
63 /*-- Data structures */
64 
65 /* Allocated for each disk page */
66 struct zram_table_entry {
67 	union {
68 		unsigned long handle;
69 		unsigned long element;
70 	};
71 	unsigned long flags;
72 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
73 	ktime_t ac_time;
74 #endif
75 };
76 
77 struct zram_stats {
78 	atomic64_t compr_data_size;	/* compressed size of pages stored */
79 	atomic64_t failed_reads;	/* can happen when memory is too low */
80 	atomic64_t failed_writes;	/* can happen when memory is too low */
81 	atomic64_t invalid_io;	/* non-page-aligned I/O requests */
82 	atomic64_t notify_free;	/* no. of swap slot free notifications */
83 	atomic64_t same_pages;		/* no. of same element filled pages */
84 	atomic64_t huge_pages;		/* no. of huge pages */
85 	atomic64_t huge_pages_since;	/* no. of huge pages since zram set up */
86 	atomic64_t pages_stored;	/* no. of pages currently stored */
87 	atomic_long_t max_used_pages;	/* no. of maximum pages stored */
88 	atomic64_t writestall;		/* no. of write slow paths */
89 	atomic64_t miss_free;		/* no. of missed free */
90 #ifdef	CONFIG_ZRAM_WRITEBACK
91 	atomic64_t bd_count;		/* no. of pages in backing device */
92 	atomic64_t bd_reads;		/* no. of reads from backing device */
93 	atomic64_t bd_writes;		/* no. of writes from backing device */
94 #endif
95 };
96 
97 #ifdef CONFIG_ZRAM_MULTI_COMP
98 #define ZRAM_PRIMARY_COMP	0U
99 #define ZRAM_SECONDARY_COMP	1U
100 #define ZRAM_MAX_COMPS	4U
101 #else
102 #define ZRAM_PRIMARY_COMP	0U
103 #define ZRAM_SECONDARY_COMP	0U
104 #define ZRAM_MAX_COMPS	1U
105 #endif
106 
107 struct zram {
108 	struct zram_table_entry *table;
109 	struct zs_pool *mem_pool;
110 	struct zcomp *comps[ZRAM_MAX_COMPS];
111 	struct gendisk *disk;
112 	/* Prevent concurrent execution of device init */
113 	struct rw_semaphore init_lock;
114 	/*
115 	 * the number of pages zram can consume for storing compressed data
116 	 */
117 	unsigned long limit_pages;
118 
119 	struct zram_stats stats;
120 	/*
121 	 * This is the limit on amount of *uncompressed* worth of data
122 	 * we can store in a disk.
123 	 */
124 	u64 disksize;	/* bytes */
125 	const char *comp_algs[ZRAM_MAX_COMPS];
126 	s8 num_active_comps;
127 	/*
128 	 * zram is claimed so open request will be failed
129 	 */
130 	bool claim; /* Protected by disk->open_mutex */
131 #ifdef CONFIG_ZRAM_WRITEBACK
132 	struct file *backing_dev;
133 	spinlock_t wb_limit_lock;
134 	bool wb_limit_enable;
135 	u64 bd_wb_limit;
136 	struct block_device *bdev;
137 	unsigned long *bitmap;
138 	unsigned long nr_pages;
139 #endif
140 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
141 	struct dentry *debugfs_dir;
142 #endif
143 };
144 #endif
145