/*1* Compressed RAM block device2*3* Copyright (C) 2008, 2009, 2010 Nitin Gupta4* 2012, 2013 Minchan Kim5*6* This code is released using a dual license strategy: BSD/GPL7* You can choose the licence that better fits your requirements.8*9* Released under the terms of 3-clause BSD License10* Released under the terms of GNU General Public License Version 2.011*12*/1314#ifndef _ZRAM_DRV_H_15#define _ZRAM_DRV_H_1617#include <linux/rwsem.h>18#include <linux/zsmalloc.h>1920#include "zcomp.h"2122#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)23#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)24#define ZRAM_LOGICAL_BLOCK_SHIFT 1225#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)26#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \27(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))2829/*30* ZRAM is mainly used for memory efficiency so we want to keep memory31* footprint small and thus squeeze size and zram pageflags into a flags32* member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding33* header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT34* bits), the higher bits are for zram_pageflags.35*36* We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.37*/38#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)3940/* Only 2 bits are allowed for comp priority index */41#define ZRAM_COMP_PRIORITY_MASK 0x34243/* Flags for zram pages (table[page_no].flags) */44enum zram_pageflags {45ZRAM_SAME = ZRAM_FLAG_SHIFT, /* Page consists the same element */46ZRAM_ENTRY_LOCK, /* entry access lock bit */47ZRAM_WB, /* page is stored on backing_device */48ZRAM_PP_SLOT, /* Selected for post-processing */49ZRAM_HUGE, /* Incompressible page */50ZRAM_IDLE, /* not accessed page since last idle marking */51ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */5253ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */54ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */5556__NR_ZRAM_PAGEFLAGS,57};5859/*60* Allocated for each disk page. We use bit-lock (ZRAM_ENTRY_LOCK bit61* of flags) to save memory. There can be plenty of entries and standard62* locking primitives (e.g. mutex) will significantly increase sizeof()63* of each entry and hence of the meta table.64*/65struct zram_table_entry {66unsigned long handle;67unsigned long flags;68#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME69ktime_t ac_time;70#endif71struct lockdep_map dep_map;72};7374struct zram_stats {75atomic64_t compr_data_size; /* compressed size of pages stored */76atomic64_t failed_reads; /* can happen when memory is too low */77atomic64_t failed_writes; /* can happen when memory is too low */78atomic64_t notify_free; /* no. of swap slot free notifications */79atomic64_t same_pages; /* no. of same element filled pages */80atomic64_t huge_pages; /* no. of huge pages */81atomic64_t huge_pages_since; /* no. of huge pages since zram set up */82atomic64_t pages_stored; /* no. of pages currently stored */83atomic_long_t max_used_pages; /* no. of maximum pages stored */84atomic64_t miss_free; /* no. of missed free */85#ifdef CONFIG_ZRAM_WRITEBACK86atomic64_t bd_count; /* no. of pages in backing device */87atomic64_t bd_reads; /* no. of reads from backing device */88atomic64_t bd_writes; /* no. of writes from backing device */89#endif90};9192#ifdef CONFIG_ZRAM_MULTI_COMP93#define ZRAM_PRIMARY_COMP 0U94#define ZRAM_SECONDARY_COMP 1U95#define ZRAM_MAX_COMPS 4U96#else97#define ZRAM_PRIMARY_COMP 0U98#define ZRAM_SECONDARY_COMP 0U99#define ZRAM_MAX_COMPS 1U100#endif101102struct zram {103struct zram_table_entry *table;104struct zs_pool *mem_pool;105struct zcomp *comps[ZRAM_MAX_COMPS];106struct zcomp_params params[ZRAM_MAX_COMPS];107struct gendisk *disk;108/* Prevent concurrent execution of device init */109struct rw_semaphore init_lock;110/*111* the number of pages zram can consume for storing compressed data112*/113unsigned long limit_pages;114115struct zram_stats stats;116/*117* This is the limit on amount of *uncompressed* worth of data118* we can store in a disk.119*/120u64 disksize; /* bytes */121const char *comp_algs[ZRAM_MAX_COMPS];122s8 num_active_comps;123/*124* zram is claimed so open request will be failed125*/126bool claim; /* Protected by disk->open_mutex */127#ifdef CONFIG_ZRAM_WRITEBACK128struct file *backing_dev;129spinlock_t wb_limit_lock;130bool wb_limit_enable;131u64 bd_wb_limit;132struct block_device *bdev;133unsigned long *bitmap;134unsigned long nr_pages;135#endif136#ifdef CONFIG_ZRAM_MEMORY_TRACKING137struct dentry *debugfs_dir;138#endif139atomic_t pp_in_progress;140};141#endif142143144