Commit 7e529283 authored by Minchan Kim's avatar Minchan Kim Committed by Linus Torvalds

zram: refactor flags and writeback stuff

Rename some variables and restructure some code for better readability in
writeback and zs_free_page.

Link: http://lkml.kernel.org/r/20181127055429.251614-4-minchan@kernel.orgSigned-off-by: default avatarMinchan Kim <minchan@kernel.org>
Reviewed-by: default avatarSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Reviewed-by: default avatarJoey Pabalinas <joeypabalinas@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 5547932d
...@@ -55,17 +55,17 @@ static void zram_free_page(struct zram *zram, size_t index); ...@@ -55,17 +55,17 @@ static void zram_free_page(struct zram *zram, size_t index);
static int zram_slot_trylock(struct zram *zram, u32 index) static int zram_slot_trylock(struct zram *zram, u32 index)
{ {
return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].value); return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags);
} }
static void zram_slot_lock(struct zram *zram, u32 index) static void zram_slot_lock(struct zram *zram, u32 index)
{ {
bit_spin_lock(ZRAM_LOCK, &zram->table[index].value); bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags);
} }
static void zram_slot_unlock(struct zram *zram, u32 index) static void zram_slot_unlock(struct zram *zram, u32 index)
{ {
bit_spin_unlock(ZRAM_LOCK, &zram->table[index].value); bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags);
} }
static inline bool init_done(struct zram *zram) static inline bool init_done(struct zram *zram)
...@@ -76,7 +76,7 @@ static inline bool init_done(struct zram *zram) ...@@ -76,7 +76,7 @@ static inline bool init_done(struct zram *zram)
static inline bool zram_allocated(struct zram *zram, u32 index) static inline bool zram_allocated(struct zram *zram, u32 index)
{ {
return (zram->table[index].value >> (ZRAM_FLAG_SHIFT + 1)) || return (zram->table[index].flags >> (ZRAM_FLAG_SHIFT + 1)) ||
zram->table[index].handle; zram->table[index].handle;
} }
...@@ -99,19 +99,19 @@ static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle) ...@@ -99,19 +99,19 @@ static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
static bool zram_test_flag(struct zram *zram, u32 index, static bool zram_test_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
return zram->table[index].value & BIT(flag); return zram->table[index].flags & BIT(flag);
} }
static void zram_set_flag(struct zram *zram, u32 index, static void zram_set_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
zram->table[index].value |= BIT(flag); zram->table[index].flags |= BIT(flag);
} }
static void zram_clear_flag(struct zram *zram, u32 index, static void zram_clear_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
zram->table[index].value &= ~BIT(flag); zram->table[index].flags &= ~BIT(flag);
} }
static inline void zram_set_element(struct zram *zram, u32 index, static inline void zram_set_element(struct zram *zram, u32 index,
...@@ -127,15 +127,15 @@ static unsigned long zram_get_element(struct zram *zram, u32 index) ...@@ -127,15 +127,15 @@ static unsigned long zram_get_element(struct zram *zram, u32 index)
static size_t zram_get_obj_size(struct zram *zram, u32 index) static size_t zram_get_obj_size(struct zram *zram, u32 index)
{ {
return zram->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1); return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
} }
static void zram_set_obj_size(struct zram *zram, static void zram_set_obj_size(struct zram *zram,
u32 index, size_t size) u32 index, size_t size)
{ {
unsigned long flags = zram->table[index].value >> ZRAM_FLAG_SHIFT; unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
zram->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size; zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
} }
#if PAGE_SIZE != 4096 #if PAGE_SIZE != 4096
...@@ -282,16 +282,11 @@ static ssize_t mem_used_max_store(struct device *dev, ...@@ -282,16 +282,11 @@ static ssize_t mem_used_max_store(struct device *dev,
} }
#ifdef CONFIG_ZRAM_WRITEBACK #ifdef CONFIG_ZRAM_WRITEBACK
static bool zram_wb_enabled(struct zram *zram)
{
return zram->backing_dev;
}
static void reset_bdev(struct zram *zram) static void reset_bdev(struct zram *zram)
{ {
struct block_device *bdev; struct block_device *bdev;
if (!zram_wb_enabled(zram)) if (!zram->backing_dev)
return; return;
bdev = zram->bdev; bdev = zram->bdev;
...@@ -318,7 +313,7 @@ static ssize_t backing_dev_show(struct device *dev, ...@@ -318,7 +313,7 @@ static ssize_t backing_dev_show(struct device *dev,
ssize_t ret; ssize_t ret;
down_read(&zram->init_lock); down_read(&zram->init_lock);
if (!zram_wb_enabled(zram)) { if (!zram->backing_dev) {
memcpy(buf, "none\n", 5); memcpy(buf, "none\n", 5);
up_read(&zram->init_lock); up_read(&zram->init_lock);
return 5; return 5;
...@@ -447,7 +442,7 @@ static ssize_t backing_dev_store(struct device *dev, ...@@ -447,7 +442,7 @@ static ssize_t backing_dev_store(struct device *dev,
return err; return err;
} }
static unsigned long get_entry_bdev(struct zram *zram) static unsigned long alloc_block_bdev(struct zram *zram)
{ {
unsigned long blk_idx = 1; unsigned long blk_idx = 1;
retry: retry:
...@@ -462,11 +457,11 @@ static unsigned long get_entry_bdev(struct zram *zram) ...@@ -462,11 +457,11 @@ static unsigned long get_entry_bdev(struct zram *zram)
return blk_idx; return blk_idx;
} }
static void put_entry_bdev(struct zram *zram, unsigned long entry) static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
{ {
int was_set; int was_set;
was_set = test_and_clear_bit(entry, zram->bitmap); was_set = test_and_clear_bit(blk_idx, zram->bitmap);
WARN_ON_ONCE(!was_set); WARN_ON_ONCE(!was_set);
} }
...@@ -579,7 +574,7 @@ static int write_to_bdev(struct zram *zram, struct bio_vec *bvec, ...@@ -579,7 +574,7 @@ static int write_to_bdev(struct zram *zram, struct bio_vec *bvec,
if (!bio) if (!bio)
return -ENOMEM; return -ENOMEM;
entry = get_entry_bdev(zram); entry = alloc_block_bdev(zram);
if (!entry) { if (!entry) {
bio_put(bio); bio_put(bio);
return -ENOSPC; return -ENOSPC;
...@@ -590,7 +585,7 @@ static int write_to_bdev(struct zram *zram, struct bio_vec *bvec, ...@@ -590,7 +585,7 @@ static int write_to_bdev(struct zram *zram, struct bio_vec *bvec,
if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len,
bvec->bv_offset)) { bvec->bv_offset)) {
bio_put(bio); bio_put(bio);
put_entry_bdev(zram, entry); free_block_bdev(zram, entry);
return -EIO; return -EIO;
} }
...@@ -608,18 +603,7 @@ static int write_to_bdev(struct zram *zram, struct bio_vec *bvec, ...@@ -608,18 +603,7 @@ static int write_to_bdev(struct zram *zram, struct bio_vec *bvec,
return 0; return 0;
} }
static void zram_wb_clear(struct zram *zram, u32 index)
{
unsigned long entry;
zram_clear_flag(zram, index, ZRAM_WB);
entry = zram_get_element(zram, index);
zram_set_element(zram, index, 0);
put_entry_bdev(zram, entry);
}
#else #else
static bool zram_wb_enabled(struct zram *zram) { return false; }
static inline void reset_bdev(struct zram *zram) {}; static inline void reset_bdev(struct zram *zram) {};
static int write_to_bdev(struct zram *zram, struct bio_vec *bvec, static int write_to_bdev(struct zram *zram, struct bio_vec *bvec,
u32 index, struct bio *parent, u32 index, struct bio *parent,
...@@ -634,7 +618,8 @@ static int read_from_bdev(struct zram *zram, struct bio_vec *bvec, ...@@ -634,7 +618,8 @@ static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
{ {
return -EIO; return -EIO;
} }
static void zram_wb_clear(struct zram *zram, u32 index) {}
static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
#endif #endif
#ifdef CONFIG_ZRAM_MEMORY_TRACKING #ifdef CONFIG_ZRAM_MEMORY_TRACKING
...@@ -656,11 +641,6 @@ static void zram_accessed(struct zram *zram, u32 index) ...@@ -656,11 +641,6 @@ static void zram_accessed(struct zram *zram, u32 index)
zram->table[index].ac_time = ktime_get_boottime(); zram->table[index].ac_time = ktime_get_boottime();
} }
static void zram_reset_access(struct zram *zram, u32 index)
{
zram->table[index].ac_time = 0;
}
static ssize_t read_block_state(struct file *file, char __user *buf, static ssize_t read_block_state(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
...@@ -741,7 +721,6 @@ static void zram_debugfs_unregister(struct zram *zram) ...@@ -741,7 +721,6 @@ static void zram_debugfs_unregister(struct zram *zram)
static void zram_debugfs_create(void) {}; static void zram_debugfs_create(void) {};
static void zram_debugfs_destroy(void) {}; static void zram_debugfs_destroy(void) {};
static void zram_accessed(struct zram *zram, u32 index) {}; static void zram_accessed(struct zram *zram, u32 index) {};
static void zram_reset_access(struct zram *zram, u32 index) {};
static void zram_debugfs_register(struct zram *zram) {}; static void zram_debugfs_register(struct zram *zram) {};
static void zram_debugfs_unregister(struct zram *zram) {}; static void zram_debugfs_unregister(struct zram *zram) {};
#endif #endif
...@@ -942,17 +921,18 @@ static void zram_free_page(struct zram *zram, size_t index) ...@@ -942,17 +921,18 @@ static void zram_free_page(struct zram *zram, size_t index)
{ {
unsigned long handle; unsigned long handle;
zram_reset_access(zram, index); #ifdef CONFIG_ZRAM_MEMORY_TRACKING
zram->table[index].ac_time = 0;
#endif
if (zram_test_flag(zram, index, ZRAM_HUGE)) { if (zram_test_flag(zram, index, ZRAM_HUGE)) {
zram_clear_flag(zram, index, ZRAM_HUGE); zram_clear_flag(zram, index, ZRAM_HUGE);
atomic64_dec(&zram->stats.huge_pages); atomic64_dec(&zram->stats.huge_pages);
} }
if (zram_wb_enabled(zram) && zram_test_flag(zram, index, ZRAM_WB)) { if (zram_test_flag(zram, index, ZRAM_WB)) {
zram_wb_clear(zram, index); zram_clear_flag(zram, index, ZRAM_WB);
atomic64_dec(&zram->stats.pages_stored); free_block_bdev(zram, zram_get_element(zram, index));
return; goto out;
} }
/* /*
...@@ -961,10 +941,8 @@ static void zram_free_page(struct zram *zram, size_t index) ...@@ -961,10 +941,8 @@ static void zram_free_page(struct zram *zram, size_t index)
*/ */
if (zram_test_flag(zram, index, ZRAM_SAME)) { if (zram_test_flag(zram, index, ZRAM_SAME)) {
zram_clear_flag(zram, index, ZRAM_SAME); zram_clear_flag(zram, index, ZRAM_SAME);
zram_set_element(zram, index, 0);
atomic64_dec(&zram->stats.same_pages); atomic64_dec(&zram->stats.same_pages);
atomic64_dec(&zram->stats.pages_stored); goto out;
return;
} }
handle = zram_get_handle(zram, index); handle = zram_get_handle(zram, index);
...@@ -975,10 +953,11 @@ static void zram_free_page(struct zram *zram, size_t index) ...@@ -975,10 +953,11 @@ static void zram_free_page(struct zram *zram, size_t index)
atomic64_sub(zram_get_obj_size(zram, index), atomic64_sub(zram_get_obj_size(zram, index),
&zram->stats.compr_data_size); &zram->stats.compr_data_size);
out:
atomic64_dec(&zram->stats.pages_stored); atomic64_dec(&zram->stats.pages_stored);
zram_set_handle(zram, index, 0); zram_set_handle(zram, index, 0);
zram_set_obj_size(zram, index, 0); zram_set_obj_size(zram, index, 0);
WARN_ON_ONCE(zram->table[index].flags & ~(1UL << ZRAM_LOCK));
} }
static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index, static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
...@@ -989,24 +968,20 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index, ...@@ -989,24 +968,20 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
unsigned int size; unsigned int size;
void *src, *dst; void *src, *dst;
if (zram_wb_enabled(zram)) { zram_slot_lock(zram, index);
zram_slot_lock(zram, index); if (zram_test_flag(zram, index, ZRAM_WB)) {
if (zram_test_flag(zram, index, ZRAM_WB)) { struct bio_vec bvec;
struct bio_vec bvec;
zram_slot_unlock(zram, index);
bvec.bv_page = page;
bvec.bv_len = PAGE_SIZE;
bvec.bv_offset = 0;
return read_from_bdev(zram, &bvec,
zram_get_element(zram, index),
bio, partial_io);
}
zram_slot_unlock(zram, index); zram_slot_unlock(zram, index);
bvec.bv_page = page;
bvec.bv_len = PAGE_SIZE;
bvec.bv_offset = 0;
return read_from_bdev(zram, &bvec,
zram_get_element(zram, index),
bio, partial_io);
} }
zram_slot_lock(zram, index);
handle = zram_get_handle(zram, index); handle = zram_get_handle(zram, index);
if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) { if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
unsigned long value; unsigned long value;
...@@ -1118,7 +1093,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, ...@@ -1118,7 +1093,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
if (unlikely(comp_len >= huge_class_size)) { if (unlikely(comp_len >= huge_class_size)) {
comp_len = PAGE_SIZE; comp_len = PAGE_SIZE;
if (zram_wb_enabled(zram) && allow_wb) { if (zram->backing_dev && allow_wb) {
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comp);
ret = write_to_bdev(zram, bvec, index, bio, &element); ret = write_to_bdev(zram, bvec, index, bio, &element);
if (!ret) { if (!ret) {
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
/* /*
* The lower ZRAM_FLAG_SHIFT bits of table.value is for * The lower ZRAM_FLAG_SHIFT bits of table.flags is for
* object size (excluding header), the higher bits is for * object size (excluding header), the higher bits is for
* zram_pageflags. * zram_pageflags.
* *
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
*/ */
#define ZRAM_FLAG_SHIFT 24 #define ZRAM_FLAG_SHIFT 24
/* Flags for zram pages (table[page_no].value) */ /* Flags for zram pages (table[page_no].flags) */
enum zram_pageflags { enum zram_pageflags {
/* zram slot is locked */ /* zram slot is locked */
ZRAM_LOCK = ZRAM_FLAG_SHIFT, ZRAM_LOCK = ZRAM_FLAG_SHIFT,
...@@ -60,7 +60,7 @@ struct zram_table_entry { ...@@ -60,7 +60,7 @@ struct zram_table_entry {
unsigned long handle; unsigned long handle;
unsigned long element; unsigned long element;
}; };
unsigned long value; unsigned long flags;
#ifdef CONFIG_ZRAM_MEMORY_TRACKING #ifdef CONFIG_ZRAM_MEMORY_TRACKING
ktime_t ac_time; ktime_t ac_time;
#endif #endif
...@@ -105,8 +105,8 @@ struct zram { ...@@ -105,8 +105,8 @@ struct zram {
* zram is claimed so open request will be failed * zram is claimed so open request will be failed
*/ */
bool claim; /* Protected by bdev->bd_mutex */ bool claim; /* Protected by bdev->bd_mutex */
#ifdef CONFIG_ZRAM_WRITEBACK
struct file *backing_dev; struct file *backing_dev;
#ifdef CONFIG_ZRAM_WRITEBACK
struct block_device *bdev; struct block_device *bdev;
unsigned int old_block_size; unsigned int old_block_size;
unsigned long *bitmap; unsigned long *bitmap;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment