blob: 3a1cac486e968a8da04fb3da580ecc934ed1e7b9 [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
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070018#include <linux/rwsem.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080019#include <linux/zsmalloc.h>
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070020#include <linux/crypto.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053021
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -070022#include "zcomp.h"
23
Nitin Gupta306b0c92009-09-22 10:26:53 +053024#define SECTOR_SHIFT 9
Nitin Gupta306b0c92009-09-22 10:26:53 +053025#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
26#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
Jerome Marchand924bd882011-06-10 15:28:48 +020027#define ZRAM_LOGICAL_BLOCK_SHIFT 12
28#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
29#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
30 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
Nitin Gupta306b0c92009-09-22 10:26:53 +053031
Weijie Yangd2d5e762014-08-06 16:08:31 -070032
33/*
34 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
35 * object size (excluding header), the higher bits is for
36 * zram_pageflags.
37 *
38 * zram is mainly used for memory efficiency so we want to keep memory
39 * footprint small so we can squeeze size and flags into a field.
40 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
41 * the higher bits is for zram_pageflags.
42 */
43#define ZRAM_FLAG_SHIFT 24
44
45/* Flags for zram pages (table[page_no].value) */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053046enum zram_pageflags {
Minchan Kime28229032018-06-07 17:05:39 -070047 /* zram slot is locked */
48 ZRAM_LOCK = ZRAM_FLAG_SHIFT,
49 ZRAM_SAME, /* Page consists the same element */
Minchan Kim598d0532017-09-06 16:20:03 -070050 ZRAM_WB, /* page is stored on backing_device */
Minchan Kimf185f712018-06-07 17:05:42 -070051 ZRAM_HUGE, /* Incompressible page */
Nitin Gupta306b0c92009-09-22 10:26:53 +053052
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053053 __NR_ZRAM_PAGEFLAGS,
Nitin Gupta306b0c92009-09-22 10:26:53 +053054};
55
56/*-- Data structures */
57
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053058/* Allocated for each disk page */
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070059struct zram_table_entry {
zhouxianrong74ccaa72017-02-24 14:59:27 -080060 union {
61 unsigned long handle;
62 unsigned long element;
63 };
Weijie Yangd2d5e762014-08-06 16:08:31 -070064 unsigned long value;
Minchan Kimf1dcb852018-06-07 17:05:49 -070065#ifdef CONFIG_ZRAM_MEMORY_TRACKING
66 ktime_t ac_time;
67#endif
Weijie Yangd2d5e762014-08-06 16:08:31 -070068};
Nitin Gupta306b0c92009-09-22 10:26:53 +053069
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053070struct zram_stats {
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070071 atomic64_t compr_data_size; /* compressed size of pages stored */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080072 atomic64_t num_reads; /* failed + successful */
73 atomic64_t num_writes; /* --do-- */
Chao Yu0cf1e9d2014-08-29 15:18:37 -070074 atomic64_t failed_reads; /* can happen when memory is too low */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080075 atomic64_t failed_writes; /* can happen when memory is too low */
76 atomic64_t invalid_io; /* non-page-aligned I/O requests */
77 atomic64_t notify_free; /* no. of swap slot free notifications */
zhouxianrong74ccaa72017-02-24 14:59:27 -080078 atomic64_t same_pages; /* no. of same element filled pages */
Minchan Kimf185f712018-06-07 17:05:42 -070079 atomic64_t huge_pages; /* no. of huge pages */
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070080 atomic64_t pages_stored; /* no. of pages currently stored */
Minchan Kim461a8ee2014-10-09 15:29:55 -070081 atomic_long_t max_used_pages; /* no. of maximum pages stored */
Sergey Senozhatsky623e47f2016-05-20 17:00:02 -070082 atomic64_t writestall; /* no. of write slow paths */
Nitin Gupta306b0c92009-09-22 10:26:53 +053083};
84
Minchan Kim6cb89542017-05-03 14:55:47 -070085struct zram {
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070086 struct zram_table_entry *table;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090087 struct zs_pool *mem_pool;
Minchan Kim08eee692015-02-12 15:00:45 -080088 struct zcomp *comp;
Nitin Gupta306b0c92009-09-22 10:26:53 +053089 struct gendisk *disk;
Minchan Kim08eee692015-02-12 15:00:45 -080090 /* Prevent concurrent execution of device init */
Jerome Marchand0900bea2011-09-06 15:02:11 +020091 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +053092 /*
Minchan Kim08eee692015-02-12 15:00:45 -080093 * the number of pages zram can consume for storing compressed data
94 */
95 unsigned long limit_pages;
Minchan Kim08eee692015-02-12 15:00:45 -080096
97 struct zram_stats stats;
Minchan Kim08eee692015-02-12 15:00:45 -080098 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053099 * This is the limit on amount of *uncompressed* worth of data
100 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530101 */
Nitin Gupta33863c22010-08-09 22:56:47 +0530102 u64 disksize; /* bytes */
Sergey Senozhatsky415403b2016-07-26 15:22:48 -0700103 char compressor[CRYPTO_MAX_ALG_NAME];
Sergey Senozhatskyf405c442015-06-25 15:00:21 -0700104 /*
105 * zram is claimed so open request will be failed
106 */
107 bool claim; /* Protected by bdev->bd_mutex */
Minchan Kim9ac886a2017-09-06 16:19:54 -0700108#ifdef CONFIG_ZRAM_WRITEBACK
109 struct file *backing_dev;
110 struct block_device *bdev;
111 unsigned int old_block_size;
Minchan Kim0e05f382017-09-06 16:19:57 -0700112 unsigned long *bitmap;
113 unsigned long nr_pages;
114 spinlock_t bitmap_lock;
Minchan Kim9ac886a2017-09-06 16:19:54 -0700115#endif
Minchan Kimf1dcb852018-06-07 17:05:49 -0700116#ifdef CONFIG_ZRAM_MEMORY_TRACKING
117 struct dentry *debugfs_dir;
118#endif
Nitin Gupta306b0c92009-09-22 10:26:53 +0530119};
Nitin Gupta6a907722010-01-28 21:13:37 +0530120#endif