blob: 8e92339686d7467584220ff1805002953d5e6621 [file] [log] [blame]
Nitin Gupta306b0c92009-09-22 10:26:53 +05301/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05302 * Compressed RAM block device
Nitin Gupta306b0c92009-09-22 10:26:53 +05303 *
Nitin Gupta1130ebb2010-01-28 21:21:35 +05304 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
Minchan Kim7bfb3de2014-01-30 15:45:55 -08005 * 2012, 2013 Minchan Kim
Nitin Gupta306b0c92009-09-22 10:26:53 +05306 *
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 *
Nitin Gupta306b0c92009-09-22 10:26:53 +053013 */
14
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053015#ifndef _ZRAM_DRV_H_
16#define _ZRAM_DRV_H_
Nitin Gupta306b0c92009-09-22 10:26:53 +053017
Nitin Gupta6a907722010-01-28 21:13:37 +053018#include <linux/spinlock.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080019#include <linux/zsmalloc.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053020
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -070021#include "zcomp.h"
22
Nitin Gupta306b0c92009-09-22 10:26:53 +053023/*-- Configurable parameters */
24
Nitin Gupta306b0c92009-09-22 10:26:53 +053025/*
Nitin Gupta306b0c92009-09-22 10:26:53 +053026 * Pages that compress to size greater than this are stored
27 * uncompressed in memory.
28 */
Nitin Gupta2ccbec02011-09-09 19:01:00 -040029static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
Nitin Gupta306b0c92009-09-22 10:26:53 +053030
31/*
Nitin Gupta97a06382010-05-13 14:24:21 +053032 * NOTE: max_zpage_size must be less than or equal to:
Minchan Kim55dcbbb2012-10-10 08:49:52 +090033 * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
34 * always return failure.
Nitin Gupta306b0c92009-09-22 10:26:53 +053035 */
36
37/*-- End of configurable params */
38
39#define SECTOR_SHIFT 9
Nitin Gupta306b0c92009-09-22 10:26:53 +053040#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
41#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
Jerome Marchand924bd882011-06-10 15:28:48 +020042#define ZRAM_LOGICAL_BLOCK_SHIFT 12
43#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
44#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
45 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
Nitin Gupta306b0c92009-09-22 10:26:53 +053046
Weijie Yangd2d5e762014-08-06 16:08:31 -070047
48/*
49 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
50 * object size (excluding header), the higher bits is for
51 * zram_pageflags.
52 *
53 * zram is mainly used for memory efficiency so we want to keep memory
54 * footprint small so we can squeeze size and flags into a field.
55 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
56 * the higher bits is for zram_pageflags.
57 */
58#define ZRAM_FLAG_SHIFT 24
59
60/* Flags for zram pages (table[page_no].value) */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053061enum zram_pageflags {
Nitin Gupta306b0c92009-09-22 10:26:53 +053062 /* Page consists entirely of zeros */
Mahendran Ganeshd49b1c22014-12-12 16:57:04 -080063 ZRAM_ZERO = ZRAM_FLAG_SHIFT,
64 ZRAM_ACCESS, /* page is now accessed */
Nitin Gupta306b0c92009-09-22 10:26:53 +053065
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053066 __NR_ZRAM_PAGEFLAGS,
Nitin Gupta306b0c92009-09-22 10:26:53 +053067};
68
69/*-- Data structures */
70
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053071/* Allocated for each disk page */
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070072struct zram_table_entry {
Minchan Kimc2344342012-06-08 15:39:25 +090073 unsigned long handle;
Weijie Yangd2d5e762014-08-06 16:08:31 -070074 unsigned long value;
75};
Nitin Gupta306b0c92009-09-22 10:26:53 +053076
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053077struct zram_stats {
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070078 atomic64_t compr_data_size; /* compressed size of pages stored */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080079 atomic64_t num_reads; /* failed + successful */
80 atomic64_t num_writes; /* --do-- */
Chao Yu0cf1e9d2014-08-29 15:18:37 -070081 atomic64_t failed_reads; /* can happen when memory is too low */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080082 atomic64_t failed_writes; /* can happen when memory is too low */
83 atomic64_t invalid_io; /* non-page-aligned I/O requests */
84 atomic64_t notify_free; /* no. of swap slot free notifications */
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070085 atomic64_t zero_pages; /* no. of zero filled pages */
86 atomic64_t pages_stored; /* no. of pages currently stored */
Minchan Kim461a8ee2014-10-09 15:29:55 -070087 atomic_long_t max_used_pages; /* no. of maximum pages stored */
Nitin Gupta306b0c92009-09-22 10:26:53 +053088};
89
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090090struct zram_meta {
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070091 struct zram_table_entry *table;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090092 struct zs_pool *mem_pool;
93};
94
95struct zram {
96 struct zram_meta *meta;
Minchan Kim08eee692015-02-12 15:00:45 -080097 struct zcomp *comp;
Nitin Gupta306b0c92009-09-22 10:26:53 +053098 struct gendisk *disk;
Minchan Kim08eee692015-02-12 15:00:45 -080099 /* Prevent concurrent execution of device init */
Jerome Marchand0900bea2011-09-06 15:02:11 +0200100 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530101 /*
Minchan Kim08eee692015-02-12 15:00:45 -0800102 * the number of pages zram can consume for storing compressed data
103 */
104 unsigned long limit_pages;
105 int max_comp_streams;
106
107 struct zram_stats stats;
108 atomic_t refcount; /* refcount for zram_meta */
109 /* wait all IO under all of cpu are done */
110 wait_queue_head_t io_done;
111 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530112 * This is the limit on amount of *uncompressed* worth of data
113 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530114 */
Nitin Gupta33863c22010-08-09 22:56:47 +0530115 u64 disksize; /* bytes */
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700116 char compressor[10];
Sergey Senozhatskyf405c442015-06-25 15:00:21 -0700117 /*
118 * zram is claimed so open request will be failed
119 */
120 bool claim; /* Protected by bdev->bd_mutex */
Nitin Gupta306b0c92009-09-22 10:26:53 +0530121};
Nitin Gupta6a907722010-01-28 21:13:37 +0530122#endif