zram_drv.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/rwsem.h>
  17. #include <linux/zsmalloc.h>
  18. #include <linux/crypto.h>
  19. #include "zcomp.h"
  20. #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
  21. #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
  22. #define ZRAM_LOGICAL_BLOCK_SHIFT 12
  23. #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
  24. #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
  25. (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
  26. /*
  27. * ZRAM is mainly used for memory efficiency so we want to keep memory
  28. * footprint small and thus squeeze size and zram pageflags into a flags
  29. * member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding
  30. * header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT
  31. * bits), the higher bits are for zram_pageflags.
  32. *
  33. * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.
  34. */
  35. #define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)
  36. /* Only 2 bits are allowed for comp priority index */
  37. #define ZRAM_COMP_PRIORITY_MASK 0x3
  38. /* Flags for zram pages (table[page_no].flags) */
  39. enum zram_pageflags {
  40. ZRAM_SAME = ZRAM_FLAG_SHIFT, /* Page consists the same element */
  41. ZRAM_WB, /* page is stored on backing_device */
  42. ZRAM_UNDER_WB, /* page is under writeback */
  43. ZRAM_HUGE, /* Incompressible page */
  44. ZRAM_IDLE, /* not accessed page since last idle marking */
  45. ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */
  46. ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */
  47. ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */
  48. __NR_ZRAM_PAGEFLAGS,
  49. };
  50. /*-- Data structures */
  51. /* Allocated for each disk page */
  52. struct zram_table_entry {
  53. union {
  54. unsigned long handle;
  55. unsigned long element;
  56. };
  57. unsigned int flags;
  58. spinlock_t lock;
  59. #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
  60. ktime_t ac_time;
  61. #endif
  62. };
  63. struct zram_stats {
  64. atomic64_t compr_data_size; /* compressed size of pages stored */
  65. atomic64_t failed_reads; /* can happen when memory is too low */
  66. atomic64_t failed_writes; /* can happen when memory is too low */
  67. atomic64_t notify_free; /* no. of swap slot free notifications */
  68. atomic64_t same_pages; /* no. of same element filled pages */
  69. atomic64_t huge_pages; /* no. of huge pages */
  70. atomic64_t huge_pages_since; /* no. of huge pages since zram set up */
  71. atomic64_t pages_stored; /* no. of pages currently stored */
  72. atomic_long_t max_used_pages; /* no. of maximum pages stored */
  73. atomic64_t writestall; /* no. of write slow paths */
  74. atomic64_t miss_free; /* no. of missed free */
  75. #ifdef CONFIG_ZRAM_WRITEBACK
  76. atomic64_t bd_count; /* no. of pages in backing device */
  77. atomic64_t bd_reads; /* no. of reads from backing device */
  78. atomic64_t bd_writes; /* no. of writes from backing device */
  79. #endif
  80. };
  81. #ifdef CONFIG_ZRAM_MULTI_COMP
  82. #define ZRAM_PRIMARY_COMP 0U
  83. #define ZRAM_SECONDARY_COMP 1U
  84. #define ZRAM_MAX_COMPS 4U
  85. #else
  86. #define ZRAM_PRIMARY_COMP 0U
  87. #define ZRAM_SECONDARY_COMP 0U
  88. #define ZRAM_MAX_COMPS 1U
  89. #endif
  90. struct zram {
  91. struct zram_table_entry *table;
  92. struct zs_pool *mem_pool;
  93. struct zcomp *comps[ZRAM_MAX_COMPS];
  94. struct zcomp_params params[ZRAM_MAX_COMPS];
  95. struct gendisk *disk;
  96. /* Prevent concurrent execution of device init */
  97. struct rw_semaphore init_lock;
  98. /*
  99. * the number of pages zram can consume for storing compressed data
  100. */
  101. unsigned long limit_pages;
  102. struct zram_stats stats;
  103. /*
  104. * This is the limit on amount of *uncompressed* worth of data
  105. * we can store in a disk.
  106. */
  107. u64 disksize; /* bytes */
  108. const char *comp_algs[ZRAM_MAX_COMPS];
  109. s8 num_active_comps;
  110. /*
  111. * zram is claimed so open request will be failed
  112. */
  113. bool claim; /* Protected by disk->open_mutex */
  114. #ifdef CONFIG_ZRAM_WRITEBACK
  115. struct file *backing_dev;
  116. spinlock_t wb_limit_lock;
  117. bool wb_limit_enable;
  118. u64 bd_wb_limit;
  119. struct block_device *bdev;
  120. unsigned long *bitmap;
  121. unsigned long nr_pages;
  122. #endif
  123. #ifdef CONFIG_ZRAM_MEMORY_TRACKING
  124. struct dentry *debugfs_dir;
  125. #endif
  126. atomic_t pp_in_progress;
  127. };
  128. #endif