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 21 #include "zcomp.h" 22 23 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 24 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) 25 #define ZRAM_LOGICAL_BLOCK_SHIFT 12 26 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) 27 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ 28 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) 29 30 /* 31 * ZRAM is mainly used for memory efficiency so we want to keep memory 32 * footprint small and thus squeeze size and zram pageflags into a flags 33 * member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding 34 * header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT 35 * bits), the higher bits are for zram_pageflags. 36 * 37 * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow. 38 */ 39 #define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1) 40 41 /* Only 2 bits are allowed for comp priority index */ 42 #define ZRAM_COMP_PRIORITY_MASK 0x3 43 44 /* Flags for zram pages (table[page_no].flags) */ 45 enum zram_pageflags { 46 ZRAM_SAME = ZRAM_FLAG_SHIFT, /* Page consists the same element */ 47 ZRAM_ENTRY_LOCK, /* entry access lock bit */ 48 ZRAM_WB, /* page is stored on backing_device */ 49 ZRAM_PP_SLOT, /* Selected for post-processing */ 50 ZRAM_HUGE, /* Incompressible page */ 51 ZRAM_IDLE, /* not accessed page since last idle marking */ 52 ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */ 53 54 ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */ 55 ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */ 56 57 __NR_ZRAM_PAGEFLAGS, 58 }; 59 60 /* 61 * Allocated for each disk page. We use bit-lock (ZRAM_ENTRY_LOCK bit 62 * of flags) to save memory. There can be plenty of entries and standard 63 * locking primitives (e.g. mutex) will significantly increase sizeof() 64 * of each entry and hence of the meta table. 65 */ 66 struct zram_table_entry { 67 unsigned long handle; 68 unsigned long flags; 69 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME 70 ktime_t ac_time; 71 #endif 72 struct lockdep_map dep_map; 73 }; 74 75 struct zram_stats { 76 atomic64_t compr_data_size; /* compressed size of pages stored */ 77 atomic64_t failed_reads; /* can happen when memory is too low */ 78 atomic64_t failed_writes; /* can happen when memory is too low */ 79 atomic64_t notify_free; /* no. of swap slot free notifications */ 80 atomic64_t same_pages; /* no. of same element filled pages */ 81 atomic64_t huge_pages; /* no. of huge pages */ 82 atomic64_t huge_pages_since; /* no. of huge pages since zram set up */ 83 atomic64_t pages_stored; /* no. of pages currently stored */ 84 atomic_long_t max_used_pages; /* no. of maximum pages stored */ 85 atomic64_t miss_free; /* no. of missed free */ 86 #ifdef CONFIG_ZRAM_WRITEBACK 87 atomic64_t bd_count; /* no. of pages in backing device */ 88 atomic64_t bd_reads; /* no. of reads from backing device */ 89 atomic64_t bd_writes; /* no. of writes from backing device */ 90 #endif 91 }; 92 93 #ifdef CONFIG_ZRAM_MULTI_COMP 94 #define ZRAM_PRIMARY_COMP 0U 95 #define ZRAM_SECONDARY_COMP 1U 96 #define ZRAM_MAX_COMPS 4U 97 #else 98 #define ZRAM_PRIMARY_COMP 0U 99 #define ZRAM_SECONDARY_COMP 0U 100 #define ZRAM_MAX_COMPS 1U 101 #endif 102 103 struct zram { 104 struct zram_table_entry *table; 105 struct zs_pool *mem_pool; 106 struct zcomp *comps[ZRAM_MAX_COMPS]; 107 struct zcomp_params params[ZRAM_MAX_COMPS]; 108 struct gendisk *disk; 109 /* Prevent concurrent execution of device init */ 110 struct rw_semaphore init_lock; 111 /* 112 * the number of pages zram can consume for storing compressed data 113 */ 114 unsigned long limit_pages; 115 116 struct zram_stats stats; 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 const char *comp_algs[ZRAM_MAX_COMPS]; 123 s8 num_active_comps; 124 /* 125 * zram is claimed so open request will be failed 126 */ 127 bool claim; /* Protected by disk->open_mutex */ 128 #ifdef CONFIG_ZRAM_WRITEBACK 129 struct file *backing_dev; 130 spinlock_t wb_limit_lock; 131 bool wb_limit_enable; 132 u64 bd_wb_limit; 133 struct block_device *bdev; 134 unsigned long *bitmap; 135 unsigned long nr_pages; 136 #endif 137 #ifdef CONFIG_ZRAM_MEMORY_TRACKING 138 struct dentry *debugfs_dir; 139 #endif 140 atomic_t pp_in_progress; 141 }; 142 #endif 143