blob: c6ee271317f5b344619bdc7c01bc16a858f18a9f [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/*
24 * Some arbitrary value. This is just to catch
25 * invalid value for num_devices module parameter.
26 */
27static const unsigned max_num_devices = 32;
28
Nitin Gupta306b0c92009-09-22 10:26:53 +053029/*-- Configurable parameters */
30
Nitin Gupta306b0c92009-09-22 10:26:53 +053031/*
Nitin Gupta306b0c92009-09-22 10:26:53 +053032 * Pages that compress to size greater than this are stored
33 * uncompressed in memory.
34 */
Nitin Gupta2ccbec02011-09-09 19:01:00 -040035static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
Nitin Gupta306b0c92009-09-22 10:26:53 +053036
37/*
Nitin Gupta97a06382010-05-13 14:24:21 +053038 * NOTE: max_zpage_size must be less than or equal to:
Minchan Kim55dcbbb2012-10-10 08:49:52 +090039 * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
40 * always return failure.
Nitin Gupta306b0c92009-09-22 10:26:53 +053041 */
42
43/*-- End of configurable params */
44
45#define SECTOR_SHIFT 9
Nitin Gupta306b0c92009-09-22 10:26:53 +053046#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
47#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
Jerome Marchand924bd882011-06-10 15:28:48 +020048#define ZRAM_LOGICAL_BLOCK_SHIFT 12
49#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
50#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
51 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
Nitin Gupta306b0c92009-09-22 10:26:53 +053052
Weijie Yangd2d5e762014-08-06 16:08:31 -070053
54/*
55 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
56 * object size (excluding header), the higher bits is for
57 * zram_pageflags.
58 *
59 * zram is mainly used for memory efficiency so we want to keep memory
60 * footprint small so we can squeeze size and flags into a field.
61 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
62 * the higher bits is for zram_pageflags.
63 */
64#define ZRAM_FLAG_SHIFT 24
65
66/* Flags for zram pages (table[page_no].value) */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053067enum zram_pageflags {
Nitin Gupta306b0c92009-09-22 10:26:53 +053068 /* Page consists entirely of zeros */
Weijie Yangd2d5e762014-08-06 16:08:31 -070069 ZRAM_ZERO = ZRAM_FLAG_SHIFT + 1,
70 ZRAM_ACCESS, /* page in now accessed */
Nitin Gupta306b0c92009-09-22 10:26:53 +053071
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053072 __NR_ZRAM_PAGEFLAGS,
Nitin Gupta306b0c92009-09-22 10:26:53 +053073};
74
75/*-- Data structures */
76
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053077/* Allocated for each disk page */
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070078struct zram_table_entry {
Minchan Kimc2344342012-06-08 15:39:25 +090079 unsigned long handle;
Weijie Yangd2d5e762014-08-06 16:08:31 -070080 unsigned long value;
81};
Nitin Gupta306b0c92009-09-22 10:26:53 +053082
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053083struct zram_stats {
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070084 atomic64_t compr_data_size; /* compressed size of pages stored */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080085 atomic64_t num_reads; /* failed + successful */
86 atomic64_t num_writes; /* --do-- */
Chao Yu0cf1e9d2014-08-29 15:18:37 -070087 atomic64_t failed_reads; /* can happen when memory is too low */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080088 atomic64_t failed_writes; /* can happen when memory is too low */
89 atomic64_t invalid_io; /* non-page-aligned I/O requests */
90 atomic64_t notify_free; /* no. of swap slot free notifications */
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070091 atomic64_t zero_pages; /* no. of zero filled pages */
92 atomic64_t pages_stored; /* no. of pages currently stored */
Minchan Kim461a8ee2014-10-09 15:29:55 -070093 atomic_long_t max_used_pages; /* no. of maximum pages stored */
Nitin Gupta306b0c92009-09-22 10:26:53 +053094};
95
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090096struct zram_meta {
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070097 struct zram_table_entry *table;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090098 struct zs_pool *mem_pool;
99};
100
101struct zram {
102 struct zram_meta *meta;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530103 struct request_queue *queue;
104 struct gendisk *disk;
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -0700105 struct zcomp *comp;
106
Jerome Marchand0900bea2011-09-06 15:02:11 +0200107 /* Prevent concurrent execution of device init, reset and R/W request */
108 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530109 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530110 * This is the limit on amount of *uncompressed* worth of data
111 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530112 */
Nitin Gupta33863c22010-08-09 22:56:47 +0530113 u64 disksize; /* bytes */
Sergey Senozhatskybeca3ec2014-04-07 15:38:14 -0700114 int max_comp_streams;
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530115 struct zram_stats stats;
Minchan Kim9ada9da2014-10-09 15:29:53 -0700116 /*
117 * the number of pages zram can consume for storing compressed data
118 */
119 unsigned long limit_pages;
120
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -0700121 char compressor[10];
Nitin Gupta306b0c92009-09-22 10:26:53 +0530122};
Nitin Gupta6a907722010-01-28 21:13:37 +0530123#endif