Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Compressed RAM block device
0003  *
0004  * Copyright (C) 2008, 2009, 2010  Nitin Gupta
0005  *               2012, 2013 Minchan Kim
0006  *
0007  * This code is released using a dual license strategy: BSD/GPL
0008  * You can choose the licence that better fits your requirements.
0009  *
0010  * Released under the terms of 3-clause BSD License
0011  * Released under the terms of GNU General Public License Version 2.0
0012  *
0013  */
0014 
0015 #ifndef _ZRAM_DRV_H_
0016 #define _ZRAM_DRV_H_
0017 
0018 #include <linux/rwsem.h>
0019 #include <linux/zsmalloc.h>
0020 #include <linux/crypto.h>
0021 
0022 #include "zcomp.h"
0023 
0024 #define SECTORS_PER_PAGE_SHIFT  (PAGE_SHIFT - SECTOR_SHIFT)
0025 #define SECTORS_PER_PAGE    (1 << SECTORS_PER_PAGE_SHIFT)
0026 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
0027 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
0028 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK   \
0029     (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
0030 
0031 
0032 /*
0033  * The lower ZRAM_FLAG_SHIFT bits of table.flags is for
0034  * object size (excluding header), the higher bits is for
0035  * zram_pageflags.
0036  *
0037  * zram is mainly used for memory efficiency so we want to keep memory
0038  * footprint small so we can squeeze size and flags into a field.
0039  * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
0040  * the higher bits is for zram_pageflags.
0041  */
0042 #define ZRAM_FLAG_SHIFT 24
0043 
0044 /* Flags for zram pages (table[page_no].flags) */
0045 enum zram_pageflags {
0046     /* zram slot is locked */
0047     ZRAM_LOCK = ZRAM_FLAG_SHIFT,
0048     ZRAM_SAME,  /* Page consists the same element */
0049     ZRAM_WB,    /* page is stored on backing_device */
0050     ZRAM_UNDER_WB,  /* page is under writeback */
0051     ZRAM_HUGE,  /* Incompressible page */
0052     ZRAM_IDLE,  /* not accessed page since last idle marking */
0053 
0054     __NR_ZRAM_PAGEFLAGS,
0055 };
0056 
0057 /*-- Data structures */
0058 
0059 /* Allocated for each disk page */
0060 struct zram_table_entry {
0061     union {
0062         unsigned long handle;
0063         unsigned long element;
0064     };
0065     unsigned long flags;
0066 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
0067     ktime_t ac_time;
0068 #endif
0069 };
0070 
0071 struct zram_stats {
0072     atomic64_t compr_data_size; /* compressed size of pages stored */
0073     atomic64_t num_reads;   /* failed + successful */
0074     atomic64_t num_writes;  /* --do-- */
0075     atomic64_t failed_reads;    /* can happen when memory is too low */
0076     atomic64_t failed_writes;   /* can happen when memory is too low */
0077     atomic64_t invalid_io;  /* non-page-aligned I/O requests */
0078     atomic64_t notify_free; /* no. of swap slot free notifications */
0079     atomic64_t same_pages;      /* no. of same element filled pages */
0080     atomic64_t huge_pages;      /* no. of huge pages */
0081     atomic64_t huge_pages_since;    /* no. of huge pages since zram set up */
0082     atomic64_t pages_stored;    /* no. of pages currently stored */
0083     atomic_long_t max_used_pages;   /* no. of maximum pages stored */
0084     atomic64_t writestall;      /* no. of write slow paths */
0085     atomic64_t miss_free;       /* no. of missed free */
0086 #ifdef  CONFIG_ZRAM_WRITEBACK
0087     atomic64_t bd_count;        /* no. of pages in backing device */
0088     atomic64_t bd_reads;        /* no. of reads from backing device */
0089     atomic64_t bd_writes;       /* no. of writes from backing device */
0090 #endif
0091 };
0092 
0093 struct zram {
0094     struct zram_table_entry *table;
0095     struct zs_pool *mem_pool;
0096     struct zcomp *comp;
0097     struct gendisk *disk;
0098     /* Prevent concurrent execution of device init */
0099     struct rw_semaphore init_lock;
0100     /*
0101      * the number of pages zram can consume for storing compressed data
0102      */
0103     unsigned long limit_pages;
0104 
0105     struct zram_stats stats;
0106     /*
0107      * This is the limit on amount of *uncompressed* worth of data
0108      * we can store in a disk.
0109      */
0110     u64 disksize;   /* bytes */
0111     char compressor[CRYPTO_MAX_ALG_NAME];
0112     /*
0113      * zram is claimed so open request will be failed
0114      */
0115     bool claim; /* Protected by disk->open_mutex */
0116 #ifdef CONFIG_ZRAM_WRITEBACK
0117     struct file *backing_dev;
0118     spinlock_t wb_limit_lock;
0119     bool wb_limit_enable;
0120     u64 bd_wb_limit;
0121     struct block_device *bdev;
0122     unsigned long *bitmap;
0123     unsigned long nr_pages;
0124 #endif
0125 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
0126     struct dentry *debugfs_dir;
0127 #endif
0128 };
0129 #endif