xref: /openbmc/linux/drivers/block/zram/zram_drv.h (revision 5f32c314)
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/mutex.h>
20 #include <linux/zsmalloc.h>
21 
22 /*
23  * Some arbitrary value. This is just to catch
24  * invalid value for num_devices module parameter.
25  */
26 static const unsigned max_num_devices = 32;
27 
28 /*-- Configurable parameters */
29 
30 /*
31  * Pages that compress to size greater than this are stored
32  * uncompressed in memory.
33  */
34 static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
35 
36 /*
37  * NOTE: max_zpage_size must be less than or equal to:
38  *   ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
39  * always return failure.
40  */
41 
42 /*-- End of configurable params */
43 
44 #define SECTOR_SHIFT		9
45 #define SECTOR_SIZE		(1 << SECTOR_SHIFT)
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 /* Flags for zram pages (table[page_no].flags) */
54 enum zram_pageflags {
55 	/* Page consists entirely of zeros */
56 	ZRAM_ZERO,
57 
58 	__NR_ZRAM_PAGEFLAGS,
59 };
60 
61 /*-- Data structures */
62 
63 /* Allocated for each disk page */
64 struct table {
65 	unsigned long handle;
66 	u16 size;	/* object size (excluding header) */
67 	u8 count;	/* object ref count (not yet used) */
68 	u8 flags;
69 } __aligned(4);
70 
71 struct zram_stats {
72 	atomic64_t compr_size;	/* compressed size of pages stored */
73 	atomic64_t num_reads;	/* failed + successful */
74 	atomic64_t num_writes;	/* --do-- */
75 	atomic64_t failed_reads;	/* should NEVER! happen */
76 	atomic64_t failed_writes;	/* can happen when memory is too low */
77 	atomic64_t invalid_io;	/* non-page-aligned I/O requests */
78 	atomic64_t notify_free;	/* no. of swap slot free notifications */
79 	atomic_t pages_zero;		/* no. of zero filled pages */
80 	atomic_t pages_stored;	/* no. of pages currently stored */
81 	atomic_t good_compress;	/* % of pages with compression ratio<=50% */
82 	atomic_t bad_compress;	/* % of pages with compression ratio>=75% */
83 };
84 
85 struct zram_meta {
86 	rwlock_t tb_lock;	/* protect table */
87 	void *compress_workmem;
88 	void *compress_buffer;
89 	struct table *table;
90 	struct zs_pool *mem_pool;
91 	struct mutex buffer_lock; /* protect compress buffers */
92 };
93 
94 struct zram {
95 	struct zram_meta *meta;
96 	struct request_queue *queue;
97 	struct gendisk *disk;
98 	int init_done;
99 	/* Prevent concurrent execution of device init, reset and R/W request */
100 	struct rw_semaphore init_lock;
101 	/*
102 	 * This is the limit on amount of *uncompressed* worth of data
103 	 * we can store in a disk.
104 	 */
105 	u64 disksize;	/* bytes */
106 
107 	struct zram_stats stats;
108 };
109 #endif
110