These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / block / zram / zram_drv.h
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 /*-- Configurable parameters */
24
25 /*
26  * Pages that compress to size greater than this are stored
27  * uncompressed in memory.
28  */
29 static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
30
31 /*
32  * NOTE: max_zpage_size must be less than or equal to:
33  *   ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
34  * always return failure.
35  */
36
37 /*-- End of configurable params */
38
39 #define SECTOR_SHIFT            9
40 #define SECTORS_PER_PAGE_SHIFT  (PAGE_SHIFT - SECTOR_SHIFT)
41 #define SECTORS_PER_PAGE        (1 << SECTORS_PER_PAGE_SHIFT)
42 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
43 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
44 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK   \
45         (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
46
47
48 /*
49  * The lower ZRAM_FLAG_SHIFT bits of table.value is for
50  * object size (excluding header), the higher bits is for
51  * zram_pageflags.
52  *
53  * zram is mainly used for memory efficiency so we want to keep memory
54  * footprint small so we can squeeze size and flags into a field.
55  * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
56  * the higher bits is for zram_pageflags.
57  */
58 #define ZRAM_FLAG_SHIFT 24
59
60 /* Flags for zram pages (table[page_no].value) */
61 enum zram_pageflags {
62         /* Page consists entirely of zeros */
63         ZRAM_ZERO = ZRAM_FLAG_SHIFT,
64         ZRAM_ACCESS,    /* page is now accessed */
65
66         __NR_ZRAM_PAGEFLAGS,
67 };
68
69 /*-- Data structures */
70
71 /* Allocated for each disk page */
72 struct zram_table_entry {
73         unsigned long handle;
74         unsigned long value;
75 #ifdef CONFIG_PREEMPT_RT_BASE
76         spinlock_t lock;
77 #endif
78 };
79
80 struct zram_stats {
81         atomic64_t compr_data_size;     /* compressed size of pages stored */
82         atomic64_t num_reads;   /* failed + successful */
83         atomic64_t num_writes;  /* --do-- */
84         atomic64_t failed_reads;        /* can happen when memory is too low */
85         atomic64_t failed_writes;       /* can happen when memory is too low */
86         atomic64_t invalid_io;  /* non-page-aligned I/O requests */
87         atomic64_t notify_free; /* no. of swap slot free notifications */
88         atomic64_t zero_pages;          /* no. of zero filled pages */
89         atomic64_t pages_stored;        /* no. of pages currently stored */
90         atomic_long_t max_used_pages;   /* no. of maximum pages stored */
91 };
92
93 struct zram_meta {
94         struct zram_table_entry *table;
95         struct zs_pool *mem_pool;
96 };
97
98 struct zram {
99         struct zram_meta *meta;
100         struct zcomp *comp;
101         struct gendisk *disk;
102         /* Prevent concurrent execution of device init */
103         struct rw_semaphore init_lock;
104         /*
105          * the number of pages zram can consume for storing compressed data
106          */
107         unsigned long limit_pages;
108         int max_comp_streams;
109
110         struct zram_stats stats;
111         atomic_t refcount; /* refcount for zram_meta */
112         /* wait all IO under all of cpu are done */
113         wait_queue_head_t io_done;
114         /*
115          * This is the limit on amount of *uncompressed* worth of data
116          * we can store in a disk.
117          */
118         u64 disksize;   /* bytes */
119         char compressor[10];
120         /*
121          * zram is claimed so open request will be failed
122          */
123         bool claim; /* Protected by bdev->bd_mutex */
124 };
125
126 #ifndef CONFIG_PREEMPT_RT_BASE
127 static inline void zram_lock_table(struct zram_table_entry *table)
128 {
129         bit_spin_lock(ZRAM_ACCESS, &table->value);
130 }
131
132 static inline void zram_unlock_table(struct zram_table_entry *table)
133 {
134         bit_spin_unlock(ZRAM_ACCESS, &table->value);
135 }
136
137 static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
138 #else /* CONFIG_PREEMPT_RT_BASE */
139 static inline void zram_lock_table(struct zram_table_entry *table)
140 {
141         spin_lock(&table->lock);
142         __set_bit(ZRAM_ACCESS, &table->value);
143 }
144
145 static inline void zram_unlock_table(struct zram_table_entry *table)
146 {
147         __clear_bit(ZRAM_ACCESS, &table->value);
148         spin_unlock(&table->lock);
149 }
150
151 static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
152 {
153         size_t num_pages = disksize >> PAGE_SHIFT;
154         size_t index;
155
156         for (index = 0; index < num_pages; index++) {
157                 spinlock_t *lock = &meta->table[index].lock;
158                 spin_lock_init(lock);
159         }
160 }
161 #endif /* CONFIG_PREEMPT_RT_BASE */
162
163 #endif