xref: /openbmc/linux/drivers/block/zram/zram_drv.h (revision 8c0b9ee8)
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/spinlock.h>
19 #include <linux/zsmalloc.h>
20 
21 #include "zcomp.h"
22 
23 /*
24  * Some arbitrary value. This is just to catch
25  * invalid value for num_devices module parameter.
26  */
27 static const unsigned max_num_devices = 32;
28 
29 /*-- Configurable parameters */
30 
31 /*
32  * Pages that compress to size greater than this are stored
33  * uncompressed in memory.
34  */
35 static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
36 
37 /*
38  * NOTE: max_zpage_size must be less than or equal to:
39  *   ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
40  * always return failure.
41  */
42 
43 /*-- End of configurable params */
44 
45 #define SECTOR_SHIFT		9
46 #define SECTORS_PER_PAGE_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
47 #define SECTORS_PER_PAGE	(1 << SECTORS_PER_PAGE_SHIFT)
48 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
49 #define ZRAM_LOGICAL_BLOCK_SIZE	(1 << ZRAM_LOGICAL_BLOCK_SHIFT)
50 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK	\
51 	(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
52 
53 
54 /*
55  * The lower ZRAM_FLAG_SHIFT bits of table.value is for
56  * object size (excluding header), the higher bits is for
57  * zram_pageflags.
58  *
59  * zram is mainly used for memory efficiency so we want to keep memory
60  * footprint small so we can squeeze size and flags into a field.
61  * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
62  * the higher bits is for zram_pageflags.
63  */
64 #define ZRAM_FLAG_SHIFT 24
65 
66 /* Flags for zram pages (table[page_no].value) */
67 enum zram_pageflags {
68 	/* Page consists entirely of zeros */
69 	ZRAM_ZERO = ZRAM_FLAG_SHIFT,
70 	ZRAM_ACCESS,	/* page is now accessed */
71 
72 	__NR_ZRAM_PAGEFLAGS,
73 };
74 
75 /*-- Data structures */
76 
77 /* Allocated for each disk page */
78 struct zram_table_entry {
79 	unsigned long handle;
80 	unsigned long value;
81 };
82 
83 struct zram_stats {
84 	atomic64_t compr_data_size;	/* compressed size of pages stored */
85 	atomic64_t num_reads;	/* failed + successful */
86 	atomic64_t num_writes;	/* --do-- */
87 	atomic64_t failed_reads;	/* can happen when memory is too low */
88 	atomic64_t failed_writes;	/* can happen when memory is too low */
89 	atomic64_t invalid_io;	/* non-page-aligned I/O requests */
90 	atomic64_t notify_free;	/* no. of swap slot free notifications */
91 	atomic64_t zero_pages;		/* no. of zero filled pages */
92 	atomic64_t pages_stored;	/* no. of pages currently stored */
93 	atomic_long_t max_used_pages;	/* no. of maximum pages stored */
94 };
95 
96 struct zram_meta {
97 	struct zram_table_entry *table;
98 	struct zs_pool *mem_pool;
99 };
100 
101 struct zram {
102 	struct zram_meta *meta;
103 	struct zcomp *comp;
104 	struct gendisk *disk;
105 	/* Prevent concurrent execution of device init */
106 	struct rw_semaphore init_lock;
107 	/*
108 	 * the number of pages zram can consume for storing compressed data
109 	 */
110 	unsigned long limit_pages;
111 	int max_comp_streams;
112 
113 	struct zram_stats stats;
114 	atomic_t refcount; /* refcount for zram_meta */
115 	/* wait all IO under all of cpu are done */
116 	wait_queue_head_t io_done;
117 	/*
118 	 * This is the limit on amount of *uncompressed* worth of data
119 	 * we can store in a disk.
120 	 */
121 	u64 disksize;	/* bytes */
122 	char compressor[10];
123 };
124 #endif
125