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#ifdef CONFIG_ZRAM_GROUP 25#include "zram_group.h" 26#endif 27 28#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 29#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) 30#define ZRAM_LOGICAL_BLOCK_SHIFT 12 31#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) 32#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ 33 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) 34 35 36/* 37 * ZRAM is mainly used for memory efficiency so we want to keep memory 38 * footprint small and thus squeeze size and zram pageflags into a flags 39 * member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding 40 * header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT 41 * bits), the higher bits are for zram_pageflags. 42 * 43 * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow. 44 */ 45#ifdef CONFIG_ZRAM_GROUP 46/* reserve 16 bits for group id */ 47#define ZRAM_SIZE_SHIFT 24 48#define ZRAM_GRPID_SHIFT 16 49#define ZRAM_GRPID_MASK (((1UL << ZRAM_GRPID_SHIFT) - 1) << ZRAM_SIZE_SHIFT) 50#define ZRAM_FLAG_SHIFT (ZRAM_SIZE_SHIFT + ZRAM_GRPID_SHIFT) 51#else 52#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1) 53#endif 54 55/* Only 2 bits are allowed for comp priority index */ 56#define ZRAM_COMP_PRIORITY_MASK 0x3 57 58/* Flags for zram pages (table[page_no].flags) */ 59enum zram_pageflags { 60 /* zram slot is locked */ 61 ZRAM_LOCK = ZRAM_FLAG_SHIFT, 62 ZRAM_SAME, /* Page consists the same element */ 63 ZRAM_WB, /* page is stored on backing_device */ 64 ZRAM_UNDER_WB, /* page is under writeback */ 65 ZRAM_HUGE, /* Incompressible page */ 66 ZRAM_IDLE, /* not accessed page since last idle marking */ 67#ifdef CONFIG_ZRAM_GROUP_WRITEBACK 68 ZRAM_GWB, /* obj is group writeback*/ 69 ZRAM_FAULT, /* obj is needed by a pagefault req */ 70#endif 71 ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */ 72 73 ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */ 74 ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */ 75 76 __NR_ZRAM_PAGEFLAGS, 77}; 78 79/*-- Data structures */ 80 81/* Allocated for each disk page */ 82struct zram_table_entry { 83 union { 84 unsigned long handle; 85 unsigned long element; 86 }; 87 unsigned long flags; 88#ifdef CONFIG_ZRAM_MEMORY_TRACKING 89 ktime_t ac_time; 90#endif 91}; 92 93struct 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 writestall; /* no. of write slow paths */ 104 atomic64_t miss_free; /* no. of missed free */ 105#ifdef CONFIG_ZRAM_WRITEBACK 106 atomic64_t bd_count; /* no. of pages in backing device */ 107 atomic64_t bd_reads; /* no. of reads from backing device */ 108 atomic64_t bd_writes; /* no. of writes from backing device */ 109#endif 110}; 111 112#ifdef CONFIG_ZRAM_MULTI_COMP 113#define ZRAM_PRIMARY_COMP 0U 114#define ZRAM_SECONDARY_COMP 1U 115#define ZRAM_MAX_COMPS 4U 116#else 117#define ZRAM_PRIMARY_COMP 0U 118#define ZRAM_SECONDARY_COMP 0U 119#define ZRAM_MAX_COMPS 1U 120#endif 121 122struct zram { 123 struct zram_table_entry *table; 124#ifdef CONFIG_ZRAM_GROUP 125 struct zram_group *zgrp; 126 unsigned int zgrp_ctrl; 127#endif 128 struct zs_pool *mem_pool; 129 struct zcomp *comps[ZRAM_MAX_COMPS]; 130 struct gendisk *disk; 131 /* Prevent concurrent execution of device init */ 132 struct rw_semaphore init_lock; 133 /* 134 * the number of pages zram can consume for storing compressed data 135 */ 136 unsigned long limit_pages; 137 138 struct zram_stats stats; 139 /* 140 * This is the limit on amount of *uncompressed* worth of data 141 * we can store in a disk. 142 */ 143 u64 disksize; /* bytes */ 144 const char *comp_algs[ZRAM_MAX_COMPS]; 145 s8 num_active_comps; 146 /* 147 * zram is claimed so open request will be failed 148 */ 149 bool claim; /* Protected by disk->open_mutex */ 150#ifdef CONFIG_ZRAM_WRITEBACK 151 struct file *backing_dev; 152 spinlock_t wb_limit_lock; 153 bool wb_limit_enable; 154 u64 bd_wb_limit; 155 struct block_device *bdev; 156 unsigned long *bitmap; 157 unsigned long nr_pages; 158#endif 159#ifdef CONFIG_ZRAM_MEMORY_TRACKING 160 struct dentry *debugfs_dir; 161#endif 162}; 163 164static inline int zram_slot_trylock(struct zram *zram, u32 index) 165{ 166 return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags); 167} 168 169static inline void zram_slot_lock(struct zram *zram, u32 index) 170{ 171 bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags); 172} 173 174static inline void zram_slot_unlock(struct zram *zram, u32 index) 175{ 176 bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags); 177} 178 179static inline unsigned long zram_get_handle(struct zram *zram, u32 index) 180{ 181 return zram->table[index].handle; 182} 183 184static inline void zram_set_handle(struct zram *zram, u32 index, unsigned long handle) 185{ 186 zram->table[index].handle = handle; 187} 188 189/* flag operations require table entry bit_spin_lock() being held */ 190static inline bool zram_test_flag(struct zram *zram, u32 index, 191 enum zram_pageflags flag) 192{ 193 return zram->table[index].flags & BIT(flag); 194} 195 196static inline void zram_set_flag(struct zram *zram, u32 index, 197 enum zram_pageflags flag) 198{ 199 zram->table[index].flags |= BIT(flag); 200} 201 202static inline void zram_clear_flag(struct zram *zram, u32 index, 203 enum zram_pageflags flag) 204{ 205 zram->table[index].flags &= ~BIT(flag); 206} 207#ifdef CONFIG_ZRAM_GROUP 208static inline size_t zram_get_obj_size(struct zram *zram, u32 index) 209{ 210 return zram->table[index].flags & (BIT(ZRAM_SIZE_SHIFT) - 1); 211} 212 213static inline void zram_set_obj_size(struct zram *zram, u32 index, size_t size) 214{ 215 unsigned long flags = zram->table[index].flags >> ZRAM_SIZE_SHIFT; 216 217 zram->table[index].flags = (flags << ZRAM_SIZE_SHIFT) | size; 218} 219 220void zram_group_init(struct zram *zram, u32 nr_obj); 221void zram_group_deinit(struct zram *zram); 222void zram_group_track_obj(struct zram *zram, u32 index, struct mem_cgroup *memcg); 223void zram_group_untrack_obj(struct zram *zram, u32 index); 224#ifdef CONFIG_ZRAM_GROUP_WRITEBACK 225int zram_group_fault_obj(struct zram *zram, u32 index); 226#endif 227 228#ifdef CONFIG_ZRAM_GROUP_DEBUG 229void group_debug(struct zram *zram, u32 op, u32 index, u32 gid); 230#endif 231 232#else 233static inline size_t zram_get_obj_size(struct zram *zram, u32 index) 234{ 235 return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1); 236} 237 238static inline void zram_set_obj_size(struct zram *zram, u32 index, size_t size) 239{ 240 unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT; 241 242 zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size; 243} 244#endif 245#endif 246