^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Compressed RAM block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2008, 2009, 2010 Nitin Gupta
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * 2012, 2013 Minchan Kim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This code is released using a dual license strategy: BSD/GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * You can choose the licence that better fits your requirements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Released under the terms of 3-clause BSD License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Released under the terms of GNU General Public License Version 2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #ifndef _ZRAM_DRV_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define _ZRAM_DRV_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/zsmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "zcomp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define ZRAM_LOGICAL_BLOCK_SHIFT 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * The lower ZRAM_FLAG_SHIFT bits of table.flags is for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * object size (excluding header), the higher bits is for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * zram_pageflags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * zram is mainly used for memory efficiency so we want to keep memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * footprint small so we can squeeze size and flags into a field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * the higher bits is for zram_pageflags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define ZRAM_FLAG_SHIFT 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Flags for zram pages (table[page_no].flags) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) enum zram_pageflags {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* zram slot is locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ZRAM_LOCK = ZRAM_FLAG_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ZRAM_SAME, /* Page consists the same element */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ZRAM_WB, /* page is stored on backing_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ZRAM_UNDER_WB, /* page is under writeback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ZRAM_HUGE, /* Incompressible page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ZRAM_IDLE, /* not accessed page since last idle marking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) __NR_ZRAM_PAGEFLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*-- Data structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Allocated for each disk page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct zram_table_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned long handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned long element;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #ifdef CONFIG_ZRAM_MEMORY_TRACKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ktime_t ac_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct zram_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) atomic64_t compr_data_size; /* compressed size of pages stored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) atomic64_t num_reads; /* failed + successful */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) atomic64_t num_writes; /* --do-- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) atomic64_t failed_reads; /* can happen when memory is too low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) atomic64_t failed_writes; /* can happen when memory is too low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) atomic64_t invalid_io; /* non-page-aligned I/O requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) atomic64_t notify_free; /* no. of swap slot free notifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) atomic64_t same_pages; /* no. of same element filled pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) atomic64_t huge_pages; /* no. of huge pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) atomic64_t pages_stored; /* no. of pages currently stored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) atomic_long_t max_used_pages; /* no. of maximum pages stored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) atomic64_t writestall; /* no. of write slow paths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) atomic64_t miss_free; /* no. of missed free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #ifdef CONFIG_ZRAM_WRITEBACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) atomic64_t bd_count; /* no. of pages in backing device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) atomic64_t bd_reads; /* no. of reads from backing device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) atomic64_t bd_writes; /* no. of writes from backing device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct zram {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct zram_table_entry *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct zs_pool *mem_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct zcomp *comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Prevent concurrent execution of device init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct rw_semaphore init_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * the number of pages zram can consume for storing compressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned long limit_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct zram_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * This is the limit on amount of *uncompressed* worth of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * we can store in a disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u64 disksize; /* bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) char compressor[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * zram is claimed so open request will be failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) bool claim; /* Protected by bdev->bd_mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct file *backing_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #ifdef CONFIG_ZRAM_WRITEBACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) spinlock_t wb_limit_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) bool wb_limit_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) u64 bd_wb_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned int old_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long *bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #ifdef CONFIG_ZRAM_MEMORY_TRACKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct dentry *debugfs_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #endif