[RESEND RFC PATCH v5 2/4] zram: support compression at the granularity of multi-pages
From: Nanzhe Zhao
Date: Fri Dec 04 2026 - 21:34:35 EST
From: Tangquan Zheng <zhengtangquan@xxxxxxxx>
Currently, when a large folio with nr_pages is submitted to zram, it is
divided into nr_pages parts for compression and storage individually.
By transitioning to a higher granularity, we can notably enhance
compression rates while simultaneously reducing CPU consumption.
This patch introduces the capability for large folios to be divided
based on the granularity specified by ZSMALLOC_MULTI_PAGES_ORDER, which
defaults to 2. For instance, for folios sized at 128KiB, compression
will occur in eight 16KiB multi-pages.
Analogous to struct folio, we define the concept of a head slot to
manage the lifetime and serialization of multi-page slots. For a
multi-page slot, the head slot is the entry whose index is naturally
aligned to (1 << ZCOMP_MULTI_PAGES_ORDER). For a single-page slot, the
head slot is simply the slot itself.
Multi-page slot (order 2, 4 slots):
+-------------+-------------+-------------+-------------+
| slot 0 | slot 1 | slot 2 | slot 3 |
| [ head ] | [ tail ] | [ tail ] | [ tail ] |
+-------------+-------------+-------------+-------------+
|
+-- owns the bit-lock and the multi-page object handle
Single-page slot (1 slot):
+-------------+
| slot N |
| [ head ] | (head is itself)
+-------------+
|
+-- owns its own bit-lock and handle
Operations on multi-page slots, including slot_free() and
zram_slot_free_notify(), serialize on the bit-lock of the head slot.
The head slot stores the handle to the compressed multi-page object
in zsmalloc, while tail slots carry the ZRAM_COMP_MULTI_PAGES flag.
When the last live slot releases its flag, slot_free() releases the
entire multi-page object through the head slot.
This modification will notably reduce CPU consumption and enhance
compression ratios.
Note on MIPS D-cache aliasing: flush_dcache_page() on MIPS only flushes
a single page, but MIPS does not support THP_SWAP, so multi-page swap-in
is unreachable there. Non-swap multi-page block I/O is outside the scope
of this patch.
Signed-off-by: Tangquan Zheng <zhengtangquan@xxxxxxxx>
Co-developed-by: Barry Song <baohua@xxxxxxxxxx>
Signed-off-by: Barry Song <baohua@xxxxxxxxxx>
Co-developed-by: Nanzhe Zhao <zhaonanzhe@xxxxxxxxxx>
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xxxxxxxxxx>
---
Documentation/ABI/testing/sysfs-block-zram | 13 +
Documentation/admin-guide/blockdev/zram.rst | 3 +
drivers/block/zram/Kconfig | 10 +
drivers/block/zram/zcomp.c | 36 +-
drivers/block/zram/zcomp.h | 18 +-
drivers/block/zram/zram_drv.c | 706 +++++++++++++++++---
drivers/block/zram/zram_drv.h | 43 ++
7 files changed, 737 insertions(+), 92 deletions(-)
diff --git a/Documentation/ABI/testing/sysfs-block-zram b/Documentation/ABI/testing/sysfs-block-zram
index 1ea9018a627f..fda6844dc53e 100644
--- a/Documentation/ABI/testing/sysfs-block-zram
+++ b/Documentation/ABI/testing/sysfs-block-zram
@@ -5,6 +5,9 @@ Description:
The disksize file is read-write and specifies the disk size
which represents the limit on the *uncompressed* worth of data
that can be stored in this disk.
+ The value is rounded up to the compression granularity
+ (PAGE_SIZE by default, or PAGE_SIZE << CONFIG_ZSMALLOC_MULTI_PAGES_ORDER
+ when CONFIG_ZRAM_MULTI_PAGES is enabled).
Unit: bytes
What: /sys/block/zram<id>/initstate
@@ -83,6 +86,16 @@ Description:
format is not documented intentionally and may change
anytime without any notice.
+What: /sys/block/zram<id>/multi_pages_debug_stat
+Date: September 2026
+Contact: Tangquan Zheng <zhengtangquan@xxxxxxxx>
+Description:
+ The multi_pages_debug_stat file is read-only and represents
+ debugging statistics for multi-page compression and
+ decompression when CONFIG_ZRAM_MULTI_PAGES is enabled. Its
+ format is not documented intentionally and may change anytime
+ without any notice.
+
What: /sys/block/zram<id>/backing_dev
Date: June 2017
Contact: Minchan Kim <minchan@xxxxxxxxxx>
diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst
index db256c53b127..fd393a79f729 100644
--- a/Documentation/admin-guide/blockdev/zram.rst
+++ b/Documentation/admin-guide/blockdev/zram.rst
@@ -161,6 +161,8 @@ Set Disksize
Set disk size by writing the value to sysfs node 'disksize'.
The value can be either in bytes or you can use mem suffixes.
+When CONFIG_ZRAM_MULTI_PAGES is enabled, the disksize is rounded up to the
+multi-page compression granularity (PAGE_SIZE << CONFIG_ZSMALLOC_MULTI_PAGES_ORDER).
Examples::
# Initialize /dev/zram0 with 50MB disksize
@@ -255,6 +257,7 @@ comp_algorithm RW show and change the compression algorithm
algorithm_params WO setup compression algorithm parameters
compact WO trigger memory compaction
debug_stat RO this file is used for zram debugging purposes
+multi_pages_debug_stat RO debugging info for multi-page compression
backing_dev RW set up backend storage for zram to write out
idle WO mark allocated slot as idle
====================== ====== ===============================================
diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig
index 402b7b175863..33f8c55b3119 100644
--- a/drivers/block/zram/Kconfig
+++ b/drivers/block/zram/Kconfig
@@ -145,3 +145,13 @@ config ZRAM_MULTI_COMP
re-compress pages using a potentially slower but more effective
compression algorithm. Note, that IDLE page recompression
requires ZRAM_TRACK_ENTRY_ACTIME.
+
+config ZRAM_MULTI_PAGES
+ bool "Enable multiple pages compression and decompression"
+ depends on ZRAM && ZSMALLOC_MULTI_PAGES
+ depends on !ZRAM_WRITEBACK && !ZRAM_MULTI_COMP
+ help
+ Initially, zram divided large folios into blocks of nr_pages, each sized
+ equal to PAGE_SIZE, for compression. This option fine-tunes zram to
+ improve compression granularity by dividing large folios into larger
+ parts defined by the configuration option ZSMALLOC_MULTI_PAGES_ORDER.
diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index adf6d368ae6c..568dfad3c01c 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -85,6 +85,10 @@ static void zcomp_dstrm_ctx_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
{
zcomp_dstrm_ctx_free(comp, zstrm);
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ vfree(zstrm->decomp_buf);
+ zstrm->decomp_buf = NULL;
+#endif
vfree(zstrm->local_copy);
zstrm->local_copy = NULL;
}
@@ -252,13 +256,13 @@ void zcomp_dstrm_put(struct zcomp *comp, struct zcomp_dstrm *zstrm,
}
int zcomp_compress(struct zcomp *comp, struct zcomp_cstrm *zstrm,
- const void *src, unsigned int *dst_len)
+ const void *src, unsigned int src_len, unsigned int *dst_len)
{
struct zcomp_req req = {
.src = src,
.dst = zstrm->buffer,
- .src_len = PAGE_SIZE,
- .dst_len = 2 * PAGE_SIZE,
+ .src_len = src_len,
+ .dst_len = 2 * src_len,
};
int ret;
@@ -279,12 +283,13 @@ static int zcomp_decompress_buf(struct zcomp *comp, struct zcomp_dstrm *zstrm,
}
int zcomp_decompress(struct zcomp *comp, struct zcomp_dstrm *zstrm,
- struct scatterlist *sg, unsigned int src_len, void *dst)
+ struct scatterlist *sg, unsigned int src_len, void *dst,
+ unsigned int dst_len)
{
struct zcomp_req req = {
.dst = dst,
.src_len = src_len,
- .dst_len = PAGE_SIZE,
+ .dst_len = dst_len,
};
void *src;
int ret;
@@ -331,6 +336,9 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
struct zcomp_cstrm *cstrm = NULL;
struct zcomp_dstrm *dstrm;
void *buffer = NULL, *local_copy;
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ void *decomp_buf = NULL;
+#endif
int ret;
/*
@@ -352,7 +360,7 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
* the case when compressed size is larger than the original
* one
*/
- buffer = vzalloc(2 * PAGE_SIZE);
+ buffer = vzalloc(2 * ZCOMP_MULTI_PAGES_SIZE);
if (!buffer) {
zcomp_cstrm_ctx_free(comp, cstrm);
return -ENOMEM;
@@ -366,13 +374,24 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
goto err;
}
- local_copy = vzalloc(PAGE_SIZE);
+ local_copy = vzalloc(ZCOMP_MULTI_PAGES_SIZE);
if (!local_copy) {
zcomp_dstrm_ctx_free(comp, dstrm);
ret = -ENOMEM;
goto err;
}
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ /* Scratch buffer for partial multi-page reads (swapin path). */
+ decomp_buf = vzalloc(ZCOMP_MULTI_PAGES_SIZE);
+ if (!decomp_buf) {
+ vfree(local_copy);
+ zcomp_dstrm_ctx_free(comp, dstrm);
+ ret = -ENOMEM;
+ goto err;
+ }
+#endif
+
/*
* Publish under the stream locks to serialize with tasks that may
* hold a stale pointer to this CPU's stream across an offline/online
@@ -386,6 +405,9 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
down_write(&dstrm->lock);
dstrm->local_copy = local_copy;
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ dstrm->decomp_buf = decomp_buf;
+#endif
up_write(&dstrm->lock);
return 0;
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index b9a2b57a649b..b8288658520e 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -47,6 +47,15 @@ struct zcomp_ctx {
void *context;
};
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+#define ZCOMP_MULTI_PAGES_ORDER (_AC(CONFIG_ZSMALLOC_MULTI_PAGES_ORDER, UL))
+#define ZCOMP_MULTI_PAGES_NR (1 << ZCOMP_MULTI_PAGES_ORDER)
+#define ZCOMP_MULTI_PAGES_SIZE (PAGE_SIZE * ZCOMP_MULTI_PAGES_NR)
+#define MULTI_PAGE_SHIFT (ZCOMP_MULTI_PAGES_ORDER + PAGE_SHIFT)
+#else
+#define ZCOMP_MULTI_PAGES_SIZE PAGE_SIZE
+#endif
+
struct zcomp_cstrm {
struct mutex lock;
/* compression buffer */
@@ -65,6 +74,10 @@ struct zcomp_dstrm {
struct mutex excl_lock;
/* local copy of handle memory */
void *local_copy;
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ /* Temporary buffer for partial multi-page decompression */
+ void *decomp_buf;
+#endif
struct zcomp_ctx ctx;
};
@@ -124,8 +137,9 @@ void zcomp_dstrm_put(struct zcomp *comp, struct zcomp_dstrm *zstrm,
enum zcomp_dstrm_mode mode);
int zcomp_compress(struct zcomp *comp, struct zcomp_cstrm *zstrm,
- const void *src, unsigned int *dst_len);
+ const void *src, unsigned int src_len, unsigned int *dst_len);
int zcomp_decompress(struct zcomp *comp, struct zcomp_dstrm *zstrm,
- struct scatterlist *sg, unsigned int src_len, void *dst);
+ struct scatterlist *sg, unsigned int src_len, void *dst,
+ unsigned int dst_len);
#endif /* _ZCOMP_H_ */
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 95cec8b34239..f2476ce6afa8 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -53,11 +53,12 @@ static unsigned int num_devices = 1;
* Pages that compress to sizes equals or greater than this are stored
* uncompressed in memory.
*/
-static size_t huge_class_size;
+static size_t huge_class_size[ZSMALLOC_TYPE_MAX];
static const struct block_device_operations zram_devops;
static void slot_free(struct zram *zram, unsigned long index);
+static void __slot_free(struct zram *zram, unsigned long index, int nr_pages);
/*
* entry locking rules:
@@ -353,11 +354,12 @@ static inline void zram_fill_page(void *ptr, unsigned long len,
memset_l(ptr, value, len / sizeof(unsigned long));
}
-static bool page_same_filled(void *ptr, unsigned long *element)
+static bool page_same_filled(void *ptr, unsigned long *element,
+ unsigned int page_size)
{
unsigned long *page;
unsigned long val;
- unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
+ unsigned int pos, last_pos = page_size / sizeof(*page) - 1;
page = (unsigned long *)ptr;
val = page[0];
@@ -514,7 +516,7 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,
static int read_from_zspool_raw(struct zram *zram, struct page *page,
unsigned long index);
static int read_from_zspool(struct zram *zram, struct page *page,
- unsigned long index);
+ unsigned long index, unsigned long page_size);
struct zram_wb_ctl {
/* idle list is accessed only by the writeback task, no concurency */
@@ -1123,7 +1125,7 @@ static int zram_writeback_slots(struct zram *zram,
if (zram->compressed_wb)
err = read_from_zspool_raw(zram, req->page, index);
else
- err = read_from_zspool(zram, req->page, index);
+ err = read_from_zspool(zram, req->page, index, PAGE_SIZE);
if (err)
goto next;
slot_unlock(zram, index);
@@ -1415,7 +1417,7 @@ static int decompress_bdev_page(struct zram *zram, struct page *page,
zstrm = zcomp_dstrm_get(zram->comps[prio], ZCOMP_DSTRM_EXCLUSIVE);
ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size,
- zstrm->local_copy);
+ zstrm->local_copy, PAGE_SIZE);
if (!ret) {
void *dst = kmap_local_page(page);
@@ -2035,6 +2037,31 @@ static ssize_t debug_stat_show(struct device *dev,
return ret;
}
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+static ssize_t multi_pages_debug_stat_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct zram *zram = dev_to_zram(dev);
+ ssize_t ret;
+
+ guard(rwsem_read)(&zram->dev_lock);
+ ret = sysfs_emit(buf,
+ "zram_bio write/read multi_pages count:%8llu %8llu\n"
+ "zram_bio failed write/read multi_pages count: %8llu %8llu\n"
+ "zram_bio partial write/read multi_pages count: %8llu %8llu\n"
+ "multi_pages_miss_free: %8llu\n",
+ (u64)atomic64_read(&zram->stats.zram_bio_write_multi_pages_count),
+ (u64)atomic64_read(&zram->stats.zram_bio_read_multi_pages_count),
+ (u64)atomic64_read(&zram->stats.multi_pages_failed_writes),
+ (u64)atomic64_read(&zram->stats.multi_pages_failed_reads),
+ (u64)atomic64_read(&zram->stats.zram_bio_write_multi_pages_partial_count),
+ (u64)atomic64_read(&zram->stats.zram_bio_read_multi_pages_partial_count),
+ (u64)atomic64_read(&zram->stats.multi_pages_miss_free));
+
+ return ret;
+}
+#endif
+
static void zram_meta_free(struct zram *zram, u64 disksize)
{
unsigned long num_pages = disksize >> PAGE_SHIFT;
@@ -2056,6 +2083,7 @@ static void zram_meta_free(struct zram *zram, u64 disksize)
static bool zram_meta_alloc(struct zram *zram, u64 disksize)
{
unsigned long num_pages;
+ int i;
num_pages = disksize >> PAGE_SHIFT;
zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
@@ -2069,9 +2097,10 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize)
return false;
}
- if (!huge_class_size)
- huge_class_size = zs_huge_class_size(zram->mem_pool,
- ZSMALLOC_TYPE_BASEPAGE);
+ for (i = 0; i < ZSMALLOC_TYPE_MAX; i++) {
+ if (!huge_class_size[i])
+ huge_class_size[i] = zs_huge_class_size(zram->mem_pool, i);
+ }
lockdep_register_key(&zram->table_lock_key);
lockdep_init_map(&zram->table_lock_map, "zram->table[index].lock",
@@ -2080,7 +2109,83 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize)
return true;
}
-static void slot_free(struct zram *zram, unsigned long index)
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+
+static inline unsigned long zram_multi_pages_head_index(unsigned long index)
+{
+ return index & ~((unsigned long)ZCOMP_MULTI_PAGES_NR - 1);
+}
+
+/*
+ * Count how many slots in the window of ZCOMP_MULTI_PAGES_NR slots at
+ * @index's aligned head carry ZRAM_COMP_MULTI_PAGES.
+ *
+ * The flags are changed under the head slot's lock.
+ */
+static inline int __test_multi_pages_comp(struct zram *zram,
+ unsigned long index)
+{
+ unsigned long head = zram_multi_pages_head_index(index);
+ int i, count = 0;
+
+ for (i = 0; i < ZCOMP_MULTI_PAGES_NR; i++) {
+ /*
+ * Check the slot's multi-page flag without lock as a hint.
+ * Callers will recheck while holding the head slot lock.
+ */
+ if (data_race(READ_ONCE(zram->table[head + i].attr.flags)) &
+ BIT(ZRAM_COMP_MULTI_PAGES))
+ count++;
+ }
+
+ return count;
+}
+
+static void set_slots_multi_pages(struct zram *zram, unsigned long index,
+ int nr_pages)
+{
+ int i;
+
+ for (i = 0; i < nr_pages; i++)
+ set_slot_flag(zram, index + i, ZRAM_COMP_MULTI_PAGES);
+}
+
+#endif
+
+/*
+ * Return the head slot index of @index. For a multi-page slot this is
+ * the aligned first slot. For a single-page slot it is @index itself.
+ */
+static unsigned long zram_slot_head_index(struct zram *zram, unsigned long index)
+{
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ if (__test_multi_pages_comp(zram, index))
+ return zram_multi_pages_head_index(index);
+#endif
+
+ return index;
+}
+
+static unsigned int zram_slot_page_size(struct zram *zram, unsigned long index)
+{
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ if (__test_multi_pages_comp(zram, zram_multi_pages_head_index(index)))
+ return ZCOMP_MULTI_PAGES_SIZE;
+#endif
+
+ return PAGE_SIZE;
+}
+
+static int zram_slot_nr_pages(struct zram *zram, unsigned long index)
+{
+ return zram_slot_page_size(zram, index) >> PAGE_SHIFT;
+}
+
+/*
+ * Release the object at @index and subtract @nr_pages from pages_stored.
+ * @nr_pages must match what was accounted when the object was stored.
+ */
+static void __slot_free(struct zram *zram, unsigned long index, int nr_pages)
{
struct zs_handle *handle;
@@ -2115,7 +2220,7 @@ static void slot_free(struct zram *zram, unsigned long index)
*/
if (test_slot_flag(zram, index, ZRAM_SAME)) {
clear_slot_flag(zram, index, ZRAM_SAME);
- zram_stat_sub(zram, ZRAM_STAT_SAME_PAGES, 1);
+ zram_stat_sub(zram, ZRAM_STAT_SAME_PAGES, nr_pages);
goto out;
}
@@ -2128,44 +2233,76 @@ static void slot_free(struct zram *zram, unsigned long index)
zram_stat_sub(zram, ZRAM_STAT_COMPR_DATA_SIZE,
get_slot_size(zram, index));
out:
- zram_stat_sub(zram, ZRAM_STAT_PAGES_STORED, 1);
+ zram_stat_sub(zram, ZRAM_STAT_PAGES_STORED, nr_pages);
zram->table[index].element = 0;
set_slot_size(zram, index, 0);
}
+static void slot_free(struct zram *zram, unsigned long index)
+{
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ unsigned long head = zram_multi_pages_head_index(index);
+ int nr_pages = 1;
+ int i;
+
+ /* Clear all slots' MULTI flags first. */
+ if (__test_multi_pages_comp(zram, head)) {
+ index = head;
+ nr_pages = ZCOMP_MULTI_PAGES_NR;
+ for (i = 0; i < nr_pages; i++)
+ clear_slot_flag(zram, head + i, ZRAM_COMP_MULTI_PAGES);
+ }
+
+ /*
+ * Then release the whole multi-page slot (by releasing the head
+ * slot's handle).
+ */
+ __slot_free(zram, index, nr_pages);
+
+ /*
+ * Drop single-page objects left behind by a failed
+ * zram_slot_free_notify trylock.
+ */
+ for (i = 1; i < nr_pages; i++)
+ __slot_free(zram, index + i, 1);
+#else
+ __slot_free(zram, index, 1);
+#endif
+}
+
static int read_same_filled_page(struct zram *zram, struct page *page,
- unsigned long index)
+ unsigned long index, unsigned long page_size)
{
void *mem;
mem = kmap_local_page(page);
- zram_fill_page(mem, PAGE_SIZE, zram->table[index].element);
+ zram_fill_page(mem, page_size, zram->table[index].element);
kunmap_local(mem);
return 0;
}
static int read_incompressible_page(struct zram *zram, struct page *page,
- unsigned long index)
+ unsigned long index,
+ unsigned long page_size)
{
struct scatterlist sg[2];
struct zs_handle *handle;
- void *src, *dst;
+ void *dst;
handle = &zram->table[index].handle;
- zs_obj_read_sg_begin(zram->mem_pool, handle, sg, PAGE_SIZE);
+ zs_obj_read_sg_begin(zram->mem_pool, handle, sg, page_size);
/* an incompressible object never spans two pages */
- src = kmap_local_page(sg_page(sg));
dst = kmap_local_page(page);
- copy_page(dst, src);
+ sg_copy_to_buffer(sg, sg_nents(sg), dst, page_size);
kunmap_local(dst);
- kunmap_local(src);
zs_obj_read_sg_end(zram->mem_pool, handle);
return 0;
}
static int read_compressed_page(struct zram *zram, struct page *page,
- unsigned long index)
+ unsigned long index,
+ unsigned long page_size)
{
enum zcomp_dstrm_mode mode = ZCOMP_DSTRM_EXCLUSIVE;
struct zcomp_dstrm *zstrm;
@@ -2185,7 +2322,8 @@ static int read_compressed_page(struct zram *zram, struct page *page,
zstrm = zcomp_dstrm_get(zram->comps[prio], mode);
dst = kmap_local_page(page);
- ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size, dst);
+ ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size, dst,
+ page_size);
kunmap_local(dst);
zcomp_dstrm_put(zram->comps[prio], zstrm, mode);
zs_obj_read_sg_end(zram->mem_pool, handle);
@@ -2226,20 +2364,21 @@ static int read_from_zspool_raw(struct zram *zram, struct page *page,
* Corresponding ZRAM slot should be locked.
*/
static int read_from_zspool(struct zram *zram, struct page *page,
- unsigned long index)
+ unsigned long index, unsigned long page_size)
{
if (test_slot_flag(zram, index, ZRAM_SAME) ||
!zs_handle_is_allocated(&zram->table[index].handle))
- return read_same_filled_page(zram, page, index);
+ return read_same_filled_page(zram, page, index, page_size);
if (!test_slot_flag(zram, index, ZRAM_HUGE))
- return read_compressed_page(zram, page, index);
+ return read_compressed_page(zram, page, index, page_size);
else
- return read_incompressible_page(zram, page, index);
+ return read_incompressible_page(zram, page, index, page_size);
}
-static int zram_read_page(struct zram *zram, struct page *page,
- unsigned long index, struct bio *parent)
+static int zram_read_page_size(struct zram *zram, struct page *page,
+ unsigned long index, struct bio *parent,
+ unsigned long page_size)
{
int ret;
@@ -2247,7 +2386,7 @@ static int zram_read_page(struct zram *zram, struct page *page,
mark_slot_accessed(zram, index);
if (!test_slot_flag(zram, index, ZRAM_WB)) {
/* Slot should be locked through out the function call */
- ret = read_from_zspool(zram, page, index);
+ ret = read_from_zspool(zram, page, index, page_size);
slot_unlock(zram, index);
} else {
unsigned long blk_idx = zram->table[index].blk_idx;
@@ -2263,6 +2402,12 @@ static int zram_read_page(struct zram *zram, struct page *page,
return ret;
}
+static int zram_read_page(struct zram *zram, struct page *page,
+ unsigned long index, struct bio *parent)
+{
+ return zram_read_page_size(zram, page, index, parent, PAGE_SIZE);
+}
+
/*
* Use a temporary buffer to decompress the page, as the decompressor
* always expects a full page for the output.
@@ -2291,56 +2436,51 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
}
static int write_same_filled_page(struct zram *zram, unsigned long fill,
- unsigned long index)
+ unsigned long index, int nr_pages)
{
- slot_lock(zram, index);
- slot_free(zram, index);
+ __slot_free(zram, index, zram_slot_nr_pages(zram, index));
set_slot_flag(zram, index, ZRAM_SAME);
zram->table[index].element = fill;
mark_slot_accessed(zram, index);
- slot_unlock(zram, index);
- zram_stat_add(zram, ZRAM_STAT_SAME_PAGES, 1);
- zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
+ zram_stat_add(zram, ZRAM_STAT_SAME_PAGES, nr_pages);
+ zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, nr_pages);
return 0;
}
static int write_incompressible_page(struct zram *zram, struct page *page,
- unsigned long index)
+ unsigned long index, unsigned int page_size,
+ int nr_pages)
{
void *src;
int ret;
- slot_lock(zram, index);
- slot_free(zram, index);
+ __slot_free(zram, index, zram_slot_nr_pages(zram, index));
src = kmap_local_page(page);
ret = zs_store(zram->mem_pool, &zram->table[index].handle, src,
- PAGE_SIZE, GFP_NOIO | __GFP_NOWARN |
+ page_size, GFP_NOIO | __GFP_NOWARN |
__GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page));
kunmap_local(src);
- if (ret) {
- slot_unlock(zram, index);
+ if (ret)
return ret;
- }
update_used_max(zram);
set_slot_flag(zram, index, ZRAM_HUGE);
- set_slot_size(zram, index, PAGE_SIZE);
+ set_slot_size(zram, index, page_size);
mark_slot_accessed(zram, index);
- slot_unlock(zram, index);
- zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, PAGE_SIZE);
+ zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, page_size);
zram_stat_add(zram, ZRAM_STAT_HUGE_PAGES, 1);
zram_stat_add(zram, ZRAM_STAT_HUGE_PAGES_SINCE, 1);
- zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
+ zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, nr_pages);
return 0;
}
-static int zram_write_page(struct zram *zram, struct page *page,
- unsigned long index)
+static int __zram_write_page(struct zram *zram, struct page *page,
+ unsigned long index, unsigned int page_size)
{
int ret = 0;
unsigned int comp_len;
@@ -2348,12 +2488,19 @@ static int zram_write_page(struct zram *zram, struct page *page,
struct zcomp_cstrm *zstrm;
unsigned long element;
bool same_filled;
+ int huge_class_idx = ZSMALLOC_TYPE_BASEPAGE;
+ int nr_pages = page_size >> PAGE_SHIFT;
+
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ if (page_size > PAGE_SIZE)
+ huge_class_idx = ZSMALLOC_TYPE_MULTI_PAGES;
+#endif
mem = kmap_local_page(page);
- same_filled = page_same_filled(mem, &element);
+ same_filled = page_same_filled(mem, &element, page_size);
kunmap_local(mem);
if (same_filled)
- return write_same_filled_page(zram, element, index);
+ return write_same_filled_page(zram, element, index, nr_pages);
if (!zram_can_store_page(zram))
return -ENOMEM;
@@ -2361,7 +2508,7 @@ static int zram_write_page(struct zram *zram, struct page *page,
zstrm = zcomp_cstrm_get(zram->comps[ZRAM_PRIMARY_COMP]);
mem = kmap_local_page(page);
ret = zcomp_compress(zram->comps[ZRAM_PRIMARY_COMP], zstrm,
- mem, &comp_len);
+ mem, page_size, &comp_len);
kunmap_local(mem);
if (unlikely(ret)) {
@@ -2370,36 +2517,49 @@ static int zram_write_page(struct zram *zram, struct page *page,
return ret;
}
- if (comp_len >= huge_class_size) {
+ if (comp_len >= huge_class_size[huge_class_idx]) {
zcomp_cstrm_put(zstrm);
- return write_incompressible_page(zram, page, index);
+ return write_incompressible_page(zram, page, index, page_size,
+ nr_pages);
}
- slot_lock(zram, index);
- slot_free(zram, index);
+ __slot_free(zram, index, zram_slot_nr_pages(zram, index));
ret = zs_store(zram->mem_pool, &zram->table[index].handle,
zstrm->buffer, comp_len,
GFP_NOIO | __GFP_NOWARN |
__GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page));
zcomp_cstrm_put(zstrm);
- if (ret) {
- slot_unlock(zram, index);
+ if (ret)
return ret;
- }
update_used_max(zram);
set_slot_size(zram, index, comp_len);
mark_slot_accessed(zram, index);
- slot_unlock(zram, index);
/* Update stats */
- zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
+ zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, nr_pages);
zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, comp_len);
return ret;
}
+static int zram_write_page(struct zram *zram, struct page *page,
+ unsigned long index, unsigned int page_size)
+{
+ int ret;
+
+ slot_lock(zram, index);
+ ret = __zram_write_page(zram, page, index, page_size);
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ if (!ret && page_size > PAGE_SIZE)
+ set_slots_multi_pages(zram, index, page_size >> PAGE_SHIFT);
+#endif
+ slot_unlock(zram, index);
+
+ return ret;
+}
+
/*
* This is a partial IO. Read the full page before writing the changes.
*/
@@ -2415,7 +2575,7 @@ static int zram_bvec_write_partial(struct zram *zram, struct bio_vec *bvec,
ret = zram_read_page(zram, page, index, NULL);
if (!ret) {
memcpy_from_bvec(page_address(page) + offset, bvec);
- ret = zram_write_page(zram, page, index);
+ ret = zram_write_page(zram, page, index, PAGE_SIZE);
}
__free_page(page);
return ret;
@@ -2426,7 +2586,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
{
if (is_partial_io(bvec))
return zram_bvec_write_partial(zram, bvec, index, offset);
- return zram_write_page(zram, bvec->bv_page, index);
+ return zram_write_page(zram, bvec->bv_page, index, PAGE_SIZE);
}
#ifdef CONFIG_ZRAM_MULTI_COMP
@@ -2514,7 +2674,7 @@ static int recompress_slot(struct zram *zram, unsigned long index,
if (comp_len_old < threshold)
return 0;
- ret = read_from_zspool(zram, page, index);
+ ret = read_from_zspool(zram, page, index, PAGE_SIZE);
if (ret)
return ret;
@@ -2527,7 +2687,8 @@ static int recompress_slot(struct zram *zram, unsigned long index,
zstrm = zcomp_cstrm_get(zram->comps[prio]);
src = kmap_local_page(page);
- ret = zcomp_compress(zram->comps[prio], zstrm, src, &comp_len_new);
+ ret = zcomp_compress(zram->comps[prio], zstrm, src, PAGE_SIZE,
+ &comp_len_new);
kunmap_local(src);
/*
@@ -2667,7 +2828,7 @@ static ssize_t recompress_store(struct device *dev,
}
}
- if (threshold >= huge_class_size)
+ if (threshold >= huge_class_size[ZSMALLOC_TYPE_BASEPAGE])
return -EINVAL;
guard(rwsem_write)(&zram->dev_lock);
@@ -2741,8 +2902,11 @@ static void zram_bio_discard(struct zram *zram, struct bio *bio)
{
size_t n = bio->bi_iter.bi_size;
unsigned long index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
- u32 offset = (bio->bi_iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
+ unsigned long head = zram_slot_head_index(zram, index);
+ u32 page_offset = (bio->bi_iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
SECTOR_SHIFT;
+ unsigned int page_size;
+ u32 offset;
/*
* zram manages data in physical block size units. Because logical block
@@ -2753,30 +2917,353 @@ static void zram_bio_discard(struct zram *zram, struct bio *bio)
* and re-compressing and then re-storing it, this isn't reasonable
* because our intent with a discard request is to save memory. So
* skipping this logical block is appropriate here.
+ *
+ * For a multi-page slot, the offset can be inside a
+ * ZCOMP_MULTI_PAGES_SIZE object. We also do not want to read part of
+ * the object and re-compress it, so skip the whole multi-page slot.
*/
+ slot_lock(zram, head);
+ page_size = zram_slot_page_size(zram, head);
+
+ /*
+ * The multi-page slot may have been released. @index is then a
+ * single-page slot. Lock it and read its size again.
+ */
+ if (index != head && page_size == PAGE_SIZE) {
+ slot_unlock(zram, head);
+ head = index;
+ slot_lock(zram, head);
+ page_size = zram_slot_page_size(zram, head);
+ }
+
+ offset = page_offset + ((index - head) << PAGE_SHIFT);
if (offset) {
- if (n <= (PAGE_SIZE - offset))
+ if (n <= page_size - offset) {
+ slot_unlock(zram, head);
goto end_bio;
-
- n -= (PAGE_SIZE - offset);
- index++;
+ }
+ n -= page_size - offset;
+ index = head + (page_size >> PAGE_SHIFT);
}
+ slot_unlock(zram, head);
while (n >= PAGE_SIZE) {
- slot_lock(zram, index);
+ unsigned long head = zram_slot_head_index(zram, index);
+ unsigned int page_size;
+
+ slot_lock(zram, head);
+ page_size = zram_slot_page_size(zram, head);
+ if (n < page_size) {
+ /* the request ends inside this slot, leave it alone */
+ slot_unlock(zram, head);
+ break;
+ }
slot_free(zram, index);
- slot_unlock(zram, index);
+ slot_unlock(zram, head);
zram_stat_add(zram, ZRAM_STAT_NOTIFY_FREE, 1);
- index++;
- n -= PAGE_SIZE;
+ index = head + (page_size >> PAGE_SHIFT);
+ n -= page_size;
}
end_bio:
bio_endio(bio);
}
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+
+static inline bool want_multi_pages_comp(struct zram *zram, struct bio *bio)
+{
+ unsigned long index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
+
+ /*
+ * For a synchronous device such as zram, swap submits one bio with a
+ * single bvec that carries one folio. Bios with more than one bvec
+ * are not supported.
+ */
+ WARN_ON_ONCE(bio->bi_vcnt != 1);
+
+ if (bio->bi_io_vec->bv_len >= ZCOMP_MULTI_PAGES_SIZE)
+ return true;
+
+ return !!__test_multi_pages_comp(zram, index);
+}
+
+static inline bool test_multi_pages_comp(struct zram *zram, struct bio *bio)
+{
+ unsigned long index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
+
+ return !!__test_multi_pages_comp(zram, index);
+}
+
+static inline bool is_multi_pages_partial_io(struct bio_vec *bvec)
+{
+ return bvec->bv_len != ZCOMP_MULTI_PAGES_SIZE;
+}
+
+static int zram_read_partial_from_zspool(struct zram *zram, struct bio_vec *bvec,
+ unsigned long index, int offset)
+{
+ struct zs_handle *handle = &zram->table[index].handle;
+ struct zcomp_dstrm *zstrm;
+ struct scatterlist sg[2];
+ unsigned int size;
+ void *src, *dst;
+ u32 prio;
+ int ret;
+ unsigned long page_size = ZCOMP_MULTI_PAGES_SIZE;
+
+ if (test_slot_flag(zram, index, ZRAM_SAME) ||
+ !zs_handle_is_allocated(handle)) {
+ void *mem;
+
+ mem = kmap_local_page(bvec->bv_page);
+ zram_fill_page(mem + bvec->bv_offset, bvec->bv_len,
+ zram->table[index].element);
+ kunmap_local(mem);
+ return 0;
+ }
+
+ size = get_slot_size(zram, index);
+ zs_obj_read_sg_begin(zram->mem_pool, handle, sg, size);
+
+ if (size == page_size) {
+ src = kmap_local_page(sg_page(sg));
+ dst = kmap_local_page(bvec->bv_page);
+ memcpy(dst + bvec->bv_offset, src + offset, bvec->bv_len);
+ kunmap_local(dst);
+ kunmap_local(src);
+ ret = 0;
+ } else {
+ prio = get_slot_comp_priority(zram, index);
+ zstrm = zcomp_dstrm_get(zram->comps[prio],
+ ZCOMP_DSTRM_EXCLUSIVE);
+ ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size,
+ zstrm->decomp_buf, page_size);
+ if (!ret) {
+ dst = kmap_local_page(bvec->bv_page);
+ memcpy(dst + bvec->bv_offset,
+ zstrm->decomp_buf + offset, bvec->bv_len);
+ kunmap_local(dst);
+ }
+ zcomp_dstrm_put(zram->comps[prio], zstrm,
+ ZCOMP_DSTRM_EXCLUSIVE);
+ }
+ zs_obj_read_sg_end(zram->mem_pool, handle);
+
+ return ret;
+}
+
+static int zram_bvec_read_multi_pages_partial(struct zram *zram,
+ struct bio_vec *bvec,
+ unsigned long index, int offset,
+ struct bio *bio)
+{
+ int ret;
+
+ slot_lock(zram, index);
+ if (!test_slot_flag(zram, index, ZRAM_WB)) {
+ ret = zram_read_partial_from_zspool(zram, bvec, index, offset);
+ slot_unlock(zram, index);
+ } else {
+ unsigned long blk_idx = zram->table[index].blk_idx;
+
+ slot_unlock(zram, index);
+ ret = read_from_bdev(zram, bvec->bv_page, index, blk_idx, bio);
+ }
+
+ if (WARN_ON(ret < 0))
+ pr_err("Decompression failed! err=%d, page=%lu offset=%d\n",
+ ret, index, offset);
+ if (!ret)
+ atomic64_inc(&zram->stats.zram_bio_read_multi_pages_partial_count);
+
+ return ret;
+}
+
+static int zram_bvec_read_multi_pages(struct zram *zram, struct bio_vec *bvec,
+ unsigned long index, int offset,
+ struct bio *bio)
+{
+ if (is_multi_pages_partial_io(bvec))
+ return zram_bvec_read_multi_pages_partial(zram, bvec, index,
+ offset, bio);
+
+ return zram_read_page_size(zram, bvec->bv_page, index, bio,
+ ZCOMP_MULTI_PAGES_SIZE);
+}
+
+static int zram_bvec_write_multi_pages_partial(struct zram *zram,
+ struct bio_vec *bvec,
+ unsigned long index, int offset,
+ struct bio *bio)
+{
+ struct page *page;
+ void *src, *dst;
+ unsigned int first, last, i;
+ int ret;
+
+ /*
+ * Recheck the MULTI flags under the slot lock. The multi-page slot
+ * may have been released since the hint. If so, let the caller
+ * retry the whole bio through the single-page path.
+ */
+ slot_lock(zram, index);
+ if (!__test_multi_pages_comp(zram, index)) {
+ slot_unlock(zram, index);
+ return -EAGAIN;
+ }
+ mark_slot_accessed(zram, index);
+
+ page = alloc_pages(GFP_NOIO | __GFP_COMP, ZCOMP_MULTI_PAGES_ORDER);
+ if (!page) {
+ slot_unlock(zram, index);
+ return -ENOMEM;
+ }
+
+ ret = read_from_zspool(zram, page, index, ZCOMP_MULTI_PAGES_SIZE);
+ if (!ret) {
+ src = kmap_local_page(bvec->bv_page);
+ dst = kmap_local_page(page);
+ memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len);
+ kunmap_local(dst);
+ kunmap_local(src);
+
+ atomic64_inc(&zram->stats.zram_bio_write_multi_pages_partial_count);
+ ret = __zram_write_page(zram, page, index,
+ ZCOMP_MULTI_PAGES_SIZE);
+ if (!ret) {
+ first = offset >> PAGE_SHIFT;
+ last = (offset + bvec->bv_len - 1) >> PAGE_SHIFT;
+ for (i = first; i <= last; i++)
+ set_slot_flag(zram, index + i,
+ ZRAM_COMP_MULTI_PAGES);
+ }
+ }
+ slot_unlock(zram, index);
+
+ __free_pages(page, ZCOMP_MULTI_PAGES_ORDER);
+ return ret;
+}
+
+static int zram_bvec_write_multi_pages(struct zram *zram, struct bio_vec *bvec,
+ unsigned long index, int offset,
+ struct bio *bio)
+{
+ if (is_multi_pages_partial_io(bvec))
+ return zram_bvec_write_multi_pages_partial(zram, bvec, index,
+ offset, bio);
+
+ return zram_write_page(zram, bvec->bv_page, index,
+ ZCOMP_MULTI_PAGES_SIZE);
+}
+
+static void zram_bio_read_multi_pages(struct zram *zram, struct bio *bio)
+{
+ unsigned long start_time = bio_start_io_acct(bio);
+ struct bvec_iter iter = bio->bi_iter;
+
+ do {
+ unsigned long index = zram_multi_pages_head_index(
+ iter.bi_sector >> SECTORS_PER_PAGE_SHIFT);
+ u32 offset = (iter.bi_sector & (SECTORS_PER_MULTI_PAGE - 1)) <<
+ SECTOR_SHIFT;
+ struct bio_vec bv = multi_pages_bio_iter_iovec(bio, iter);
+
+ atomic64_inc(&zram->stats.zram_bio_read_multi_pages_count);
+ bv.bv_len = min_t(u32, bv.bv_len,
+ ZCOMP_MULTI_PAGES_SIZE - offset);
+
+ if (zram_bvec_read_multi_pages(zram, &bv, index, offset, bio) < 0) {
+ atomic64_inc(&zram->stats.multi_pages_failed_reads);
+ bio->bi_status = BLK_STS_IOERR;
+ break;
+ }
+ flush_dcache_page(bv.bv_page);
+
+ slot_lock(zram, index);
+ mark_slot_accessed(zram, index);
+ slot_unlock(zram, index);
+
+ bio_advance_iter_single(bio, &iter, bv.bv_len);
+ } while (iter.bi_size);
+
+ bio_end_io_acct(bio, start_time);
+ bio_endio(bio);
+}
+
+static int zram_bio_write_multi_pages(struct zram *zram, struct bio *bio)
+{
+ struct bvec_iter iter = bio->bi_iter;
+ int ret;
+
+ if (bio->bi_iter.bi_size >= ZCOMP_MULTI_PAGES_SIZE) {
+ unsigned long start =
+ bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
+
+ /*
+ * Swapout of a large folio always starts at a slot index
+ * naturally aligned to the folio. We do not support
+ * multi-page compression that starts at a misaligned slot
+ * index. On the swap path that cannot happen.
+ */
+ WARN_ON_ONCE(start != zram_multi_pages_head_index(start));
+ }
+
+ do {
+ unsigned long index = zram_multi_pages_head_index(
+ iter.bi_sector >> SECTORS_PER_PAGE_SHIFT);
+ u32 offset = (iter.bi_sector & (SECTORS_PER_MULTI_PAGE - 1)) <<
+ SECTOR_SHIFT;
+ struct bio_vec bv = multi_pages_bio_iter_iovec(bio, iter);
+
+ bv.bv_len = min_t(u32, bv.bv_len,
+ ZCOMP_MULTI_PAGES_SIZE - offset);
+
+ atomic64_inc(&zram->stats.zram_bio_write_multi_pages_count);
+ ret = zram_bvec_write_multi_pages(zram, &bv, index, offset, bio);
+ if (ret == -EAGAIN)
+ return -EAGAIN;
+ if (ret < 0) {
+ atomic64_inc(&zram->stats.multi_pages_failed_writes);
+ return -EIO;
+ }
+
+ slot_lock(zram, index);
+ mark_slot_accessed(zram, index);
+ slot_unlock(zram, index);
+
+ bio_advance_iter_single(bio, &iter, bv.bv_len);
+ } while (iter.bi_size);
+
+ return 0;
+}
+#else /* CONFIG_ZRAM_MULTI_PAGES */
+static inline bool test_multi_pages_comp(struct zram *zram, struct bio *bio)
+{
+ return false;
+}
+
+static inline bool want_multi_pages_comp(struct zram *zram, struct bio *bio)
+{
+ return false;
+}
+
+static inline void zram_bio_read_multi_pages(struct zram *zram,
+ struct bio *bio) { }
+static inline int zram_bio_write_multi_pages(struct zram *zram,
+ struct bio *bio)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+
static void zram_bio_read(struct zram *zram, struct bio *bio)
{
+ if (test_multi_pages_comp(zram, bio)) {
+ zram_bio_read_multi_pages(zram, bio);
+ return;
+ }
+
unsigned long start_time = bio_start_io_acct(bio);
struct bvec_iter iter = bio->bi_iter;
@@ -2802,9 +3289,8 @@ static void zram_bio_read(struct zram *zram, struct bio *bio)
bio_endio(bio);
}
-static void zram_bio_write(struct zram *zram, struct bio *bio)
+static int zram_bio_write_page(struct zram *zram, struct bio *bio)
{
- unsigned long start_time = bio_start_io_acct(bio);
struct bvec_iter iter = bio->bi_iter;
do {
@@ -2817,13 +3303,33 @@ static void zram_bio_write(struct zram *zram, struct bio *bio)
if (zram_bvec_write(zram, &bv, index, offset) < 0) {
atomic64_inc(&zram->stats.failed_writes);
- bio->bi_status = BLK_STS_IOERR;
- break;
+ return -EIO;
}
bio_advance_iter_single(bio, &iter, bv.bv_len);
} while (iter.bi_size);
+ return 0;
+}
+
+static void zram_bio_write(struct zram *zram, struct bio *bio)
+{
+ unsigned long start_time = bio_start_io_acct(bio);
+ int ret = -EAGAIN;
+
+ if (want_multi_pages_comp(zram, bio))
+ ret = zram_bio_write_multi_pages(zram, bio);
+
+ /*
+ * If the multi-page slot has been freed, fall back to the single-page
+ * path.
+ */
+ if (ret == -EAGAIN)
+ ret = zram_bio_write_page(zram, bio);
+
+ if (ret < 0)
+ bio->bi_status = BLK_STS_IOERR;
+
bio_end_io_acct(bio, start_time);
bio_endio(bio);
}
@@ -2855,18 +3361,46 @@ static void zram_submit_bio(struct bio *bio)
static void zram_slot_free_notify(struct block_device *bdev,
unsigned long index)
{
- struct zram *zram;
-
- zram = bdev->bd_disk->private_data;
+ struct zram *zram = bdev->bd_disk->private_data;
zram_stat_add(zram, ZRAM_STAT_NOTIFY_FREE, 1);
+
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ {
+ unsigned long head = zram_slot_head_index(zram, index);
+
+ /*
+ * Try to lock the head slot to serialize with concurrent
+ * slot_free(). On failure, skip this free, same as the
+ * single-page path.
+ */
+ if (!slot_trylock(zram, head)) {
+ atomic64_inc(&zram->stats.miss_free);
+ atomic64_inc(&zram->stats.multi_pages_miss_free);
+ return;
+ }
+
+ if (__test_multi_pages_comp(zram, head) > 1) {
+ clear_slot_flag(zram, index, ZRAM_COMP_MULTI_PAGES);
+ slot_unlock(zram, head);
+ return;
+ }
+
+ /*
+ * Free the whole multi-page slot if this is the last slot
+ * holding the MULTI_PAGE flag.
+ */
+ slot_free(zram, index);
+ slot_unlock(zram, head);
+ }
+#else
if (!slot_trylock(zram, index)) {
atomic64_inc(&zram->stats.miss_free);
return;
}
-
slot_free(zram, index);
slot_unlock(zram, index);
+#endif
}
static void zram_comp_params_reset(struct zram *zram)
@@ -2946,7 +3480,7 @@ static ssize_t disksize_store(struct device *dev, struct device_attribute *attr,
return -EBUSY;
}
- disksize = PAGE_ALIGN(disksize);
+ disksize = ALIGN(disksize, ZCOMP_MULTI_PAGES_SIZE);
num_pages = disksize >> PAGE_SHIFT;
/* Slots are addressed by an unsigned long index */
if (!num_pages || ((u64)num_pages << PAGE_SHIFT) != disksize)
@@ -3044,6 +3578,9 @@ static const struct block_device_operations zram_devops = {
static DEVICE_ATTR_RO(io_stat);
static DEVICE_ATTR_RO(mm_stat);
static DEVICE_ATTR_RO(debug_stat);
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+static DEVICE_ATTR_RO(multi_pages_debug_stat);
+#endif
static DEVICE_ATTR_WO(compact);
static DEVICE_ATTR_RW(disksize);
static DEVICE_ATTR_RO(initstate);
@@ -3088,6 +3625,9 @@ static struct attribute *zram_disk_attrs[] = {
&dev_attr_io_stat.attr,
&dev_attr_mm_stat.attr,
&dev_attr_debug_stat.attr,
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ &dev_attr_multi_pages_debug_stat.attr,
+#endif
#ifdef CONFIG_ZRAM_MULTI_COMP
&dev_attr_recomp_algorithm.attr,
&dev_attr_recompress.attr,
diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
index 1fbb00dc2c29..596961912992 100644
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -29,6 +29,11 @@
#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+#define SECTORS_PER_MULTI_PAGE_SHIFT (MULTI_PAGE_SHIFT - SECTOR_SHIFT)
+#define SECTORS_PER_MULTI_PAGE (1 << SECTORS_PER_MULTI_PAGE_SHIFT)
+#endif
+
/*
* ZRAM is mainly used for memory efficiency so we want to keep memory
* footprint small and thus squeeze size and zram pageflags into a flags
@@ -38,7 +43,12 @@
*
* We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.
*/
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + CONFIG_ZSMALLOC_MULTI_PAGES_ORDER + 1)
+#else
#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)
+#endif
+
/* Only 2 bits are allowed for comp priority index */
#define ZRAM_COMP_PRIORITY_MASK 0x3
@@ -56,6 +66,10 @@ enum zram_pageflags {
ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */
ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ ZRAM_COMP_MULTI_PAGES, /* Compressed by multi-pages */
+#endif
+
__NR_ZRAM_PAGEFLAGS,
};
@@ -123,6 +137,15 @@ struct zram_stats {
atomic64_t bd_reads; /* no. of reads from backing device */
atomic64_t bd_writes; /* no. of writes from backing device */
#endif
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+ atomic64_t zram_bio_write_multi_pages_count;
+ atomic64_t zram_bio_read_multi_pages_count;
+ atomic64_t multi_pages_failed_writes;
+ atomic64_t multi_pages_failed_reads;
+ atomic64_t zram_bio_write_multi_pages_partial_count;
+ atomic64_t zram_bio_read_multi_pages_partial_count;
+ atomic64_t multi_pages_miss_free;
+#endif
};
#ifdef CONFIG_ZRAM_MULTI_COMP
@@ -181,4 +204,24 @@ struct zram {
struct dentry *debugfs_dir;
#endif
};
+
+#ifdef CONFIG_ZRAM_MULTI_PAGES
+#define multi_pages_bvec_iter_offset(bvec, iter) \
+ (mp_bvec_iter_offset((bvec), (iter)) % ZCOMP_MULTI_PAGES_SIZE)
+
+#define multi_pages_bvec_iter_len(bvec, iter) \
+ min_t(unsigned int, mp_bvec_iter_len((bvec), (iter)), \
+ ZCOMP_MULTI_PAGES_SIZE - \
+ multi_pages_bvec_iter_offset((bvec), (iter)))
+
+#define multi_pages_bvec_iter_bvec(bvec, iter) \
+((struct bio_vec) { \
+ .bv_page = bvec_iter_page((bvec), (iter)), \
+ .bv_len = multi_pages_bvec_iter_len((bvec), (iter)), \
+ .bv_offset = bvec_iter_offset((bvec), (iter)), \
+})
+
+#define multi_pages_bio_iter_iovec(bio, iter) \
+ multi_pages_bvec_iter_bvec((bio)->bi_io_vec, (iter))
+#endif
#endif
--
2.43.0