Re: [RFC PATCH v4 2/4] zram: support compression at the granularity of multi-pages
From: Nanzhe Zhao
Date: Fri Nov 27 2026 - 03:28:55 EST
> If an unaligned write bio with bv_len >= ZCOMP_MULTI_PAGES_SIZE or a multi-bvec
> bio enters the multi-page path
In __swap_writeout(), folios are added via bvec_set_folio(&sio->bvecs[...],
folio, folio_size(folio), 0), which always starts at the folio-aligned base
(offset 0) and covers the entire folio. Therefore, there are no unaligned
writes, nor can a single folio be split across multiple bvecs.
> and lands on a head slot that holds a single-page (PAGE_SIZE) object
Swap cluster allocation for large folios (__swap_cluster_alloc_entries())
only allocates naturally order-aligned slots from high-order clusters, so a
large folio swapout cannot land unaligned on a single-page slot.
> When a single-page bio (bv_len == PAGE_SIZE) writes a subpage belonging to
> a large folio
Swap always writes a whole folio at once, so a bv_len == PAGE_SIZE bio can
only be a whole order-0 folio, never a subpage of a large folio.
> [Severity: Critical]
> Does zram_read_partial_from_zspool() ignore bvec->bv_len and
> bvec->bv_offset?
>
> zram_bvec_read_multi_pages_partial() passes only bvec->bv_page and the
> slot offset to zram_read_partial_from_zspool(), and all three branches
> above always write PAGE_SIZE bytes to offset 0 of page. When a partial
> multi-page read has bv.bv_len > PAGE_SIZE (for example, 2 or 3 pages) or a
> non-zero bvec->bv_offset, zram_bio_read_multi_pages() still advances the
> bio iterator by bv.bv_len, writing to the wrong offset of bv_page or
> leaving the remaining pages of the read uninitialized.
Agreed. zram_read_partial_from_zspool() should respect bvec->bv_len and
bvec->bv_offset instead of hardcoding PAGE_SIZE and offset 0. I will
pass struct bio_vec *bvec and use its length and offset:
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -2872,8 +2872,8 @@ 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 page *page,
- unsigned long index, int offset)
+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;
@@ -2889,7 +2889,8 @@ static int zram_read_partial_from_zspool(struct zram *zram, struct page *page,
void *mem;
- mem = kmap_local_page(page);
- zram_fill_page(mem, PAGE_SIZE, zram->table[index].element);
+ 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;
}
@@ -2900,8 +2901,8 @@ static int zram_read_partial_from_zspool(struct zram *zram, struct page *page,
if (size == page_size) {
src = kmap_local_page(sg_page(sg));
- dst = kmap_local_page(page);
- memcpy(dst, src + offset, PAGE_SIZE);
+ 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;
@@ -2917,8 +2918,8 @@ static int zram_read_partial_from_zspool(struct zram *zram, struct page *page,
zstrm->decomp_buf, page_size);
if (!ret) {
- dst = kmap_local_page(page);
- memcpy(dst, zstrm->decomp_buf + offset, PAGE_SIZE);
+ dst = kmap_local_page(bvec->bv_page);
+ memcpy(dst + bvec->bv_offset, zstrm->decomp_buf + offset,
+ bvec->bv_len);
kunmap_local(dst);
}
@@ -2937,8 +2938,8 @@ static int zram_bvec_read_multi_pages_partial(struct zram *zram,
slot_lock(zram, index);
if (!test_slot_flag(zram, index, ZRAM_WB)) {
- ret = zram_read_partial_from_zspool(zram, bvec->bv_page,
+ ret = zram_read_partial_from_zspool(zram, bvec,
index, offset);
slot_unlock(zram, index);
> [Severity: Critical]
> Does using mp_bvec_iter_page() in multi_pages_bvec_iter_bvec() keep
> .bv_page stuck at the first page of a large folio across iterations?
>
> Unlike bvec_iter_page(), mp_bvec_iter_page(bvec, iter) always returns
> bvec[iter.bi_idx].bv_page without adding mp_bvec_iter_page_idx(bvec,
> iter), while multi_pages_bvec_iter_offset() wraps .bv_offset modulo
> ZCOMP_MULTI_PAGES_SIZE. When a folio larger than ZCOMP_MULTI_PAGES_SIZE
> (such as a 64KiB or 128KiB folio in a single multi-page bvec) is read or
> written, every iteration of zram_bio_write_multi_pages() and
> zram_bio_read_multi_pages() accesses subpage 0 of the folio, repeatedly
> compressing or overwriting the first ZCOMP_MULTI_PAGES_SIZE bytes and
> leaving the rest of the folio unread or unwritten.
Agreed. For multi-page bvecs spanning multiple compression windows,
multi_pages_bvec_iter_bvec() needs to advance the subpage and offset.
I will switch to bvec_iter_page() and bvec_iter_offset():
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -194,5 +194,5 @@
#define multi_pages_bvec_iter_bvec(bvec, iter) \
((struct bio_vec) { \
- .bv_page = mp_bvec_iter_page((bvec), (iter)), \
+ .bv_page = bvec_iter_page((bvec), (iter)), \
.bv_len = multi_pages_bvec_iter_len((bvec), (iter)), \
- .bv_offset = multi_pages_bvec_iter_offset((bvec), (iter)), \
+ .bv_offset = bvec_iter_offset((bvec), (iter)), \
})