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 |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 5 | * |
| 6 | * This code is released using a dual license strategy: BSD/GPL |
| 7 | * You can choose the licence that better fits your requirements. |
| 8 | * |
| 9 | * Released under the terms of 3-clause BSD License |
| 10 | * Released under the terms of GNU General Public License Version 2.0 |
| 11 | * |
| 12 | * Project home: http://compcache.googlecode.com |
| 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 | |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/mutex.h> |
| 20 | |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 21 | #include "../zsmalloc/zsmalloc.h" |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Some arbitrary value. This is just to catch |
| 25 | * invalid value for num_devices module parameter. |
| 26 | */ |
| 27 | static const unsigned max_num_devices = 32; |
| 28 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 29 | /*-- Configurable parameters */ |
| 30 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 31 | /* |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 32 | * Pages that compress to size greater than this are stored |
| 33 | * uncompressed in memory. |
| 34 | */ |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 35 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 36 | |
| 37 | /* |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 38 | * NOTE: max_zpage_size must be less than or equal to: |
Minchan Kim | 55dcbbb | 2012-10-10 08:49:52 +0900 | [diff] [blame] | 39 | * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would |
| 40 | * always return failure. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 41 | */ |
| 42 | |
| 43 | /*-- End of configurable params */ |
| 44 | |
| 45 | #define SECTOR_SHIFT 9 |
| 46 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) |
| 47 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 48 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 49 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
| 50 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) |
| 51 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ |
| 52 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 53 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 54 | /* Flags for zram pages (table[page_no].flags) */ |
| 55 | enum zram_pageflags { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 56 | /* Page consists entirely of zeros */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 57 | ZRAM_ZERO, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 58 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 59 | __NR_ZRAM_PAGEFLAGS, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /*-- Data structures */ |
| 63 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 64 | /* Allocated for each disk page */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 65 | struct table { |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 66 | unsigned long handle; |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 67 | u16 size; /* object size (excluding header) */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 68 | u8 count; /* object ref count (not yet used) */ |
| 69 | u8 flags; |
Sam Hansen | 80677c2 | 2012-06-07 16:03:48 -0700 | [diff] [blame] | 70 | } __aligned(4); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 71 | |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 72 | /* |
| 73 | * All 64bit fields should only be manipulated by 64bit atomic accessors. |
| 74 | * All modifications to 32bit counter should be protected by zram->lock. |
| 75 | */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 76 | struct zram_stats { |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 77 | atomic64_t compr_size; /* compressed size of pages stored */ |
| 78 | atomic64_t num_reads; /* failed + successful */ |
| 79 | atomic64_t num_writes; /* --do-- */ |
| 80 | atomic64_t failed_reads; /* should NEVER! happen */ |
| 81 | atomic64_t failed_writes; /* can happen when memory is too low */ |
| 82 | atomic64_t invalid_io; /* non-page-aligned I/O requests */ |
| 83 | atomic64_t notify_free; /* no. of swap slot free notifications */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 84 | u32 pages_zero; /* no. of zero filled pages */ |
| 85 | u32 pages_stored; /* no. of pages currently stored */ |
| 86 | u32 good_compress; /* % of pages with compression ratio<=50% */ |
Minchan Kim | 130f315 | 2012-06-08 15:39:27 +0900 | [diff] [blame] | 87 | u32 bad_compress; /* % of pages with compression ratio>=75% */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 88 | }; |
| 89 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 90 | struct zram_meta { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 91 | void *compress_workmem; |
| 92 | void *compress_buffer; |
| 93 | struct table *table; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 94 | struct zs_pool *mem_pool; |
| 95 | }; |
| 96 | |
Minchan Kim | a0c516c | 2013-08-12 15:13:56 +0900 | [diff] [blame] | 97 | struct zram_slot_free { |
| 98 | unsigned long index; |
| 99 | struct zram_slot_free *next; |
| 100 | }; |
| 101 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 102 | struct zram { |
| 103 | struct zram_meta *meta; |
Jiang Liu | 57ab048 | 2013-06-07 00:07:23 +0800 | [diff] [blame] | 104 | struct rw_semaphore lock; /* protect compression buffers, table, |
| 105 | * 32bit stat counters against concurrent |
| 106 | * notifications, reads and writes */ |
Minchan Kim | a0c516c | 2013-08-12 15:13:56 +0900 | [diff] [blame] | 107 | |
| 108 | struct work_struct free_work; /* handle pending free request */ |
| 109 | struct zram_slot_free *slot_free_rq; /* list head of free request */ |
| 110 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 111 | struct request_queue *queue; |
| 112 | struct gendisk *disk; |
| 113 | int init_done; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 114 | /* Prevent concurrent execution of device init, reset and R/W request */ |
| 115 | struct rw_semaphore init_lock; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 116 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 117 | * This is the limit on amount of *uncompressed* worth of data |
| 118 | * we can store in a disk. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 119 | */ |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 120 | u64 disksize; /* bytes */ |
Minchan Kim | a0c516c | 2013-08-12 15:13:56 +0900 | [diff] [blame] | 121 | spinlock_t slot_free_lock; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 122 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 123 | struct zram_stats stats; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 124 | }; |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 125 | #endif |