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 | 16a4bfb | 2010-06-01 13:31:24 +0530 | [diff] [blame] | 21 | #include "zram_ioctl.h" |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 22 | #include "xvmalloc.h" |
| 23 | |
| 24 | /* |
| 25 | * Some arbitrary value. This is just to catch |
| 26 | * invalid value for num_devices module parameter. |
| 27 | */ |
| 28 | static const unsigned max_num_devices = 32; |
| 29 | |
| 30 | /* |
| 31 | * Stored at beginning of each compressed object. |
| 32 | * |
| 33 | * It stores back-reference to table entry which points to this |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 34 | * object. This is required to support memory defragmentation. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 35 | */ |
| 36 | struct zobj_header { |
| 37 | #if 0 |
| 38 | u32 table_idx; |
| 39 | #endif |
| 40 | }; |
| 41 | |
| 42 | /*-- Configurable parameters */ |
| 43 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 44 | /* Default zram disk size: 25% of total RAM */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 45 | static const unsigned default_disksize_perc_ram = 25; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 46 | |
| 47 | /* |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 48 | * Pages that compress to size greater than this are stored |
| 49 | * uncompressed in memory. |
| 50 | */ |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 51 | static const unsigned max_zpage_size = PAGE_SIZE / 4 * 3; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 52 | |
| 53 | /* |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 54 | * NOTE: max_zpage_size must be less than or equal to: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 55 | * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header) |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 56 | * otherwise, xv_malloc() would always return failure. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 57 | */ |
| 58 | |
| 59 | /*-- End of configurable params */ |
| 60 | |
| 61 | #define SECTOR_SHIFT 9 |
| 62 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) |
| 63 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 64 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) |
| 65 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 66 | /* Flags for zram pages (table[page_no].flags) */ |
| 67 | enum zram_pageflags { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 68 | /* Page is stored uncompressed */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 69 | ZRAM_UNCOMPRESSED, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 70 | |
| 71 | /* Page consists entirely of zeros */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 72 | ZRAM_ZERO, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 73 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 74 | __NR_ZRAM_PAGEFLAGS, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /*-- Data structures */ |
| 78 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 79 | /* Allocated for each disk page */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 80 | struct table { |
| 81 | struct page *page; |
| 82 | u16 offset; |
| 83 | u8 count; /* object ref count (not yet used) */ |
| 84 | u8 flags; |
C yp | ef4ffb7 | 2010-01-06 13:42:00 +0100 | [diff] [blame] | 85 | } __attribute__((aligned(4))); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 86 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 87 | struct zram_stats { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 88 | /* basic stats */ |
| 89 | size_t compr_size; /* compressed size of pages stored - |
| 90 | * needed to enforce memlimit */ |
| 91 | /* more stats */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 92 | #if defined(CONFIG_ZRAM_STATS) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 93 | u64 num_reads; /* failed + successful */ |
| 94 | u64 num_writes; /* --do-- */ |
C yp | ef4ffb7 | 2010-01-06 13:42:00 +0100 | [diff] [blame] | 95 | u64 failed_reads; /* should NEVER! happen */ |
| 96 | u64 failed_writes; /* can happen when memory is too low */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 97 | u64 invalid_io; /* non-page-aligned I/O requests */ |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 98 | u64 notify_free; /* no. of swap slot free notifications */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 99 | u32 pages_zero; /* no. of zero filled pages */ |
| 100 | u32 pages_stored; /* no. of pages currently stored */ |
| 101 | u32 good_compress; /* % of pages with compression ratio<=50% */ |
| 102 | u32 pages_expand; /* % of incompressible pages */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 103 | #endif |
| 104 | }; |
| 105 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 106 | struct zram { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 107 | struct xv_pool *mem_pool; |
| 108 | void *compress_workmem; |
| 109 | void *compress_buffer; |
| 110 | struct table *table; |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 111 | spinlock_t stat64_lock; /* protect 64-bit stats */ |
Nitin Gupta | a1dd52a | 2010-06-01 13:31:23 +0530 | [diff] [blame] | 112 | struct mutex lock; /* protect compression buffers against |
| 113 | * concurrent writes */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 114 | struct request_queue *queue; |
| 115 | struct gendisk *disk; |
| 116 | int init_done; |
| 117 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 118 | * This is the limit on amount of *uncompressed* worth of data |
| 119 | * we can store in a disk. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 120 | */ |
| 121 | size_t disksize; /* bytes */ |
| 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 | }; |
| 125 | |
| 126 | /*-- */ |
| 127 | |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 128 | /* Debugging and Stats */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 129 | #if defined(CONFIG_ZRAM_STATS) |
| 130 | static void zram_stat_inc(u32 *v) |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 131 | { |
| 132 | *v = *v + 1; |
| 133 | } |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 134 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 135 | static void zram_stat_dec(u32 *v) |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 136 | { |
| 137 | *v = *v - 1; |
| 138 | } |
| 139 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 140 | static void zram_stat64_inc(struct zram *zram, u64 *v) |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 141 | { |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 142 | spin_lock(&zram->stat64_lock); |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 143 | *v = *v + 1; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 144 | spin_unlock(&zram->stat64_lock); |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 145 | } |
| 146 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 147 | static u64 zram_stat64_read(struct zram *zram, u64 *v) |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 148 | { |
| 149 | u64 val; |
| 150 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 151 | spin_lock(&zram->stat64_lock); |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 152 | val = *v; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 153 | spin_unlock(&zram->stat64_lock); |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 154 | |
| 155 | return val; |
| 156 | } |
| 157 | #else |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 158 | #define zram_stat_inc(v) |
| 159 | #define zram_stat_dec(v) |
| 160 | #define zram_stat64_inc(r, v) |
| 161 | #define zram_stat64_read(r, v) |
| 162 | #endif /* CONFIG_ZRAM_STATS */ |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 163 | |
| 164 | #endif |