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 | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 21 | #include "xvmalloc.h" |
| 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 | |
| 29 | /* |
| 30 | * Stored at beginning of each compressed object. |
| 31 | * |
| 32 | * It stores back-reference to table entry which points to this |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 33 | * object. This is required to support memory defragmentation. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 34 | */ |
| 35 | struct zobj_header { |
| 36 | #if 0 |
| 37 | u32 table_idx; |
| 38 | #endif |
| 39 | }; |
| 40 | |
| 41 | /*-- Configurable parameters */ |
| 42 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 43 | /* Default zram disk size: 25% of total RAM */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 44 | static const unsigned default_disksize_perc_ram = 25; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 45 | |
| 46 | /* |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 47 | * Pages that compress to size greater than this are stored |
| 48 | * uncompressed in memory. |
| 49 | */ |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 50 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 51 | |
| 52 | /* |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 53 | * NOTE: max_zpage_size must be less than or equal to: |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 54 | * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header) |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 55 | * otherwise, xv_malloc() would always return failure. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 56 | */ |
| 57 | |
| 58 | /*-- End of configurable params */ |
| 59 | |
| 60 | #define SECTOR_SHIFT 9 |
| 61 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) |
| 62 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 63 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 64 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
| 65 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) |
| 66 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ |
| 67 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 68 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 69 | /* Flags for zram pages (table[page_no].flags) */ |
| 70 | enum zram_pageflags { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 71 | /* Page is stored uncompressed */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 72 | ZRAM_UNCOMPRESSED, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 73 | |
| 74 | /* Page consists entirely of zeros */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 75 | ZRAM_ZERO, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 76 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 77 | __NR_ZRAM_PAGEFLAGS, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /*-- Data structures */ |
| 81 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 82 | /* Allocated for each disk page */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 83 | struct table { |
| 84 | struct page *page; |
| 85 | u16 offset; |
| 86 | u8 count; /* object ref count (not yet used) */ |
| 87 | u8 flags; |
C yp | ef4ffb7 | 2010-01-06 13:42:00 +0100 | [diff] [blame] | 88 | } __attribute__((aligned(4))); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 89 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 90 | struct zram_stats { |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 91 | u64 compr_size; /* compressed size of pages stored */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 92 | u64 num_reads; /* failed + successful */ |
| 93 | u64 num_writes; /* --do-- */ |
C yp | ef4ffb7 | 2010-01-06 13:42:00 +0100 | [diff] [blame] | 94 | u64 failed_reads; /* should NEVER! happen */ |
| 95 | u64 failed_writes; /* can happen when memory is too low */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 96 | u64 invalid_io; /* non-page-aligned I/O requests */ |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 97 | u64 notify_free; /* no. of swap slot free notifications */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 98 | u32 pages_zero; /* no. of zero filled pages */ |
| 99 | u32 pages_stored; /* no. of pages currently stored */ |
| 100 | u32 good_compress; /* % of pages with compression ratio<=50% */ |
| 101 | u32 pages_expand; /* % of incompressible pages */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 102 | }; |
| 103 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 104 | struct zram { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 105 | struct xv_pool *mem_pool; |
| 106 | void *compress_workmem; |
| 107 | void *compress_buffer; |
| 108 | struct table *table; |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 109 | spinlock_t stat64_lock; /* protect 64-bit stats */ |
Jerome Marchand | c5bde23 | 2011-06-10 15:28:49 +0200 | [diff] [blame] | 110 | struct rw_semaphore lock; /* protect compression buffers and table |
| 111 | * against concurrent read and writes */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 112 | struct request_queue *queue; |
| 113 | struct gendisk *disk; |
| 114 | int init_done; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 115 | /* Prevent concurrent execution of device init, reset and R/W request */ |
| 116 | struct rw_semaphore init_lock; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 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 | */ |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 121 | u64 disksize; /* bytes */ |
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 | }; |
| 125 | |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 126 | extern struct zram *zram_devices; |
Noah Watkins | efd54f4 | 2011-07-20 17:06:08 -0600 | [diff] [blame] | 127 | extern unsigned int zram_num_devices; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 128 | #ifdef CONFIG_SYSFS |
| 129 | extern struct attribute_group zram_disk_attr_group; |
| 130 | #endif |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 131 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 132 | extern int zram_init_device(struct zram *zram); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 133 | extern void __zram_reset_device(struct zram *zram); |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 134 | |
| 135 | #endif |