xref: /linux/drivers/block/zram/zram_drv.h (revision 2da68a77)
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 /* Flags for zram pages (table[page_no].flags) */
44 enum zram_pageflags {
45 	/* zram slot is locked */
46 	ZRAM_LOCK = ZRAM_FLAG_SHIFT,
47 	ZRAM_SAME,	/* Page consists the same element */
48 	ZRAM_WB,	/* page is stored on backing_device */
49 	ZRAM_UNDER_WB,	/* page is under writeback */
50 	ZRAM_HUGE,	/* Incompressible page */
51 	ZRAM_IDLE,	/* not accessed page since last idle marking */
52 
53 	__NR_ZRAM_PAGEFLAGS,
54 };
55 
56 /*-- Data structures */
57 
58 /* Allocated for each disk page */
59 struct zram_table_entry {
60 	union {
61 		unsigned long handle;
62 		unsigned long element;
63 	};
64 	unsigned long flags;
65 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
66 	ktime_t ac_time;
67 #endif
68 };
69 
70 struct zram_stats {
71 	atomic64_t compr_data_size;	/* compressed size of pages stored */
72 	atomic64_t num_reads;	/* failed + successful */
73 	atomic64_t num_writes;	/* --do-- */
74 	atomic64_t failed_reads;	/* can happen when memory is too low */
75 	atomic64_t failed_writes;	/* can happen when memory is too low */
76 	atomic64_t invalid_io;	/* non-page-aligned I/O requests */
77 	atomic64_t notify_free;	/* no. of swap slot free notifications */
78 	atomic64_t same_pages;		/* no. of same element filled pages */
79 	atomic64_t huge_pages;		/* no. of huge pages */
80 	atomic64_t huge_pages_since;	/* no. of huge pages since zram set up */
81 	atomic64_t pages_stored;	/* no. of pages currently stored */
82 	atomic_long_t max_used_pages;	/* no. of maximum pages stored */
83 	atomic64_t writestall;		/* no. of write slow paths */
84 	atomic64_t miss_free;		/* no. of missed free */
85 #ifdef	CONFIG_ZRAM_WRITEBACK
86 	atomic64_t bd_count;		/* no. of pages in backing device */
87 	atomic64_t bd_reads;		/* no. of reads from backing device */
88 	atomic64_t bd_writes;		/* no. of writes from backing device */
89 #endif
90 };
91 
92 struct zram {
93 	struct zram_table_entry *table;
94 	struct zs_pool *mem_pool;
95 	struct zcomp *comp;
96 	struct gendisk *disk;
97 	/* Prevent concurrent execution of device init */
98 	struct rw_semaphore init_lock;
99 	/*
100 	 * the number of pages zram can consume for storing compressed data
101 	 */
102 	unsigned long limit_pages;
103 
104 	struct zram_stats stats;
105 	/*
106 	 * This is the limit on amount of *uncompressed* worth of data
107 	 * we can store in a disk.
108 	 */
109 	u64 disksize;	/* bytes */
110 	char compressor[CRYPTO_MAX_ALG_NAME];
111 	/*
112 	 * zram is claimed so open request will be failed
113 	 */
114 	bool claim; /* Protected by disk->open_mutex */
115 #ifdef CONFIG_ZRAM_WRITEBACK
116 	struct file *backing_dev;
117 	spinlock_t wb_limit_lock;
118 	bool wb_limit_enable;
119 	u64 bd_wb_limit;
120 	struct block_device *bdev;
121 	unsigned long *bitmap;
122 	unsigned long nr_pages;
123 #endif
124 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
125 	struct dentry *debugfs_dir;
126 #endif
127 };
128 #endif
129