Skip to content

Commit 8d7b666

Browse files
Richard Changkawasaki
authored andcommitted
zram: refactor writeback helpers
Move writeback-related functions and data structures from zram_drv.[ch] to a new zram_wb.[ch] file. This is a pure refactoring patch with no functional changes, preparing the ground for the upcoming asynchronous writeback implementation. Signed-off-by: Richard Chang <[email protected]>
1 parent 36a8aec commit 8d7b666

5 files changed

Lines changed: 74 additions & 43 deletions

File tree

drivers/block/zram/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o
88
zram-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o
99
zram-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o
1010
zram-$(CONFIG_ZRAM_BACKEND_842) += backend_842.o
11+
zram-$(CONFIG_ZRAM_WRITEBACK) += zram_wb.o
1112

1213
obj-$(CONFIG_ZRAM) += zram.o

drivers/block/zram/zram_drv.c

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/kernel_read_file.h>
3737

3838
#include "zram_drv.h"
39+
#include "zram_wb.h"
3940

4041
static DEFINE_IDR(zram_index_idr);
4142
/* idr index must be protected */
@@ -233,22 +234,6 @@ static void zram_accessed(struct zram *zram, u32 index)
233234
}
234235

235236
#if defined CONFIG_ZRAM_WRITEBACK || defined CONFIG_ZRAM_MULTI_COMP
236-
struct zram_pp_slot {
237-
unsigned long index;
238-
struct list_head entry;
239-
};
240-
241-
/*
242-
* A post-processing bucket is, essentially, a size class, this defines
243-
* the range (in bytes) of pp-slots sizes in particular bucket.
244-
*/
245-
#define PP_BUCKET_SIZE_RANGE 64
246-
#define NUM_PP_BUCKETS ((PAGE_SIZE / PP_BUCKET_SIZE_RANGE) + 1)
247-
248-
struct zram_pp_ctl {
249-
struct list_head pp_buckets[NUM_PP_BUCKETS];
250-
};
251-
252237
static struct zram_pp_ctl *init_pp_ctl(void)
253238
{
254239
struct zram_pp_ctl *ctl;
@@ -697,31 +682,6 @@ static ssize_t backing_dev_store(struct device *dev,
697682
return err;
698683
}
699684

700-
static unsigned long alloc_block_bdev(struct zram *zram)
701-
{
702-
unsigned long blk_idx = 1;
703-
retry:
704-
/* skip 0 bit to confuse zram.handle = 0 */
705-
blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
706-
if (blk_idx == zram->nr_pages)
707-
return 0;
708-
709-
if (test_and_set_bit(blk_idx, zram->bitmap))
710-
goto retry;
711-
712-
atomic64_inc(&zram->stats.bd_count);
713-
return blk_idx;
714-
}
715-
716-
static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
717-
{
718-
int was_set;
719-
720-
was_set = test_and_clear_bit(blk_idx, zram->bitmap);
721-
WARN_ON_ONCE(!was_set);
722-
atomic64_dec(&zram->stats.bd_count);
723-
}
724-
725685
static void read_from_bdev_async(struct zram *zram, struct page *page,
726686
unsigned long entry, struct bio *parent)
727687
{
@@ -1111,8 +1071,6 @@ static int read_from_bdev(struct zram *zram, struct page *page,
11111071
{
11121072
return -EIO;
11131073
}
1114-
1115-
static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
11161074
#endif
11171075

11181076
#ifdef CONFIG_ZRAM_MEMORY_TRACKING

drivers/block/zram/zram_drv.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,23 @@ struct zram {
139139
#endif
140140
atomic_t pp_in_progress;
141141
};
142+
143+
#if defined CONFIG_ZRAM_WRITEBACK || defined CONFIG_ZRAM_MULTI_COMP
144+
struct zram_pp_slot {
145+
unsigned long index;
146+
struct list_head entry;
147+
};
148+
149+
/*
150+
* A post-processing bucket is, essentially, a size class, this defines
151+
* the range (in bytes) of pp-slots sizes in particular bucket.
152+
*/
153+
#define PP_BUCKET_SIZE_RANGE 64
154+
#define NUM_PP_BUCKETS ((PAGE_SIZE / PP_BUCKET_SIZE_RANGE) + 1)
155+
156+
struct zram_pp_ctl {
157+
struct list_head pp_buckets[NUM_PP_BUCKETS];
158+
};
159+
#endif
160+
142161
#endif

drivers/block/zram/zram_wb.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#define KMSG_COMPONENT "zram_wb"
4+
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
5+
6+
#include <linux/module.h>
7+
#include <linux/kernel.h>
8+
9+
#include "zram_wb.h"
10+
11+
unsigned long alloc_block_bdev(struct zram *zram)
12+
{
13+
unsigned long blk_idx = 1;
14+
retry:
15+
/* skip 0 bit to confuse zram.handle = 0 */
16+
blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
17+
if (blk_idx == zram->nr_pages)
18+
return 0;
19+
20+
if (test_and_set_bit(blk_idx, zram->bitmap))
21+
goto retry;
22+
23+
atomic64_inc(&zram->stats.bd_count);
24+
return blk_idx;
25+
}
26+
27+
void free_block_bdev(struct zram *zram, unsigned long blk_idx)
28+
{
29+
int was_set;
30+
31+
was_set = test_and_clear_bit(blk_idx, zram->bitmap);
32+
WARN_ON_ONCE(!was_set);
33+
atomic64_dec(&zram->stats.bd_count);
34+
}
35+

drivers/block/zram/zram_wb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
3+
#ifndef _ZRAM_WRITEBACK_H_
4+
#define _ZRAM_WRITEBACK_H_
5+
6+
#include <linux/bio.h>
7+
#include "zram_drv.h"
8+
9+
#if IS_ENABLED(CONFIG_ZRAM_WRITEBACK)
10+
unsigned long alloc_block_bdev(struct zram *zram);
11+
void free_block_bdev(struct zram *zram, unsigned long blk_idx);
12+
#else
13+
inline unsigned long alloc_block_bdev(struct zram *zram) { return 0; }
14+
inline void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
15+
#endif
16+
17+
#endif /* _ZRAM_WRITEBACK_H_ */
18+

0 commit comments

Comments
 (0)