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 <asm/byteorder.h> 19 #include <linux/rwsem.h> 20 #include <linux/zsmalloc.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 * ZRAM is mainly used for memory efficiency so we want to keep memory 33 * footprint small and thus squeeze size and zram pageflags into a flags 34 * member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding 35 * header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT 36 * bits), the higher bits are for zram_pageflags. 37 * 38 * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow. 39 */ 40 #define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1) 41 42 /* Only 2 bits are allowed for comp priority index */ 43 #define ZRAM_COMP_PRIORITY_MASK 0x3 44 45 /* Flags for zram pages (table[page_no].flags) */ 46 enum zram_pageflags { 47 ZRAM_SAME = ZRAM_FLAG_SHIFT, /* Page consists the same element */ 48 ZRAM_ENTRY_LOCK, /* entry access lock bit */ 49 ZRAM_WB, /* page is stored on backing_device */ 50 ZRAM_PP_SLOT, /* Selected for post-processing */ 51 ZRAM_HUGE, /* Incompressible page */ 52 ZRAM_IDLE, /* not accessed page since last idle marking */ 53 ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */ 54 55 ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */ 56 ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */ 57 58 __NR_ZRAM_PAGEFLAGS, 59 }; 60 61 /* 62 * The slot lock is a bit-wait lock on the whole __lock word, while 63 * flags and ac_time alias that word as two u32s. The lock bit must 64 * land in the slot that ZRAM_ENTRY_LOCK reserves in attr.flags; on 65 * 64-bit big-endian the flags word maps to the upper half of __lock, 66 * so the bit position has to be shifted up. 67 */ 68 #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN) 69 #define ZRAM_ENTRY_LOCK_BIT (ZRAM_ENTRY_LOCK + 32) 70 #else 71 #define ZRAM_ENTRY_LOCK_BIT ZRAM_ENTRY_LOCK 72 #endif 73 74 /* 75 * Allocated for each disk page. We use bit-lock (ZRAM_ENTRY_LOCK bit 76 * of flags) to save memory. There can be plenty of entries and standard 77 * locking primitives (e.g. mutex) will significantly increase sizeof() 78 * of each entry and hence of the meta table. 79 */ 80 struct zram_table_entry { 81 unsigned long handle; 82 union { 83 unsigned long __lock; 84 struct attr { 85 u32 flags; 86 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME 87 u32 ac_time; 88 #endif 89 } attr; 90 }; 91 }; 92 93 struct zram_stats { 94 atomic64_t compr_data_size; /* compressed size of pages stored */ 95 atomic64_t failed_reads; /* can happen when memory is too low */ 96 atomic64_t failed_writes; /* can happen when memory is too low */ 97 atomic64_t notify_free; /* no. of swap slot free notifications */ 98 atomic64_t same_pages; /* no. of same element filled pages */ 99 atomic64_t huge_pages; /* no. of huge pages */ 100 atomic64_t huge_pages_since; /* no. of huge pages since zram set up */ 101 atomic64_t pages_stored; /* no. of pages currently stored */ 102 atomic_long_t max_used_pages; /* no. of maximum pages stored */ 103 atomic64_t miss_free; /* no. of missed free */ 104 #ifdef CONFIG_ZRAM_WRITEBACK 105 atomic64_t bd_count; /* no. of pages in backing device */ 106 atomic64_t bd_reads; /* no. of reads from backing device */ 107 atomic64_t bd_writes; /* no. of writes from backing device */ 108 #endif 109 }; 110 111 #ifdef CONFIG_ZRAM_MULTI_COMP 112 #define ZRAM_PRIMARY_COMP 0U 113 #define ZRAM_SECONDARY_COMP 1U 114 #define ZRAM_MAX_COMPS 4U 115 #else 116 #define ZRAM_PRIMARY_COMP 0U 117 #define ZRAM_SECONDARY_COMP 0U 118 #define ZRAM_MAX_COMPS 1U 119 #endif 120 121 struct zram { 122 struct zram_table_entry *table; 123 struct lockdep_map table_lock_map; 124 struct lock_class_key table_lock_key; 125 struct zs_pool *mem_pool; 126 struct zcomp *comps[ZRAM_MAX_COMPS]; 127 struct zcomp_params params[ZRAM_MAX_COMPS]; 128 struct gendisk *disk; 129 /* Locks the device either in exclusive or in shared mode */ 130 struct rw_semaphore dev_lock; 131 /* 132 * the number of pages zram can consume for storing compressed data 133 */ 134 unsigned long limit_pages; 135 136 struct zram_stats stats; 137 /* 138 * This is the limit on amount of *uncompressed* worth of data 139 * we can store in a disk. 140 */ 141 u64 disksize; /* bytes */ 142 const char *comp_algs[ZRAM_MAX_COMPS]; 143 /* 144 * zram is claimed so open request will be failed 145 */ 146 bool claim; /* Protected by disk->open_mutex */ 147 #ifdef CONFIG_ZRAM_WRITEBACK 148 struct file *backing_dev; 149 bool wb_limit_enable; 150 bool compressed_wb; 151 u32 wb_batch_size; 152 u64 bd_wb_limit; 153 struct block_device *bdev; 154 unsigned long *bitmap; 155 unsigned long nr_pages; 156 #endif 157 #ifdef CONFIG_ZRAM_MEMORY_TRACKING 158 struct dentry *debugfs_dir; 159 #endif 160 }; 161 #endif 162