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