zram_drv.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Compressed RAM block device
  3. *
  4. * Copyright (C) 2008, 2009, 2010 Nitin Gupta
  5. * 2012, 2013 Minchan Kim
  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. *
  13. */
  14. #ifndef _ZRAM_DRV_H_
  15. #define _ZRAM_DRV_H_
  16. #include <linux/spinlock.h>
  17. #include <linux/zsmalloc.h>
  18. #include "zcomp.h"
  19. /*
  20. * Some arbitrary value. This is just to catch
  21. * invalid value for num_devices module parameter.
  22. */
  23. static const unsigned max_num_devices = 32;
  24. /*-- Configurable parameters */
  25. /* Default zram disk size: 50% of total RAM */
  26. static const unsigned default_disksize_perc_ram = 50;
  27. /* Is totalram_pages less than SUPPOSED_TOTALRAM, promote its default size */
  28. #define SUPPOSED_TOTALRAM 0x20000 /* 512MB */
  29. /*
  30. * Pages that compress to size greater than this are stored
  31. * uncompressed in memory.
  32. */
  33. static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
  34. /*
  35. * NOTE: max_zpage_size must be less than or equal to:
  36. * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
  37. * always return failure.
  38. */
  39. /*-- End of configurable params */
  40. #define SECTOR_SHIFT 9
  41. #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
  42. #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
  43. #define ZRAM_LOGICAL_BLOCK_SHIFT 12
  44. #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
  45. #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
  46. (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
  47. /*
  48. * The lower ZRAM_FLAG_SHIFT bits of table.value is for
  49. * object size (excluding header), the higher bits is for
  50. * zram_pageflags.
  51. *
  52. * zram is mainly used for memory efficiency so we want to keep memory
  53. * footprint small so we can squeeze size and flags into a field.
  54. * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
  55. * the higher bits is for zram_pageflags.
  56. */
  57. #define ZRAM_FLAG_SHIFT 24
  58. /* Flags for zram pages (table[page_no].value) */
  59. #ifdef CONFIG_ZSM
  60. enum zram_pageflags {
  61. /* Page consists entirely of zeros */
  62. ZRAM_FIRST_NODE ,
  63. ZRAM_RB_NODE,
  64. ZRAM_ZSM_NODE,
  65. ZRAM_ZSM_DONE_NODE,
  66. ZRAM_ZERO = ZRAM_FLAG_SHIFT + 1,
  67. ZRAM_ACCESS, /* page in now accessed */
  68. __NR_ZRAM_PAGEFLAGS,
  69. };
  70. #else
  71. enum zram_pageflags {
  72. /* Page consists entirely of zeros */
  73. ZRAM_ZERO = ZRAM_FLAG_SHIFT + 1,
  74. ZRAM_ACCESS, /* page in now accessed */
  75. __NR_ZRAM_PAGEFLAGS,
  76. };
  77. #endif
  78. /*-- Data structures */
  79. /* Allocated for each disk page */
  80. #ifdef CONFIG_ZSM
  81. struct zram_table_entry {
  82. unsigned long handle;
  83. unsigned long value;
  84. struct rb_node node;
  85. struct list_head head;
  86. u32 copy_count;
  87. u32 next_index;
  88. u32 copy_index;
  89. u32 checksum;
  90. u8 flags;
  91. };
  92. #else
  93. struct zram_table_entry {
  94. unsigned long handle;
  95. unsigned long value;
  96. };
  97. #endif
  98. struct zram_stats {
  99. atomic64_t compr_data_size; /* compressed size of pages stored */
  100. atomic64_t num_reads; /* failed + successful */
  101. atomic64_t num_writes; /* --do-- */
  102. atomic64_t failed_reads; /* can happen when memory is too low */
  103. atomic64_t failed_writes; /* can happen when memory is too low */
  104. atomic64_t invalid_io; /* non-page-aligned I/O requests */
  105. atomic64_t notify_free; /* no. of swap slot free notifications */
  106. atomic64_t zero_pages; /* no. of zero filled pages */
  107. atomic64_t pages_stored; /* no. of pages currently stored */
  108. atomic_long_t max_used_pages; /* no. of maximum pages stored */
  109. #ifdef CONFIG_ZSM
  110. atomic64_t zsm_saved; /* saved physical size*/
  111. atomic64_t zsm_saved4k;
  112. #endif
  113. };
  114. struct zram_meta {
  115. struct zram_table_entry *table;
  116. struct zs_pool *mem_pool;
  117. };
  118. struct zram {
  119. struct zram_meta *meta;
  120. struct request_queue *queue;
  121. struct gendisk *disk;
  122. struct zcomp *comp;
  123. /* Prevent concurrent execution of device init, reset and R/W request */
  124. struct rw_semaphore init_lock;
  125. /*
  126. * This is the limit on amount of *uncompressed* worth of data
  127. * we can store in a disk.
  128. */
  129. u64 disksize; /* bytes */
  130. int max_comp_streams;
  131. struct zram_stats stats;
  132. /*
  133. * the number of pages zram can consume for storing compressed data
  134. */
  135. unsigned long limit_pages;
  136. char compressor[10];
  137. };
  138. /* mlog */
  139. unsigned long zram_mlog(void);
  140. #endif