Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 2 | * Compressed RAM block device |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 3 | * |
Nitin Gupta | 1130ebb | 2010-01-28 21:21:35 +0530 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
Minchan Kim | 7bfb3de | 2014-01-30 15:45:55 -0800 | [diff] [blame] | 5 | * 2012, 2013 Minchan Kim |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 6 | * |
| 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 Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 13 | */ |
| 14 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 15 | #ifndef _ZRAM_DRV_H_ |
| 16 | #define _ZRAM_DRV_H_ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 17 | |
Sergey Senozhatsky | 415403b | 2016-07-26 15:22:48 -0700 | [diff] [blame] | 18 | #include <linux/rwsem.h> |
Minchan Kim | bcf1647 | 2014-01-30 15:45:50 -0800 | [diff] [blame] | 19 | #include <linux/zsmalloc.h> |
Sergey Senozhatsky | 415403b | 2016-07-26 15:22:48 -0700 | [diff] [blame] | 20 | #include <linux/crypto.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 21 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 22 | #include "zcomp.h" |
| 23 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 24 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 25 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 26 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
| 27 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) |
| 28 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ |
| 29 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 30 | |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * The lower ZRAM_FLAG_SHIFT bits of table.value is for |
| 34 | * object size (excluding header), the higher bits is for |
| 35 | * zram_pageflags. |
| 36 | * |
| 37 | * zram is mainly used for memory efficiency so we want to keep memory |
| 38 | * footprint small so we can squeeze size and flags into a field. |
| 39 | * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header), |
| 40 | * the higher bits is for zram_pageflags. |
| 41 | */ |
| 42 | #define ZRAM_FLAG_SHIFT 24 |
| 43 | |
| 44 | /* Flags for zram pages (table[page_no].value) */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 45 | enum zram_pageflags { |
Minchan Kim | db8ffbd | 2017-09-06 16:20:03 -0700 | [diff] [blame] | 46 | /* Page consists the same element */ |
zhouxianrong | 8e19d54 | 2017-02-24 14:59:27 -0800 | [diff] [blame] | 47 | ZRAM_SAME = ZRAM_FLAG_SHIFT, |
Mahendran Ganesh | d49b1c2 | 2014-12-12 16:57:04 -0800 | [diff] [blame] | 48 | ZRAM_ACCESS, /* page is now accessed */ |
Minchan Kim | db8ffbd | 2017-09-06 16:20:03 -0700 | [diff] [blame] | 49 | ZRAM_WB, /* page is stored on backing_device */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 50 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 51 | __NR_ZRAM_PAGEFLAGS, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /*-- Data structures */ |
| 55 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 56 | /* Allocated for each disk page */ |
Sergey Senozhatsky | cb8f2ee | 2014-08-06 16:08:25 -0700 | [diff] [blame] | 57 | struct zram_table_entry { |
zhouxianrong | 8e19d54 | 2017-02-24 14:59:27 -0800 | [diff] [blame] | 58 | union { |
| 59 | unsigned long handle; |
| 60 | unsigned long element; |
| 61 | }; |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame] | 62 | unsigned long value; |
| 63 | }; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 64 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 65 | struct zram_stats { |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 66 | atomic64_t compr_data_size; /* compressed size of pages stored */ |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 67 | atomic64_t num_reads; /* failed + successful */ |
| 68 | atomic64_t num_writes; /* --do-- */ |
Chao Yu | 0cf1e9d | 2014-08-29 15:18:37 -0700 | [diff] [blame] | 69 | atomic64_t failed_reads; /* can happen when memory is too low */ |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 70 | atomic64_t failed_writes; /* can happen when memory is too low */ |
| 71 | atomic64_t invalid_io; /* non-page-aligned I/O requests */ |
| 72 | atomic64_t notify_free; /* no. of swap slot free notifications */ |
zhouxianrong | 8e19d54 | 2017-02-24 14:59:27 -0800 | [diff] [blame] | 73 | atomic64_t same_pages; /* no. of same element filled pages */ |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 74 | atomic64_t pages_stored; /* no. of pages currently stored */ |
Minchan Kim | 461a8ee | 2014-10-09 15:29:55 -0700 | [diff] [blame] | 75 | atomic_long_t max_used_pages; /* no. of maximum pages stored */ |
Sergey Senozhatsky | 623e47f | 2016-05-20 17:00:02 -0700 | [diff] [blame] | 76 | atomic64_t writestall; /* no. of write slow paths */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 77 | }; |
| 78 | |
Minchan Kim | beb6602 | 2017-05-03 14:55:47 -0700 | [diff] [blame] | 79 | struct zram { |
Sergey Senozhatsky | cb8f2ee | 2014-08-06 16:08:25 -0700 | [diff] [blame] | 80 | struct zram_table_entry *table; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 81 | struct zs_pool *mem_pool; |
Minchan Kim | 08eee69 | 2015-02-12 15:00:45 -0800 | [diff] [blame] | 82 | struct zcomp *comp; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 83 | struct gendisk *disk; |
Minchan Kim | 08eee69 | 2015-02-12 15:00:45 -0800 | [diff] [blame] | 84 | /* Prevent concurrent execution of device init */ |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 85 | struct rw_semaphore init_lock; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 86 | /* |
Minchan Kim | 08eee69 | 2015-02-12 15:00:45 -0800 | [diff] [blame] | 87 | * the number of pages zram can consume for storing compressed data |
| 88 | */ |
| 89 | unsigned long limit_pages; |
Minchan Kim | 08eee69 | 2015-02-12 15:00:45 -0800 | [diff] [blame] | 90 | |
| 91 | struct zram_stats stats; |
Minchan Kim | 08eee69 | 2015-02-12 15:00:45 -0800 | [diff] [blame] | 92 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 93 | * This is the limit on amount of *uncompressed* worth of data |
| 94 | * we can store in a disk. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 95 | */ |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 96 | u64 disksize; /* bytes */ |
Sergey Senozhatsky | 415403b | 2016-07-26 15:22:48 -0700 | [diff] [blame] | 97 | char compressor[CRYPTO_MAX_ALG_NAME]; |
Sergey Senozhatsky | f405c44 | 2015-06-25 15:00:21 -0700 | [diff] [blame] | 98 | /* |
| 99 | * zram is claimed so open request will be failed |
| 100 | */ |
| 101 | bool claim; /* Protected by bdev->bd_mutex */ |
Minchan Kim | 013bf95 | 2017-09-06 16:19:54 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_ZRAM_WRITEBACK |
| 103 | struct file *backing_dev; |
| 104 | struct block_device *bdev; |
| 105 | unsigned int old_block_size; |
Minchan Kim | 1363d46 | 2017-09-06 16:19:57 -0700 | [diff] [blame] | 106 | unsigned long *bitmap; |
| 107 | unsigned long nr_pages; |
| 108 | spinlock_t bitmap_lock; |
Minchan Kim | 013bf95 | 2017-09-06 16:19:54 -0700 | [diff] [blame] | 109 | #endif |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 110 | }; |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 111 | #endif |